ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/packet.c
Revision: 1011
Committed: Fri Sep 18 10:14:09 2009 UTC (15 years, 11 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/packet.c
File size: 13739 byte(s)
Log Message:
- move list manipulation routines from tools.c to list.c
- mem_frob() goes to memory.c
- sort out redundant/unneeded header includes

File Contents

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

Properties

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