]>
jfr.im git - solanum.git/blob - src/packet.c
2 * ircd-ratbox: A slightly useful ircd.
3 * packet.c: Packet handlers.
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
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.
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.
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
24 * $Id: packet.c 3446 2007-05-14 22:21:16Z jilles $
34 #include "irc_string.h"
38 static char readBuf
[READBUF_SIZE
];
39 static void client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
);
43 * parse_client_queued - parse client queued messages
46 parse_client_queued(struct Client
*client_p
)
51 if(IsAnyDead(client_p
))
54 if(IsUnknown(client_p
))
58 if(client_p
->localClient
->sent_parsed
>= client_p
->localClient
->allow_read
)
61 dolen
= linebuf_get(&client_p
->localClient
->
62 buf_recvq
, readBuf
, READBUF_SIZE
,
63 LINEBUF_COMPLETE
, LINEBUF_PARSED
);
65 if(dolen
<= 0 || IsDead(client_p
))
68 client_dopacket(client_p
, readBuf
, dolen
);
69 client_p
->localClient
->sent_parsed
++;
72 if(IsAnyDead(client_p
))
74 /* if theyve dropped out of the unknown state, break and move
75 * to the parsing for their appropriate status. --fl
77 if(!IsUnknown(client_p
))
79 /* reset their flood limits, they're now
82 client_p
->localClient
->sent_parsed
= 0;
89 if(IsAnyServer(client_p
) || IsExemptFlood(client_p
))
91 while (!IsAnyDead(client_p
) && (dolen
= linebuf_get(&client_p
->localClient
->buf_recvq
,
92 readBuf
, READBUF_SIZE
, LINEBUF_COMPLETE
,
95 client_dopacket(client_p
, readBuf
, dolen
);
98 else if(IsClient(client_p
))
101 if(IsOper(client_p
) && ConfigFileEntry
.no_oper_flood
)
104 * Handle flood protection here - if we exceed our flood limit on
105 * messages in this loop, we simply drop out of the loop prematurely.
110 /* This flood protection works as follows:
112 * A client is given allow_read lines to send to the server. Every
113 * time a line is parsed, sent_parsed is increased. sent_parsed
114 * is decreased by 1 every time flood_recalc is called.
116 * Thus a client can 'burst' allow_read lines to the server, any
117 * excess lines will be parsed one per flood_recalc() call.
119 * Therefore a client will be penalised more if they keep flooding,
120 * as sent_parsed will always hover around the allow_read limit
121 * and no 'bursts' will be permitted.
125 if(client_p
->localClient
->sent_parsed
>= client_p
->localClient
->allow_read
)
129 /* allow opers 4 times the amount of messages as users. why 4?
132 else if(client_p
->localClient
->sent_parsed
>= (4 * client_p
->localClient
->allow_read
))
135 dolen
= linebuf_get(&client_p
->localClient
->
136 buf_recvq
, readBuf
, READBUF_SIZE
,
137 LINEBUF_COMPLETE
, LINEBUF_PARSED
);
142 client_dopacket(client_p
, readBuf
, dolen
);
143 if(IsAnyDead(client_p
))
145 client_p
->localClient
->sent_parsed
++;
152 * marks the end of the clients grace period
155 flood_endgrace(struct Client
*client_p
)
157 SetFloodDone(client_p
);
159 /* Drop their flood limit back down */
160 client_p
->localClient
->allow_read
= MAX_FLOOD
;
162 /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
165 client_p
->localClient
->sent_parsed
= 0;
171 * recalculate the number of allowed flood lines. this should be called
172 * once a second on any given client. We then attempt to flush some data.
175 flood_recalc(int fd
, void *data
)
177 struct Client
*client_p
= data
;
178 struct LocalUser
*lclient_p
= client_p
->localClient
;
180 /* This can happen in the event that the client detached. */
184 /* allow a bursting client their allocation per second, allow
185 * a client whos flooding an extra 2 per second
187 if(IsFloodDone(client_p
))
188 lclient_p
->sent_parsed
-= 2;
190 lclient_p
->sent_parsed
= 0;
192 if(lclient_p
->sent_parsed
< 0)
193 lclient_p
->sent_parsed
= 0;
195 if(--lclient_p
->actually_read
< 0)
196 lclient_p
->actually_read
= 0;
198 parse_client_queued(client_p
);
200 if(IsAnyDead(client_p
))
203 /* and finally, reset the flood check */
204 rb_setflush(fd
, 1000, flood_recalc
, client_p
);
208 * read_ctrl_packet - Read a 'packet' of data from a servlink control
209 * link and process it.
212 read_ctrl_packet(int fd
, void *data
)
214 struct Client
*server
= data
;
215 struct LocalUser
*lserver
= server
->localClient
;
216 struct SlinkRpl
*reply
;
218 unsigned char tmp
[2];
219 unsigned char *len
= tmp
;
220 struct SlinkRplDef
*replydef
;
221 #ifdef USE_IODEBUG_HOOKS
225 s_assert(lserver
!= NULL
);
226 if(IsAnyDead(server
))
229 reply
= &lserver
->slinkrpl
;
234 reply
->gotdatalen
= 0;
238 length
= read(fd
, tmp
, 1);
242 if((length
== -1) && ignoreErrno(errno
))
244 error_exit_client(server
, length
);
248 reply
->command
= tmp
[0];
251 for (replydef
= slinkrpltab
; replydef
->handler
; replydef
++)
253 if((int)replydef
->replyid
== reply
->command
)
257 /* we should be able to trust a local slink process...
258 * and if it sends an invalid command, that's a bug.. */
259 s_assert(replydef
->handler
);
261 if((replydef
->flags
& SLINKRPL_FLAG_DATA
) && (reply
->gotdatalen
< 2))
263 /* we need a datalen u16 which we don't have yet... */
264 length
= read(fd
, len
, (2 - reply
->gotdatalen
));
267 if((length
== -1) && ignoreErrno(errno
))
269 error_exit_client(server
, length
);
273 if(reply
->gotdatalen
== 0)
275 reply
->datalen
= *len
<< 8;
280 if(length
&& (reply
->gotdatalen
== 1))
282 reply
->datalen
|= *len
;
284 if(reply
->datalen
> 0)
285 reply
->data
= rb_malloc(reply
->datalen
);
288 if(reply
->gotdatalen
< 2)
289 return; /* wait for more data */
292 if(reply
->readdata
< reply
->datalen
) /* try to get any remaining data */
294 length
= read(fd
, (reply
->data
+ reply
->readdata
),
295 (reply
->datalen
- reply
->readdata
));
298 if((length
== -1) && ignoreErrno(errno
))
300 error_exit_client(server
, length
);
304 reply
->readdata
+= length
;
305 if(reply
->readdata
< reply
->datalen
)
306 return; /* wait for more data */
309 #ifdef USE_IODEBUG_HOOKS
310 hdata
.client
= server
;
312 hdata
.arg2
= reply
->command
;
314 call_hook(h_iorecvctrl_id
, &hdata
);
317 /* we now have the command and any data, pass it off to the handler */
318 (*replydef
->handler
) (reply
->command
, reply
->datalen
, reply
->data
, server
);
321 if(reply
->datalen
> 0)
322 rb_free(reply
->data
);
325 if(IsAnyDead(server
))
329 /* If we get here, we need to register for another COMM_SELECT_READ */
330 rb_setselect(fd
, FDLIST_SERVER
, COMM_SELECT_READ
, read_ctrl_packet
, server
, 0);
334 * read_packet - Read a 'packet' of data from a connection and process it.
337 read_packet(int fd
, void *data
)
339 struct Client
*client_p
= data
;
340 struct LocalUser
*lclient_p
= client_p
->localClient
;
345 #ifdef USE_IODEBUG_HOOKS
348 if(IsAnyDead(client_p
))
352 * Read some data. We *used to* do anti-flood protection here, but
353 * I personally think it makes the code too hairy to make sane.
356 length
= client_p
->localClient
->F
->read_impl(client_p
->localClient
->F
, readBuf
, READBUF_SIZE
);
360 if((length
== -1) && ignoreErrno(errno
))
362 rb_setselect(client_p
->localClient
->F
->fd
, FDLIST_IDLECLIENT
,
363 COMM_SELECT_READ
, read_packet
, client_p
, 0);
366 error_exit_client(client_p
, length
);
370 #ifdef USE_IODEBUG_HOOKS
371 hdata
.client
= client_p
;
372 hdata
.arg1
= readBuf
;
374 call_hook(h_iorecv_id
, &hdata
);
377 if(client_p
->localClient
->lasttime
< rb_current_time())
378 client_p
->localClient
->lasttime
= rb_current_time();
379 client_p
->flags
&= ~FLAGS_PINGSENT
;
382 * Before we even think of parsing what we just read, stick
383 * it on the end of the receive queue and do it when its
386 if(IsHandshake(client_p
) || IsUnknown(client_p
))
389 lbuf_len
= linebuf_parse(&client_p
->localClient
->buf_recvq
, readBuf
, length
, binary
);
391 lclient_p
->actually_read
+= lbuf_len
;
393 if(IsAnyDead(client_p
))
396 /* Attempt to parse what we have */
397 parse_client_queued(client_p
);
399 if(IsAnyDead(client_p
))
402 /* Check to make sure we're not flooding */
403 if(!IsAnyServer(client_p
) &&
404 (linebuf_alloclen(&client_p
->localClient
->buf_recvq
) > ConfigFileEntry
.client_flood
))
406 if(!(ConfigFileEntry
.no_oper_flood
&& IsOper(client_p
)))
408 exit_client(client_p
, client_p
, client_p
, "Excess Flood");
413 /* If we get here, we need to register for another COMM_SELECT_READ */
414 if(PARSE_AS_SERVER(client_p
))
416 rb_setselect(client_p
->localClient
->F
->fd
, FDLIST_SERVER
, COMM_SELECT_READ
,
417 read_packet
, client_p
, 0);
421 rb_setselect(client_p
->localClient
->F
->fd
, FDLIST_IDLECLIENT
,
422 COMM_SELECT_READ
, read_packet
, client_p
, 0);
427 * client_dopacket - copy packet to client buf and parse it
428 * client_p - pointer to client structure for which the buffer data
430 * buffer - pointr to the buffer containing the newly read data
431 * length - number of valid bytes of data in the buffer
434 * It is implicitly assumed that dopacket is called only
435 * with client_p of "local" variation, which contains all the
436 * necessary fields (buffer etc..)
439 client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
)
441 s_assert(client_p
!= NULL
);
442 s_assert(buffer
!= NULL
);
444 if(client_p
== NULL
|| buffer
== NULL
)
446 if(IsAnyDead(client_p
))
449 * Update messages received
451 ++me
.localClient
->receiveM
;
452 ++client_p
->localClient
->receiveM
;
455 * Update bytes received
457 client_p
->localClient
->receiveB
+= length
;
459 if(client_p
->localClient
->receiveB
> 1023)
461 client_p
->localClient
->receiveK
+= (client_p
->localClient
->receiveB
>> 10);
462 client_p
->localClient
->receiveB
&= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
465 me
.localClient
->receiveB
+= length
;
467 if(me
.localClient
->receiveB
> 1023)
469 me
.localClient
->receiveK
+= (me
.localClient
->receiveB
>> 10);
470 me
.localClient
->receiveB
&= 0x03ff;
473 parse(client_p
, buffer
, buffer
+ length
);