/[svn]/ircd-hybrid-7.2/src/packet.c
ViewVC logotype

Contents of /ircd-hybrid-7.2/src/packet.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 428 - (show annotations)
Sat Feb 11 12:21:51 2006 UTC (13 years, 8 months ago) by michael
File MIME type: text/x-chdr
File size: 13836 byte(s)
- Fixed bug where all ssl clients would get disconnected in certain situations.
  (reported by many people)

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

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.26