]>
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" | |
34 | #include "irc_string.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 | ||
60eb0cdc | 61 | dolen = rb_linebuf_get(&client_p->localClient-> |
54ac8b60 VY |
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 | ||
89 | if(IsAnyServer(client_p) || IsExemptFlood(client_p)) | |
90 | { | |
60eb0cdc | 91 | while (!IsAnyDead(client_p) && (dolen = rb_linebuf_get(&client_p->localClient->buf_recvq, |
54ac8b60 VY |
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 | ||
60eb0cdc | 135 | dolen = rb_linebuf_get(&client_p->localClient-> |
54ac8b60 VY |
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 | /* flood_endgrace() | |
151 | * | |
152 | * marks the end of the clients grace period | |
153 | */ | |
154 | void | |
155 | flood_endgrace(struct Client *client_p) | |
156 | { | |
157 | SetFloodDone(client_p); | |
158 | ||
159 | /* Drop their flood limit back down */ | |
160 | client_p->localClient->allow_read = MAX_FLOOD; | |
161 | ||
162 | /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST, | |
163 | * so reset it. | |
164 | */ | |
165 | client_p->localClient->sent_parsed = 0; | |
166 | } | |
167 | ||
5a606a8f VY |
168 | /*\r |
169 | * flood_recalc\r | |
170 | *\r | |
171 | * recalculate the number of allowed flood lines. this should be called\r | |
172 | * once a second on any given client. We then attempt to flush some data.\r | |
173 | */\r | |
174 | void\r | |
175 | flood_recalc(void *unused)\r | |
176 | {\r | |
177 | rb_dlink_node *ptr, *next;\r | |
178 | struct Client *client_p;\r | |
179 | \r | |
180 | RB_DLINK_FOREACH_SAFE(ptr, next, lclient_list.head)\r | |
181 | {\r | |
182 | client_p = ptr->data;\r | |
183 | \r | |
184 | if(unlikely(IsMe(client_p)))\r | |
185 | continue;\r | |
186 | \r | |
187 | if(unlikely(client_p->localClient == NULL))\r | |
188 | continue;\r | |
189 | \r | |
190 | if(IsFloodDone(client_p))\r | |
191 | client_p->localClient->sent_parsed -= 2;\r | |
192 | else\r | |
193 | client_p->localClient->sent_parsed = 0;\r | |
194 | \r | |
195 | if(client_p->localClient->sent_parsed < 0)\r | |
196 | client_p->localClient->sent_parsed = 0;\r | |
197 | \r | |
198 | if(--client_p->localClient->actually_read < 0)\r | |
199 | client_p->localClient->actually_read = 0;\r | |
200 | \r | |
201 | parse_client_queued(client_p);\r | |
202 | \r | |
203 | if(unlikely(IsAnyDead(client_p)))\r | |
204 | continue;\r | |
205 | \r | |
206 | }\r | |
207 | \r | |
208 | RB_DLINK_FOREACH_SAFE(ptr, next, unknown_list.head)\r | |
209 | {\r | |
210 | client_p = ptr->data;\r | |
211 | \r | |
212 | if(client_p->localClient == NULL)\r | |
213 | continue;\r | |
214 | \r | |
215 | client_p->localClient->sent_parsed--;\r | |
216 | \r | |
217 | if(client_p->localClient->sent_parsed < 0)\r | |
218 | client_p->localClient->sent_parsed = 0;\r | |
219 | \r | |
220 | if(--client_p->localClient->actually_read < 0)\r | |
221 | client_p->localClient->actually_read = 0;\r | |
222 | \r | |
223 | parse_client_queued(client_p);\r | |
224 | }\r | |
54ac8b60 VY |
225 | } |
226 | ||
54ac8b60 VY |
227 | /* |
228 | * read_packet - Read a 'packet' of data from a connection and process it. | |
229 | */ | |
230 | void | |
d91ce397 | 231 | read_packet(rb_fde_t * F, void *data) |
54ac8b60 VY |
232 | { |
233 | struct Client *client_p = data; | |
234 | struct LocalUser *lclient_p = client_p->localClient; | |
235 | int length = 0; | |
236 | int lbuf_len; | |
237 | ||
238 | int binary = 0; | |
239 | #ifdef USE_IODEBUG_HOOKS | |
240 | hook_data_int hdata; | |
241 | #endif | |
54ac8b60 | 242 | |
d91ce397 | 243 | while(1) |
54ac8b60 | 244 | { |
d91ce397 VY |
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 | ||
255 | if(length <= 0) | |
54ac8b60 | 256 | { |
d91ce397 VY |
257 | if(rb_ignore_errno(errno)) |
258 | { | |
259 | rb_setselect(client_p->localClient->F, | |
260 | RB_SELECT_READ, read_packet, client_p); | |
261 | } else | |
262 | error_exit_client(client_p, length); | |
263 | return; | |
264 | } | |
265 | if(length == 0) | |
266 | { | |
267 | error_exit_client(client_p, length); | |
54ac8b60 VY |
268 | return; |
269 | } | |
54ac8b60 VY |
270 | |
271 | #ifdef USE_IODEBUG_HOOKS | |
d91ce397 VY |
272 | hdata.client = client_p; |
273 | hdata.arg1 = readBuf; | |
274 | hdata.arg2 = length; | |
275 | call_hook(h_iorecv_id, &hdata); | |
54ac8b60 VY |
276 | #endif |
277 | ||
d91ce397 VY |
278 | if(client_p->localClient->lasttime < rb_current_time()) |
279 | client_p->localClient->lasttime = rb_current_time(); | |
280 | client_p->flags &= ~FLAGS_PINGSENT; | |
54ac8b60 | 281 | |
d91ce397 VY |
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; | |
54ac8b60 | 289 | |
d91ce397 | 290 | lbuf_len = rb_linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length, binary); |
54ac8b60 | 291 | |
d91ce397 | 292 | lclient_p->actually_read += lbuf_len; |
54ac8b60 | 293 | |
d91ce397 VY |
294 | if(IsAnyDead(client_p)) |
295 | return; | |
296 | ||
297 | /* Attempt to parse what we have */ | |
298 | parse_client_queued(client_p); | |
54ac8b60 | 299 | |
d91ce397 | 300 | if(IsAnyDead(client_p)) |
54ac8b60 | 301 | return; |
d91ce397 VY |
302 | |
303 | /* Check to make sure we're not flooding */ | |
304 | if(!IsAnyServer(client_p) && | |
305 | (rb_linebuf_alloclen(&client_p->localClient->buf_recvq) > ConfigFileEntry.client_flood)) | |
306 | { | |
307 | if(!(ConfigFileEntry.no_oper_flood && IsOper(client_p))) | |
308 | { | |
309 | exit_client(client_p, client_p, client_p, "Excess Flood"); | |
310 | return; | |
311 | } | |
54ac8b60 | 312 | } |
54ac8b60 | 313 | |
d91ce397 VY |
314 | /* bail if short read */ |
315 | if(length < READBUF_SIZE) | |
316 | { | |
317 | rb_setselect(client_p->localClient->F, RB_SELECT_READ, read_packet, client_p); | |
318 | return; | |
319 | } | |
54ac8b60 VY |
320 | } |
321 | } | |
322 | ||
323 | /* | |
324 | * client_dopacket - copy packet to client buf and parse it | |
325 | * client_p - pointer to client structure for which the buffer data | |
326 | * applies. | |
327 | * buffer - pointr to the buffer containing the newly read data | |
328 | * length - number of valid bytes of data in the buffer | |
329 | * | |
330 | * Note: | |
331 | * It is implicitly assumed that dopacket is called only | |
332 | * with client_p of "local" variation, which contains all the | |
333 | * necessary fields (buffer etc..) | |
334 | */ | |
335 | void | |
336 | client_dopacket(struct Client *client_p, char *buffer, size_t length) | |
337 | { | |
338 | s_assert(client_p != NULL); | |
339 | s_assert(buffer != NULL); | |
340 | ||
341 | if(client_p == NULL || buffer == NULL) | |
342 | return; | |
343 | if(IsAnyDead(client_p)) | |
344 | return; | |
345 | /* | |
346 | * Update messages received | |
347 | */ | |
348 | ++me.localClient->receiveM; | |
349 | ++client_p->localClient->receiveM; | |
350 | ||
351 | /* | |
352 | * Update bytes received | |
353 | */ | |
354 | client_p->localClient->receiveB += length; | |
355 | ||
356 | if(client_p->localClient->receiveB > 1023) | |
357 | { | |
358 | client_p->localClient->receiveK += (client_p->localClient->receiveB >> 10); | |
359 | client_p->localClient->receiveB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */ | |
360 | } | |
361 | ||
362 | me.localClient->receiveB += length; | |
363 | ||
364 | if(me.localClient->receiveB > 1023) | |
365 | { | |
366 | me.localClient->receiveK += (me.localClient->receiveB >> 10); | |
367 | me.localClient->receiveB &= 0x03ff; | |
368 | } | |
369 | ||
370 | parse(client_p, buffer, buffer + length); | |
371 | } |