]>
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 $
37 static char readBuf
[READBUF_SIZE
];
38 static void client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
);
42 * parse_client_queued - parse client queued messages
45 parse_client_queued(struct Client
*client_p
)
50 if(IsAnyDead(client_p
))
53 if(IsUnknown(client_p
))
57 if(client_p
->localClient
->sent_parsed
>= client_p
->localClient
->allow_read
)
60 dolen
= rb_linebuf_get(&client_p
->localClient
->
61 buf_recvq
, readBuf
, READBUF_SIZE
,
62 LINEBUF_COMPLETE
, LINEBUF_PARSED
);
64 if(dolen
<= 0 || IsDead(client_p
))
67 client_dopacket(client_p
, readBuf
, dolen
);
68 client_p
->localClient
->sent_parsed
++;
71 if(IsAnyDead(client_p
))
73 /* if theyve dropped out of the unknown state, break and move
74 * to the parsing for their appropriate status. --fl
76 if(!IsUnknown(client_p
))
78 /* reset their flood limits, they're now
81 client_p
->localClient
->sent_parsed
= 0;
87 if(IsAnyServer(client_p
) || IsExemptFlood(client_p
))
89 while (!IsAnyDead(client_p
) && (dolen
= rb_linebuf_get(&client_p
->localClient
->buf_recvq
,
90 readBuf
, READBUF_SIZE
, LINEBUF_COMPLETE
,
93 client_dopacket(client_p
, readBuf
, dolen
);
96 else if(IsClient(client_p
))
99 if(IsOper(client_p
) && ConfigFileEntry
.no_oper_flood
)
102 * Handle flood protection here - if we exceed our flood limit on
103 * messages in this loop, we simply drop out of the loop prematurely.
108 /* This flood protection works as follows:
110 * A client is given allow_read lines to send to the server. Every
111 * time a line is parsed, sent_parsed is increased. sent_parsed
112 * is decreased by 1 every time flood_recalc is called.
114 * Thus a client can 'burst' allow_read lines to the server, any
115 * excess lines will be parsed one per flood_recalc() call.
117 * Therefore a client will be penalised more if they keep flooding,
118 * as sent_parsed will always hover around the allow_read limit
119 * and no 'bursts' will be permitted.
123 if(client_p
->localClient
->sent_parsed
>= client_p
->localClient
->allow_read
)
127 /* allow opers 4 times the amount of messages as users. why 4?
130 else if(client_p
->localClient
->sent_parsed
>= (4 * client_p
->localClient
->allow_read
))
133 dolen
= rb_linebuf_get(&client_p
->localClient
->
134 buf_recvq
, readBuf
, READBUF_SIZE
,
135 LINEBUF_COMPLETE
, LINEBUF_PARSED
);
140 client_dopacket(client_p
, readBuf
, dolen
);
141 if(IsAnyDead(client_p
))
143 client_p
->localClient
->sent_parsed
++;
151 * recalculate the number of allowed flood lines. this should be called
152 * once a second on any given client. We then attempt to flush some data.
155 flood_recalc(void *unused
)
157 rb_dlink_node
*ptr
, *next
;
158 struct Client
*client_p
;
160 RB_DLINK_FOREACH_SAFE(ptr
, next
, lclient_list
.head
)
162 client_p
= ptr
->data
;
164 if(unlikely(IsMe(client_p
)))
167 if(unlikely(client_p
->localClient
== NULL
))
170 if(IsFloodDone(client_p
))
171 client_p
->localClient
->sent_parsed
-= 2;
173 client_p
->localClient
->sent_parsed
= 0;
175 if(client_p
->localClient
->sent_parsed
< 0)
176 client_p
->localClient
->sent_parsed
= 0;
178 if(--client_p
->localClient
->actually_read
< 0)
179 client_p
->localClient
->actually_read
= 0;
181 parse_client_queued(client_p
);
183 if(unlikely(IsAnyDead(client_p
)))
188 RB_DLINK_FOREACH_SAFE(ptr
, next
, unknown_list
.head
)
190 client_p
= ptr
->data
;
192 if(client_p
->localClient
== NULL
)
195 client_p
->localClient
->sent_parsed
--;
197 if(client_p
->localClient
->sent_parsed
< 0)
198 client_p
->localClient
->sent_parsed
= 0;
200 if(--client_p
->localClient
->actually_read
< 0)
201 client_p
->localClient
->actually_read
= 0;
203 parse_client_queued(client_p
);
209 * read_packet - Read a 'packet' of data from a connection and process it.
212 read_packet(rb_fde_t
*F
, void *data
)
214 struct Client
*client_p
= data
;
215 struct LocalUser
*lclient_p
= client_p
->localClient
;
220 #ifdef USE_IODEBUG_HOOKS
225 while(1) /* note..for things like rt sigio to work you *must* loop on read until you get EAGAIN */
227 if(IsAnyDead(client_p
))
231 * Read some data. We *used to* do anti-flood protection here, but
232 * I personally think it makes the code too hairy to make sane.
235 length
= rb_read(client_p
->localClient
->F
, readBuf
, READBUF_SIZE
);
238 if(rb_ignore_errno(errno
))
240 rb_setselect(client_p
->localClient
->F
,
241 RB_SELECT_READ
, read_packet
, client_p
);
243 error_exit_client(client_p
, length
);
248 error_exit_client(client_p
, length
);
252 #ifdef USE_IODEBUG_HOOKS
253 hdata
.client
= client_p
;
254 hdata
.arg1
= readBuf
;
256 call_hook(h_iorecv_id
, &hdata
);
259 if(client_p
->localClient
->lasttime
< rb_current_time())
260 client_p
->localClient
->lasttime
= rb_current_time();
261 client_p
->flags
&= ~FLAGS_PINGSENT
;
264 * Before we even think of parsing what we just read, stick
265 * it on the end of the receive queue and do it when its
268 if(IsHandshake(client_p
) || IsUnknown(client_p
))
271 lbuf_len
= rb_linebuf_parse(&client_p
->localClient
->buf_recvq
, readBuf
, length
, binary
);
273 lclient_p
->actually_read
+= lbuf_len
;
275 if(IsAnyDead(client_p
))
278 /* Attempt to parse what we have */
279 parse_client_queued(client_p
);
281 if(IsAnyDead(client_p
))
284 /* Check to make sure we're not flooding */
285 if(!IsAnyServer(client_p
) &&
286 (rb_linebuf_alloclen(&client_p
->localClient
->buf_recvq
) > ConfigFileEntry
.client_flood
))
288 if(!(ConfigFileEntry
.no_oper_flood
&& IsOper(client_p
)))
290 exit_client(client_p
, client_p
, client_p
, "Excess Flood");
296 /* bail if short read */
297 if(length
< READBUF_SIZE
)
299 rb_setselect(client_p
->localClient
->F
, RB_SELECT_READ
, read_packet
, client_p
);
306 * client_dopacket - copy packet to client buf and parse it
307 * client_p - pointer to client structure for which the buffer data
309 * buffer - pointr to the buffer containing the newly read data
310 * length - number of valid bytes of data in the buffer
313 * It is implicitly assumed that dopacket is called only
314 * with client_p of "local" variation, which contains all the
315 * necessary fields (buffer etc..)
318 client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
)
320 s_assert(client_p
!= NULL
);
321 s_assert(buffer
!= NULL
);
323 if(client_p
== NULL
|| buffer
== NULL
)
325 if(IsAnyDead(client_p
))
328 * Update messages received
330 ++me
.localClient
->receiveM
;
331 ++client_p
->localClient
->receiveM
;
334 * Update bytes received
336 client_p
->localClient
->receiveB
+= length
;
337 me
.localClient
->receiveB
+= length
;
339 parse(client_p
, buffer
, buffer
+ length
);
344 * marks the end of the clients grace period
347 flood_endgrace(struct Client
*client_p
)
349 SetFloodDone(client_p
);
351 /* Drop their flood limit back down */
352 client_p
->localClient
->allow_read
= MAX_FLOOD
;
354 /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
357 client_p
->localClient
->sent_parsed
= 0;