ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/packet.c
Revision: 1798
Committed: Sun Mar 31 17:09:50 2013 UTC (12 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 10511 byte(s)
Log Message:
- Cleanup/reorganize header file layout
- Fixed naming convention in some places

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

Properties

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