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
35 #include "s_newconf.h"
37 static char readBuf
[READBUF_SIZE
];
38 static void client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
);
41 * parse_client_queued - parse client queued messages
44 parse_client_queued(struct Client
*client_p
)
49 if(IsAnyDead(client_p
))
52 if(IsUnknown(client_p
))
54 allow_read
= ConfigFileEntry
.client_flood_burst_max
;
57 if(client_p
->localClient
->sent_parsed
>= 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;
86 /* If sent_parsed is impossibly high, drop it down.
87 * This is useful if the configuration is changed.
89 if(client_p
->localClient
->sent_parsed
> allow_read
)
90 client_p
->localClient
->sent_parsed
= allow_read
;
93 if(IsAnyServer(client_p
) || IsExemptFlood(client_p
))
95 while (!IsAnyDead(client_p
) && (dolen
= rb_linebuf_get(&client_p
->localClient
->buf_recvq
,
96 readBuf
, READBUF_SIZE
, LINEBUF_COMPLETE
,
99 client_dopacket(client_p
, readBuf
, dolen
);
102 else if(IsClient(client_p
))
104 if(IsFloodDone(client_p
))
105 allow_read
= ConfigFileEntry
.client_flood_burst_max
;
107 allow_read
= ConfigFileEntry
.client_flood_burst_rate
;
108 allow_read
*= ConfigFileEntry
.client_flood_message_time
;
109 /* allow opers 4 times the amount of messages as users. why 4?
112 if(IsOperGeneral(client_p
) && ConfigFileEntry
.no_oper_flood
)
115 * Handle flood protection here - if we exceed our flood limit on
116 * messages in this loop, we simply drop out of the loop prematurely.
121 /* This flood protection works as follows:
123 * A client is given allow_read lines to send to the server. Every
124 * time a line is parsed, sent_parsed is increased. sent_parsed
125 * is decreased by 1 every time flood_recalc is called.
127 * Thus a client can 'burst' allow_read lines to the server, any
128 * excess lines will be parsed one per flood_recalc() call.
130 * Therefore a client will be penalised more if they keep flooding,
131 * as sent_parsed will always hover around the allow_read limit
132 * and no 'bursts' will be permitted.
134 if(client_p
->localClient
->sent_parsed
>= allow_read
)
137 /* post_registration_delay hack. Don't process any messages from a new client for $n seconds,
138 * to allow network bots to do their thing before channels can be joined.
140 if (rb_current_time() < client_p
->localClient
->firsttime
+ ConfigFileEntry
.post_registration_delay
)
143 dolen
= rb_linebuf_get(&client_p
->localClient
->
144 buf_recvq
, readBuf
, READBUF_SIZE
,
145 LINEBUF_COMPLETE
, LINEBUF_PARSED
);
150 client_dopacket(client_p
, readBuf
, dolen
);
151 if(IsAnyDead(client_p
))
154 client_p
->localClient
->sent_parsed
+= ConfigFileEntry
.client_flood_message_time
;
156 /* If sent_parsed is impossibly high, drop it down.
157 * This is useful if the configuration is changed.
159 if(client_p
->localClient
->sent_parsed
> allow_read
+
160 ConfigFileEntry
.client_flood_message_time
- 1)
161 client_p
->localClient
->sent_parsed
= allow_read
+
162 ConfigFileEntry
.client_flood_message_time
- 1;
168 * marks the end of the clients grace period
171 flood_endgrace(struct Client
*client_p
)
173 SetFloodDone(client_p
);
175 /* sent_parsed could be way over client_flood_burst_max but under
176 * client_flood_burst_rate so reset it.
178 client_p
->localClient
->sent_parsed
= 0;
184 * recalculate the number of allowed flood lines. this should be called
185 * once a second on any given client. We then attempt to flush some data.
188 flood_recalc(void *unused
)
190 rb_dlink_node
*ptr
, *next
;
191 struct Client
*client_p
;
193 RB_DLINK_FOREACH_SAFE(ptr
, next
, lclient_list
.head
)
195 client_p
= ptr
->data
;
197 if(rb_unlikely(IsMe(client_p
)))
200 if(rb_unlikely(client_p
->localClient
== NULL
))
203 if(IsFloodDone(client_p
))
204 client_p
->localClient
->sent_parsed
-= ConfigFileEntry
.client_flood_message_num
;
206 client_p
->localClient
->sent_parsed
= 0;
208 if(client_p
->localClient
->sent_parsed
< 0)
209 client_p
->localClient
->sent_parsed
= 0;
211 parse_client_queued(client_p
);
213 if(rb_unlikely(IsAnyDead(client_p
)))
218 RB_DLINK_FOREACH_SAFE(ptr
, next
, unknown_list
.head
)
220 client_p
= ptr
->data
;
222 if(client_p
->localClient
== NULL
)
225 client_p
->localClient
->sent_parsed
--;
227 if(client_p
->localClient
->sent_parsed
< 0)
228 client_p
->localClient
->sent_parsed
= 0;
230 parse_client_queued(client_p
);
235 * read_packet - Read a 'packet' of data from a connection and process it.
238 read_packet(rb_fde_t
* F
, void *data
)
240 struct Client
*client_p
= data
;
246 if(IsAnyDead(client_p
))
250 * Read some data. We *used to* do anti-flood protection here, but
251 * I personally think it makes the code too hairy to make sane.
254 length
= rb_read(client_p
->localClient
->F
, readBuf
, READBUF_SIZE
);
258 if(rb_ignore_errno(errno
))
259 rb_setselect(client_p
->localClient
->F
,
260 RB_SELECT_READ
, read_packet
, client_p
);
262 error_exit_client(client_p
, length
);
267 error_exit_client(client_p
, length
);
271 if(client_p
->localClient
->lasttime
< rb_current_time())
272 client_p
->localClient
->lasttime
= rb_current_time();
273 client_p
->flags
&= ~FLAGS_PINGSENT
;
276 * Before we even think of parsing what we just read, stick
277 * it on the end of the receive queue and do it when its
280 if(IsHandshake(client_p
) || IsUnknown(client_p
))
283 (void) rb_linebuf_parse(&client_p
->localClient
->buf_recvq
, readBuf
, length
, binary
);
285 if(IsAnyDead(client_p
))
288 /* Attempt to parse what we have */
289 parse_client_queued(client_p
);
291 if(IsAnyDead(client_p
))
294 /* Check to make sure we're not flooding */
295 if(!IsAnyServer(client_p
) &&
296 (rb_linebuf_alloclen(&client_p
->localClient
->buf_recvq
) > ConfigFileEntry
.client_flood_max_lines
))
298 if(!(ConfigFileEntry
.no_oper_flood
&& IsOperGeneral(client_p
)))
300 exit_client(client_p
, client_p
, client_p
, "Excess Flood");
305 /* bail if short read, but not for SCTP as it returns data in packets */
306 if (length
< READBUF_SIZE
&& !(rb_get_type(client_p
->localClient
->F
) & RB_FD_SCTP
)) {
307 rb_setselect(client_p
->localClient
->F
, RB_SELECT_READ
, read_packet
, client_p
);
314 * client_dopacket - copy packet to client buf and parse it
315 * client_p - pointer to client structure for which the buffer data
317 * buffer - pointr to the buffer containing the newly read data
318 * length - number of valid bytes of data in the buffer
321 * It is implicitly assumed that dopacket is called only
322 * with client_p of "local" variation, which contains all the
323 * necessary fields (buffer etc..)
326 client_dopacket(struct Client
*client_p
, char *buffer
, size_t length
)
328 s_assert(client_p
!= NULL
);
329 s_assert(buffer
!= NULL
);
331 if(client_p
== NULL
|| buffer
== NULL
)
333 if(IsAnyDead(client_p
))
336 * Update messages received
338 ++me
.localClient
->receiveM
;
339 ++client_p
->localClient
->receiveM
;
342 * Update bytes received
344 client_p
->localClient
->receiveB
+= length
;
346 if(client_p
->localClient
->receiveB
> 1023)
348 client_p
->localClient
->receiveK
+= (client_p
->localClient
->receiveB
>> 10);
349 client_p
->localClient
->receiveB
&= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
352 me
.localClient
->receiveB
+= length
;
354 if(me
.localClient
->receiveB
> 1023)
356 me
.localClient
->receiveK
+= (me
.localClient
->receiveB
>> 10);
357 me
.localClient
->receiveB
&= 0x03ff;
360 parse(client_p
, buffer
, buffer
+ length
);