]> jfr.im git - solanum.git/blame - src/packet.c
Merge pull request #33 from Argure/master
[solanum.git] / src / packet.c
CommitLineData
54ac8b60
VY
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * packet.c: Packet handlers.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: packet.c 3446 2007-05-14 22:21:16Z jilles $
25 */
26#include "stdinc.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 "packet.h"
4562c604 34#include "match.h"
54ac8b60
VY
35#include "hook.h"
36#include "send.h"
37
38static char readBuf[READBUF_SIZE];
39static void client_dopacket(struct Client *client_p, char *buffer, size_t length);
40
54ac8b60
VY
41/*
42 * parse_client_queued - parse client queued messages
43 */
44static void
45parse_client_queued(struct Client *client_p)
46{
47 int dolen = 0;
d182b854 48 int allow_read;
54ac8b60
VY
49
50 if(IsAnyDead(client_p))
51 return;
52
53 if(IsUnknown(client_p))
54 {
d182b854 55 allow_read = ConfigFileEntry.client_flood_burst_max;
54ac8b60
VY
56 for (;;)
57 {
d182b854 58 if(client_p->localClient->sent_parsed >= allow_read)
54ac8b60
VY
59 break;
60
60eb0cdc 61 dolen = rb_linebuf_get(&client_p->localClient->
54ac8b60
VY
62 buf_recvq, readBuf, READBUF_SIZE,
63 LINEBUF_COMPLETE, LINEBUF_PARSED);
64
65 if(dolen <= 0 || IsDead(client_p))
66 break;
67
68 client_dopacket(client_p, readBuf, dolen);
69 client_p->localClient->sent_parsed++;
70
71 /* He's dead cap'n */
72 if(IsAnyDead(client_p))
73 return;
74 /* if theyve dropped out of the unknown state, break and move
75 * to the parsing for their appropriate status. --fl
76 */
77 if(!IsUnknown(client_p))
78 {
79 /* reset their flood limits, they're now
80 * graced to flood
81 */
82 client_p->localClient->sent_parsed = 0;
83 break;
84 }
85
86 }
5a72f20c
JT
87 /* If sent_parsed is impossibly high, drop it down.
88 * This is useful if the configuration is changed.
89 */
90 if(client_p->localClient->sent_parsed > allow_read)
91 client_p->localClient->sent_parsed = allow_read;
54ac8b60
VY
92 }
93
94 if(IsAnyServer(client_p) || IsExemptFlood(client_p))
95 {
60eb0cdc 96 while (!IsAnyDead(client_p) && (dolen = rb_linebuf_get(&client_p->localClient->buf_recvq,
54ac8b60
VY
97 readBuf, READBUF_SIZE, LINEBUF_COMPLETE,
98 LINEBUF_PARSED)) > 0)
99 {
100 client_dopacket(client_p, readBuf, dolen);
101 }
102 }
103 else if(IsClient(client_p))
104 {
d182b854
JT
105 if(IsFloodDone(client_p))
106 allow_read = ConfigFileEntry.client_flood_burst_max;
107 else
108 allow_read = ConfigFileEntry.client_flood_burst_rate;
a75bf40d 109 allow_read *= ConfigFileEntry.client_flood_message_time;
d182b854
JT
110 /* allow opers 4 times the amount of messages as users. why 4?
111 * why not. :) --fl_
112 */
54ac8b60 113 if(IsOper(client_p) && ConfigFileEntry.no_oper_flood)
d182b854 114 allow_read *= 4;
54ac8b60
VY
115 /*
116 * Handle flood protection here - if we exceed our flood limit on
117 * messages in this loop, we simply drop out of the loop prematurely.
118 * -- adrian
119 */
120 for (;;)
121 {
122 /* This flood protection works as follows:
123 *
124 * A client is given allow_read lines to send to the server. Every
125 * time a line is parsed, sent_parsed is increased. sent_parsed
126 * is decreased by 1 every time flood_recalc is called.
127 *
128 * Thus a client can 'burst' allow_read lines to the server, any
129 * excess lines will be parsed one per flood_recalc() call.
130 *
131 * Therefore a client will be penalised more if they keep flooding,
132 * as sent_parsed will always hover around the allow_read limit
133 * and no 'bursts' will be permitted.
134 */
d182b854 135 if(client_p->localClient->sent_parsed >= allow_read)
54ac8b60
VY
136 break;
137
60eb0cdc 138 dolen = rb_linebuf_get(&client_p->localClient->
54ac8b60
VY
139 buf_recvq, readBuf, READBUF_SIZE,
140 LINEBUF_COMPLETE, LINEBUF_PARSED);
141
142 if(!dolen)
143 break;
144
145 client_dopacket(client_p, readBuf, dolen);
146 if(IsAnyDead(client_p))
147 return;
e6e54763
SB
148
149 client_p->localClient->sent_parsed += ConfigFileEntry.client_flood_message_time;
54ac8b60 150 }
5a72f20c
JT
151 /* If sent_parsed is impossibly high, drop it down.
152 * This is useful if the configuration is changed.
153 */
154 if(client_p->localClient->sent_parsed > allow_read +
155 ConfigFileEntry.client_flood_message_time - 1)
156 client_p->localClient->sent_parsed = allow_read +
157 ConfigFileEntry.client_flood_message_time - 1;
54ac8b60
VY
158 }
159}
160
161/* flood_endgrace()
162 *
163 * marks the end of the clients grace period
164 */
165void
166flood_endgrace(struct Client *client_p)
167{
168 SetFloodDone(client_p);
169
d182b854
JT
170 /* sent_parsed could be way over client_flood_burst_max but under
171 * client_flood_burst_rate so reset it.
54ac8b60
VY
172 */
173 client_p->localClient->sent_parsed = 0;
174}
175
1087485c
JT
176/*
177 * flood_recalc
178 *
179 * recalculate the number of allowed flood lines. this should be called
180 * once a second on any given client. We then attempt to flush some data.
181 */
182void
183flood_recalc(void *unused)
184{
185 rb_dlink_node *ptr, *next;
186 struct Client *client_p;
187
188 RB_DLINK_FOREACH_SAFE(ptr, next, lclient_list.head)
189 {
190 client_p = ptr->data;
191
af41336a 192 if(rb_unlikely(IsMe(client_p)))
1087485c
JT
193 continue;
194
af41336a 195 if(rb_unlikely(client_p->localClient == NULL))
1087485c
JT
196 continue;
197
198 if(IsFloodDone(client_p))
e6e54763 199 client_p->localClient->sent_parsed -= ConfigFileEntry.client_flood_message_num;
1087485c
JT
200 else
201 client_p->localClient->sent_parsed = 0;
202
203 if(client_p->localClient->sent_parsed < 0)
204 client_p->localClient->sent_parsed = 0;
205
1087485c
JT
206 parse_client_queued(client_p);
207
af41336a 208 if(rb_unlikely(IsAnyDead(client_p)))
1087485c
JT
209 continue;
210
211 }
212
213 RB_DLINK_FOREACH_SAFE(ptr, next, unknown_list.head)
214 {
215 client_p = ptr->data;
216
217 if(client_p->localClient == NULL)
218 continue;
219
220 client_p->localClient->sent_parsed--;
221
222 if(client_p->localClient->sent_parsed < 0)
223 client_p->localClient->sent_parsed = 0;
224
1087485c
JT
225 parse_client_queued(client_p);
226 }
54ac8b60
VY
227}
228
54ac8b60
VY
229/*
230 * read_packet - Read a 'packet' of data from a connection and process it.
231 */
232void
d91ce397 233read_packet(rb_fde_t * F, void *data)
54ac8b60
VY
234{
235 struct Client *client_p = data;
54ac8b60
VY
236 int length = 0;
237 int lbuf_len;
238
239 int binary = 0;
240#ifdef USE_IODEBUG_HOOKS
241 hook_data_int hdata;
242#endif
54ac8b60 243
d91ce397 244 while(1)
54ac8b60 245 {
d91ce397
VY
246 if(IsAnyDead(client_p))
247 return;
248
249 /*
250 * Read some data. We *used to* do anti-flood protection here, but
251 * I personally think it makes the code too hairy to make sane.
252 * -- adrian
253 */
254 length = rb_read(client_p->localClient->F, readBuf, READBUF_SIZE);
255
9f316874 256 if(length < 0)
54ac8b60 257 {
9f316874 258 if(rb_ignore_errno(errno))
d91ce397
VY
259 rb_setselect(client_p->localClient->F,
260 RB_SELECT_READ, read_packet, client_p);
9f316874 261 else
d91ce397
VY
262 error_exit_client(client_p, length);
263 return;
264 }
9f316874 265 else if(length == 0)
d91ce397
VY
266 {
267 error_exit_client(client_p, length);
54ac8b60
VY
268 return;
269 }
54ac8b60
VY
270
271#ifdef USE_IODEBUG_HOOKS
d91ce397
VY
272 hdata.client = client_p;
273 hdata.arg1 = readBuf;
274 hdata.arg2 = length;
275 call_hook(h_iorecv_id, &hdata);
54ac8b60
VY
276#endif
277
d91ce397
VY
278 if(client_p->localClient->lasttime < rb_current_time())
279 client_p->localClient->lasttime = rb_current_time();
280 client_p->flags &= ~FLAGS_PINGSENT;
54ac8b60 281
d91ce397
VY
282 /*
283 * Before we even think of parsing what we just read, stick
284 * it on the end of the receive queue and do it when its
285 * turn comes around.
286 */
287 if(IsHandshake(client_p) || IsUnknown(client_p))
288 binary = 1;
54ac8b60 289
d91ce397 290 lbuf_len = rb_linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length, binary);
54ac8b60 291
d91ce397
VY
292 if(IsAnyDead(client_p))
293 return;
294
295 /* Attempt to parse what we have */
296 parse_client_queued(client_p);
54ac8b60 297
d91ce397 298 if(IsAnyDead(client_p))
54ac8b60 299 return;
d91ce397
VY
300
301 /* Check to make sure we're not flooding */
302 if(!IsAnyServer(client_p) &&
e6e54763 303 (rb_linebuf_alloclen(&client_p->localClient->buf_recvq) > ConfigFileEntry.client_flood_max_lines))
d91ce397
VY
304 {
305 if(!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
306 {
307 exit_client(client_p, client_p, client_p, "Excess Flood");
308 return;
309 }
54ac8b60 310 }
54ac8b60 311
d91ce397
VY
312 /* bail if short read */
313 if(length < READBUF_SIZE)
314 {
315 rb_setselect(client_p->localClient->F, RB_SELECT_READ, read_packet, client_p);
316 return;
317 }
54ac8b60
VY
318 }
319}
320
321/*
322 * client_dopacket - copy packet to client buf and parse it
323 * client_p - pointer to client structure for which the buffer data
324 * applies.
325 * buffer - pointr to the buffer containing the newly read data
326 * length - number of valid bytes of data in the buffer
327 *
328 * Note:
329 * It is implicitly assumed that dopacket is called only
330 * with client_p of "local" variation, which contains all the
331 * necessary fields (buffer etc..)
332 */
333void
334client_dopacket(struct Client *client_p, char *buffer, size_t length)
335{
336 s_assert(client_p != NULL);
337 s_assert(buffer != NULL);
338
339 if(client_p == NULL || buffer == NULL)
340 return;
341 if(IsAnyDead(client_p))
342 return;
343 /*
344 * Update messages received
345 */
346 ++me.localClient->receiveM;
347 ++client_p->localClient->receiveM;
348
349 /*
350 * Update bytes received
351 */
352 client_p->localClient->receiveB += length;
353
354 if(client_p->localClient->receiveB > 1023)
355 {
356 client_p->localClient->receiveK += (client_p->localClient->receiveB >> 10);
357 client_p->localClient->receiveB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
358 }
359
360 me.localClient->receiveB += length;
361
362 if(me.localClient->receiveB > 1023)
363 {
364 me.localClient->receiveK += (me.localClient->receiveB >> 10);
365 me.localClient->receiveB &= 0x03ff;
366 }
367
368 parse(client_p, buffer, buffer + length);
369}