]> jfr.im git - irc/rqf/shadowircd.git/blob - src/packet.c
[svn] Apply ratbox flood fix.
[irc/rqf/shadowircd.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 "tools.h"
28 #include "commio.h"
29 #include "s_conf.h"
30 #include "s_serv.h"
31 #include "client.h"
32 #include "common.h"
33 #include "ircd.h"
34 #include "parse.h"
35 #include "packet.h"
36 #include "irc_string.h"
37 #include "memory.h"
38 #include "hook.h"
39 #include "send.h"
40
41 static char readBuf[READBUF_SIZE];
42 static void client_dopacket(struct Client *client_p, char *buffer, size_t length);
43
44
45 /*
46 * parse_client_queued - parse client queued messages
47 */
48 static void
49 parse_client_queued(struct Client *client_p)
50 {
51 int dolen = 0;
52 int checkflood = 1;
53
54 if(IsAnyDead(client_p))
55 return;
56
57 if(IsUnknown(client_p))
58 {
59 for (;;)
60 {
61 if(client_p->localClient->sent_parsed >= client_p->localClient->allow_read)
62 break;
63
64 dolen = linebuf_get(&client_p->localClient->
65 buf_recvq, readBuf, READBUF_SIZE,
66 LINEBUF_COMPLETE, LINEBUF_PARSED);
67
68 if(dolen <= 0 || IsDead(client_p))
69 break;
70
71 client_dopacket(client_p, readBuf, dolen);
72 client_p->localClient->sent_parsed++;
73
74 /* He's dead cap'n */
75 if(IsAnyDead(client_p))
76 return;
77 /* if theyve dropped out of the unknown state, break and move
78 * to the parsing for their appropriate status. --fl
79 */
80 if(!IsUnknown(client_p))
81 {
82 /* reset their flood limits, they're now
83 * graced to flood
84 */
85 client_p->localClient->sent_parsed = 0;
86 break;
87 }
88
89 }
90 }
91
92 if(IsAnyServer(client_p) || IsExemptFlood(client_p))
93 {
94 while (!IsAnyDead(client_p) && (dolen = linebuf_get(&client_p->localClient->buf_recvq,
95 readBuf, READBUF_SIZE, LINEBUF_COMPLETE,
96 LINEBUF_PARSED)) > 0)
97 {
98 client_dopacket(client_p, readBuf, dolen);
99 }
100 }
101 else if(IsClient(client_p))
102 {
103
104 if(IsOper(client_p) && ConfigFileEntry.no_oper_flood)
105 checkflood = 0;
106 /*
107 * Handle flood protection here - if we exceed our flood limit on
108 * messages in this loop, we simply drop out of the loop prematurely.
109 * -- adrian
110 */
111 for (;;)
112 {
113 /* This flood protection works as follows:
114 *
115 * A client is given allow_read lines to send to the server. Every
116 * time a line is parsed, sent_parsed is increased. sent_parsed
117 * is decreased by 1 every time flood_recalc is called.
118 *
119 * Thus a client can 'burst' allow_read lines to the server, any
120 * excess lines will be parsed one per flood_recalc() call.
121 *
122 * Therefore a client will be penalised more if they keep flooding,
123 * as sent_parsed will always hover around the allow_read limit
124 * and no 'bursts' will be permitted.
125 */
126 if(checkflood)
127 {
128 if(client_p->localClient->sent_parsed >= client_p->localClient->allow_read)
129 break;
130 }
131
132 /* allow opers 4 times the amount of messages as users. why 4?
133 * why not. :) --fl_
134 */
135 else if(client_p->localClient->sent_parsed >= (4 * client_p->localClient->allow_read))
136 break;
137
138 dolen = linebuf_get(&client_p->localClient->
139 buf_recvq, readBuf, READBUF_SIZE,
140 LINEBUF_COMPLETE, LINEBUF_PARSED);
141
142 if(!dolen)
143 break;
144
145 client_dopacket(client_p, readBuf, dolen);
146 if(IsAnyDead(client_p))
147 return;
148 client_p->localClient->sent_parsed++;
149 }
150 }
151 }
152
153 /* flood_endgrace()
154 *
155 * marks the end of the clients grace period
156 */
157 void
158 flood_endgrace(struct Client *client_p)
159 {
160 SetFloodDone(client_p);
161
162 /* Drop their flood limit back down */
163 client_p->localClient->allow_read = MAX_FLOOD;
164
165 /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
166 * so reset it.
167 */
168 client_p->localClient->sent_parsed = 0;
169 }
170
171 /*
172 * flood_recalc
173 *
174 * recalculate the number of allowed flood lines. this should be called
175 * once a second on any given client. We then attempt to flush some data.
176 */
177 void
178 flood_recalc(int fd, void *data)
179 {
180 struct Client *client_p = data;
181 struct LocalUser *lclient_p = client_p->localClient;
182
183 /* This can happen in the event that the client detached. */
184 if(!lclient_p)
185 return;
186
187 /* allow a bursting client their allocation per second, allow
188 * a client whos flooding an extra 2 per second
189 */
190 if(IsFloodDone(client_p))
191 lclient_p->sent_parsed -= 2;
192 else
193 lclient_p->sent_parsed = 0;
194
195 if(lclient_p->sent_parsed < 0)
196 lclient_p->sent_parsed = 0;
197
198 if(--lclient_p->actually_read < 0)
199 lclient_p->actually_read = 0;
200
201 parse_client_queued(client_p);
202
203 if(IsAnyDead(client_p))
204 return;
205
206 /* and finally, reset the flood check */
207 comm_setflush(fd, 1000, flood_recalc, client_p);
208 }
209
210 /*
211 * read_ctrl_packet - Read a 'packet' of data from a servlink control
212 * link and process it.
213 */
214 void
215 read_ctrl_packet(int fd, void *data)
216 {
217 struct Client *server = data;
218 struct LocalUser *lserver = server->localClient;
219 struct SlinkRpl *reply;
220 int length = 0;
221 unsigned char tmp[2];
222 unsigned char *len = tmp;
223 struct SlinkRplDef *replydef;
224 #ifdef USE_IODEBUG_HOOKS
225 hook_data_int hdata;
226 #endif
227
228 s_assert(lserver != NULL);
229 if(IsAnyDead(server))
230 return;
231
232 reply = &lserver->slinkrpl;
233
234
235 if(!reply->command)
236 {
237 reply->gotdatalen = 0;
238 reply->readdata = 0;
239 reply->data = NULL;
240
241 length = read(fd, tmp, 1);
242
243 if(length <= 0)
244 {
245 if((length == -1) && ignoreErrno(errno))
246 goto nodata;
247 error_exit_client(server, length);
248 return;
249 }
250
251 reply->command = tmp[0];
252 }
253
254 for (replydef = slinkrpltab; replydef->handler; replydef++)
255 {
256 if((int)replydef->replyid == reply->command)
257 break;
258 }
259
260 /* we should be able to trust a local slink process...
261 * and if it sends an invalid command, that's a bug.. */
262 s_assert(replydef->handler);
263
264 if((replydef->flags & SLINKRPL_FLAG_DATA) && (reply->gotdatalen < 2))
265 {
266 /* we need a datalen u16 which we don't have yet... */
267 length = read(fd, len, (2 - reply->gotdatalen));
268 if(length <= 0)
269 {
270 if((length == -1) && ignoreErrno(errno))
271 goto nodata;
272 error_exit_client(server, length);
273 return;
274 }
275
276 if(reply->gotdatalen == 0)
277 {
278 reply->datalen = *len << 8;
279 reply->gotdatalen++;
280 length--;
281 len++;
282 }
283 if(length && (reply->gotdatalen == 1))
284 {
285 reply->datalen |= *len;
286 reply->gotdatalen++;
287 if(reply->datalen > 0)
288 reply->data = MyMalloc(reply->datalen);
289 }
290
291 if(reply->gotdatalen < 2)
292 return; /* wait for more data */
293 }
294
295 if(reply->readdata < reply->datalen) /* try to get any remaining data */
296 {
297 length = read(fd, (reply->data + reply->readdata),
298 (reply->datalen - reply->readdata));
299 if(length <= 0)
300 {
301 if((length == -1) && ignoreErrno(errno))
302 goto nodata;
303 error_exit_client(server, length);
304 return;
305 }
306
307 reply->readdata += length;
308 if(reply->readdata < reply->datalen)
309 return; /* wait for more data */
310 }
311
312 #ifdef USE_IODEBUG_HOOKS
313 hdata.client = server;
314 hdata.arg1 = NULL;
315 hdata.arg2 = reply->command;
316 hdata.data = NULL;
317 call_hook(h_iorecvctrl_id, &hdata);
318 #endif
319
320 /* we now have the command and any data, pass it off to the handler */
321 (*replydef->handler) (reply->command, reply->datalen, reply->data, server);
322
323 /* reset SlinkRpl */
324 if(reply->datalen > 0)
325 MyFree(reply->data);
326 reply->command = 0;
327
328 if(IsAnyDead(server))
329 return;
330
331 nodata:
332 /* If we get here, we need to register for another COMM_SELECT_READ */
333 comm_setselect(fd, FDLIST_SERVER, COMM_SELECT_READ, read_ctrl_packet, server, 0);
334 }
335
336 /*
337 * read_packet - Read a 'packet' of data from a connection and process it.
338 */
339 void
340 read_packet(int fd, void *data)
341 {
342 struct Client *client_p = data;
343 struct LocalUser *lclient_p = client_p->localClient;
344 int length = 0;
345 int lbuf_len;
346
347 int binary = 0;
348 #ifdef USE_IODEBUG_HOOKS
349 hook_data_int hdata;
350 #endif
351 if(IsAnyDead(client_p))
352 return;
353
354 /*
355 * Read some data. We *used to* do anti-flood protection here, but
356 * I personally think it makes the code too hairy to make sane.
357 * -- adrian
358 */
359 length = read(client_p->localClient->fd, readBuf, READBUF_SIZE);
360
361 if(length <= 0)
362 {
363 if((length == -1) && ignoreErrno(errno))
364 {
365 comm_setselect(client_p->localClient->fd, FDLIST_IDLECLIENT,
366 COMM_SELECT_READ, read_packet, client_p, 0);
367 return;
368 }
369 error_exit_client(client_p, length);
370 return;
371 }
372
373 #ifdef USE_IODEBUG_HOOKS
374 hdata.client = client_p;
375 hdata.arg1 = readBuf;
376 hdata.arg2 = length;
377 call_hook(h_iorecv_id, &hdata);
378 #endif
379
380 if(client_p->localClient->lasttime < CurrentTime)
381 client_p->localClient->lasttime = CurrentTime;
382 client_p->flags &= ~FLAGS_PINGSENT;
383
384 /*
385 * Before we even think of parsing what we just read, stick
386 * it on the end of the receive queue and do it when its
387 * turn comes around.
388 */
389 if(IsHandshake(client_p) || IsUnknown(client_p))
390 binary = 1;
391
392 lbuf_len = linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length, binary);
393
394 lclient_p->actually_read += lbuf_len;
395
396 if(IsAnyDead(client_p))
397 return;
398
399 /* Attempt to parse what we have */
400 parse_client_queued(client_p);
401
402 if(IsAnyDead(client_p))
403 return;
404
405 /* Check to make sure we're not flooding */
406 if(!IsAnyServer(client_p) &&
407 (linebuf_alloclen(&client_p->localClient->buf_recvq) > ConfigFileEntry.client_flood))
408 {
409 if(!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
410 {
411 exit_client(client_p, client_p, client_p, "Excess Flood");
412 return;
413 }
414 }
415
416 /* If we get here, we need to register for another COMM_SELECT_READ */
417 if(PARSE_AS_SERVER(client_p))
418 {
419 comm_setselect(client_p->localClient->fd, FDLIST_SERVER, COMM_SELECT_READ,
420 read_packet, client_p, 0);
421 }
422 else
423 {
424 comm_setselect(client_p->localClient->fd, FDLIST_IDLECLIENT,
425 COMM_SELECT_READ, read_packet, client_p, 0);
426 }
427 }
428
429 /*
430 * client_dopacket - copy packet to client buf and parse it
431 * client_p - pointer to client structure for which the buffer data
432 * applies.
433 * buffer - pointr to the buffer containing the newly read data
434 * length - number of valid bytes of data in the buffer
435 *
436 * Note:
437 * It is implicitly assumed that dopacket is called only
438 * with client_p of "local" variation, which contains all the
439 * necessary fields (buffer etc..)
440 */
441 void
442 client_dopacket(struct Client *client_p, char *buffer, size_t length)
443 {
444 s_assert(client_p != NULL);
445 s_assert(buffer != NULL);
446
447 if(client_p == NULL || buffer == NULL)
448 return;
449 if(IsAnyDead(client_p))
450 return;
451 /*
452 * Update messages received
453 */
454 ++me.localClient->receiveM;
455 ++client_p->localClient->receiveM;
456
457 /*
458 * Update bytes received
459 */
460 client_p->localClient->receiveB += length;
461
462 if(client_p->localClient->receiveB > 1023)
463 {
464 client_p->localClient->receiveK += (client_p->localClient->receiveB >> 10);
465 client_p->localClient->receiveB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
466 }
467
468 me.localClient->receiveB += length;
469
470 if(me.localClient->receiveB > 1023)
471 {
472 me.localClient->receiveK += (me.localClient->receiveB >> 10);
473 me.localClient->receiveB &= 0x03ff;
474 }
475
476 parse(client_p, buffer, buffer + length);
477 }