ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/packet.c
Revision: 2916
Committed: Sat Jan 25 21:09:18 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 10512 byte(s)
Log Message:
- Clean up all files in include/ (fixed indentation, removed whitespaces/tabs)
- Fixed copyright years

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 1997-2014 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * USA
20     */
21 michael 2916
22     /*! \file packet.c
23     * \brief Packet handlers.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 1011 #include "list.h"
29 adx 30 #include "s_bsd.h"
30 michael 1309 #include "conf.h"
31 adx 30 #include "s_serv.h"
32     #include "client.h"
33     #include "ircd.h"
34     #include "parse.h"
35     #include "fdlist.h"
36     #include "packet.h"
37     #include "irc_string.h"
38     #include "memory.h"
39     #include "hook.h"
40     #include "send.h"
41 michael 1243 #include "s_misc.h"
42 adx 30
43     #define READBUF_SIZE 16384
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 michael 2916 {
123 adx 30 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 michael 2916 for (; ;)
132 adx 30 {
133     if (IsDefunct(client_p))
134 michael 2916 return;
135 adx 30
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 michael 2916 break;
143 adx 30
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 adx 163 else if (IsClient(client_p))
168 adx 30 {
169 michael 1219 if (ConfigFileEntry.no_oper_flood && (HasUMode(client_p, UMODE_OPER) || IsCanFlood(client_p)))
170 adx 30 {
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 michael 2916 for (; ;)
183 adx 30 {
184     if (IsDefunct(client_p))
185 michael 2916 break;
186 adx 30
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 michael 2916
206 adx 30 /* 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 michael 2916
253 adx 30 /* 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 michael 2916
261 adx 30 if (lclient_p->sent_parsed < 0)
262     lclient_p->sent_parsed = 0;
263 michael 2916
264 adx 30 parse_client_queued(client_p);
265 michael 2916
266 adx 30 /* 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_packet - Read a 'packet' of data from a connection and process it.
276     */
277     void
278     read_packet(fde_t *fd, void *data)
279     {
280     struct Client *client_p = data;
281     int length = 0;
282    
283     if (IsDefunct(client_p))
284     return;
285    
286     /*
287     * Read some data. We *used to* do anti-flood protection here, but
288     * I personally think it makes the code too hairy to make sane.
289     * -- adrian
290     */
291 michael 2916 do
292     {
293 adx 30 #ifdef HAVE_LIBCRYPTO
294     if (fd->ssl)
295     {
296     length = SSL_read(fd->ssl, readBuf, READBUF_SIZE);
297    
298     /* translate openssl error codes, sigh */
299     if (length < 0)
300     switch (SSL_get_error(fd->ssl, length))
301 michael 2881 {
302 adx 30 case SSL_ERROR_WANT_WRITE:
303 michael 2881 comm_setselect(fd, COMM_SELECT_WRITE, (PF *)sendq_unblocked, client_p, 0);
304     return;
305     case SSL_ERROR_WANT_READ:
306     errno = EWOULDBLOCK;
307 adx 30 case SSL_ERROR_SYSCALL:
308 michael 2881 break;
309 michael 428 case SSL_ERROR_SSL:
310     if (errno == EAGAIN)
311     break;
312 adx 30 default:
313 michael 2881 length = errno = 0;
314     }
315 adx 30 }
316     else
317     #endif
318     {
319     length = recv(fd->fd, readBuf, READBUF_SIZE, 0);
320     }
321    
322     if (length <= 0)
323     {
324     /*
325     * If true, then we can recover from this error. Just jump out of
326     * the loop and re-register a new io-request.
327     */
328     if (length < 0 && ignoreErrno(errno))
329     break;
330    
331     dead_link_on_read(client_p, length);
332     return;
333     }
334    
335 michael 1798 dbuf_put(&client_p->localClient->buf_recvq, readBuf, length);
336 adx 30
337 michael 1241 if (client_p->localClient->lasttime < CurrentTime)
338     client_p->localClient->lasttime = CurrentTime;
339     if (client_p->localClient->lasttime > client_p->localClient->since)
340     client_p->localClient->since = CurrentTime;
341 adx 30 ClearPingSent(client_p);
342    
343     /* Attempt to parse what we have */
344     parse_client_queued(client_p);
345    
346     if (IsDefunct(client_p))
347     return;
348    
349     /* Check to make sure we're not flooding */
350     if (!(IsServer(client_p) || IsHandshake(client_p) || IsConnecting(client_p))
351     && (dbuf_length(&client_p->localClient->buf_recvq) >
352 michael 1632 get_recvq(&client_p->localClient->confs)))
353 adx 30 {
354 michael 1219 if (!(ConfigFileEntry.no_oper_flood && HasUMode(client_p, UMODE_OPER)))
355 adx 30 {
356     exit_client(client_p, client_p, "Excess Flood");
357     return;
358     }
359     }
360     }
361     #ifdef HAVE_LIBCRYPTO
362     while (length == sizeof(readBuf) || fd->ssl);
363     #else
364     while (length == sizeof(readBuf));
365     #endif
366    
367     /* If we get here, we need to register for another COMM_SELECT_READ */
368     comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
369     }
370    
371     /*
372     * client_dopacket - copy packet to client buf and parse it
373     * client_p - pointer to client structure for which the buffer data
374     * applies.
375     * buffer - pointr to the buffer containing the newly read data
376     * length - number of valid bytes of data in the buffer
377     *
378     * Note:
379     * It is implicitly assumed that dopacket is called only
380     * with client_p of "local" variation, which contains all the
381     * necessary fields (buffer etc..)
382     */
383     static void
384     client_dopacket(struct Client *client_p, char *buffer, size_t length)
385     {
386 michael 2916 /*
387 adx 30 * Update messages received
388     */
389     ++me.localClient->recv.messages;
390     ++client_p->localClient->recv.messages;
391    
392 michael 2916 /*
393 adx 30 * Update bytes received
394     */
395     client_p->localClient->recv.bytes += length;
396     me.localClient->recv.bytes += length;
397    
398     parse(client_p, buffer, buffer + length);
399     }

Properties

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