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