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