]> jfr.im git - solanum.git/blob - src/packet.c
Minor cleanup to command throttling code:
[solanum.git] / src / packet.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * packet.c: Packet handlers.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: packet.c 3446 2007-05-14 22:21:16Z jilles $
25 */
26 #include "stdinc.h"
27 #include "s_conf.h"
28 #include "s_serv.h"
29 #include "client.h"
30 #include "common.h"
31 #include "ircd.h"
32 #include "parse.h"
33 #include "packet.h"
34 #include "match.h"
35 #include "hook.h"
36 #include "send.h"
37
38 static char readBuf[READBUF_SIZE];
39 static void client_dopacket(struct Client *client_p, char *buffer, size_t length);
40
41 /*
42 * parse_client_queued - parse client queued messages
43 */
44 static void
45 parse_client_queued(struct Client *client_p)
46 {
47 int dolen = 0;
48 int allow_read;
49
50 if(IsAnyDead(client_p))
51 return;
52
53 if(IsUnknown(client_p))
54 {
55 allow_read = ConfigFileEntry.client_flood_burst_max;
56 for (;;)
57 {
58 if(client_p->localClient->sent_parsed >= allow_read)
59 break;
60
61 dolen = rb_linebuf_get(&client_p->localClient->
62 buf_recvq, readBuf, READBUF_SIZE,
63 LINEBUF_COMPLETE, LINEBUF_PARSED);
64
65 if(dolen <= 0 || IsDead(client_p))
66 break;
67
68 client_dopacket(client_p, readBuf, dolen);
69 client_p->localClient->sent_parsed++;
70
71 /* He's dead cap'n */
72 if(IsAnyDead(client_p))
73 return;
74 /* if theyve dropped out of the unknown state, break and move
75 * to the parsing for their appropriate status. --fl
76 */
77 if(!IsUnknown(client_p))
78 {
79 /* reset their flood limits, they're now
80 * graced to flood
81 */
82 client_p->localClient->sent_parsed = 0;
83 break;
84 }
85
86 }
87 }
88
89 if(IsAnyServer(client_p) || IsExemptFlood(client_p))
90 {
91 while (!IsAnyDead(client_p) && (dolen = rb_linebuf_get(&client_p->localClient->buf_recvq,
92 readBuf, READBUF_SIZE, LINEBUF_COMPLETE,
93 LINEBUF_PARSED)) > 0)
94 {
95 client_dopacket(client_p, readBuf, dolen);
96 }
97 }
98 else if(IsClient(client_p))
99 {
100 if(IsFloodDone(client_p))
101 allow_read = ConfigFileEntry.client_flood_burst_max;
102 else
103 allow_read = ConfigFileEntry.client_flood_burst_rate;
104 /* allow opers 4 times the amount of messages as users. why 4?
105 * why not. :) --fl_
106 */
107 if(IsOper(client_p) && ConfigFileEntry.no_oper_flood)
108 allow_read *= 4;
109 /*
110 * Handle flood protection here - if we exceed our flood limit on
111 * messages in this loop, we simply drop out of the loop prematurely.
112 * -- adrian
113 */
114 for (;;)
115 {
116 /* This flood protection works as follows:
117 *
118 * A client is given allow_read lines to send to the server. Every
119 * time a line is parsed, sent_parsed is increased. sent_parsed
120 * is decreased by 1 every time flood_recalc is called.
121 *
122 * Thus a client can 'burst' allow_read lines to the server, any
123 * excess lines will be parsed one per flood_recalc() call.
124 *
125 * Therefore a client will be penalised more if they keep flooding,
126 * as sent_parsed will always hover around the allow_read limit
127 * and no 'bursts' will be permitted.
128 */
129 if(client_p->localClient->sent_parsed >= allow_read)
130 break;
131
132 dolen = rb_linebuf_get(&client_p->localClient->
133 buf_recvq, readBuf, READBUF_SIZE,
134 LINEBUF_COMPLETE, LINEBUF_PARSED);
135
136 if(!dolen)
137 break;
138
139 client_dopacket(client_p, readBuf, dolen);
140 if(IsAnyDead(client_p))
141 return;
142
143 client_p->localClient->sent_parsed += ConfigFileEntry.client_flood_message_time;
144 }
145 }
146 }
147
148 /* flood_endgrace()
149 *
150 * marks the end of the clients grace period
151 */
152 void
153 flood_endgrace(struct Client *client_p)
154 {
155 SetFloodDone(client_p);
156
157 /* sent_parsed could be way over client_flood_burst_max but under
158 * client_flood_burst_rate so reset it.
159 */
160 client_p->localClient->sent_parsed = 0;
161 }
162
163 /*
164 * flood_recalc
165 *
166 * recalculate the number of allowed flood lines. this should be called
167 * once a second on any given client. We then attempt to flush some data.
168 */
169 void
170 flood_recalc(void *unused)
171 {
172 rb_dlink_node *ptr, *next;
173 struct Client *client_p;
174
175 RB_DLINK_FOREACH_SAFE(ptr, next, lclient_list.head)
176 {
177 client_p = ptr->data;
178
179 if(rb_unlikely(IsMe(client_p)))
180 continue;
181
182 if(rb_unlikely(client_p->localClient == NULL))
183 continue;
184
185 if(IsFloodDone(client_p))
186 client_p->localClient->sent_parsed -= ConfigFileEntry.client_flood_message_num;
187 else
188 client_p->localClient->sent_parsed = 0;
189
190 if(client_p->localClient->sent_parsed < 0)
191 client_p->localClient->sent_parsed = 0;
192
193 parse_client_queued(client_p);
194
195 if(rb_unlikely(IsAnyDead(client_p)))
196 continue;
197
198 }
199
200 RB_DLINK_FOREACH_SAFE(ptr, next, unknown_list.head)
201 {
202 client_p = ptr->data;
203
204 if(client_p->localClient == NULL)
205 continue;
206
207 client_p->localClient->sent_parsed--;
208
209 if(client_p->localClient->sent_parsed < 0)
210 client_p->localClient->sent_parsed = 0;
211
212 parse_client_queued(client_p);
213 }
214 }
215
216 /*
217 * read_packet - Read a 'packet' of data from a connection and process it.
218 */
219 void
220 read_packet(rb_fde_t * F, void *data)
221 {
222 struct Client *client_p = data;
223 int length = 0;
224 int lbuf_len;
225
226 int binary = 0;
227 #ifdef USE_IODEBUG_HOOKS
228 hook_data_int hdata;
229 #endif
230
231 while(1)
232 {
233 if(IsAnyDead(client_p))
234 return;
235
236 /*
237 * Read some data. We *used to* do anti-flood protection here, but
238 * I personally think it makes the code too hairy to make sane.
239 * -- adrian
240 */
241 length = rb_read(client_p->localClient->F, readBuf, READBUF_SIZE);
242
243 if(length < 0)
244 {
245 if(rb_ignore_errno(errno))
246 rb_setselect(client_p->localClient->F,
247 RB_SELECT_READ, read_packet, client_p);
248 else
249 error_exit_client(client_p, length);
250 return;
251 }
252 else if(length == 0)
253 {
254 error_exit_client(client_p, length);
255 return;
256 }
257
258 #ifdef USE_IODEBUG_HOOKS
259 hdata.client = client_p;
260 hdata.arg1 = readBuf;
261 hdata.arg2 = length;
262 call_hook(h_iorecv_id, &hdata);
263 #endif
264
265 if(client_p->localClient->lasttime < rb_current_time())
266 client_p->localClient->lasttime = rb_current_time();
267 client_p->flags &= ~FLAGS_PINGSENT;
268
269 /*
270 * Before we even think of parsing what we just read, stick
271 * it on the end of the receive queue and do it when its
272 * turn comes around.
273 */
274 if(IsHandshake(client_p) || IsUnknown(client_p))
275 binary = 1;
276
277 lbuf_len = rb_linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length, binary);
278
279 if(IsAnyDead(client_p))
280 return;
281
282 /* Attempt to parse what we have */
283 parse_client_queued(client_p);
284
285 if(IsAnyDead(client_p))
286 return;
287
288 /* Check to make sure we're not flooding */
289 if(!IsAnyServer(client_p) &&
290 (rb_linebuf_alloclen(&client_p->localClient->buf_recvq) > ConfigFileEntry.client_flood_max_lines))
291 {
292 if(!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
293 {
294 exit_client(client_p, client_p, client_p, "Excess Flood");
295 return;
296 }
297 }
298
299 /* bail if short read */
300 if(length < READBUF_SIZE)
301 {
302 rb_setselect(client_p->localClient->F, RB_SELECT_READ, read_packet, client_p);
303 return;
304 }
305 }
306 }
307
308 /*
309 * client_dopacket - copy packet to client buf and parse it
310 * client_p - pointer to client structure for which the buffer data
311 * applies.
312 * buffer - pointr to the buffer containing the newly read data
313 * length - number of valid bytes of data in the buffer
314 *
315 * Note:
316 * It is implicitly assumed that dopacket is called only
317 * with client_p of "local" variation, which contains all the
318 * necessary fields (buffer etc..)
319 */
320 void
321 client_dopacket(struct Client *client_p, char *buffer, size_t length)
322 {
323 s_assert(client_p != NULL);
324 s_assert(buffer != NULL);
325
326 if(client_p == NULL || buffer == NULL)
327 return;
328 if(IsAnyDead(client_p))
329 return;
330 /*
331 * Update messages received
332 */
333 ++me.localClient->receiveM;
334 ++client_p->localClient->receiveM;
335
336 /*
337 * Update bytes received
338 */
339 client_p->localClient->receiveB += length;
340
341 if(client_p->localClient->receiveB > 1023)
342 {
343 client_p->localClient->receiveK += (client_p->localClient->receiveB >> 10);
344 client_p->localClient->receiveB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
345 }
346
347 me.localClient->receiveB += length;
348
349 if(me.localClient->receiveB > 1023)
350 {
351 me.localClient->receiveK += (me.localClient->receiveB >> 10);
352 me.localClient->receiveB &= 0x03ff;
353 }
354
355 parse(client_p, buffer, buffer + length);
356 }