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