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