1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* packet.c: Packet handlers. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
#include "stdinc.h" |
25 |
#include "list.h" |
26 |
#include "s_bsd.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 "fdlist.h" |
34 |
#include "packet.h" |
35 |
#include "irc_string.h" |
36 |
#include "memory.h" |
37 |
#include "hook.h" |
38 |
#include "send.h" |
39 |
|
40 |
#define READBUF_SIZE 16384 |
41 |
|
42 |
struct Callback *iorecv_cb = NULL; |
43 |
struct Callback *iorecvctrl_cb = NULL; |
44 |
|
45 |
static char readBuf[READBUF_SIZE]; |
46 |
static void client_dopacket(struct Client *, char *, size_t); |
47 |
|
48 |
/* extract_one_line() |
49 |
* |
50 |
* inputs - pointer to a dbuf queue |
51 |
* - pointer to buffer to copy data to |
52 |
* output - length of <buffer> |
53 |
* side effects - one line is copied and removed from the dbuf |
54 |
*/ |
55 |
static int |
56 |
extract_one_line(struct dbuf_queue *qptr, char *buffer) |
57 |
{ |
58 |
struct dbuf_block *block; |
59 |
int line_bytes = 0, empty_bytes = 0, phase = 0; |
60 |
unsigned int idx; |
61 |
|
62 |
char c; |
63 |
dlink_node *ptr; |
64 |
|
65 |
/* |
66 |
* Phase 0: "empty" characters before the line |
67 |
* Phase 1: copying the line |
68 |
* Phase 2: "empty" characters after the line |
69 |
* (delete them as well and free some space in the dbuf) |
70 |
* |
71 |
* Empty characters are CR, LF and space (but, of course, not |
72 |
* in the middle of a line). We try to remove as much of them as we can, |
73 |
* since they simply eat server memory. |
74 |
* |
75 |
* --adx |
76 |
*/ |
77 |
DLINK_FOREACH(ptr, qptr->blocks.head) |
78 |
{ |
79 |
block = ptr->data; |
80 |
|
81 |
for (idx = 0; idx < block->size; idx++) |
82 |
{ |
83 |
c = block->data[idx]; |
84 |
if (IsEol(c) || (c == ' ' && phase != 1)) |
85 |
{ |
86 |
empty_bytes++; |
87 |
if (phase == 1) |
88 |
phase = 2; |
89 |
} |
90 |
else switch (phase) |
91 |
{ |
92 |
case 0: phase = 1; |
93 |
case 1: if (line_bytes++ < IRCD_BUFSIZE - 2) |
94 |
*buffer++ = c; |
95 |
break; |
96 |
case 2: *buffer = '\0'; |
97 |
dbuf_delete(qptr, line_bytes + empty_bytes); |
98 |
return IRCD_MIN(line_bytes, IRCD_BUFSIZE - 2); |
99 |
} |
100 |
} |
101 |
} |
102 |
|
103 |
/* |
104 |
* Now, if we haven't reached phase 2, ignore all line bytes |
105 |
* that we have read, since this is a partial line case. |
106 |
*/ |
107 |
if (phase != 2) |
108 |
line_bytes = 0; |
109 |
else |
110 |
*buffer = '\0'; |
111 |
|
112 |
/* Remove what is now unnecessary */ |
113 |
dbuf_delete(qptr, line_bytes + empty_bytes); |
114 |
return IRCD_MIN(line_bytes, IRCD_BUFSIZE - 2); |
115 |
} |
116 |
|
117 |
/* |
118 |
* parse_client_queued - parse client queued messages |
119 |
*/ |
120 |
static void |
121 |
parse_client_queued(struct Client *client_p) |
122 |
{ |
123 |
int dolen = 0; |
124 |
int checkflood = 1; |
125 |
struct LocalUser *lclient_p = client_p->localClient; |
126 |
|
127 |
if (IsUnknown(client_p)) |
128 |
{ |
129 |
int i = 0; |
130 |
|
131 |
for(;;) |
132 |
{ |
133 |
if (IsDefunct(client_p)) |
134 |
return; |
135 |
|
136 |
/* rate unknown clients at MAX_FLOOD per loop */ |
137 |
if (i >= MAX_FLOOD) |
138 |
break; |
139 |
|
140 |
dolen = extract_one_line(&lclient_p->buf_recvq, readBuf); |
141 |
if (dolen == 0) |
142 |
break; |
143 |
|
144 |
client_dopacket(client_p, readBuf, dolen); |
145 |
i++; |
146 |
|
147 |
/* if they've dropped out of the unknown state, break and move |
148 |
* to the parsing for their appropriate status. --fl |
149 |
*/ |
150 |
if(!IsUnknown(client_p)) |
151 |
break; |
152 |
} |
153 |
} |
154 |
|
155 |
if (IsServer(client_p) || IsConnecting(client_p) || IsHandshake(client_p)) |
156 |
{ |
157 |
while (1) |
158 |
{ |
159 |
if (IsDefunct(client_p)) |
160 |
return; |
161 |
if ((dolen = extract_one_line(&lclient_p->buf_recvq, |
162 |
readBuf)) == 0) |
163 |
break; |
164 |
client_dopacket(client_p, readBuf, dolen); |
165 |
} |
166 |
} |
167 |
else if (IsClient(client_p)) |
168 |
{ |
169 |
if (ConfigFileEntry.no_oper_flood && (HasUMode(client_p, UMODE_OPER) || IsCanFlood(client_p))) |
170 |
{ |
171 |
if (ConfigFileEntry.true_no_oper_flood) |
172 |
checkflood = -1; |
173 |
else |
174 |
checkflood = 0; |
175 |
} |
176 |
|
177 |
/* |
178 |
* Handle flood protection here - if we exceed our flood limit on |
179 |
* messages in this loop, we simply drop out of the loop prematurely. |
180 |
* -- adrian |
181 |
*/ |
182 |
for (;;) |
183 |
{ |
184 |
if (IsDefunct(client_p)) |
185 |
break; |
186 |
|
187 |
/* This flood protection works as follows: |
188 |
* |
189 |
* A client is given allow_read lines to send to the server. Every |
190 |
* time a line is parsed, sent_parsed is increased. sent_parsed |
191 |
* is decreased by 1 every time flood_recalc is called. |
192 |
* |
193 |
* Thus a client can 'burst' allow_read lines to the server, any |
194 |
* excess lines will be parsed one per flood_recalc() call. |
195 |
* |
196 |
* Therefore a client will be penalised more if they keep flooding, |
197 |
* as sent_parsed will always hover around the allow_read limit |
198 |
* and no 'bursts' will be permitted. |
199 |
*/ |
200 |
if (checkflood > 0) |
201 |
{ |
202 |
if(lclient_p->sent_parsed >= lclient_p->allow_read) |
203 |
break; |
204 |
} |
205 |
|
206 |
/* allow opers 4 times the amount of messages as users. why 4? |
207 |
* why not. :) --fl_ |
208 |
*/ |
209 |
else if (lclient_p->sent_parsed >= (4 * lclient_p->allow_read) && |
210 |
checkflood != -1) |
211 |
break; |
212 |
|
213 |
dolen = extract_one_line(&lclient_p->buf_recvq, readBuf); |
214 |
if (dolen == 0) |
215 |
break; |
216 |
|
217 |
client_dopacket(client_p, readBuf, dolen); |
218 |
lclient_p->sent_parsed++; |
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
/* flood_endgrace() |
224 |
* |
225 |
* marks the end of the clients grace period |
226 |
*/ |
227 |
void |
228 |
flood_endgrace(struct Client *client_p) |
229 |
{ |
230 |
SetFloodDone(client_p); |
231 |
|
232 |
/* Drop their flood limit back down */ |
233 |
client_p->localClient->allow_read = MAX_FLOOD; |
234 |
|
235 |
/* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST, |
236 |
* so reset it. |
237 |
*/ |
238 |
client_p->localClient->sent_parsed = 0; |
239 |
} |
240 |
|
241 |
/* |
242 |
* flood_recalc |
243 |
* |
244 |
* recalculate the number of allowed flood lines. this should be called |
245 |
* once a second on any given client. We then attempt to flush some data. |
246 |
*/ |
247 |
void |
248 |
flood_recalc(fde_t *fd, void *data) |
249 |
{ |
250 |
struct Client *client_p = data; |
251 |
struct LocalUser *lclient_p = client_p->localClient; |
252 |
|
253 |
/* allow a bursting client their allocation per second, allow |
254 |
* a client whos flooding an extra 2 per second |
255 |
*/ |
256 |
if (IsFloodDone(client_p)) |
257 |
lclient_p->sent_parsed -= 2; |
258 |
else |
259 |
lclient_p->sent_parsed = 0; |
260 |
|
261 |
if (lclient_p->sent_parsed < 0) |
262 |
lclient_p->sent_parsed = 0; |
263 |
|
264 |
parse_client_queued(client_p); |
265 |
|
266 |
/* And now, try flushing .. */ |
267 |
if (!IsDead(client_p)) |
268 |
{ |
269 |
/* and finally, reset the flood check */ |
270 |
comm_setflush(fd, 1000, flood_recalc, client_p); |
271 |
} |
272 |
} |
273 |
|
274 |
/* |
275 |
* read_ctrl_packet - Read a 'packet' of data from a servlink control |
276 |
* link and process it. |
277 |
*/ |
278 |
void |
279 |
read_ctrl_packet(fde_t *fd, void *data) |
280 |
{ |
281 |
struct Client *server = data; |
282 |
struct LocalUser *lserver = server->localClient; |
283 |
struct SlinkRpl *reply; |
284 |
int length = 0; |
285 |
unsigned char tmp[2]; |
286 |
unsigned char *len = tmp; |
287 |
struct SlinkRplDef *replydef; |
288 |
|
289 |
assert(lserver != NULL); |
290 |
|
291 |
reply = &lserver->slinkrpl; |
292 |
|
293 |
if (IsDefunct(server)) |
294 |
return; |
295 |
|
296 |
if (!reply->command) |
297 |
{ |
298 |
reply->gotdatalen = 0; |
299 |
reply->readdata = 0; |
300 |
reply->data = NULL; |
301 |
|
302 |
length = recv(fd->fd, tmp, 1, 0); |
303 |
|
304 |
if (length <= 0) |
305 |
{ |
306 |
if ((length == -1) && ignoreErrno(errno)) |
307 |
goto nodata; |
308 |
dead_link_on_read(server, length); |
309 |
return; |
310 |
} |
311 |
reply->command = tmp[0]; |
312 |
} |
313 |
|
314 |
for (replydef = slinkrpltab; replydef->handler; replydef++) |
315 |
{ |
316 |
if (replydef->replyid == (unsigned int)reply->command) |
317 |
break; |
318 |
} |
319 |
|
320 |
/* we should be able to trust a local slink process... |
321 |
* and if it sends an invalid command, that's a bug.. */ |
322 |
assert(replydef->handler); |
323 |
|
324 |
if ((replydef->flags & SLINKRPL_FLAG_DATA) && (reply->gotdatalen < 2)) |
325 |
{ |
326 |
/* we need a datalen u16 which we don't have yet... */ |
327 |
length = recv(fd->fd, len, (2 - reply->gotdatalen), 0); |
328 |
if (length <= 0) |
329 |
{ |
330 |
if ((length == -1) && ignoreErrno(errno)) |
331 |
goto nodata; |
332 |
dead_link_on_read(server, length); |
333 |
return; |
334 |
} |
335 |
|
336 |
if (reply->gotdatalen == 0) |
337 |
{ |
338 |
reply->datalen = *len << 8; |
339 |
reply->gotdatalen++; |
340 |
length--; |
341 |
len++; |
342 |
} |
343 |
if (length && (reply->gotdatalen == 1)) |
344 |
{ |
345 |
reply->datalen |= *len; |
346 |
reply->gotdatalen++; |
347 |
if (reply->datalen > 0) |
348 |
reply->data = MyMalloc(reply->datalen); |
349 |
} |
350 |
|
351 |
if (reply->gotdatalen < 2) |
352 |
return; /* wait for more data */ |
353 |
} |
354 |
|
355 |
if (reply->readdata < reply->datalen) /* try to get any remaining data */ |
356 |
{ |
357 |
length = recv(fd->fd, (reply->data + reply->readdata), |
358 |
(reply->datalen - reply->readdata), 0); |
359 |
if (length <= 0) |
360 |
{ |
361 |
if ((length == -1) && ignoreErrno(errno)) |
362 |
goto nodata; |
363 |
dead_link_on_read(server, length); |
364 |
return; |
365 |
} |
366 |
|
367 |
reply->readdata += length; |
368 |
if (reply->readdata < reply->datalen) |
369 |
return; /* wait for more data */ |
370 |
} |
371 |
|
372 |
execute_callback(iorecvctrl_cb, server, reply->command); |
373 |
|
374 |
/* we now have the command and any data, pass it off to the handler */ |
375 |
(*replydef->handler)(reply->command, reply->datalen, reply->data, server); |
376 |
|
377 |
/* reset SlinkRpl */ |
378 |
if (reply->datalen > 0) |
379 |
MyFree(reply->data); |
380 |
reply->command = 0; |
381 |
|
382 |
if (IsDead(server)) |
383 |
return; |
384 |
|
385 |
nodata: |
386 |
/* If we get here, we need to register for another COMM_SELECT_READ */ |
387 |
comm_setselect(fd, COMM_SELECT_READ, read_ctrl_packet, server, 0); |
388 |
} |
389 |
|
390 |
/* |
391 |
* iorecv_default - append a packet to the recvq dbuf |
392 |
*/ |
393 |
void * |
394 |
iorecv_default(va_list args) |
395 |
{ |
396 |
struct Client *client_p = va_arg(args, struct Client *); |
397 |
int length = va_arg(args, int); |
398 |
char *buf = va_arg(args, char *); |
399 |
|
400 |
dbuf_put(&client_p->localClient->buf_recvq, buf, length); |
401 |
return NULL; |
402 |
} |
403 |
|
404 |
/* |
405 |
* read_packet - Read a 'packet' of data from a connection and process it. |
406 |
*/ |
407 |
void |
408 |
read_packet(fde_t *fd, void *data) |
409 |
{ |
410 |
struct Client *client_p = data; |
411 |
int length = 0; |
412 |
|
413 |
if (IsDefunct(client_p)) |
414 |
return; |
415 |
|
416 |
/* |
417 |
* Read some data. We *used to* do anti-flood protection here, but |
418 |
* I personally think it makes the code too hairy to make sane. |
419 |
* -- adrian |
420 |
*/ |
421 |
do { |
422 |
#ifdef HAVE_LIBCRYPTO |
423 |
if (fd->ssl) |
424 |
{ |
425 |
length = SSL_read(fd->ssl, readBuf, READBUF_SIZE); |
426 |
|
427 |
/* translate openssl error codes, sigh */ |
428 |
if (length < 0) |
429 |
switch (SSL_get_error(fd->ssl, length)) |
430 |
{ |
431 |
case SSL_ERROR_WANT_WRITE: |
432 |
fd->flags.pending_read = 1; |
433 |
SetSendqBlocked(client_p); |
434 |
comm_setselect(fd, COMM_SELECT_WRITE, (PF *) sendq_unblocked, |
435 |
client_p, 0); |
436 |
return; |
437 |
case SSL_ERROR_WANT_READ: |
438 |
errno = EWOULDBLOCK; |
439 |
case SSL_ERROR_SYSCALL: |
440 |
break; |
441 |
case SSL_ERROR_SSL: |
442 |
if (errno == EAGAIN) |
443 |
break; |
444 |
default: |
445 |
length = errno = 0; |
446 |
} |
447 |
} |
448 |
else |
449 |
#endif |
450 |
{ |
451 |
length = recv(fd->fd, readBuf, READBUF_SIZE, 0); |
452 |
} |
453 |
|
454 |
if (length <= 0) |
455 |
{ |
456 |
/* |
457 |
* If true, then we can recover from this error. Just jump out of |
458 |
* the loop and re-register a new io-request. |
459 |
*/ |
460 |
if (length < 0 && ignoreErrno(errno)) |
461 |
break; |
462 |
|
463 |
dead_link_on_read(client_p, length); |
464 |
return; |
465 |
} |
466 |
|
467 |
execute_callback(iorecv_cb, client_p, length, readBuf); |
468 |
|
469 |
if (client_p->localClient->lasttime < CurrentTime) |
470 |
client_p->localClient->lasttime = CurrentTime; |
471 |
if (client_p->localClient->lasttime > client_p->localClient->since) |
472 |
client_p->localClient->since = CurrentTime; |
473 |
ClearPingSent(client_p); |
474 |
|
475 |
/* Attempt to parse what we have */ |
476 |
parse_client_queued(client_p); |
477 |
|
478 |
if (IsDefunct(client_p)) |
479 |
return; |
480 |
|
481 |
/* Check to make sure we're not flooding */ |
482 |
/* TBD - ConfigFileEntry.client_flood should be a size_t */ |
483 |
if (!(IsServer(client_p) || IsHandshake(client_p) || IsConnecting(client_p)) |
484 |
&& (dbuf_length(&client_p->localClient->buf_recvq) > |
485 |
(unsigned int)ConfigFileEntry.client_flood)) |
486 |
{ |
487 |
if (!(ConfigFileEntry.no_oper_flood && HasUMode(client_p, UMODE_OPER))) |
488 |
{ |
489 |
exit_client(client_p, client_p, "Excess Flood"); |
490 |
return; |
491 |
} |
492 |
} |
493 |
} |
494 |
#ifdef HAVE_LIBCRYPTO |
495 |
while (length == sizeof(readBuf) || fd->ssl); |
496 |
#else |
497 |
while (length == sizeof(readBuf)); |
498 |
#endif |
499 |
|
500 |
/* If we get here, we need to register for another COMM_SELECT_READ */ |
501 |
comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0); |
502 |
} |
503 |
|
504 |
/* |
505 |
* client_dopacket - copy packet to client buf and parse it |
506 |
* client_p - pointer to client structure for which the buffer data |
507 |
* applies. |
508 |
* buffer - pointr to the buffer containing the newly read data |
509 |
* length - number of valid bytes of data in the buffer |
510 |
* |
511 |
* Note: |
512 |
* It is implicitly assumed that dopacket is called only |
513 |
* with client_p of "local" variation, which contains all the |
514 |
* necessary fields (buffer etc..) |
515 |
*/ |
516 |
static void |
517 |
client_dopacket(struct Client *client_p, char *buffer, size_t length) |
518 |
{ |
519 |
/* |
520 |
* Update messages received |
521 |
*/ |
522 |
++me.localClient->recv.messages; |
523 |
++client_p->localClient->recv.messages; |
524 |
|
525 |
/* |
526 |
* Update bytes received |
527 |
*/ |
528 |
client_p->localClient->recv.bytes += length; |
529 |
me.localClient->recv.bytes += length; |
530 |
|
531 |
parse(client_p, buffer, buffer + length); |
532 |
} |