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