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