]> jfr.im git - solanum.git/blame - ircd/packet.c
Correct order of chunking and encoding steps.
[solanum.git] / ircd / packet.c
CommitLineData
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"
d4f7eb4c 35#include "s_newconf.h"
54ac8b60
VY
36
37static char readBuf[READBUF_SIZE];
38static 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 */
43static void
44parse_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 */
d4f7eb4c 112 if(IsOperGeneral(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
b3a00991 137 /* post_registration_delay hack. Don't process any messages from a new client for $n seconds,
2d656284
SB
138 * to allow network bots to do their thing before channels can be joined.
139 */
b3a00991 140 if (rb_current_time() < client_p->localClient->firsttime + ConfigFileEntry.post_registration_delay)
2d656284
SB
141 break;
142
60eb0cdc 143 dolen = rb_linebuf_get(&client_p->localClient->
54ac8b60
VY
144 buf_recvq, readBuf, READBUF_SIZE,
145 LINEBUF_COMPLETE, LINEBUF_PARSED);
146
147 if(!dolen)
148 break;
149
150 client_dopacket(client_p, readBuf, dolen);
151 if(IsAnyDead(client_p))
152 return;
e6e54763
SB
153
154 client_p->localClient->sent_parsed += ConfigFileEntry.client_flood_message_time;
54ac8b60 155 }
5a72f20c
JT
156 /* If sent_parsed is impossibly high, drop it down.
157 * This is useful if the configuration is changed.
158 */
159 if(client_p->localClient->sent_parsed > allow_read +
160 ConfigFileEntry.client_flood_message_time - 1)
161 client_p->localClient->sent_parsed = allow_read +
162 ConfigFileEntry.client_flood_message_time - 1;
54ac8b60
VY
163 }
164}
165
166/* flood_endgrace()
167 *
168 * marks the end of the clients grace period
169 */
170void
171flood_endgrace(struct Client *client_p)
172{
173 SetFloodDone(client_p);
174
d182b854
JT
175 /* sent_parsed could be way over client_flood_burst_max but under
176 * client_flood_burst_rate so reset it.
54ac8b60
VY
177 */
178 client_p->localClient->sent_parsed = 0;
179}
180
1087485c
JT
181/*
182 * flood_recalc
183 *
184 * recalculate the number of allowed flood lines. this should be called
185 * once a second on any given client. We then attempt to flush some data.
186 */
187void
188flood_recalc(void *unused)
189{
190 rb_dlink_node *ptr, *next;
191 struct Client *client_p;
192
193 RB_DLINK_FOREACH_SAFE(ptr, next, lclient_list.head)
194 {
195 client_p = ptr->data;
196
af41336a 197 if(rb_unlikely(IsMe(client_p)))
1087485c 198 continue;
55abcbb2 199
af41336a 200 if(rb_unlikely(client_p->localClient == NULL))
1087485c 201 continue;
55abcbb2 202
1087485c 203 if(IsFloodDone(client_p))
e6e54763 204 client_p->localClient->sent_parsed -= ConfigFileEntry.client_flood_message_num;
1087485c
JT
205 else
206 client_p->localClient->sent_parsed = 0;
55abcbb2 207
1087485c
JT
208 if(client_p->localClient->sent_parsed < 0)
209 client_p->localClient->sent_parsed = 0;
210
1087485c 211 parse_client_queued(client_p);
55abcbb2 212
af41336a 213 if(rb_unlikely(IsAnyDead(client_p)))
1087485c
JT
214 continue;
215
216 }
217
218 RB_DLINK_FOREACH_SAFE(ptr, next, unknown_list.head)
219 {
220 client_p = ptr->data;
221
222 if(client_p->localClient == NULL)
223 continue;
224
225 client_p->localClient->sent_parsed--;
226
227 if(client_p->localClient->sent_parsed < 0)
228 client_p->localClient->sent_parsed = 0;
229
1087485c
JT
230 parse_client_queued(client_p);
231 }
54ac8b60
VY
232}
233
54ac8b60
VY
234/*
235 * read_packet - Read a 'packet' of data from a connection and process it.
236 */
237void
d91ce397 238read_packet(rb_fde_t * F, void *data)
54ac8b60
VY
239{
240 struct Client *client_p = data;
54ac8b60 241 int length = 0;
54ac8b60 242 int binary = 0;
54ac8b60 243
d91ce397 244 while(1)
54ac8b60 245 {
d91ce397
VY
246 if(IsAnyDead(client_p))
247 return;
248
249 /*
250 * Read some data. We *used to* do anti-flood protection here, but
251 * I personally think it makes the code too hairy to make sane.
252 * -- adrian
253 */
254 length = rb_read(client_p->localClient->F, readBuf, READBUF_SIZE);
255
9f316874 256 if(length < 0)
54ac8b60 257 {
9f316874 258 if(rb_ignore_errno(errno))
55abcbb2 259 rb_setselect(client_p->localClient->F,
d91ce397 260 RB_SELECT_READ, read_packet, client_p);
9f316874 261 else
d91ce397
VY
262 error_exit_client(client_p, length);
263 return;
264 }
9f316874 265 else if(length == 0)
d91ce397
VY
266 {
267 error_exit_client(client_p, length);
54ac8b60
VY
268 return;
269 }
54ac8b60 270
d91ce397
VY
271 if(client_p->localClient->lasttime < rb_current_time())
272 client_p->localClient->lasttime = rb_current_time();
273 client_p->flags &= ~FLAGS_PINGSENT;
54ac8b60 274
d1c028f2
DS
275 if (client_p->flags & FLAGS_PINGWARN)
276 {
277 /*
278 * if we warned about this server being unresponsive
279 * before, let's let everyone know there's no need
280 * to panic
281 */
282 client_p->flags &= ~FLAGS_PINGWARN;
283 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
284 "Received response from previously unresponsive link %s",
285 client_p->name);
286 ilog(L_SERVER,
287 "Received response from previously unresponsive link %s",
288 log_client_name(client_p, HIDE_IP));
289 }
290
291
d91ce397
VY
292 /*
293 * Before we even think of parsing what we just read, stick
294 * it on the end of the receive queue and do it when its
295 * turn comes around.
296 */
297 if(IsHandshake(client_p) || IsUnknown(client_p))
298 binary = 1;
54ac8b60 299
32fb5895 300 (void) rb_linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length, binary);
54ac8b60 301
d91ce397
VY
302 if(IsAnyDead(client_p))
303 return;
55abcbb2 304
d91ce397
VY
305 /* Attempt to parse what we have */
306 parse_client_queued(client_p);
54ac8b60 307
d91ce397 308 if(IsAnyDead(client_p))
54ac8b60 309 return;
55abcbb2 310
d91ce397
VY
311 /* Check to make sure we're not flooding */
312 if(!IsAnyServer(client_p) &&
e6e54763 313 (rb_linebuf_alloclen(&client_p->localClient->buf_recvq) > ConfigFileEntry.client_flood_max_lines))
d91ce397 314 {
d4f7eb4c 315 if(!(ConfigFileEntry.no_oper_flood && IsOperGeneral(client_p)))
d91ce397
VY
316 {
317 exit_client(client_p, client_p, client_p, "Excess Flood");
318 return;
319 }
54ac8b60 320 }
54ac8b60 321
c6ad9b0c
SA
322 /* bail if short read, but not for SCTP as it returns data in packets */
323 if (length < READBUF_SIZE && !(rb_get_type(client_p->localClient->F) & RB_FD_SCTP)) {
d91ce397
VY
324 rb_setselect(client_p->localClient->F, RB_SELECT_READ, read_packet, client_p);
325 return;
326 }
54ac8b60
VY
327 }
328}
329
330/*
331 * client_dopacket - copy packet to client buf and parse it
332 * client_p - pointer to client structure for which the buffer data
333 * applies.
334 * buffer - pointr to the buffer containing the newly read data
335 * length - number of valid bytes of data in the buffer
336 *
337 * Note:
338 * It is implicitly assumed that dopacket is called only
339 * with client_p of "local" variation, which contains all the
340 * necessary fields (buffer etc..)
341 */
28464944 342static void
54ac8b60
VY
343client_dopacket(struct Client *client_p, char *buffer, size_t length)
344{
345 s_assert(client_p != NULL);
346 s_assert(buffer != NULL);
347
348 if(client_p == NULL || buffer == NULL)
349 return;
350 if(IsAnyDead(client_p))
351 return;
55abcbb2 352 /*
54ac8b60
VY
353 * Update messages received
354 */
355 ++me.localClient->receiveM;
356 ++client_p->localClient->receiveM;
357
55abcbb2 358 /*
54ac8b60
VY
359 * Update bytes received
360 */
361 client_p->localClient->receiveB += length;
362
363 if(client_p->localClient->receiveB > 1023)
364 {
365 client_p->localClient->receiveK += (client_p->localClient->receiveB >> 10);
366 client_p->localClient->receiveB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
367 }
368
369 me.localClient->receiveB += length;
370
371 if(me.localClient->receiveB > 1023)
372 {
373 me.localClient->receiveK += (me.localClient->receiveB >> 10);
374 me.localClient->receiveB &= 0x03ff;
375 }
376
377 parse(client_p, buffer, buffer + length);
378}