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