ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/packet.c
Revision: 1516
Committed: Thu Aug 30 15:08:36 2012 UTC (11 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 10827 byte(s)
Log Message:
- Removed general::client_flood configuration option and added the
  new 'recvq' configuration directive to class{} blocks.
  The max size of a receive queue can be seen in "STATS Y" for each class
- Update NEWS

File Contents

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

Properties

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