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