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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22 * USA
23 *
24 * $Id: packet.c 26688 2009-10-07 20:43:14Z leeh $
25 */
26 #include "stdinc.h"
27 #include "struct.h"
28 #include "s_conf.h"
29 #include "s_serv.h"
30 #include "client.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_log.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 /*
44 * parse_client_queued - parse client queued messages
45 */
46 static void
parse_client_queued(struct Client * client_p)47 parse_client_queued(struct Client *client_p)
48 {
49 int tested = 0;
50 int dolen = 0;
51 int checkflood = 1;
52
53 if(IsAnyDead(client_p))
54 return;
55
56 if(IsUnknown(client_p))
57 {
58 for(;;)
59 {
60 if(client_p->localClient->sent_parsed >= client_p->localClient->allow_read)
61 break;
62
63 dolen = rb_linebuf_get(&client_p->localClient->buf_recvq, readBuf,
64 READBUF_SIZE, 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
89 if(IsAnyServer(client_p) || IsExemptFlood(client_p))
90 {
91 while(!IsAnyDead(client_p)
92 && (dolen =
93 rb_linebuf_get(&client_p->localClient->buf_recvq, readBuf, READBUF_SIZE,
94 LINEBUF_COMPLETE, LINEBUF_PARSED)) > 0)
95 {
96 client_dopacket(client_p, readBuf, dolen);
97 }
98 }
99 else if(IsClient(client_p))
100 {
101
102 if(IsOper(client_p) && ConfigFileEntry.no_oper_flood)
103 checkflood = 0;
104 /*
105 * Handle flood protection here - if we exceed our flood limit on
106 * messages in this loop, we simply drop out of the loop prematurely.
107 * -- adrian
108 */
109 for(;;)
110 {
111 /* if we have a post reg delay, dont parse anything from a client until
112 * the delay has passed.. Everything else will continue to be queued,
113 * so just hope the queue is big enough for them.. --anfl
114 */
115 if(!tested &&
116 (client_p->localClient->firsttime + ConfigFileEntry.post_registration_delay) >
117 rb_current_time())
118 break;
119 else
120 tested = 1;
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 */
135 if(checkflood)
136 {
137 if(client_p->localClient->sent_parsed >=
138 client_p->localClient->allow_read)
139 break;
140 }
141
142 /* allow opers 4 times the amount of messages as users. why 4?
143 * why not. :) --fl_
144 */
145 else if(client_p->localClient->sent_parsed >=
146 (4 * client_p->localClient->allow_read))
147 break;
148
149 dolen = rb_linebuf_get(&client_p->localClient->buf_recvq, readBuf,
150 READBUF_SIZE, LINEBUF_COMPLETE, LINEBUF_PARSED);
151
152 if(!dolen)
153 break;
154
155 client_dopacket(client_p, readBuf, dolen);
156 if(IsAnyDead(client_p))
157 return;
158 client_p->localClient->sent_parsed++;
159 }
160 }
161 }
162
163 /*
164 * flood_recalc
165 *
166 * recalculate the number of allowed flood lines. this should be called
167 * once a second on any given client. We then attempt to flush some data.
168 */
169 void
flood_recalc(void * unused)170 flood_recalc(void *unused)
171 {
172 rb_dlink_node *ptr, *next;
173 struct Client *client_p;
174
175 RB_DLINK_FOREACH_SAFE(ptr, next, lclient_list.head)
176 {
177 client_p = ptr->data;
178
179 if(rb_unlikely(IsMe(client_p)))
180 continue;
181
182 if(rb_unlikely(client_p->localClient == NULL))
183 continue;
184
185 if(IsFloodDone(client_p))
186 client_p->localClient->sent_parsed -= 2;
187 else
188 client_p->localClient->sent_parsed = 0;
189
190 if(client_p->localClient->sent_parsed < 0)
191 client_p->localClient->sent_parsed = 0;
192
193 if(--client_p->localClient->actually_read < 0)
194 client_p->localClient->actually_read = 0;
195
196 parse_client_queued(client_p);
197
198 if(rb_unlikely(IsAnyDead(client_p)))
199 continue;
200
201 if(!IsFloodDone(client_p)
202 && ((client_p->localClient->firsttime + 30) < rb_current_time()))
203 flood_endgrace(client_p);
204 }
205
206 RB_DLINK_FOREACH_SAFE(ptr, next, unknown_list.head)
207 {
208 client_p = ptr->data;
209
210 if(client_p->localClient == NULL)
211 continue;
212
213 client_p->localClient->sent_parsed--;
214
215 if(client_p->localClient->sent_parsed < 0)
216 client_p->localClient->sent_parsed = 0;
217
218 if(--client_p->localClient->actually_read < 0)
219 client_p->localClient->actually_read = 0;
220
221 parse_client_queued(client_p);
222 }
223 }
224
225
226 /*
227 * read_packet - Read a 'packet' of data from a connection and process it.
228 */
229 void
read_packet(rb_fde_t * F,void * data)230 read_packet(rb_fde_t *F, void *data)
231 {
232 struct Client *client_p = data;
233 struct LocalUser *lclient_p = client_p->localClient;
234 int length = 0;
235 int lbuf_len;
236
237 int binary = 0;
238 #ifdef USE_IODEBUG_HOOKS
239 hook_data_int hdata;
240 #endif
241
242
243 while(1) /* note..for things like rt sigio to work you *must* loop on read until you get EAGAIN */
244 {
245 if(IsAnyDead(client_p))
246 return;
247
248 /*
249 * Read some data. We *used to* do anti-flood protection here, but
250 * I personally think it makes the code too hairy to make sane.
251 * -- adrian
252 */
253 length = rb_read(client_p->localClient->F, readBuf, READBUF_SIZE);
254 if(length < 0)
255 {
256 if(rb_ignore_errno(errno))
257 {
258 rb_setselect(client_p->localClient->F,
259 RB_SELECT_READ, read_packet, client_p);
260 }
261 else
262 error_exit_client(client_p, length);
263 return;
264 }
265 else if(length == 0)
266 {
267 error_exit_client(client_p, length);
268 return;
269 }
270
271 #ifdef USE_IODEBUG_HOOKS
272 hdata.client = client_p;
273 hdata.arg1 = readBuf;
274 hdata.arg2 = length;
275 call_hook(h_iorecv_id, &hdata);
276 #endif
277
278 if(client_p->localClient->lasttime < rb_current_time())
279 client_p->localClient->lasttime = rb_current_time();
280 client_p->flags &= ~FLAGS_PINGSENT;
281
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;
289
290 lbuf_len =
291 rb_linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length,
292 binary);
293
294 lclient_p->actually_read += lbuf_len;
295
296 if(IsAnyDead(client_p))
297 return;
298
299 /* Attempt to parse what we have */
300 parse_client_queued(client_p);
301
302 if(IsAnyDead(client_p))
303 return;
304
305 /* Check to make sure we're not flooding */
306 if(!IsAnyServer(client_p) &&
307 (rb_linebuf_alloclen(&client_p->localClient->buf_recvq) >
308 ConfigFileEntry.client_flood))
309 {
310 if(!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
311 {
312 exit_client(client_p, client_p, client_p, "Excess Flood");
313 return;
314 }
315
316 }
317
318 /* bail if short read */
319 if(length < READBUF_SIZE)
320 {
321 rb_setselect(client_p->localClient->F, RB_SELECT_READ, read_packet,
322 client_p);
323 return;
324 }
325 }
326 }
327
328 /*
329 * client_dopacket - copy packet to client buf and parse it
330 * client_p - pointer to client structure for which the buffer data
331 * applies.
332 * buffer - pointr to the buffer containing the newly read data
333 * length - number of valid bytes of data in the buffer
334 *
335 * Note:
336 * It is implicitly assumed that dopacket is called only
337 * with client_p of "local" variation, which contains all the
338 * necessary fields (buffer etc..)
339 */
340 void
client_dopacket(struct Client * client_p,char * buffer,size_t length)341 client_dopacket(struct Client *client_p, char *buffer, size_t length)
342 {
343 s_assert(client_p != NULL);
344 s_assert(buffer != NULL);
345
346 if(client_p == NULL || buffer == NULL)
347 return;
348 if(IsAnyDead(client_p))
349 return;
350 /*
351 * Update messages received
352 */
353 ++me.localClient->receiveM;
354 ++client_p->localClient->receiveM;
355
356 /*
357 * Update bytes received
358 */
359 client_p->localClient->receiveB += length;
360 me.localClient->receiveB += length;
361
362 parse(client_p, buffer, buffer + length);
363 }
364
365 /* flood_endgrace()
366 *
367 * marks the end of the clients grace period
368 */
369 void
flood_endgrace(struct Client * client_p)370 flood_endgrace(struct Client *client_p)
371 {
372 SetFloodDone(client_p);
373 /* Drop their flood limit back down */
374 client_p->localClient->allow_read = MAX_FLOOD;
375
376 /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
377 * so reset it.
378 */
379 client_p->localClient->sent_parsed = 0;
380 }
381