mirror of
https://github.com/matrix-construct/construct
synced 2024-11-26 08:42:34 +01:00
Trying to fix packet.c
This commit is contained in:
parent
87116eca09
commit
4714a17f54
3 changed files with 363 additions and 478 deletions
|
@ -627,6 +627,7 @@ extern void close_connection(struct Client *);
|
||||||
extern void init_uid(void);
|
extern void init_uid(void);
|
||||||
extern char *generate_uid(void);
|
extern char *generate_uid(void);
|
||||||
|
|
||||||
|
void flood_endgrace(struct Client *);
|
||||||
void allocate_away(struct Client *);
|
void allocate_away(struct Client *);
|
||||||
void free_away(struct Client *);
|
void free_away(struct Client *);
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,6 @@
|
||||||
|
|
||||||
extern PF read_ctrl_packet;
|
extern PF read_ctrl_packet;
|
||||||
extern PF read_packet;
|
extern PF read_packet;
|
||||||
extern PF flood_recalc;
|
extern EVH flood_recalc;
|
||||||
extern void flood_endgrace(struct Client *);
|
|
||||||
|
|
||||||
#endif /* INCLUDED_packet_h */
|
#endif /* INCLUDED_packet_h */
|
||||||
|
|
369
src/packet.c
369
src/packet.c
|
@ -18,20 +18,20 @@
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program; if not, write to the Free Software
|
* along with this program; if not, write to the Free Software
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
||||||
* USA
|
* USA
|
||||||
*
|
*
|
||||||
* $Id: packet.c 3446 2007-05-14 22:21:16Z jilles $
|
* $Id: packet.c 25179 2008-03-30 16:34:57Z androsyn $
|
||||||
*/
|
*/
|
||||||
#include "stdinc.h"
|
#include "stdinc.h"
|
||||||
|
#include "struct.h"
|
||||||
#include "s_conf.h"
|
#include "s_conf.h"
|
||||||
#include "s_serv.h"
|
#include "s_serv.h"
|
||||||
#include "client.h"
|
#include "client.h"
|
||||||
#include "common.h"
|
|
||||||
#include "ircd.h"
|
#include "ircd.h"
|
||||||
#include "parse.h"
|
#include "parse.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "irc_string.h"
|
#include "match.h"
|
||||||
#include "hook.h"
|
#include "hook.h"
|
||||||
#include "send.h"
|
#include "send.h"
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ parse_client_queued(struct Client *client_p)
|
||||||
if(client_p->localClient->sent_parsed >= client_p->localClient->allow_read)
|
if(client_p->localClient->sent_parsed >= client_p->localClient->allow_read)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
dolen = linebuf_get(&client_p->localClient->
|
dolen = rb_linebuf_get(&client_p->localClient->
|
||||||
buf_recvq, readBuf, READBUF_SIZE,
|
buf_recvq, readBuf, READBUF_SIZE,
|
||||||
LINEBUF_COMPLETE, LINEBUF_PARSED);
|
LINEBUF_COMPLETE, LINEBUF_PARSED);
|
||||||
|
|
||||||
|
@ -82,13 +82,12 @@ parse_client_queued(struct Client *client_p)
|
||||||
client_p->localClient->sent_parsed = 0;
|
client_p->localClient->sent_parsed = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(IsAnyServer(client_p) || IsExemptFlood(client_p))
|
if(IsAnyServer(client_p) || IsExemptFlood(client_p))
|
||||||
{
|
{
|
||||||
while (!IsAnyDead(client_p) && (dolen = linebuf_get(&client_p->localClient->buf_recvq,
|
while (!IsAnyDead(client_p) && (dolen = rb_linebuf_get(&client_p->localClient->buf_recvq,
|
||||||
readBuf, READBUF_SIZE, LINEBUF_COMPLETE,
|
readBuf, READBUF_SIZE, LINEBUF_COMPLETE,
|
||||||
LINEBUF_PARSED)) > 0)
|
LINEBUF_PARSED)) > 0)
|
||||||
{
|
{
|
||||||
|
@ -132,7 +131,7 @@ parse_client_queued(struct Client *client_p)
|
||||||
else if(client_p->localClient->sent_parsed >= (4 * client_p->localClient->allow_read))
|
else if(client_p->localClient->sent_parsed >= (4 * client_p->localClient->allow_read))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
dolen = linebuf_get(&client_p->localClient->
|
dolen = rb_linebuf_get(&client_p->localClient->
|
||||||
buf_recvq, readBuf, READBUF_SIZE,
|
buf_recvq, readBuf, READBUF_SIZE,
|
||||||
LINEBUF_COMPLETE, LINEBUF_PARSED);
|
LINEBUF_COMPLETE, LINEBUF_PARSED);
|
||||||
|
|
||||||
|
@ -147,24 +146,6 @@ parse_client_queued(struct Client *client_p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* flood_endgrace()
|
|
||||||
*
|
|
||||||
* marks the end of the clients grace period
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
flood_endgrace(struct Client *client_p)
|
|
||||||
{
|
|
||||||
SetFloodDone(client_p);
|
|
||||||
|
|
||||||
/* Drop their flood limit back down */
|
|
||||||
client_p->localClient->allow_read = MAX_FLOOD;
|
|
||||||
|
|
||||||
/* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
|
|
||||||
* so reset it.
|
|
||||||
*/
|
|
||||||
client_p->localClient->sent_parsed = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* flood_recalc
|
* flood_recalc
|
||||||
*
|
*
|
||||||
|
@ -172,169 +153,64 @@ flood_endgrace(struct Client *client_p)
|
||||||
* once a second on any given client. We then attempt to flush some data.
|
* once a second on any given client. We then attempt to flush some data.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
flood_recalc(int fd, void *data)
|
flood_recalc(void *unused)
|
||||||
{
|
{
|
||||||
struct Client *client_p = data;
|
rb_dlink_node *ptr, *next;
|
||||||
struct LocalUser *lclient_p = client_p->localClient;
|
struct Client *client_p;
|
||||||
|
|
||||||
/* This can happen in the event that the client detached. */
|
RB_DLINK_FOREACH_SAFE(ptr, next, lclient_list.head)
|
||||||
if(!lclient_p)
|
{
|
||||||
return;
|
client_p = ptr->data;
|
||||||
|
|
||||||
/* allow a bursting client their allocation per second, allow
|
if(unlikely(IsMe(client_p)))
|
||||||
* a client whos flooding an extra 2 per second
|
continue;
|
||||||
*/
|
|
||||||
if(IsFloodDone(client_p))
|
|
||||||
lclient_p->sent_parsed -= 2;
|
|
||||||
else
|
|
||||||
lclient_p->sent_parsed = 0;
|
|
||||||
|
|
||||||
if(lclient_p->sent_parsed < 0)
|
if(unlikely(client_p->localClient == NULL))
|
||||||
lclient_p->sent_parsed = 0;
|
continue;
|
||||||
|
|
||||||
if(--lclient_p->actually_read < 0)
|
if(IsFloodDone(client_p))
|
||||||
lclient_p->actually_read = 0;
|
client_p->localClient->sent_parsed -= 2;
|
||||||
|
else
|
||||||
|
client_p->localClient->sent_parsed = 0;
|
||||||
|
|
||||||
parse_client_queued(client_p);
|
if(client_p->localClient->sent_parsed < 0)
|
||||||
|
client_p->localClient->sent_parsed = 0;
|
||||||
|
|
||||||
if(IsAnyDead(client_p))
|
if(--client_p->localClient->actually_read < 0)
|
||||||
return;
|
client_p->localClient->actually_read = 0;
|
||||||
|
|
||||||
/* and finally, reset the flood check */
|
parse_client_queued(client_p);
|
||||||
rb_setflush(fd, 1000, flood_recalc, client_p);
|
|
||||||
|
if(unlikely(IsAnyDead(client_p)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
RB_DLINK_FOREACH_SAFE(ptr, next, unknown_list.head)
|
||||||
|
{
|
||||||
|
client_p = ptr->data;
|
||||||
|
|
||||||
|
if(client_p->localClient == NULL)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
client_p->localClient->sent_parsed--;
|
||||||
|
|
||||||
|
if(client_p->localClient->sent_parsed < 0)
|
||||||
|
client_p->localClient->sent_parsed = 0;
|
||||||
|
|
||||||
|
if(--client_p->localClient->actually_read < 0)
|
||||||
|
client_p->localClient->actually_read = 0;
|
||||||
|
|
||||||
|
parse_client_queued(client_p);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* read_ctrl_packet - Read a 'packet' of data from a servlink control
|
|
||||||
* link and process it.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
read_ctrl_packet(int fd, void *data)
|
|
||||||
{
|
|
||||||
struct Client *server = data;
|
|
||||||
struct LocalUser *lserver = server->localClient;
|
|
||||||
struct SlinkRpl *reply;
|
|
||||||
int length = 0;
|
|
||||||
unsigned char tmp[2];
|
|
||||||
unsigned char *len = tmp;
|
|
||||||
struct SlinkRplDef *replydef;
|
|
||||||
#ifdef USE_IODEBUG_HOOKS
|
|
||||||
hook_data_int hdata;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
s_assert(lserver != NULL);
|
|
||||||
if(IsAnyDead(server))
|
|
||||||
return;
|
|
||||||
|
|
||||||
reply = &lserver->slinkrpl;
|
|
||||||
|
|
||||||
|
|
||||||
if(!reply->command)
|
|
||||||
{
|
|
||||||
reply->gotdatalen = 0;
|
|
||||||
reply->readdata = 0;
|
|
||||||
reply->data = NULL;
|
|
||||||
|
|
||||||
length = read(fd, tmp, 1);
|
|
||||||
|
|
||||||
if(length <= 0)
|
|
||||||
{
|
|
||||||
if((length == -1) && ignoreErrno(errno))
|
|
||||||
goto nodata;
|
|
||||||
error_exit_client(server, length);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
reply->command = tmp[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (replydef = slinkrpltab; replydef->handler; replydef++)
|
|
||||||
{
|
|
||||||
if((int)replydef->replyid == reply->command)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* we should be able to trust a local slink process...
|
|
||||||
* and if it sends an invalid command, that's a bug.. */
|
|
||||||
s_assert(replydef->handler);
|
|
||||||
|
|
||||||
if((replydef->flags & SLINKRPL_FLAG_DATA) && (reply->gotdatalen < 2))
|
|
||||||
{
|
|
||||||
/* we need a datalen u16 which we don't have yet... */
|
|
||||||
length = read(fd, len, (2 - reply->gotdatalen));
|
|
||||||
if(length <= 0)
|
|
||||||
{
|
|
||||||
if((length == -1) && ignoreErrno(errno))
|
|
||||||
goto nodata;
|
|
||||||
error_exit_client(server, length);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(reply->gotdatalen == 0)
|
|
||||||
{
|
|
||||||
reply->datalen = *len << 8;
|
|
||||||
reply->gotdatalen++;
|
|
||||||
length--;
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
if(length && (reply->gotdatalen == 1))
|
|
||||||
{
|
|
||||||
reply->datalen |= *len;
|
|
||||||
reply->gotdatalen++;
|
|
||||||
if(reply->datalen > 0)
|
|
||||||
reply->data = rb_malloc(reply->datalen);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(reply->gotdatalen < 2)
|
|
||||||
return; /* wait for more data */
|
|
||||||
}
|
|
||||||
|
|
||||||
if(reply->readdata < reply->datalen) /* try to get any remaining data */
|
|
||||||
{
|
|
||||||
length = read(fd, (reply->data + reply->readdata),
|
|
||||||
(reply->datalen - reply->readdata));
|
|
||||||
if(length <= 0)
|
|
||||||
{
|
|
||||||
if((length == -1) && ignoreErrno(errno))
|
|
||||||
goto nodata;
|
|
||||||
error_exit_client(server, length);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
reply->readdata += length;
|
|
||||||
if(reply->readdata < reply->datalen)
|
|
||||||
return; /* wait for more data */
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef USE_IODEBUG_HOOKS
|
|
||||||
hdata.client = server;
|
|
||||||
hdata.arg1 = NULL;
|
|
||||||
hdata.arg2 = reply->command;
|
|
||||||
hdata.data = NULL;
|
|
||||||
call_hook(h_iorecvctrl_id, &hdata);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* we now have the command and any data, pass it off to the handler */
|
|
||||||
(*replydef->handler) (reply->command, reply->datalen, reply->data, server);
|
|
||||||
|
|
||||||
/* reset SlinkRpl */
|
|
||||||
if(reply->datalen > 0)
|
|
||||||
rb_free(reply->data);
|
|
||||||
reply->command = 0;
|
|
||||||
|
|
||||||
if(IsAnyDead(server))
|
|
||||||
return;
|
|
||||||
|
|
||||||
nodata:
|
|
||||||
/* If we get here, we need to register for another COMM_SELECT_READ */
|
|
||||||
rb_setselect(fd, FDLIST_SERVER, COMM_SELECT_READ, read_ctrl_packet, server, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* read_packet - Read a 'packet' of data from a connection and process it.
|
* read_packet - Read a 'packet' of data from a connection and process it.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
read_packet(int fd, void *data)
|
read_packet(rb_fde_t *F, void *data)
|
||||||
{
|
{
|
||||||
struct Client *client_p = data;
|
struct Client *client_p = data;
|
||||||
struct LocalUser *lclient_p = client_p->localClient;
|
struct LocalUser *lclient_p = client_p->localClient;
|
||||||
|
@ -345,82 +221,86 @@ read_packet(int fd, void *data)
|
||||||
#ifdef USE_IODEBUG_HOOKS
|
#ifdef USE_IODEBUG_HOOKS
|
||||||
hook_data_int hdata;
|
hook_data_int hdata;
|
||||||
#endif
|
#endif
|
||||||
if(IsAnyDead(client_p))
|
|
||||||
return;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Read some data. We *used to* do anti-flood protection here, but
|
|
||||||
* I personally think it makes the code too hairy to make sane.
|
|
||||||
* -- adrian
|
|
||||||
*/
|
|
||||||
length = client_p->localClient->F->read_impl(client_p->localClient->F, readBuf, READBUF_SIZE);
|
|
||||||
|
|
||||||
if(length <= 0)
|
while(1) /* note..for things like rt sigio to work you *must* loop on read until you get EAGAIN */
|
||||||
{
|
{
|
||||||
if((length == -1) && ignoreErrno(errno))
|
if(IsAnyDead(client_p))
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Read some data. We *used to* do anti-flood protection here, but
|
||||||
|
* I personally think it makes the code too hairy to make sane.
|
||||||
|
* -- adrian
|
||||||
|
*/
|
||||||
|
length = rb_read(client_p->localClient->F, readBuf, READBUF_SIZE);
|
||||||
|
if(length < 0)
|
||||||
{
|
{
|
||||||
rb_setselect(client_p->localClient->F->fd, FDLIST_IDLECLIENT,
|
if(rb_ignore_errno(errno))
|
||||||
COMM_SELECT_READ, read_packet, client_p, 0);
|
{
|
||||||
|
rb_setselect(client_p->localClient->F,
|
||||||
|
RB_SELECT_READ, read_packet, client_p);
|
||||||
|
} else
|
||||||
|
error_exit_client(client_p, length);
|
||||||
|
return;
|
||||||
|
} else
|
||||||
|
if(length == 0)
|
||||||
|
{
|
||||||
|
error_exit_client(client_p, length);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
error_exit_client(client_p, length);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef USE_IODEBUG_HOOKS
|
#ifdef USE_IODEBUG_HOOKS
|
||||||
hdata.client = client_p;
|
hdata.client = client_p;
|
||||||
hdata.arg1 = readBuf;
|
hdata.arg1 = readBuf;
|
||||||
hdata.arg2 = length;
|
hdata.arg2 = length;
|
||||||
call_hook(h_iorecv_id, &hdata);
|
call_hook(h_iorecv_id, &hdata);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(client_p->localClient->lasttime < rb_current_time())
|
if(client_p->localClient->lasttime < rb_current_time())
|
||||||
client_p->localClient->lasttime = rb_current_time();
|
client_p->localClient->lasttime = rb_current_time();
|
||||||
client_p->flags &= ~FLAGS_PINGSENT;
|
client_p->flags &= ~FLAGS_PINGSENT;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Before we even think of parsing what we just read, stick
|
* Before we even think of parsing what we just read, stick
|
||||||
* it on the end of the receive queue and do it when its
|
* it on the end of the receive queue and do it when its
|
||||||
* turn comes around.
|
* turn comes around.
|
||||||
*/
|
*/
|
||||||
if(IsHandshake(client_p) || IsUnknown(client_p))
|
if(IsHandshake(client_p) || IsUnknown(client_p))
|
||||||
binary = 1;
|
binary = 1;
|
||||||
|
|
||||||
lbuf_len = linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length, binary);
|
lbuf_len = rb_linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length, binary);
|
||||||
|
|
||||||
lclient_p->actually_read += lbuf_len;
|
lclient_p->actually_read += lbuf_len;
|
||||||
|
|
||||||
if(IsAnyDead(client_p))
|
if(IsAnyDead(client_p))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Attempt to parse what we have */
|
/* Attempt to parse what we have */
|
||||||
parse_client_queued(client_p);
|
parse_client_queued(client_p);
|
||||||
|
|
||||||
if(IsAnyDead(client_p))
|
if(IsAnyDead(client_p))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Check to make sure we're not flooding */
|
/* Check to make sure we're not flooding */
|
||||||
if(!IsAnyServer(client_p) &&
|
if(!IsAnyServer(client_p) &&
|
||||||
(linebuf_alloclen(&client_p->localClient->buf_recvq) > ConfigFileEntry.client_flood))
|
(rb_linebuf_alloclen(&client_p->localClient->buf_recvq) > ConfigFileEntry.client_flood))
|
||||||
{
|
|
||||||
if(!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
|
|
||||||
{
|
{
|
||||||
exit_client(client_p, client_p, client_p, "Excess Flood");
|
if(!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
|
||||||
|
{
|
||||||
|
exit_client(client_p, client_p, client_p, "Excess Flood");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* bail if short read */
|
||||||
|
if(length < READBUF_SIZE)
|
||||||
|
{
|
||||||
|
rb_setselect(client_p->localClient->F, RB_SELECT_READ, read_packet, client_p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we get here, we need to register for another COMM_SELECT_READ */
|
|
||||||
if(PARSE_AS_SERVER(client_p))
|
|
||||||
{
|
|
||||||
rb_setselect(client_p->localClient->F->fd, FDLIST_SERVER, COMM_SELECT_READ,
|
|
||||||
read_packet, client_p, 0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rb_setselect(client_p->localClient->F->fd, FDLIST_IDLECLIENT,
|
|
||||||
COMM_SELECT_READ, read_packet, client_p, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -455,20 +335,25 @@ client_dopacket(struct Client *client_p, char *buffer, size_t length)
|
||||||
* Update bytes received
|
* Update bytes received
|
||||||
*/
|
*/
|
||||||
client_p->localClient->receiveB += length;
|
client_p->localClient->receiveB += length;
|
||||||
|
|
||||||
if(client_p->localClient->receiveB > 1023)
|
|
||||||
{
|
|
||||||
client_p->localClient->receiveK += (client_p->localClient->receiveB >> 10);
|
|
||||||
client_p->localClient->receiveB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
|
|
||||||
}
|
|
||||||
|
|
||||||
me.localClient->receiveB += length;
|
me.localClient->receiveB += length;
|
||||||
|
|
||||||
if(me.localClient->receiveB > 1023)
|
|
||||||
{
|
|
||||||
me.localClient->receiveK += (me.localClient->receiveB >> 10);
|
|
||||||
me.localClient->receiveB &= 0x03ff;
|
|
||||||
}
|
|
||||||
|
|
||||||
parse(client_p, buffer, buffer + length);
|
parse(client_p, buffer, buffer + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* flood_endgrace()
|
||||||
|
*
|
||||||
|
* marks the end of the clients grace period
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
flood_endgrace(struct Client *client_p)
|
||||||
|
{
|
||||||
|
SetFloodDone(client_p);
|
||||||
|
|
||||||
|
/* Drop their flood limit back down */
|
||||||
|
client_p->localClient->allow_read = MAX_FLOOD;
|
||||||
|
|
||||||
|
/* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
|
||||||
|
* so reset it.
|
||||||
|
*/
|
||||||
|
client_p->localClient->sent_parsed = 0;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue