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