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