parse: utilise MsgBuf to simplify message parsing

This commit is contained in:
William Pitcock 2016-02-10 00:22:37 -06:00
parent a8e69f5dfc
commit 4d03595da4

View file

@ -5,7 +5,7 @@
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team * Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2005 ircd-ratbox development team * Copyright (C) 2002-2005 ircd-ratbox development team
* Copyright (C) 2007 William Pitcock * Copyright (C) 2007-2016 William Pitcock
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -38,6 +38,7 @@
#include "s_stats.h" #include "s_stats.h"
#include "send.h" #include "send.h"
#include "msg.h" #include "msg.h"
#include "msgbuf.h"
#include "s_conf.h" #include "s_conf.h"
#include "s_serv.h" #include "s_serv.h"
#include "packet.h" #include "packet.h"
@ -52,7 +53,7 @@ static char *para[MAXPARA + 2];
static void cancel_clients(struct Client *, struct Client *); static void cancel_clients(struct Client *, struct Client *);
static void remove_unknown(struct Client *, char *, char *); static void remove_unknown(struct Client *, char *, char *);
static void do_numeric(char[], struct Client *, struct Client *, int, char **); static void do_numeric(int, struct Client *, struct Client *, int, const char **);
static void do_alias(struct alias_entry *, struct Client *, char *); static void do_alias(struct alias_entry *, struct Client *, char *);
static int handle_command(struct Message *, struct Client *, struct Client *, int, const char**); static int handle_command(struct Message *, struct Client *, struct Client *, int, const char**);
@ -74,55 +75,6 @@ char *reconstruct_parv(int parc, const char *parv[])
return tmpbuf; return tmpbuf;
} }
static inline int
string_to_array(char *string, char **parv)
{
char *p, *buf = string;
int x = 1;
parv[x] = NULL;
while (*buf == ' ') /* skip leading spaces */
buf++;
if(*buf == '\0') /* ignore all-space args */
return x;
do
{
if(*buf == ':') /* Last parameter */
{
buf++;
parv[x++] = buf;
parv[x] = NULL;
return x;
}
else
{
parv[x++] = buf;
parv[x] = NULL;
if((p = strchr(buf, ' ')) != NULL)
{
*p++ = '\0';
buf = p;
}
else
return x;
}
while (*buf == ' ')
buf++;
if(*buf == '\0')
return x;
}
/* we can go upto parv[MAXPARA], as parv[0] is skipped */
while (x < MAXPARA);
if(*p == ':')
p++;
parv[x++] = p;
parv[x] = NULL;
return x;
}
/* parse() /* parse()
* *
* given a raw buffer, parses it and generates parv and parc * given a raw buffer, parses it and generates parv and parc
@ -135,37 +87,34 @@ parse(struct Client *client_p, char *pbuffer, char *bufend)
char *ch; char *ch;
char *s; char *s;
char *end; char *end;
int i = 1; int i = 1, res;
char *numeric = 0; int numeric = 0;
struct Message *mptr; struct Message *mptr;
struct MsgBuf msgbuf;
s_assert(MyConnect(client_p)); s_assert(MyConnect(client_p));
s_assert(client_p->localClient->F != NULL); s_assert(client_p->localClient->F != NULL);
if(IsAnyDead(client_p)) if(IsAnyDead(client_p))
return; return;
for (ch = pbuffer; *ch == ' '; ch++) /* skip spaces */ end = bufend - 1;
/* null statement */ ;
para[0] = from->name; /* XXX this should be done before parse() is called */
if(*end == '\n')
*end-- = '\0';
if(*end == '\r')
*end = '\0';
if(*ch == ':') res = msgbuf_parse(&msgbuf, pbuffer);
if (res)
{ {
ch++; ServerStats.is_empt++;
return;
/* point sender to the sender param */
sender = ch;
if((s = strchr(ch, ' ')))
{
*s = '\0';
s++;
ch = s;
} }
if(*sender && IsServer(client_p)) if (msgbuf.origin != NULL)
{ {
from = find_client(sender); from = find_client(msgbuf.origin);
/* didnt find any matching client, issue a kill */ /* didnt find any matching client, issue a kill */
if(from == NULL) if(from == NULL)
@ -175,8 +124,6 @@ parse(struct Client *client_p, char *pbuffer, char *bufend)
return; return;
} }
para[0] = from->name;
/* fake direction, hmm. */ /* fake direction, hmm. */
if(from->from != client_p) if(from->from != client_p)
{ {
@ -185,61 +132,19 @@ parse(struct Client *client_p, char *pbuffer, char *bufend)
return; return;
} }
} }
while (*ch == ' ')
ch++;
}
if(*ch == '\0') if(IsDigit(*msgbuf.cmd) && IsDigit(*(msgbuf.cmd + 1)) && IsDigit(*(msgbuf.cmd + 2)))
{
ServerStats.is_empt++;
return;
}
/* at this point there must be some sort of command parameter */
/*
* Extract the command code from the packet. Point s to the end
* of the command code and calculate the length using pointer
* arithmetic. Note: only need length for numerics and *all*
* numerics must have parameters and thus a space after the command
* code. -avalon
*/
/* EOB is 3 chars long but is not a numeric */
if(*(ch + 3) == ' ' && /* ok, lets see if its a possible numeric.. */
IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
{ {
mptr = NULL; mptr = NULL;
numeric = ch; numeric = atoi(msgbuf.cmd);
ServerStats.is_num++; ServerStats.is_num++;
s = ch + 3; /* I know this is ' ' from above if */
*s++ = '\0'; /* blow away the ' ', and point s to next part */
} }
else else
{ {
int ii = 0; mptr = irc_dictionary_retrieve(cmd_dict, msgbuf.cmd);
if((s = strchr(ch, ' ')))
*s++ = '\0';
mptr = irc_dictionary_retrieve(cmd_dict, ch);
/* no command or its encap only, error */ /* no command or its encap only, error */
if(!mptr || !mptr->cmd) if(!mptr || !mptr->cmd)
{
/*
* Note: Give error message *only* to recognized
* persons. It's a nightmare situation to have
* two programs sending "Unknown command"'s or
* equivalent to each other at full blast....
* If it has got to person state, it at least
* seems to be well behaving. Perhaps this message
* should never be generated, though... --msa
* Hm, when is the buffer empty -- if a command
* code has been found ?? -Armin
*/
if(pbuffer[0] != '\0')
{ {
if (IsPerson(client_p)) if (IsPerson(client_p))
{ {
@ -255,33 +160,20 @@ parse(struct Client *client_p, char *pbuffer, char *bufend)
sendto_one(from, form_str(ERR_UNKNOWNCOMMAND), sendto_one(from, form_str(ERR_UNKNOWNCOMMAND),
me.name, from->name, ch); me.name, from->name, ch);
} }
}
ServerStats.is_unco++; ServerStats.is_unco++;
return; return;
} }
ii = bufend - ((s) ? s : ch); mptr->bytes += msgbuf.parselen;
mptr->bytes += ii;
} }
end = bufend - 1;
/* XXX this should be done before parse() is called */
if(*end == '\n')
*end-- = '\0';
if(*end == '\r')
*end = '\0';
if(s != NULL)
i = string_to_array(s, para);
if(mptr == NULL) if(mptr == NULL)
{ {
do_numeric(numeric, client_p, from, i, para); do_numeric(numeric, client_p, from, msgbuf.n_para, msgbuf.para);
return; return;
} }
if(handle_command(mptr, client_p, from, i, /* XXX discards const!!! */ (const char **)(void *)para) < -1) if(handle_command(mptr, client_p, from, msgbuf.n_para, /* XXX discards const!!! */ (const char **)(void *) msgbuf.para) < -1)
{ {
char *p; char *p;
for (p = pbuffer; p <= end; p += 8) for (p = pbuffer; p <= end; p += 8)
@ -592,7 +484,7 @@ remove_unknown(struct Client *client_p, char *lsender, char *lbuffer)
* a ping pong error message... * a ping pong error message...
*/ */
static void static void
do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int parc, char *parv[]) do_numeric(int numeric, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
{ {
struct Client *target_p; struct Client *target_p;
struct Channel *chptr; struct Channel *chptr;
@ -601,8 +493,8 @@ do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int
return; return;
/* Remap low number numerics. */ /* Remap low number numerics. */
if(numeric[0] == '0') if(numeric < 100)
numeric[0] = '1'; numeric += 100;
/* /*
* Prepare the parameter portion of the message into 'buffer'. * Prepare the parameter portion of the message into 'buffer'.
@ -649,10 +541,10 @@ do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int
/* note, now we send PING on server connect, we can /* note, now we send PING on server connect, we can
* also get ERR_NOSUCHSERVER.. * also get ERR_NOSUCHSERVER..
*/ */
if(atoi(numeric) != ERR_NOSUCHNICK && if(numeric != ERR_NOSUCHNICK &&
atoi(numeric) != ERR_NOSUCHSERVER) numeric != ERR_NOSUCHSERVER)
sendto_realops_snomask(SNO_GENERAL, L_ADMIN, sendto_realops_snomask(SNO_GENERAL, L_ADMIN,
"*** %s(via %s) sent a %s numeric to me: %s", "*** %s(via %s) sent a %03d numeric to me: %s",
source_p->name, source_p->name,
client_p->name, numeric, buffer); client_p->name, numeric, buffer);
return; return;
@ -666,18 +558,18 @@ do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int
} }
/* csircd will send out unknown umode flag for +a (admin), drop it here. */ /* csircd will send out unknown umode flag for +a (admin), drop it here. */
if((atoi(numeric) == ERR_UMODEUNKNOWNFLAG) && MyClient(target_p)) if(numeric == ERR_UMODEUNKNOWNFLAG && MyClient(target_p))
return; return;
/* Fake it for server hiding, if its our client */ /* Fake it for server hiding, if its our client */
sendto_one(target_p, ":%s %s %s%s", sendto_one(target_p, ":%s %03d %s%s",
get_id(source_p, target_p), numeric, get_id(source_p, target_p), numeric,
get_id(target_p, target_p), buffer); get_id(target_p, target_p), buffer);
return; return;
} }
else if((chptr = find_channel(parv[1])) != NULL) else if((chptr = find_channel(parv[1])) != NULL)
sendto_channel_flags(client_p, ALL_MEMBERS, source_p, chptr, sendto_channel_flags(client_p, ALL_MEMBERS, source_p, chptr,
"%s %s%s", "%03d %s%s",
numeric, chptr->chname, buffer); numeric, chptr->chname, buffer);
} }