ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/packet.c
Revision: 3274
Committed: Sun Apr 6 12:22:23 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 10555 byte(s)
Log Message:
- Clean up redundant/unused header includes

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
5 *
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
22 /*! \file packet.c
23 * \brief Packet handlers.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "s_bsd.h"
30 #include "conf.h"
31 #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 "send.h"
40 #include "s_misc.h"
41
42 #define READBUF_SIZE 16384
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 int line_bytes = 0, empty_bytes = 0, phase = 0;
58 unsigned int idx = 0;
59 dlink_node *ptr = NULL;
60
61 /*
62 * Phase 0: "empty" characters before the line
63 * Phase 1: copying the line
64 * Phase 2: "empty" characters after the line
65 * (delete them as well and free some space in the dbuf)
66 *
67 * Empty characters are CR, LF and space (but, of course, not
68 * in the middle of a line). We try to remove as much of them as we can,
69 * since they simply eat server memory.
70 *
71 * --adx
72 */
73 DLINK_FOREACH(ptr, qptr->blocks.head)
74 {
75 struct dbuf_block *block = ptr->data;
76
77 if (ptr == qptr->blocks.head)
78 idx = qptr->pos;
79 else
80 idx = 0;
81
82 for (; idx < block->size; ++idx)
83 {
84 char c = block->data[idx];
85
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 * 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 else if (IsClient(client_p))
169 {
170 if (ConfigFileEntry.no_oper_flood && (HasUMode(client_p, UMODE_OPER) || 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_packet - Read a 'packet' of data from a connection and process it.
277 */
278 void
279 read_packet(fde_t *fd, void *data)
280 {
281 struct Client *client_p = data;
282 int length = 0;
283
284 if (IsDefunct(client_p))
285 return;
286
287 /*
288 * Read some data. We *used to* do anti-flood protection here, but
289 * I personally think it makes the code too hairy to make sane.
290 * -- adrian
291 */
292 do
293 {
294 #ifdef HAVE_LIBCRYPTO
295 if (fd->ssl)
296 {
297 length = SSL_read(fd->ssl, readBuf, READBUF_SIZE);
298
299 /* translate openssl error codes, sigh */
300 if (length < 0)
301 switch (SSL_get_error(fd->ssl, length))
302 {
303 case SSL_ERROR_WANT_WRITE:
304 comm_setselect(fd, COMM_SELECT_WRITE, (PF *)sendq_unblocked, client_p, 0);
305 return;
306 case SSL_ERROR_WANT_READ:
307 errno = EWOULDBLOCK;
308 case SSL_ERROR_SYSCALL:
309 break;
310 case SSL_ERROR_SSL:
311 if (errno == EAGAIN)
312 break;
313 default:
314 length = errno = 0;
315 }
316 }
317 else
318 #endif
319 {
320 length = recv(fd->fd, readBuf, READBUF_SIZE, 0);
321 }
322
323 if (length <= 0)
324 {
325 /*
326 * If true, then we can recover from this error. Just jump out of
327 * the loop and re-register a new io-request.
328 */
329 if (length < 0 && ignoreErrno(errno))
330 break;
331
332 dead_link_on_read(client_p, length);
333 return;
334 }
335
336 dbuf_put(&client_p->localClient->buf_recvq, readBuf, length);
337
338 if (client_p->localClient->lasttime < CurrentTime)
339 client_p->localClient->lasttime = CurrentTime;
340 if (client_p->localClient->lasttime > client_p->localClient->since)
341 client_p->localClient->since = CurrentTime;
342 ClearPingSent(client_p);
343
344 /* Attempt to parse what we have */
345 parse_client_queued(client_p);
346
347 if (IsDefunct(client_p))
348 return;
349
350 /* Check to make sure we're not flooding */
351 if (!(IsServer(client_p) || IsHandshake(client_p) || IsConnecting(client_p))
352 && (dbuf_length(&client_p->localClient->buf_recvq) >
353 get_recvq(&client_p->localClient->confs)))
354 {
355 if (!(ConfigFileEntry.no_oper_flood && HasUMode(client_p, UMODE_OPER)))
356 {
357 exit_client(client_p, "Excess Flood");
358 return;
359 }
360 }
361 }
362 #ifdef HAVE_LIBCRYPTO
363 while (length == sizeof(readBuf) || fd->ssl);
364 #else
365 while (length == sizeof(readBuf));
366 #endif
367
368 /* If we get here, we need to register for another COMM_SELECT_READ */
369 comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
370 }
371
372 /*
373 * client_dopacket - copy packet to client buf and parse it
374 * client_p - pointer to client structure for which the buffer data
375 * applies.
376 * buffer - pointr to the buffer containing the newly read data
377 * length - number of valid bytes of data in the buffer
378 *
379 * Note:
380 * It is implicitly assumed that dopacket is called only
381 * with client_p of "local" variation, which contains all the
382 * necessary fields (buffer etc..)
383 */
384 static void
385 client_dopacket(struct Client *client_p, char *buffer, size_t length)
386 {
387 /*
388 * Update messages received
389 */
390 ++me.localClient->recv.messages;
391 ++client_p->localClient->recv.messages;
392
393 /*
394 * Update bytes received
395 */
396 client_p->localClient->recv.bytes += length;
397 me.localClient->recv.bytes += length;
398
399 parse(client_p, buffer, buffer + length);
400 }

Properties

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