1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
< |
* packet.c: Packet handlers. |
2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
4 |
> |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU General Public License |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
< |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
18 |
> |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
|
* USA |
21 |
– |
* |
22 |
– |
* $Id$ |
20 |
|
*/ |
21 |
+ |
|
22 |
+ |
/*! \file packet.c |
23 |
+ |
* \brief Packet handlers. |
24 |
+ |
* \version $Id$ |
25 |
+ |
*/ |
26 |
+ |
|
27 |
|
#include "stdinc.h" |
28 |
< |
#include "tools.h" |
28 |
> |
#include "list.h" |
29 |
|
#include "s_bsd.h" |
30 |
< |
#include "s_conf.h" |
31 |
< |
#include "s_serv.h" |
30 |
> |
#include "conf.h" |
31 |
> |
#include "server.h" |
32 |
|
#include "client.h" |
30 |
– |
#include "common.h" |
33 |
|
#include "ircd.h" |
32 |
– |
#include "list.h" |
34 |
|
#include "parse.h" |
35 |
|
#include "fdlist.h" |
36 |
|
#include "packet.h" |
37 |
|
#include "irc_string.h" |
38 |
|
#include "memory.h" |
38 |
– |
#include "hook.h" |
39 |
|
#include "send.h" |
40 |
< |
#include "irc_getnameinfo.h" |
40 |
> |
#include "misc.h" |
41 |
|
|
42 |
|
#define READBUF_SIZE 16384 |
43 |
|
|
44 |
– |
struct Callback *iorecv_cb = NULL; |
45 |
– |
struct Callback *iorecvctrl_cb = NULL; |
46 |
– |
|
44 |
|
static char readBuf[READBUF_SIZE]; |
45 |
|
static void client_dopacket(struct Client *, char *, size_t); |
46 |
|
|
54 |
|
static int |
55 |
|
extract_one_line(struct dbuf_queue *qptr, char *buffer) |
56 |
|
{ |
60 |
– |
struct dbuf_block *block; |
57 |
|
int line_bytes = 0, empty_bytes = 0, phase = 0; |
58 |
< |
unsigned int idx; |
59 |
< |
|
64 |
< |
char c; |
65 |
< |
dlink_node *ptr; |
58 |
> |
unsigned int idx = 0; |
59 |
> |
dlink_node *node = NULL; |
60 |
|
|
61 |
|
/* |
62 |
|
* Phase 0: "empty" characters before the line |
70 |
|
* |
71 |
|
* --adx |
72 |
|
*/ |
73 |
< |
DLINK_FOREACH(ptr, qptr->blocks.head) |
73 |
> |
DLINK_FOREACH(node, qptr->blocks.head) |
74 |
|
{ |
75 |
< |
block = ptr->data; |
75 |
> |
struct dbuf_block *block = node->data; |
76 |
> |
|
77 |
> |
if (node == qptr->blocks.head) |
78 |
> |
idx = qptr->pos; |
79 |
> |
else |
80 |
> |
idx = 0; |
81 |
|
|
82 |
< |
for (idx = 0; idx < block->size; idx++) |
82 |
> |
for (; idx < block->size; ++idx) |
83 |
|
{ |
84 |
< |
c = block->data[idx]; |
84 |
> |
char c = block->data[idx]; |
85 |
> |
|
86 |
|
if (IsEol(c) || (c == ' ' && phase != 1)) |
87 |
|
{ |
88 |
< |
empty_bytes++; |
88 |
> |
++empty_bytes; |
89 |
> |
|
90 |
|
if (phase == 1) |
91 |
|
phase = 2; |
92 |
|
} |
116 |
|
dbuf_delete(qptr, line_bytes + empty_bytes); |
117 |
|
return IRCD_MIN(line_bytes, IRCD_BUFSIZE - 2); |
118 |
|
} |
118 |
– |
|
119 |
|
/* |
120 |
|
* parse_client_queued - parse client queued messages |
121 |
|
*/ |
122 |
|
static void |
123 |
|
parse_client_queued(struct Client *client_p) |
124 |
< |
{ |
124 |
> |
{ |
125 |
|
int dolen = 0; |
126 |
|
int checkflood = 1; |
127 |
< |
struct LocalUser *lclient_p = client_p->localClient; |
127 |
> |
struct Connection *lclient_p = client_p->connection; |
128 |
|
|
129 |
|
if (IsUnknown(client_p)) |
130 |
|
{ |
131 |
|
int i = 0; |
132 |
|
|
133 |
< |
for(;;) |
133 |
> |
while (1) |
134 |
|
{ |
135 |
|
if (IsDefunct(client_p)) |
136 |
< |
return; |
136 |
> |
return; |
137 |
|
|
138 |
|
/* rate unknown clients at MAX_FLOOD per loop */ |
139 |
|
if (i >= MAX_FLOOD) |
140 |
|
break; |
141 |
|
|
142 |
|
dolen = extract_one_line(&lclient_p->buf_recvq, readBuf); |
143 |
+ |
|
144 |
|
if (dolen == 0) |
145 |
< |
break; |
145 |
> |
break; |
146 |
|
|
147 |
|
client_dopacket(client_p, readBuf, dolen); |
148 |
|
i++; |
150 |
|
/* if they've dropped out of the unknown state, break and move |
151 |
|
* to the parsing for their appropriate status. --fl |
152 |
|
*/ |
153 |
< |
if(!IsUnknown(client_p)) |
153 |
> |
if (!IsUnknown(client_p)) |
154 |
|
break; |
155 |
|
} |
156 |
|
} |
161 |
|
{ |
162 |
|
if (IsDefunct(client_p)) |
163 |
|
return; |
164 |
< |
if ((dolen = extract_one_line(&lclient_p->buf_recvq, |
165 |
< |
readBuf)) == 0) |
164 |
> |
|
165 |
> |
if ((dolen = extract_one_line(&lclient_p->buf_recvq, readBuf)) == 0) |
166 |
|
break; |
167 |
+ |
|
168 |
|
client_dopacket(client_p, readBuf, dolen); |
169 |
|
} |
170 |
|
} |
171 |
< |
else if(IsClient(client_p)) |
171 |
> |
else if (IsClient(client_p)) |
172 |
|
{ |
173 |
< |
if (ConfigFileEntry.no_oper_flood && (IsOper(client_p) || IsCanFlood(client_p))) |
173 |
> |
if (ConfigGeneral.no_oper_flood && (HasUMode(client_p, UMODE_OPER) || IsCanFlood(client_p))) |
174 |
|
{ |
175 |
< |
if (ConfigFileEntry.true_no_oper_flood) |
175 |
> |
if (ConfigGeneral.true_no_oper_flood) |
176 |
|
checkflood = -1; |
177 |
|
else |
178 |
|
checkflood = 0; |
183 |
|
* messages in this loop, we simply drop out of the loop prematurely. |
184 |
|
* -- adrian |
185 |
|
*/ |
186 |
< |
for (;;) |
186 |
> |
while (1) |
187 |
|
{ |
188 |
|
if (IsDefunct(client_p)) |
189 |
< |
break; |
189 |
> |
break; |
190 |
|
|
191 |
|
/* This flood protection works as follows: |
192 |
|
* |
202 |
|
* and no 'bursts' will be permitted. |
203 |
|
*/ |
204 |
|
if (checkflood > 0) |
205 |
< |
{ |
204 |
< |
if(lclient_p->sent_parsed >= lclient_p->allow_read) |
205 |
> |
if (lclient_p->sent_parsed >= lclient_p->allow_read) |
206 |
|
break; |
207 |
< |
} |
208 |
< |
|
209 |
< |
/* allow opers 4 times the amount of messages as users. why 4? |
207 |
> |
|
208 |
> |
/* |
209 |
> |
* Allow opers 4 times the amount of messages as users. why 4? |
210 |
|
* why not. :) --fl_ |
211 |
|
*/ |
212 |
< |
else if (lclient_p->sent_parsed >= (4 * lclient_p->allow_read) && |
212 |
< |
checkflood != -1) |
212 |
> |
else if (lclient_p->sent_parsed >= (4 * lclient_p->allow_read) && checkflood != -1) |
213 |
|
break; |
214 |
|
|
215 |
|
dolen = extract_one_line(&lclient_p->buf_recvq, readBuf); |
216 |
+ |
|
217 |
|
if (dolen == 0) |
218 |
|
break; |
219 |
|
|
233 |
|
SetFloodDone(client_p); |
234 |
|
|
235 |
|
/* Drop their flood limit back down */ |
236 |
< |
client_p->localClient->allow_read = MAX_FLOOD; |
236 |
> |
client_p->connection->allow_read = MAX_FLOOD; |
237 |
|
|
238 |
|
/* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST, |
239 |
|
* so reset it. |
240 |
|
*/ |
241 |
< |
client_p->localClient->sent_parsed = 0; |
241 |
> |
client_p->connection->sent_parsed = 0; |
242 |
|
} |
243 |
|
|
244 |
|
/* |
251 |
|
flood_recalc(fde_t *fd, void *data) |
252 |
|
{ |
253 |
|
struct Client *client_p = data; |
254 |
< |
struct LocalUser *lclient_p = client_p->localClient; |
255 |
< |
|
256 |
< |
/* allow a bursting client their allocation per second, allow |
254 |
> |
struct Connection *lclient_p = client_p->connection; |
255 |
> |
|
256 |
> |
/* |
257 |
> |
* Allow a bursting client their allocation per second, allow |
258 |
|
* a client whos flooding an extra 2 per second |
259 |
|
*/ |
260 |
|
if (IsFloodDone(client_p)) |
261 |
|
lclient_p->sent_parsed -= 2; |
262 |
|
else |
263 |
|
lclient_p->sent_parsed = 0; |
264 |
< |
|
264 |
> |
|
265 |
|
if (lclient_p->sent_parsed < 0) |
266 |
|
lclient_p->sent_parsed = 0; |
267 |
< |
|
267 |
> |
|
268 |
|
parse_client_queued(client_p); |
269 |
< |
|
269 |
> |
|
270 |
|
/* And now, try flushing .. */ |
271 |
|
if (!IsDead(client_p)) |
272 |
|
{ |
276 |
|
} |
277 |
|
|
278 |
|
/* |
277 |
– |
* read_ctrl_packet - Read a 'packet' of data from a servlink control |
278 |
– |
* link and process it. |
279 |
– |
*/ |
280 |
– |
void |
281 |
– |
read_ctrl_packet(fde_t *fd, void *data) |
282 |
– |
{ |
283 |
– |
struct Client *server = data; |
284 |
– |
struct LocalUser *lserver = server->localClient; |
285 |
– |
struct SlinkRpl *reply; |
286 |
– |
int length = 0; |
287 |
– |
unsigned char tmp[2]; |
288 |
– |
unsigned char *len = tmp; |
289 |
– |
struct SlinkRplDef *replydef; |
290 |
– |
|
291 |
– |
assert(lserver != NULL); |
292 |
– |
|
293 |
– |
reply = &lserver->slinkrpl; |
294 |
– |
|
295 |
– |
if (IsDefunct(server)) |
296 |
– |
return; |
297 |
– |
|
298 |
– |
if (!reply->command) |
299 |
– |
{ |
300 |
– |
reply->gotdatalen = 0; |
301 |
– |
reply->readdata = 0; |
302 |
– |
reply->data = NULL; |
303 |
– |
|
304 |
– |
length = recv(fd->fd, tmp, 1, 0); |
305 |
– |
|
306 |
– |
if (length <= 0) |
307 |
– |
{ |
308 |
– |
if ((length == -1) && ignoreErrno(errno)) |
309 |
– |
goto nodata; |
310 |
– |
dead_link_on_read(server, length); |
311 |
– |
return; |
312 |
– |
} |
313 |
– |
reply->command = tmp[0]; |
314 |
– |
} |
315 |
– |
|
316 |
– |
for (replydef = slinkrpltab; replydef->handler; replydef++) |
317 |
– |
{ |
318 |
– |
if (replydef->replyid == (unsigned int)reply->command) |
319 |
– |
break; |
320 |
– |
} |
321 |
– |
|
322 |
– |
/* we should be able to trust a local slink process... |
323 |
– |
* and if it sends an invalid command, that's a bug.. */ |
324 |
– |
assert(replydef->handler); |
325 |
– |
|
326 |
– |
if ((replydef->flags & SLINKRPL_FLAG_DATA) && (reply->gotdatalen < 2)) |
327 |
– |
{ |
328 |
– |
/* we need a datalen u16 which we don't have yet... */ |
329 |
– |
length = recv(fd->fd, len, (2 - reply->gotdatalen), 0); |
330 |
– |
if (length <= 0) |
331 |
– |
{ |
332 |
– |
if ((length == -1) && ignoreErrno(errno)) |
333 |
– |
goto nodata; |
334 |
– |
dead_link_on_read(server, length); |
335 |
– |
return; |
336 |
– |
} |
337 |
– |
|
338 |
– |
if (reply->gotdatalen == 0) |
339 |
– |
{ |
340 |
– |
reply->datalen = *len << 8; |
341 |
– |
reply->gotdatalen++; |
342 |
– |
length--; |
343 |
– |
len++; |
344 |
– |
} |
345 |
– |
if (length && (reply->gotdatalen == 1)) |
346 |
– |
{ |
347 |
– |
reply->datalen |= *len; |
348 |
– |
reply->gotdatalen++; |
349 |
– |
if (reply->datalen > 0) |
350 |
– |
reply->data = MyMalloc(reply->datalen); |
351 |
– |
} |
352 |
– |
|
353 |
– |
if (reply->gotdatalen < 2) |
354 |
– |
return; /* wait for more data */ |
355 |
– |
} |
356 |
– |
|
357 |
– |
if (reply->readdata < reply->datalen) /* try to get any remaining data */ |
358 |
– |
{ |
359 |
– |
length = recv(fd->fd, (reply->data + reply->readdata), |
360 |
– |
(reply->datalen - reply->readdata), 0); |
361 |
– |
if (length <= 0) |
362 |
– |
{ |
363 |
– |
if ((length == -1) && ignoreErrno(errno)) |
364 |
– |
goto nodata; |
365 |
– |
dead_link_on_read(server, length); |
366 |
– |
return; |
367 |
– |
} |
368 |
– |
|
369 |
– |
reply->readdata += length; |
370 |
– |
if (reply->readdata < reply->datalen) |
371 |
– |
return; /* wait for more data */ |
372 |
– |
} |
373 |
– |
|
374 |
– |
execute_callback(iorecvctrl_cb, server, reply->command); |
375 |
– |
|
376 |
– |
/* we now have the command and any data, pass it off to the handler */ |
377 |
– |
(*replydef->handler)(reply->command, reply->datalen, reply->data, server); |
378 |
– |
|
379 |
– |
/* reset SlinkRpl */ |
380 |
– |
if (reply->datalen > 0) |
381 |
– |
MyFree(reply->data); |
382 |
– |
reply->command = 0; |
383 |
– |
|
384 |
– |
if (IsDead(server)) |
385 |
– |
return; |
386 |
– |
|
387 |
– |
nodata: |
388 |
– |
/* If we get here, we need to register for another COMM_SELECT_READ */ |
389 |
– |
comm_setselect(fd, COMM_SELECT_READ, read_ctrl_packet, server, 0); |
390 |
– |
} |
391 |
– |
|
392 |
– |
/* |
279 |
|
* read_packet - Read a 'packet' of data from a connection and process it. |
280 |
|
*/ |
281 |
|
void |
292 |
|
* I personally think it makes the code too hairy to make sane. |
293 |
|
* -- adrian |
294 |
|
*/ |
295 |
< |
do { |
295 |
> |
do |
296 |
> |
{ |
297 |
|
#ifdef HAVE_LIBCRYPTO |
298 |
|
if (fd->ssl) |
299 |
|
{ |
300 |
< |
length = SSL_read(fd->ssl, readBuf, READBUF_SIZE); |
300 |
> |
length = SSL_read(fd->ssl, readBuf, sizeof(readBuf)); |
301 |
|
|
302 |
|
/* translate openssl error codes, sigh */ |
303 |
|
if (length < 0) |
304 |
|
switch (SSL_get_error(fd->ssl, length)) |
305 |
< |
{ |
305 |
> |
{ |
306 |
|
case SSL_ERROR_WANT_WRITE: |
307 |
< |
fd->flags.pending_read = 1; |
308 |
< |
SetSendqBlocked(client_p); |
309 |
< |
comm_setselect(fd, COMM_SELECT_WRITE, (PF *) sendq_unblocked, |
310 |
< |
client_p, 0); |
424 |
< |
return; |
425 |
< |
case SSL_ERROR_WANT_READ: |
426 |
< |
errno = EWOULDBLOCK; |
307 |
> |
comm_setselect(fd, COMM_SELECT_WRITE, sendq_unblocked, client_p, 0); |
308 |
> |
return; |
309 |
> |
case SSL_ERROR_WANT_READ: |
310 |
> |
errno = EWOULDBLOCK; |
311 |
|
case SSL_ERROR_SYSCALL: |
312 |
< |
break; |
312 |
> |
break; |
313 |
> |
case SSL_ERROR_SSL: |
314 |
> |
if (errno == EAGAIN) |
315 |
> |
break; |
316 |
|
default: |
317 |
< |
length = errno = 0; |
318 |
< |
} |
317 |
> |
length = errno = 0; |
318 |
> |
} |
319 |
|
} |
320 |
|
else |
321 |
|
#endif |
322 |
|
{ |
323 |
< |
length = recv(fd->fd, readBuf, READBUF_SIZE, 0); |
437 |
< |
#ifdef _WIN32 |
438 |
< |
if (length < 0) |
439 |
< |
errno = WSAGetLastError(); |
440 |
< |
#endif |
323 |
> |
length = recv(fd->fd, readBuf, sizeof(readBuf), 0); |
324 |
|
} |
325 |
|
|
326 |
|
if (length <= 0) |
336 |
|
return; |
337 |
|
} |
338 |
|
|
339 |
< |
execute_callback(iorecv_cb, client_p, length, readBuf); |
339 |
> |
dbuf_put(&client_p->connection->buf_recvq, readBuf, length); |
340 |
|
|
341 |
< |
if (client_p->lasttime < CurrentTime) |
342 |
< |
client_p->lasttime = CurrentTime; |
343 |
< |
if (client_p->lasttime > client_p->since) |
344 |
< |
client_p->since = CurrentTime; |
345 |
< |
ClearPingSent(client_p); |
341 |
> |
if (client_p->connection->lasttime < CurrentTime) |
342 |
> |
client_p->connection->lasttime = CurrentTime; |
343 |
> |
|
344 |
> |
if (client_p->connection->lasttime > client_p->connection->since) |
345 |
> |
client_p->connection->since = CurrentTime; |
346 |
|
|
347 |
< |
dbuf_put(&client_p->localClient->buf_recvq, readBuf, length); |
347 |
> |
ClearPingSent(client_p); |
348 |
|
|
349 |
|
/* Attempt to parse what we have */ |
350 |
|
parse_client_queued(client_p); |
353 |
|
return; |
354 |
|
|
355 |
|
/* Check to make sure we're not flooding */ |
473 |
– |
/* TBD - ConfigFileEntry.client_flood should be a size_t */ |
356 |
|
if (!(IsServer(client_p) || IsHandshake(client_p) || IsConnecting(client_p)) |
357 |
< |
&& (dbuf_length(&client_p->localClient->buf_recvq) > |
358 |
< |
(unsigned int)ConfigFileEntry.client_flood)) |
357 |
> |
&& (dbuf_length(&client_p->connection->buf_recvq) > |
358 |
> |
get_recvq(&client_p->connection->confs))) |
359 |
|
{ |
360 |
< |
if (!(ConfigFileEntry.no_oper_flood && IsOper(client_p))) |
360 |
> |
if (!(ConfigGeneral.no_oper_flood && HasUMode(client_p, UMODE_OPER))) |
361 |
|
{ |
362 |
< |
exit_client(client_p, client_p, "Excess Flood"); |
362 |
> |
exit_client(client_p, "Excess Flood"); |
363 |
|
return; |
364 |
|
} |
365 |
|
} |
389 |
|
static void |
390 |
|
client_dopacket(struct Client *client_p, char *buffer, size_t length) |
391 |
|
{ |
392 |
< |
/* |
392 |
> |
/* |
393 |
|
* Update messages received |
394 |
|
*/ |
395 |
< |
++me.localClient->recv.messages; |
396 |
< |
++client_p->localClient->recv.messages; |
395 |
> |
++me.connection->recv.messages; |
396 |
> |
++client_p->connection->recv.messages; |
397 |
|
|
398 |
< |
/* |
398 |
> |
/* |
399 |
|
* Update bytes received |
400 |
|
*/ |
401 |
< |
client_p->localClient->recv.bytes += length; |
402 |
< |
me.localClient->recv.bytes += length; |
401 |
> |
client_p->connection->recv.bytes += length; |
402 |
> |
me.connection->recv.bytes += length; |
403 |
|
|
404 |
|
parse(client_p, buffer, buffer + length); |
405 |
|
} |