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