]>
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 $
36 #include "irc_string.h"
41 static char readBuf
[READBUF_SIZE
];
42 static void client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
);
46 * parse_client_queued - parse client queued messages
49 parse_client_queued(struct Client
*client_p
)
54 if(IsAnyDead(client_p
))
57 if(IsUnknown(client_p
))
61 if(client_p
->localClient
->sent_parsed
>= client_p
->localClient
->allow_read
)
64 dolen
= linebuf_get(&client_p
->localClient
->
65 buf_recvq
, readBuf
, READBUF_SIZE
,
66 LINEBUF_COMPLETE
, LINEBUF_PARSED
);
68 if(dolen
<= 0 || IsDead(client_p
))
71 client_dopacket(client_p
, readBuf
, dolen
);
72 client_p
->localClient
->sent_parsed
++;
75 if(IsAnyDead(client_p
))
77 /* if theyve dropped out of the unknown state, break and move
78 * to the parsing for their appropriate status. --fl
80 if(!IsUnknown(client_p
))
82 /* reset their flood limits, they're now
85 client_p
->localClient
->sent_parsed
= 0;
92 if(IsAnyServer(client_p
) || IsExemptFlood(client_p
))
94 while (!IsAnyDead(client_p
) && (dolen
= linebuf_get(&client_p
->localClient
->buf_recvq
,
95 readBuf
, READBUF_SIZE
, LINEBUF_COMPLETE
,
98 client_dopacket(client_p
, readBuf
, dolen
);
101 else if(IsClient(client_p
))
104 if(IsOper(client_p
) && ConfigFileEntry
.no_oper_flood
)
107 * Handle flood protection here - if we exceed our flood limit on
108 * messages in this loop, we simply drop out of the loop prematurely.
113 /* This flood protection works as follows:
115 * A client is given allow_read lines to send to the server. Every
116 * time a line is parsed, sent_parsed is increased. sent_parsed
117 * is decreased by 1 every time flood_recalc is called.
119 * Thus a client can 'burst' allow_read lines to the server, any
120 * excess lines will be parsed one per flood_recalc() call.
122 * Therefore a client will be penalised more if they keep flooding,
123 * as sent_parsed will always hover around the allow_read limit
124 * and no 'bursts' will be permitted.
128 if(client_p
->localClient
->sent_parsed
>= client_p
->localClient
->allow_read
)
132 /* allow opers 4 times the amount of messages as users. why 4?
135 else if(client_p
->localClient
->sent_parsed
>= (4 * client_p
->localClient
->allow_read
))
138 dolen
= linebuf_get(&client_p
->localClient
->
139 buf_recvq
, readBuf
, READBUF_SIZE
,
140 LINEBUF_COMPLETE
, LINEBUF_PARSED
);
145 client_dopacket(client_p
, readBuf
, dolen
);
146 if(IsAnyDead(client_p
))
148 client_p
->localClient
->sent_parsed
++;
155 * marks the end of the clients grace period
158 flood_endgrace(struct Client
*client_p
)
160 SetFloodDone(client_p
);
162 /* Drop their flood limit back down */
163 client_p
->localClient
->allow_read
= MAX_FLOOD
;
165 /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
168 client_p
->localClient
->sent_parsed
= 0;
174 * recalculate the number of allowed flood lines. this should be called
175 * once a second on any given client. We then attempt to flush some data.
178 flood_recalc(int fd
, void *data
)
180 struct Client
*client_p
= data
;
181 struct LocalUser
*lclient_p
= client_p
->localClient
;
183 /* This can happen in the event that the client detached. */
187 /* allow a bursting client their allocation per second, allow
188 * a client whos flooding an extra 2 per second
190 if(IsFloodDone(client_p
))
191 lclient_p
->sent_parsed
-= 2;
193 lclient_p
->sent_parsed
= 0;
195 if(lclient_p
->sent_parsed
< 0)
196 lclient_p
->sent_parsed
= 0;
198 if(--lclient_p
->actually_read
< 0)
199 lclient_p
->actually_read
= 0;
201 parse_client_queued(client_p
);
203 if(IsAnyDead(client_p
))
206 /* and finally, reset the flood check */
207 comm_setflush(fd
, 1000, flood_recalc
, client_p
);
211 * read_ctrl_packet - Read a 'packet' of data from a servlink control
212 * link and process it.
215 read_ctrl_packet(int fd
, void *data
)
217 struct Client
*server
= data
;
218 struct LocalUser
*lserver
= server
->localClient
;
219 struct SlinkRpl
*reply
;
221 unsigned char tmp
[2];
222 unsigned char *len
= tmp
;
223 struct SlinkRplDef
*replydef
;
224 #ifdef USE_IODEBUG_HOOKS
228 s_assert(lserver
!= NULL
);
229 if(IsAnyDead(server
))
232 reply
= &lserver
->slinkrpl
;
237 reply
->gotdatalen
= 0;
241 length
= read(fd
, tmp
, 1);
245 if((length
== -1) && ignoreErrno(errno
))
247 error_exit_client(server
, length
);
251 reply
->command
= tmp
[0];
254 for (replydef
= slinkrpltab
; replydef
->handler
; replydef
++)
256 if((int)replydef
->replyid
== reply
->command
)
260 /* we should be able to trust a local slink process...
261 * and if it sends an invalid command, that's a bug.. */
262 s_assert(replydef
->handler
);
264 if((replydef
->flags
& SLINKRPL_FLAG_DATA
) && (reply
->gotdatalen
< 2))
266 /* we need a datalen u16 which we don't have yet... */
267 length
= read(fd
, len
, (2 - reply
->gotdatalen
));
270 if((length
== -1) && ignoreErrno(errno
))
272 error_exit_client(server
, length
);
276 if(reply
->gotdatalen
== 0)
278 reply
->datalen
= *len
<< 8;
283 if(length
&& (reply
->gotdatalen
== 1))
285 reply
->datalen
|= *len
;
287 if(reply
->datalen
> 0)
288 reply
->data
= MyMalloc(reply
->datalen
);
291 if(reply
->gotdatalen
< 2)
292 return; /* wait for more data */
295 if(reply
->readdata
< reply
->datalen
) /* try to get any remaining data */
297 length
= read(fd
, (reply
->data
+ reply
->readdata
),
298 (reply
->datalen
- reply
->readdata
));
301 if((length
== -1) && ignoreErrno(errno
))
303 error_exit_client(server
, length
);
307 reply
->readdata
+= length
;
308 if(reply
->readdata
< reply
->datalen
)
309 return; /* wait for more data */
312 #ifdef USE_IODEBUG_HOOKS
313 hdata
.client
= server
;
315 hdata
.arg2
= reply
->command
;
317 call_hook(h_iorecvctrl_id
, &hdata
);
320 /* we now have the command and any data, pass it off to the handler */
321 (*replydef
->handler
) (reply
->command
, reply
->datalen
, reply
->data
, server
);
324 if(reply
->datalen
> 0)
328 if(IsAnyDead(server
))
332 /* If we get here, we need to register for another COMM_SELECT_READ */
333 comm_setselect(fd
, FDLIST_SERVER
, COMM_SELECT_READ
, read_ctrl_packet
, server
, 0);
337 * read_packet - Read a 'packet' of data from a connection and process it.
340 read_packet(int fd
, void *data
)
342 struct Client
*client_p
= data
;
343 struct LocalUser
*lclient_p
= client_p
->localClient
;
348 #ifdef USE_IODEBUG_HOOKS
351 if(IsAnyDead(client_p
))
355 * Read some data. We *used to* do anti-flood protection here, but
356 * I personally think it makes the code too hairy to make sane.
359 length
= client_p
->localClient
->F
->read_impl(client_p
->localClient
->F
, readBuf
, READBUF_SIZE
);
363 if((length
== -1) && ignoreErrno(errno
))
365 comm_setselect(client_p
->localClient
->F
->fd
, FDLIST_IDLECLIENT
,
366 COMM_SELECT_READ
, read_packet
, client_p
, 0);
369 error_exit_client(client_p
, length
);
373 #ifdef USE_IODEBUG_HOOKS
374 hdata
.client
= client_p
;
375 hdata
.arg1
= readBuf
;
377 call_hook(h_iorecv_id
, &hdata
);
380 if(client_p
->localClient
->lasttime
< CurrentTime
)
381 client_p
->localClient
->lasttime
= CurrentTime
;
382 client_p
->flags
&= ~FLAGS_PINGSENT
;
385 * Before we even think of parsing what we just read, stick
386 * it on the end of the receive queue and do it when its
389 if(IsHandshake(client_p
) || IsUnknown(client_p
))
392 lbuf_len
= linebuf_parse(&client_p
->localClient
->buf_recvq
, readBuf
, length
, binary
);
394 lclient_p
->actually_read
+= lbuf_len
;
396 if(IsAnyDead(client_p
))
399 /* Attempt to parse what we have */
400 parse_client_queued(client_p
);
402 if(IsAnyDead(client_p
))
405 /* Check to make sure we're not flooding */
406 if(!IsAnyServer(client_p
) &&
407 (linebuf_alloclen(&client_p
->localClient
->buf_recvq
) > ConfigFileEntry
.client_flood
))
409 if(!(ConfigFileEntry
.no_oper_flood
&& IsOper(client_p
)))
411 exit_client(client_p
, client_p
, client_p
, "Excess Flood");
416 /* If we get here, we need to register for another COMM_SELECT_READ */
417 if(PARSE_AS_SERVER(client_p
))
419 comm_setselect(client_p
->localClient
->F
->fd
, FDLIST_SERVER
, COMM_SELECT_READ
,
420 read_packet
, client_p
, 0);
424 comm_setselect(client_p
->localClient
->F
->fd
, FDLIST_IDLECLIENT
,
425 COMM_SELECT_READ
, read_packet
, client_p
, 0);
430 * client_dopacket - copy packet to client buf and parse it
431 * client_p - pointer to client structure for which the buffer data
433 * buffer - pointr to the buffer containing the newly read data
434 * length - number of valid bytes of data in the buffer
437 * It is implicitly assumed that dopacket is called only
438 * with client_p of "local" variation, which contains all the
439 * necessary fields (buffer etc..)
442 client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
)
444 s_assert(client_p
!= NULL
);
445 s_assert(buffer
!= NULL
);
447 if(client_p
== NULL
|| buffer
== NULL
)
449 if(IsAnyDead(client_p
))
452 * Update messages received
454 ++me
.localClient
->receiveM
;
455 ++client_p
->localClient
->receiveM
;
458 * Update bytes received
460 client_p
->localClient
->receiveB
+= length
;
462 if(client_p
->localClient
->receiveB
> 1023)
464 client_p
->localClient
->receiveK
+= (client_p
->localClient
->receiveB
>> 10);
465 client_p
->localClient
->receiveB
&= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
468 me
.localClient
->receiveB
+= length
;
470 if(me
.localClient
->receiveB
> 1023)
472 me
.localClient
->receiveK
+= (me
.localClient
->receiveB
>> 10);
473 me
.localClient
->receiveB
&= 0x03ff;
476 parse(client_p
, buffer
, buffer
+ length
);