]>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 * $Id: packet.c 25179 2008-03-30 16:34:57Z androsyn $
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
= rb_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;
88 if(IsAnyServer(client_p
) || IsExemptFlood(client_p
))
90 while (!IsAnyDead(client_p
) && (dolen
= rb_linebuf_get(&client_p
->localClient
->buf_recvq
,
91 readBuf
, READBUF_SIZE
, LINEBUF_COMPLETE
,
94 client_dopacket(client_p
, readBuf
, dolen
);
97 else if(IsClient(client_p
))
100 if(IsOper(client_p
) && ConfigFileEntry
.no_oper_flood
)
103 * Handle flood protection here - if we exceed our flood limit on
104 * messages in this loop, we simply drop out of the loop prematurely.
109 /* This flood protection works as follows:
111 * A client is given allow_read lines to send to the server. Every
112 * time a line is parsed, sent_parsed is increased. sent_parsed
113 * is decreased by 1 every time flood_recalc is called.
115 * Thus a client can 'burst' allow_read lines to the server, any
116 * excess lines will be parsed one per flood_recalc() call.
118 * Therefore a client will be penalised more if they keep flooding,
119 * as sent_parsed will always hover around the allow_read limit
120 * and no 'bursts' will be permitted.
124 if(client_p
->localClient
->sent_parsed
>= client_p
->localClient
->allow_read
)
128 /* allow opers 4 times the amount of messages as users. why 4?
131 else if(client_p
->localClient
->sent_parsed
>= (4 * client_p
->localClient
->allow_read
))
134 dolen
= rb_linebuf_get(&client_p
->localClient
->
135 buf_recvq
, readBuf
, READBUF_SIZE
,
136 LINEBUF_COMPLETE
, LINEBUF_PARSED
);
141 client_dopacket(client_p
, readBuf
, dolen
);
142 if(IsAnyDead(client_p
))
144 client_p
->localClient
->sent_parsed
++;
152 * recalculate the number of allowed flood lines. this should be called
153 * once a second on any given client. We then attempt to flush some data.
156 flood_recalc(void *unused
)
158 rb_dlink_node
*ptr
, *next
;
159 struct Client
*client_p
;
161 RB_DLINK_FOREACH_SAFE(ptr
, next
, lclient_list
.head
)
163 client_p
= ptr
->data
;
165 if(unlikely(IsMe(client_p
)))
168 if(unlikely(client_p
->localClient
== NULL
))
171 if(IsFloodDone(client_p
))
172 client_p
->localClient
->sent_parsed
-= 2;
174 client_p
->localClient
->sent_parsed
= 0;
176 if(client_p
->localClient
->sent_parsed
< 0)
177 client_p
->localClient
->sent_parsed
= 0;
179 if(--client_p
->localClient
->actually_read
< 0)
180 client_p
->localClient
->actually_read
= 0;
182 parse_client_queued(client_p
);
184 if(unlikely(IsAnyDead(client_p
)))
189 RB_DLINK_FOREACH_SAFE(ptr
, next
, unknown_list
.head
)
191 client_p
= ptr
->data
;
193 if(client_p
->localClient
== NULL
)
196 client_p
->localClient
->sent_parsed
--;
198 if(client_p
->localClient
->sent_parsed
< 0)
199 client_p
->localClient
->sent_parsed
= 0;
201 if(--client_p
->localClient
->actually_read
< 0)
202 client_p
->localClient
->actually_read
= 0;
204 parse_client_queued(client_p
);
210 * read_packet - Read a 'packet' of data from a connection and process it.
213 read_packet(rb_fde_t
*F
, void *data
)
215 struct Client
*client_p
= data
;
216 struct LocalUser
*lclient_p
= client_p
->localClient
;
221 #ifdef USE_IODEBUG_HOOKS
226 while(1) /* note..for things like rt sigio to work you *must* loop on read until you get EAGAIN */
228 if(IsAnyDead(client_p
))
232 * Read some data. We *used to* do anti-flood protection here, but
233 * I personally think it makes the code too hairy to make sane.
236 length
= rb_read(client_p
->localClient
->F
, readBuf
, READBUF_SIZE
);
239 if(rb_ignore_errno(errno
))
241 rb_setselect(client_p
->localClient
->F
,
242 RB_SELECT_READ
, read_packet
, client_p
);
244 error_exit_client(client_p
, length
);
249 error_exit_client(client_p
, length
);
253 #ifdef USE_IODEBUG_HOOKS
254 hdata
.client
= client_p
;
255 hdata
.arg1
= readBuf
;
257 call_hook(h_iorecv_id
, &hdata
);
260 if(client_p
->localClient
->lasttime
< rb_current_time())
261 client_p
->localClient
->lasttime
= rb_current_time();
262 client_p
->flags
&= ~FLAGS_PINGSENT
;
265 * Before we even think of parsing what we just read, stick
266 * it on the end of the receive queue and do it when its
269 if(IsHandshake(client_p
) || IsUnknown(client_p
))
272 lbuf_len
= rb_linebuf_parse(&client_p
->localClient
->buf_recvq
, readBuf
, length
, binary
);
274 lclient_p
->actually_read
+= lbuf_len
;
276 if(IsAnyDead(client_p
))
279 /* Attempt to parse what we have */
280 parse_client_queued(client_p
);
282 if(IsAnyDead(client_p
))
285 /* Check to make sure we're not flooding */
286 if(!IsAnyServer(client_p
) &&
287 (rb_linebuf_alloclen(&client_p
->localClient
->buf_recvq
) > ConfigFileEntry
.client_flood
))
289 if(!(ConfigFileEntry
.no_oper_flood
&& IsOper(client_p
)))
291 exit_client(client_p
, client_p
, client_p
, "Excess Flood");
297 /* bail if short read */
298 if(length
< READBUF_SIZE
)
300 rb_setselect(client_p
->localClient
->F
, RB_SELECT_READ
, read_packet
, client_p
);
307 * client_dopacket - copy packet to client buf and parse it
308 * client_p - pointer to client structure for which the buffer data
310 * buffer - pointr to the buffer containing the newly read data
311 * length - number of valid bytes of data in the buffer
314 * It is implicitly assumed that dopacket is called only
315 * with client_p of "local" variation, which contains all the
316 * necessary fields (buffer etc..)
319 client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
)
321 s_assert(client_p
!= NULL
);
322 s_assert(buffer
!= NULL
);
324 if(client_p
== NULL
|| buffer
== NULL
)
326 if(IsAnyDead(client_p
))
329 * Update messages received
331 ++me
.localClient
->receiveM
;
332 ++client_p
->localClient
->receiveM
;
335 * Update bytes received
337 client_p
->localClient
->receiveB
+= length
;
338 me
.localClient
->receiveB
+= length
;
340 parse(client_p
, buffer
, buffer
+ length
);
345 * marks the end of the clients grace period
348 flood_endgrace(struct Client
*client_p
)
350 SetFloodDone(client_p
);
352 /* Drop their flood limit back down */
353 client_p
->localClient
->allow_read
= MAX_FLOOD
;
355 /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
358 client_p
->localClient
->sent_parsed
= 0;