ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/parse.c
(Generate patch)

Comparing:
ircd-hybrid-8/src/parse.c (file contents), Revision 1243 by michael, Fri Sep 30 10:47:53 2011 UTC vs.
ircd-hybrid/trunk/src/parse.c (file contents), Revision 9403 by michael, Sat May 23 14:18:18 2020 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  parse.c: The message parser.
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-2020 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
# Line 16 | Line 15
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
20 < *
21 < *  $Id$
20 > */
21 >
22 > /*! \file parse.c
23 > * \brief The message parser.
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
# Line 27 | Line 29
29   #include "parse.h"
30   #include "channel.h"
31   #include "hash.h"
32 + #include "id.h"
33   #include "irc_string.h"
31 – #include "sprintf_irc.h"
34   #include "ircd.h"
35   #include "numeric.h"
36 < #include "s_log.h"
36 > #include "log.h"
37   #include "send.h"
38 < #include "s_conf.h"
38 > #include "conf.h"
39   #include "memory.h"
40 < #include "s_user.h"
41 < #include "s_serv.h"
40 > #include "user.h"
41 > #include "server.h"
42 > #include "packet.h"
43 >
44  
45   /*
46   * (based on orabidoo's parser code)
# Line 52 | Line 56
56   * 't' points -> [MessageTree *] 'r' -> [MessageTree *] -> 'i'
57   *   -> [MessageTree *] -> [MessageTree *] -> 'e' and matches
58   *
59 < *                               'i' -> [MessageTree *] -> 'e' and matches
59 > *                               'i' -> [MessageTree *] -> 'e' and matches
60   *
61   * BUGS (Limitations!)
62 < *
62 > *
63   * I designed this trie to parse ircd commands. Hence it currently
64   * casefolds. This is trivial to fix by increasing MAXPTRLEN.
65   * This trie also "folds" '{' etc. down. This means, the input to this
# Line 65 | Line 69
69   * MAXPTRLEN 128.
70   *
71   * This is also not a patricia trie. On short ircd tokens, this is
72 < * not likely going to matter.
72 > * not likely going to matter.
73   *
74   * Diane Bruce (Dianora), June 6 2003
75   */
76  
77 < #define MAXPTRLEN       32
78 <                                /* Must be a power of 2, and
79 <                                 * larger than 26 [a-z]|[A-Z]
80 <                                 * its used to allocate the set
81 <                                 * of pointers at each node of the tree
82 <                                 * There are MAXPTRLEN pointers at each node.
83 <                                 * Obviously, there have to be more pointers
84 <                                 * Than ASCII letters. 32 is a nice number
85 <                                 * since there is then no need to shift
86 <                                 * 'A'/'a' to base 0 index, at the expense
87 <                                 * of a few never used pointers. For a small
88 <                                 * parser like this, this is a good compromise
89 <                                 * and does make it somewhat faster.
90 <                                 *
91 <                                 * - Dianora
92 <                                 */
93 <
94 < struct MessageTree
91 < {
92 <  int links; /* Count of all pointers (including msg) at this node
93 <              * used as reference count for deletion of _this_ node.
94 <              */
77 > /*
78 > * Must be a power of 2, and larger than 26 [a-z]|[A-Z]. It's used to allocate
79 > * the set of pointers at each node of the tree.
80 > * There are MAXPTRLEN pointers at each node. Obviously, there have to be more
81 > * pointers than ASCII letters. 32 is a nice number since there is then no need
82 > * to shift 'A'/'a' to base 0 index, at the expense of a few never used
83 > * pointers.
84 > * For a small parser like this, this is a good compromise and does
85 > * make it somewhat faster. - Dianora
86 > */
87 > enum { MAXPTRLEN = 32 };
88 >
89 >
90 > static struct MessageTree
91 > {
92 >  int links; /* Count of all pointers (including msg) at this node
93 >              * used as reference count for deletion of _this_ node.
94 >              */
95    struct Message *msg;
96    struct MessageTree *pointers[MAXPTRLEN];
97 < };
97 > } msg_tree;
98  
99 < static struct MessageTree msg_tree;
99 >
100 > /* remove_unknown()
101 > *
102 > * inputs       -
103 > * output       -
104 > * side effects -
105 > */
106 > static void
107 > parse_remove_unknown(struct Client *client, const char *lsender, char *lbuffer)
108 > {
109 >  /*
110 >   * Do kill if it came from a server because it means there is a ghost
111 >   * user on the other server which needs to be removed. -avalon
112 >   * Tell opers about this. -Taner
113 >   */
114 >  /*
115 >   * '[0-9]something'  is an ID      (KILL/SQUIT depending on its length)
116 >   * 'nodots'          is a nickname (KILL)
117 >   * 'no.dot.at.start' is a server   (SQUIT)
118 >   */
119 >  if ((IsDigit(*lsender) && strlen(lsender) <= IRC_MAXSID) || strchr(lsender, '.'))
120 >  {
121 >    sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
122 >                         "Unknown prefix (%s) from %s, Squitting %s",
123 >                         lbuffer, client_get_name(client, SHOW_IP), lsender);
124 >    sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
125 >                         "Unknown prefix (%s) from %s, Squitting %s",
126 >                         lbuffer, client_get_name(client, MASK_IP), lsender);
127 >    sendto_one(client, ":%s SQUIT %s :(Unknown prefix (%s) from %s)",
128 >               me.id, lsender, lbuffer, client->name);
129 >  }
130 >  else
131 >    sendto_one(client, ":%s KILL %s :%s (Unknown Client)",
132 >               me.id, lsender, me.name);
133 > }
134  
135   /*
136 < * NOTE: parse() should not be called recursively by other functions!
136 > *
137 > *      parc    number of arguments ('sender' counted as one!)
138 > *      parv[0] pointer to 'sender' (may point to empty string) (not used)
139 > *      parv[1]..parv[parc-1]
140 > *              pointers to additional parameters, this is a NULL
141 > *              terminated list (parv[parc] == NULL).
142 > *
143 > * *WARNING*
144 > *      Numerics are mostly error reports. If there is something
145 > *      wrong with the message, just *DROP* it! Don't even think of
146 > *      sending back a neat error message -- big danger of creating
147 > *      a ping pong error message...
148 > *
149 > * Rewritten by Nemesi, Jan 1999, to support numeric nicks in parv[1]
150 > *
151 > * Called when we get a numeric message from a remote _server_ and we are
152 > * supposed to forward it somewhere. Note that we always ignore numerics sent
153 > * to 'me' and simply drop the message if we can't handle with this properly:
154 > * the savvy approach is NEVER generate an error in response to an... error :)
155   */
156 < static char *sender;
157 < static char *para[MAXPARA + 1];
158 < static char buffer[1024];
159 <
160 < static int cancel_clients(struct Client *, struct Client *, char *);
161 < static void remove_unknown(struct Client *, char *, char *);
162 < static void handle_numeric(char[], struct Client *, struct Client *, int, char *[]);
163 < static void handle_command(struct Message *, struct Client *, struct Client *, unsigned int, char *[]);
112 < static void recurse_report_messages(struct Client *, const struct MessageTree *);
113 < static void add_msg_element(struct MessageTree *, struct Message *, const char *);
114 < static void del_msg_element(struct MessageTree *, const char *);
156 > static void
157 > parse_handle_numeric(unsigned int numeric, struct Client *source, int parc, char *parv[])
158 > {
159 >  /*
160 >   * Avoid trash, we need it to come from a server and have a target
161 >   */
162 >  if (parc < 2 || !IsServer(source))
163 >    return;
164  
165 +  /*
166 +   * Remap low number numerics, not that I understand WHY.. --Nemesi
167 +   */
168 +  /*
169 +   * Numerics below 100 talk about the current 'connection', you're not
170 +   * connected to a remote server so it doesn't make sense to send them
171 +   * remotely - but the information they contain may be useful, so we
172 +   * remap them up. Weird, but true.  -- Isomer
173 +   */
174 +  if (numeric < 100)
175 +    numeric += 100;
176 +
177 +  /*
178 +   * Who should receive this message ? Will we do something with it ?
179 +   * Note that we use findUser functions, so the target can't be neither
180 +   * a server, nor a channel (?) nor a list of targets (?) .. u2.10
181 +   * should never generate numeric replies to non-users anyway
182 +   * Ahem... it can be a channel actually, csc bots use it :\ --Nem
183 +   */
184 +  if (IsChanPrefix(*parv[1]))
185 +  {
186 +    struct Channel *channel = hash_find_channel(parv[1]);
187 +    if (channel == NULL)
188 +      return;
189 +
190 +    sendto_channel_butone(source, source, channel, 0, "%u %s %s",
191 +                          numeric, channel->name, parv[2]);
192 +  }
193 +  else
194 +  {
195 +    struct Client *target = find_person(source, parv[1]);
196 +    if (target == NULL || target->from == source->from)
197 +      return;
198 +
199 +    /* Fake it for server hiding, if it's our client */
200 +    if ((ConfigServerHide.hide_servers || IsHidden(source)) && MyConnect(target) &&
201 +        !HasUMode(target, UMODE_OPER))
202 +      sendto_one_numeric(target, &me, numeric | SND_EXPLICIT, "%s", parv[2]);
203 +    else
204 +      sendto_one_numeric(target, source, numeric | SND_EXPLICIT, "%s", parv[2]);
205 +  }
206 + }
207 +
208 + /* handle_command()
209 + *
210 + * inputs       - pointer to message block
211 + *              - pointer to client
212 + *              - pointer to client message is from
213 + *              - count of number of args
214 + *              - pointer to argv[] array
215 + * output       - -1 if error from server
216 + * side effects -
217 + */
218 + static void
219 + parse_handle_command(struct Message *message, struct Client *source,
220 +                     unsigned int i, char *para[])
221 + {
222 +  const struct MessageHandler *const handler = &message->handlers[source->from->handler];
223 +
224 +  if (IsServer(source->from))
225 +    ++message->rcount;
226 +  ++message->count;
227 +
228 +  if (handler->end_grace_period == true && MyClient(source))
229 +    flood_endgrace(source);
230 +
231 +  /* Check right amount of parameters is passed... --is */
232 +  if (handler->args_min &&
233 +      ((i < handler->args_min) ||
234 +       (handler->empty_last_arg != true && EmptyString(para[handler->args_min - 1]))))
235 +    sendto_one_numeric(source, &me, ERR_NEEDMOREPARAMS, message->cmd);
236 +  else
237 +    handler->handler(source, i, para);
238 + }
239  
240   /*
241   * parse a buffer.
# Line 120 | Line 243 | static void del_msg_element(struct Messa
243   * NOTE: parse() should not be called recusively by any other functions!
244   */
245   void
246 < parse(struct Client *client_p, char *pbuffer, char *bufend)
246 > parse(struct Client *client, char *pbuffer, char *bufend)
247   {
248 <  struct Client *from = client_p;
249 <  struct Message *msg_ptr = NULL;
250 <  char *ch;
251 <  char *s;
252 <  char *numeric = 0;
248 >  struct Client *from = client;
249 >  struct Message *message = NULL;
250 >  char *para[MAXPARA + 2];  /* <command> + <parameters> + NULL */
251 >  char *ch = NULL;
252 >  char *s = NULL;
253 >  unsigned int numeric = 0;
254    unsigned int parc = 0;
255    unsigned int paramcount;
132 –  int mpara = 0;
133 –
134 –  if (IsDefunct(client_p))
135 –    return;
256  
257 <  assert(client_p->localClient->fd.flags.open);
258 <  assert((bufend - pbuffer) < 512);
257 >  assert(!IsDead(client));
258 >  assert(client->connection);
259 >  assert(client->connection->fd);
260 >  assert(client->connection->fd->flags.open);
261 >  assert((bufend - pbuffer) < IRCD_BUFSIZE);
262  
263 <  for (ch = pbuffer; *ch == ' '; ch++) /* skip spaces */
264 <    /* null statement */ ;
263 >  for (ch = pbuffer; *ch == ' '; ++ch)  /* Skip spaces */
264 >    ;
265  
266    if (*ch == ':')
267    {
# Line 146 | Line 269 | parse(struct Client *client_p, char *pbu
269       * Copy the prefix to 'sender' assuming it terminates
270       * with SPACE (or NULL, which is an error, though).
271       */
272 <    sender = ++ch;
272 >    const char *const sender = ++ch;
273  
274 <    if ((s = strchr(ch, ' ')) != NULL)
274 >    if ((s = strchr(ch, ' ')))
275      {
276        *s = '\0';
277        ch = ++s;
278      }
279  
280 <    if (*sender && IsServer(client_p))
280 >    if (*sender && IsServer(client))
281      {
282 <      /*
283 <       * XXX it could be useful to know which of these occurs most frequently.
161 <       * the ID check should always come first, though, since it is so easy.
162 <       */
163 <      if ((from = find_person(client_p, sender)) == NULL)
164 <        from = hash_find_server(sender);
282 >      if ((from = hash_find_id(sender)) == NULL)
283 >        from = hash_find_client(sender);
284  
285 <      /* Hmm! If the client corresponding to the
286 <       * prefix is not found--what is the correct
287 <       * action??? Now, I will ignore the message
288 <       * (old IRC just let it through as if the
170 <       * prefix just wasn't there...) --msa
285 >      /*
286 >       * Hmm! If the client corresponding to the prefix is not found--what is
287 >       * the correct action??? Now, I will ignore the message (old IRC just
288 >       * let it through as if the prefix just wasn't there...) --msa
289         */
290        if (from == NULL)
291        {
292          ++ServerStats.is_unpf;
293 <        remove_unknown(client_p, sender, pbuffer);
293 >        parse_remove_unknown(client, sender, pbuffer);
294          return;
295        }
296  
297 <      if (from->from != client_p)
297 >      if (from->from != client)
298        {
299          ++ServerStats.is_wrdi;
300 <        cancel_clients(client_p, from, pbuffer);
300 >        sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
301 >                             "Fake direction: dropped message from %s[%s] via %s",
302 >                             from->name, from->from->name,
303 >                             client_get_name(client, SHOW_IP));
304 >        sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
305 >                             "Fake direction: dropped message from %s[%s] via %s",
306 >                             from->name, from->from->name,
307 >                             client_get_name(client, MASK_IP));
308          return;
309        }
310      }
# Line 194 | Line 319 | parse(struct Client *client_p, char *pbu
319      return;
320    }
321  
322 <  /* Extract the command code from the packet.  Point s to the end
322 >  /*
323 >   * Extract the command code from the packet. Point s to the end
324     * of the command code and calculate the length using pointer
325 <   * arithmetic.  Note: only need length for numerics and *all*
325 >   * arithmetic. Note: only need length for numerics and *all*
326     * numerics must have parameters and thus a space after the command
327     * code. -avalon
328     */
329  
330 <  /* EOB is 3 chars long but is not a numeric */
331 <  if (*(ch + 3) == ' ' && /* ok, lets see if its a possible numeric.. */
330 >  /* EOB is 3 characters long but is not a numeric */
331 >  if (*(ch + 3) == ' ' &&  /* Ok, let's see if it's a possible numeric */
332        IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
333    {
334 <    numeric = ch;
335 <    paramcount = MAXPARA;
334 >    numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
335 >    paramcount = 2;  /* Destination, and the rest of it */
336      ++ServerStats.is_num;
337 <    s = ch + 3;  /* I know this is ' ' from above if            */
338 <    *s++ = '\0'; /* blow away the ' ', and point s to next part */
337 >    s = ch + 3;  /* I know this is ' ' from above if */
338 >    *s++ = '\0';  /* Blow away the ' ', and point s to next part */
339    }
340    else
341 <  {
342 <    unsigned int ii = 0;
217 <
218 <    if ((s = strchr(ch, ' ')) != NULL)
341 >  {
342 >    if ((s = strchr(ch, ' ')))
343        *s++ = '\0';
344  
345 <    if ((msg_ptr = find_command(ch)) == NULL)
345 >    if ((message = find_command(ch)) == NULL)
346      {
347 <      /* Note: Give error message *only* to recognized
347 >      /*
348 >       * Note: Give error message *only* to recognized
349         * persons. It's a nightmare situation to have
350         * two programs sending "Unknown command"'s or
351         * equivalent to each other at full blast....
# Line 230 | Line 355 | parse(struct Client *client_p, char *pbu
355         * Hm, when is the buffer empty -- if a command
356         * code has been found ?? -Armin
357         */
358 <      if (pbuffer[0] != '\0')
234 <      {
358 >      if (*pbuffer)
359          if (IsClient(from))
360 <          sendto_one(from, form_str(ERR_UNKNOWNCOMMAND),
237 <                     me.name, from->name, ch);
238 <      }
360 >          sendto_one_numeric(from, &me, ERR_UNKNOWNCOMMAND, ch);
361  
362        ++ServerStats.is_unco;
363        return;
364      }
365  
366 <    assert(msg_ptr->cmd != NULL);
366 >    assert(message->cmd);
367  
368 <    paramcount = msg_ptr->args_max;
247 <    mpara      = msg_ptr->args_min;
368 >    paramcount = message->handlers[from->from->handler].args_max;
369  
370 <    ii = bufend - ((s) ? s : ch);
371 <    msg_ptr->bytes += ii;
370 >    size_t length = bufend - ((s) ? s : ch);
371 >    message->bytes += length;
372    }
373  
374 +  memset(para, 0, sizeof(para));
375 +
376    /*
377     * Must the following loop really be so devious? On surface it
378     * splits the message to parameters from blank spaces. But, if
# Line 257 | Line 380 | parse(struct Client *client_p, char *pbu
380     * this last parameter (about same effect as ":" has...) --msa
381     */
382  
383 <  /* Note initially true: s==NULL || *(s-1) == '\0' !! */
383 >  /* Note initially true: s == NULL || *(s - 1) == '\0' !! */
384 >
385 >  para[parc] = ch;
386  
387 <  para[parc] = from->name;
387 >  if (message && message->extra)
388 >  {
389 >    /*
390 >     * XXX: This will have to go away after the command handler rewrite
391 >     */
392 >    para[++parc] = message->extra;
393 >  }
394  
395    if (s)
396    {
397 <    if (paramcount > MAXPARA)
397 >    if (paramcount == 0 || paramcount > MAXPARA)
398        paramcount = MAXPARA;
399  
400 <    while (1)
400 >    while (true)
401      {
402         while (*s == ' ')
403           *s++ = '\0';
# Line 276 | Line 407 | parse(struct Client *client_p, char *pbu
407  
408         if (*s == ':')
409         {
410 <         /* The rest is a single parameter */
411 <         para[++parc] = s + 1;
410 >         /* The rest is single parameter--can include blanks also. */
411 >         para[++parc] = s + (numeric == 0);  /* Keep the colon if it's a numeric */
412           break;
413         }
414  
# Line 293 | Line 424 | parse(struct Client *client_p, char *pbu
424  
425    para[++parc] = NULL;
426  
427 <  if (msg_ptr != NULL)
428 <    handle_command(msg_ptr, client_p, from, parc, para);
427 >  if (message)
428 >    parse_handle_command(message, from, parc, para);
429    else
430 <    handle_numeric(numeric, client_p, from, parc, para);
300 < }
301 <
302 < /* handle_command()
303 < *
304 < * inputs       - pointer to message block
305 < *              - pointer to client
306 < *              - pointer to client message is from
307 < *              - count of number of args
308 < *              - pointer to argv[] array
309 < * output       - -1 if error from server
310 < * side effects -
311 < */
312 < static void
313 < handle_command(struct Message *mptr, struct Client *client_p,
314 <               struct Client *from, unsigned int i, char *hpara[MAXPARA])
315 < {
316 <  MessageHandler handler = 0;
317 <
318 <  if (IsServer(client_p))
319 <    mptr->rcount++;
320 <
321 <  mptr->count++;
322 <
323 <  /* New patch to avoid server flooding from unregistered connects
324 <   * - Pie-Man 07/27/2000 */
325 <  if (!IsRegistered(client_p))
326 <  {
327 <    /* if its from a possible server connection
328 <     * ignore it.. more than likely its a header thats sneaked through
329 <     */
330 <    if ((IsHandshake(client_p) || IsConnecting(client_p) ||
331 <        IsServer(client_p)) && !(mptr->flags & MFLG_UNREG))
332 <      return;
333 <  }
334 <
335 <  handler = mptr->handlers[client_p->handler];
336 <
337 <  /* check right amount of params is passed... --is */
338 <  if (i < mptr->args_min)
339 <  {
340 <    if (!IsServer(client_p))
341 <    {
342 <      sendto_one(client_p, form_str(ERR_NEEDMOREPARAMS),
343 <                   me.name, EmptyString(hpara[0]) ? "*" : hpara[0], mptr->cmd);
344 <    }
345 <    else
346 <    {
347 <      sendto_realops_flags(UMODE_ALL, L_ALL,
348 <                           "Dropping server %s due to (invalid) command '%s' "
349 <                           "with only %d arguments (expecting %d).",
350 <                           client_p->name, mptr->cmd, i, mptr->args_min);
351 <      ilog(L_CRIT, "Insufficient parameters (%d) for command '%s' from %s.",
352 <           i, mptr->cmd, client_p->name);
353 <      exit_client(client_p, client_p,
354 <                  "Not enough arguments to server command.");
355 <    }
356 <  }
357 <  else
358 <    (*handler)(client_p, from, i, hpara);
359 < }
360 <
361 < /* clear_tree_parse()
362 < *
363 < * inputs       - NONE
364 < * output       - NONE
365 < * side effects - MUST MUST be called at startup ONCE before
366 < *                any other keyword routine is used.
367 < */
368 < void
369 < clear_tree_parse(void)
370 < {
371 <  memset(&msg_tree, 0, sizeof(msg_tree));
430 >    parse_handle_numeric(numeric, from, parc, para);
431   }
432  
433   /* add_msg_element()
# Line 392 | Line 451 | clear_tree_parse(void)
451   * in the parent.
452   */
453   static void
454 < add_msg_element(struct MessageTree *mtree_p,
455 <                struct Message *msg_p, const char *cmd)
454 > add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p,
455 >                const char *cmd)
456   {
398 –  struct MessageTree *ntree_p;
399 –
457    if (*cmd == '\0')
458    {
459      mtree_p->msg = msg_p;
460 <    mtree_p->links++;           /* Have msg pointer, so up ref count */
460 >    mtree_p->links++;  /* Have msg pointer, so up ref count */
461    }
462    else
463    {
464 <    /* *cmd & (MAXPTRLEN-1)
464 >    /*
465 >     * *cmd & (MAXPTRLEN-1)
466       * convert the char pointed to at *cmd from ASCII to an integer
467       * between 0 and MAXPTRLEN.
468       * Thus 'A' -> 0x1 'B' -> 0x2 'c' -> 0x3 etc.
469       */
470 <
471 <    if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN - 1)]) == NULL)
470 >    struct MessageTree *ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN - 1)];
471 >    if (ntree_p == NULL)
472      {
473 <      ntree_p = MyMalloc(sizeof(struct MessageTree));
473 >      ntree_p = xcalloc(sizeof(*ntree_p));
474        mtree_p->pointers[*cmd & (MAXPTRLEN - 1)] = ntree_p;
475  
476 <      mtree_p->links++;         /* Have new pointer, so up ref count */
476 >      mtree_p->links++;  /* Have new pointer, so up ref count */
477      }
478  
479      add_msg_element(ntree_p, msg_p, cmd + 1);
# Line 440 | Line 498 | add_msg_element(struct MessageTree *mtre
498   *
499   * Then it pops up the recurse stack. As it comes back up the recurse
500   * The code checks to see if the child now has no pointers or msg
501 < * i.e. the links count has gone to zero. If its no longer used, the
501 > * i.e. the links count has gone to zero. If it's no longer used, the
502   * child struct MessageTree can be deleted. The parent reference
503   * to this child is then removed and the parents link count goes down.
504   * Thus, we continue to go back up removing all unused MessageTree(s)
# Line 448 | Line 506 | add_msg_element(struct MessageTree *mtre
506   static void
507   del_msg_element(struct MessageTree *mtree_p, const char *cmd)
508   {
509 <  struct MessageTree *ntree_p;
510 <
453 <  /* In case this is called for a nonexistent command
509 >  /*
510 >   * In case this is called for a nonexistent command
511     * check that there is a msg pointer here, else links-- goes -ve
512     * -db
513     */
514 <
458 <  if ((*cmd == '\0') && (mtree_p->msg != NULL))
514 >  if (*cmd == '\0' && mtree_p->msg)
515    {
516      mtree_p->msg = NULL;
517      mtree_p->links--;
518    }
519    else
520    {
521 <    if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN - 1)]) != NULL)
521 >    struct MessageTree *ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN - 1)];
522 >    if (ntree_p)
523      {
524        del_msg_element(ntree_p, cmd + 1);
525  
526        if (ntree_p->links == 0)
527        {
528 <        mtree_p->pointers[*cmd & (MAXPTRLEN - 1)] = NULL;
529 <        mtree_p->links--;
530 <        MyFree(ntree_p);
528 >        mtree_p->pointers[*cmd & (MAXPTRLEN - 1)] = NULL;
529 >        mtree_p->links--;
530 >        xfree(ntree_p);
531        }
532      }
533    }
# Line 484 | Line 541 | del_msg_element(struct MessageTree *mtre
541   * side effects - none
542   */
543   static struct Message *
544 < msg_tree_parse(const char *cmd, struct MessageTree *root)
544 > msg_tree_parse(const char *cmd)
545   {
546 <  struct MessageTree *mtree = root;
547 <  assert(cmd && *cmd);
546 >  struct MessageTree *mtree = &msg_tree;
547 >
548 >  assert(!EmptyString(cmd));
549  
550    while (IsAlpha(*cmd) && (mtree = mtree->pointers[*cmd & (MAXPTRLEN - 1)]))
551      if (*++cmd == '\0')
# Line 501 | Line 559 | msg_tree_parse(const char *cmd, struct M
559   * inputs       - pointer to struct Message
560   * output       - none
561   * side effects - load this one command name
504 – *                msg->count msg->bytes is modified in place, in
505 – *                modules address space. Might not want to do that...
562   */
563   void
564   mod_add_cmd(struct Message *msg)
565   {
566 <  struct Message *found_msg;
567 <
512 <  if (msg == NULL)
513 <    return;
514 <
515 <  /* someone loaded a module with a bad messagetab */
516 <  assert(msg->cmd != NULL);
566 >  assert(msg);
567 >  assert(msg->cmd);
568  
569 <  /* command already added? */
570 <  if ((found_msg = msg_tree_parse(msg->cmd, &msg_tree)) != NULL)
571 <    return;
521 <
522 <  add_msg_element(&msg_tree, msg, msg->cmd);
523 <  msg->count = msg->rcount = msg->bytes = 0;
569 >  /* Command already added? */
570 >  if (msg_tree_parse(msg->cmd) == NULL)
571 >    add_msg_element(&msg_tree, msg, msg->cmd);
572   }
573  
574   /* mod_del_cmd()
# Line 532 | Line 580 | mod_add_cmd(struct Message *msg)
580   void
581   mod_del_cmd(struct Message *msg)
582   {
583 <  assert(msg != NULL);
584 <
537 <  if (msg == NULL)
538 <    return;
583 >  assert(msg);
584 >  assert(msg->cmd);
585  
586 <  del_msg_element(&msg_tree, msg->cmd);
586 >  if (msg_tree_parse(msg->cmd))
587 >    del_msg_element(&msg_tree, msg->cmd);
588   }
589  
590   /* find_command()
# Line 549 | Line 596 | mod_del_cmd(struct Message *msg)
596   struct Message *
597   find_command(const char *cmd)
598   {
599 <  return msg_tree_parse(cmd, &msg_tree);
599 >  return msg_tree_parse(cmd);
600 > }
601 >
602 > static void
603 > recurse_report_messages(struct Client *source, const struct MessageTree *mtree)
604 > {
605 >  if (mtree->msg)
606 >    sendto_one_numeric(source, &me, RPL_STATSCOMMANDS,
607 >                       mtree->msg->cmd,
608 >                       mtree->msg->count,
609 >                       mtree->msg->bytes,
610 >                       mtree->msg->rcount,
611 >                       mtree->msg->ecount);
612 >
613 >  for (unsigned int i = 0; i < MAXPTRLEN; ++i)
614 >    if (mtree->pointers[i])
615 >      recurse_report_messages(source, mtree->pointers[i]);
616   }
617  
618   /* report_messages()
# Line 559 | Line 622 | find_command(const char *cmd)
622   * side effects - client is shown list of commands
623   */
624   void
625 < report_messages(struct Client *source_p)
625 > report_messages(struct Client *source)
626   {
627 <  const struct MessageTree *mtree = &msg_tree;
565 <  unsigned int i;
627 >  const struct MessageTree *const mtree = &msg_tree;
628  
629 <  for (i = 0; i < MAXPTRLEN; i++)
630 <    if (mtree->pointers[i] != NULL)
631 <      recurse_report_messages(source_p, mtree->pointers[i]);
570 < }
571 <
572 < static void
573 < recurse_report_messages(struct Client *source_p, const struct MessageTree *mtree)
574 < {
575 <  unsigned int i;
576 <
577 <  if (mtree->msg != NULL)
578 <    sendto_one(source_p, form_str(RPL_STATSCOMMANDS),
579 <               me.name, source_p->name, mtree->msg->cmd,
580 <               mtree->msg->count, mtree->msg->bytes,
581 <               mtree->msg->rcount);
582 <
583 <  for (i = 0; i < MAXPTRLEN; i++)
584 <    if (mtree->pointers[i] != NULL)
585 <      recurse_report_messages(source_p, mtree->pointers[i]);
586 < }
587 <
588 < /* cancel_clients()
589 < *
590 < * inputs       -
591 < * output       -
592 < * side effects -
593 < */
594 < static int
595 < cancel_clients(struct Client *client_p, struct Client *source_p, char *cmd)
596 < {
597 <  /* kill all possible points that are causing confusion here,
598 <   * I'm not sure I've got this all right...
599 <   * - avalon
600 <   *
601 <   * knowing avalon, probably not.
602 <   */
603 <
604 <  /* with TS, fake prefixes are a common thing, during the
605 <   * connect burst when there's a nick collision, and they
606 <   * must be ignored rather than killed because one of the
607 <   * two is surviving.. so we don't bother sending them to
608 <   * all ops everytime, as this could send 'private' stuff
609 <   * from lagged clients. we do send the ones that cause
610 <   * servers to be dropped though, as well as the ones from
611 <   * non-TS servers -orabidoo
612 <   */
613 <  /* Incorrect prefix for a server from some connection.  If it is a
614 <   * client trying to be annoying, just QUIT them, if it is a server
615 <   * then the same deal.
616 <   */
617 <  if (IsServer(source_p) || IsMe(source_p))
618 <  {
619 <    sendto_realops_flags(UMODE_DEBUG, L_ADMIN, "Message for %s[%s] from %s",
620 <                         source_p->name, source_p->from->name,
621 <                         get_client_name(client_p, SHOW_IP));
622 <    sendto_realops_flags(UMODE_DEBUG, L_OPER,  "Message for %s[%s] from %s",
623 <                         source_p->name, source_p->from->name,
624 <                         get_client_name(client_p, MASK_IP));
625 <    sendto_realops_flags(UMODE_DEBUG, L_ALL,
626 <                         "Not dropping server %s (%s) for Fake Direction",
627 <                         client_p->name, source_p->name);
628 <    return -1;
629 <    /* return exit_client(client_p, client_p, &me, "Fake Direction");*/
630 <  }
631 <
632 <  /* Ok, someone is trying to impose as a client and things are
633 <   * confused.  If we got the wrong prefix from a server, send out a
634 <   * kill, else just exit the lame client.
635 <   */
636 <  /* If the fake prefix is coming from a TS server, discard it
637 <   * silently -orabidoo
638 <   *
639 <   * all servers must be TS these days --is
640 <   */
641 <  sendto_realops_flags(UMODE_DEBUG, L_ADMIN,
642 <                       "Message for %s[%s@%s!%s] from %s (TS, ignored)",
643 <                       source_p->name, source_p->username, source_p->host,
644 <                       source_p->from->name, get_client_name(client_p, SHOW_IP));
645 <  sendto_realops_flags(UMODE_DEBUG, L_OPER,
646 <                       "Message for %s[%s@%s!%s] from %s (TS, ignored)",
647 <                       source_p->name, source_p->username, source_p->host,
648 <                       source_p->from->name, get_client_name(client_p, MASK_IP));
649 <
650 <  return 0;
651 < }
652 <
653 < /* remove_unknown()
654 < *
655 < * inputs       -
656 < * output       -
657 < * side effects -
658 < */
659 < static void
660 < remove_unknown(struct Client *client_p, char *lsender, char *lbuffer)
661 < {
662 <  /* Do kill if it came from a server because it means there is a ghost
663 <   * user on the other server which needs to be removed. -avalon
664 <   * Tell opers about this. -Taner
665 <   */
666 <  /* '[0-9]something'  is an ID      (KILL/SQUIT depending on its length)
667 <   * 'nodots'          is a nickname (KILL)
668 <   * 'no.dot.at.start' is a server   (SQUIT)
669 <   */
670 <  if ((IsDigit(*lsender) && strlen(lsender) <= IRC_MAXSID) ||
671 <      strchr(lsender, '.') != NULL)
672 <  {
673 <    sendto_realops_flags(UMODE_DEBUG, L_ADMIN,
674 <                         "Unknown prefix (%s) from %s, Squitting %s",
675 <                         lbuffer, get_client_name(client_p, SHOW_IP), lsender);
676 <    sendto_realops_flags(UMODE_DEBUG, L_OPER,
677 <                         "Unknown prefix (%s) from %s, Squitting %s",
678 <                         lbuffer, client_p->name, lsender);
679 <    sendto_one(client_p, ":%s SQUIT %s :(Unknown prefix (%s) from %s)",
680 <               me.name, lsender, lbuffer, client_p->name);
681 <  }
682 <  else
683 <    sendto_one(client_p, ":%s KILL %s :%s (Unknown Client)",
684 <               me.name, lsender, me.name);
685 < }
686 <
687 < /*
688 < *
689 < *      parc    number of arguments ('sender' counted as one!)
690 < *      parv[0] pointer to 'sender' (may point to empty string) (not used)
691 < *      parv[1]..parv[parc-1]
692 < *              pointers to additional parameters, this is a NULL
693 < *              terminated list (parv[parc] == NULL).
694 < *
695 < * *WARNING*
696 < *      Numerics are mostly error reports. If there is something
697 < *      wrong with the message, just *DROP* it! Don't even think of
698 < *      sending back a neat error message -- big danger of creating
699 < *      a ping pong error message...
700 < */
701 < static void
702 < handle_numeric(char numeric[], struct Client *client_p, struct Client *source_p,
703 <               int parc, char *parv[])
704 < {
705 <  struct Client *target_p;
706 <  struct Channel *chptr;
707 <  char *t;    /* current position within the buffer */
708 <  int i, tl;  /* current length of presently being built string in t */
709 <
710 <  if (parc < 2 || !IsServer(source_p))
711 <    return;
712 <
713 <  /* Remap low number numerics. */
714 <  if (numeric[0] == '0')
715 <    numeric[0] = '1';
716 <
717 <  /* Prepare the parameter portion of the message into 'buffer'.
718 <   * (Because the buffer is twice as large as the message buffer
719 <   * for the socket, no overflow can occur here... ...on current
720 <   * assumptions--bets are off, if these are changed --msa)
721 <   */
722 <  t = buffer;
723 <  for (i = 2; i < (parc - 1); i++)
724 <  {
725 <    tl = ircsprintf(t, " %s", parv[i]);
726 <    t += tl;
727 <  }
728 <
729 <  ircsprintf(t," :%s", parv[parc-1]);
730 <
731 <  if (((target_p = find_person(client_p, parv[1])) != NULL) ||
732 <      ((target_p = hash_find_server(parv[1])) != NULL))
733 <  {
734 <    if (IsMe(target_p))
735 <    {
736 <      int num;
737 <
738 <      /*
739 <       * We shouldn't get numerics sent to us,
740 <       * any numerics we do get indicate a bug somewhere..
741 <       */
742 <      /* ugh.  this is here because of nick collisions.  when two servers
743 <       * relink, they burst each other their nicks, then perform collides.
744 <       * if there is a nick collision, BOTH servers will kill their own
745 <       * nicks, and BOTH will kill the other servers nick, which wont exist,
746 <       * because it will have been already killed by the local server.
747 <       *
748 <       * unfortunately, as we cant guarantee other servers will do the
749 <       * "right thing" on a nick collision, we have to keep both kills.  
750 <       * ergo we need to ignore ERR_NOSUCHNICK. --fl_
751 <       */
752 <      /* quick comment. This _was_ tried. i.e. assume the other servers
753 <       * will do the "right thing" and kill a nick that is colliding.
754 <       * unfortunately, it did not work. --Dianora
755 <       */
756 <
757 <      /* Yes, a good compiler would have optimised this, but
758 <       * this is probably easier to read. -db
759 <       */
760 <      num = atoi(numeric);
761 <
762 <      if ((num != ERR_NOSUCHNICK))
763 <        sendto_realops_flags(UMODE_ALL, L_ADMIN,
764 <                             "*** %s(via %s) sent a %s numeric to me: %s",
765 <                             source_p->name, client_p->name, numeric, buffer);
766 <      return;
767 <    }
768 <    else if (target_p->from == client_p)
769 <    {
770 <      /* This message changed direction (nick collision?)
771 <       * ignore it.
772 <       */
773 <      return;
774 <    }
775 <
776 <    /* csircd will send out unknown umode flag for +a (admin), drop it here. */
777 <    if ((atoi(numeric) == ERR_UMODEUNKNOWNFLAG) && MyClient(target_p))
778 <      return;
779 <    
780 <    /* Fake it for server hiding, if its our client */
781 <    if (ConfigServerHide.hide_servers &&
782 <        MyClient(target_p) && !HasUMode(target_p, UMODE_OPER))
783 <      sendto_one(target_p, ":%s %s %s%s", me.name, numeric, target_p->name, buffer);
784 <    else
785 <      sendto_one(target_p, ":%s %s %s%s", ID_or_name(source_p, target_p->from),
786 <                 numeric, ID_or_name(target_p, target_p->from), buffer);
787 <    return;
788 <  }
789 <  else if ((chptr = hash_find_channel(parv[1])) != NULL)
790 <    sendto_channel_local(ALL_MEMBERS, 0, chptr,
791 <                         ":%s %s %s %s",
792 <                         source_p->name,
793 <                         numeric, chptr->chname, buffer);
629 >  for (unsigned int i = 0; i < MAXPTRLEN; ++i)
630 >    if (mtree->pointers[i])
631 >      recurse_report_messages(source, mtree->pointers[i]);
632   }
633  
634   /* m_not_oper()
635 < * inputs       -
635 > * inputs       -
636   * output       -
637   * side effects - just returns a nastyogram to given user
638   */
639   void
640 < m_not_oper(struct Client *client_p, struct Client *source_p,
803 <           int parc, char *parv[])
640 > m_not_oper(struct Client *source, int parc, char *parv[])
641   {
642 <  sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
806 <             me.name, source_p->name);
642 >  sendto_one_numeric(source, &me, ERR_NOPRIVILEGES);
643   }
644  
645   void
646 < m_unregistered(struct Client *client_p, struct Client *source_p,
811 <               int parc, char *parv[])
646 > m_unregistered(struct Client *source, int parc, char *parv[])
647   {
648 <  sendto_one(source_p, form_str(ERR_NOTREGISTERED), me.name,
814 <             source_p->name[0] ? source_p->name : "*");
648 >  sendto_one_numeric(source, &me, ERR_NOTREGISTERED);
649   }
650  
651   void
652 < m_registered(struct Client *client_p, struct Client *source_p,
819 <             int parc, char *parv[])
652 > m_registered(struct Client *source, int parc, char *parv[])
653   {
654 <  sendto_one(source_p, form_str(ERR_ALREADYREGISTRED),  
822 <             me.name, source_p->name);
654 >  sendto_one_numeric(source, &me, ERR_ALREADYREGISTRED);
655   }
656  
657   void
658 < m_ignore(struct Client *client_p, struct Client *source_p,
827 <         int parc, char *parv[])
658 > m_ignore(struct Client *source, int parc, char *parv[])
659   {
660    return;
661   }
831 –
832 – void
833 – rfc1459_command_send_error(struct Client *client_p, struct Client *source_p,
834 –                           int parc, char *parv[])
835 – {
836 –  const char *in_para;
837 –
838 –  in_para = (parc > 1 && *parv[1] != '\0') ? parv[1] : "<>";
839 –
840 –  ilog(L_ERROR, "Received ERROR message from %s: %s",
841 –       source_p->name, in_para);
842 –
843 –  if (client_p == source_p)
844 –  {
845 –    sendto_realops_flags(UMODE_ALL, L_ADMIN, "ERROR :from %s -- %s",
846 –                         get_client_name(client_p, HIDE_IP), in_para);
847 –    sendto_realops_flags(UMODE_ALL, L_OPER,  "ERROR :from %s -- %s",
848 –                         get_client_name(client_p, MASK_IP), in_para);
849 –  }
850 –  else
851 –  {
852 –    sendto_realops_flags(UMODE_ALL, L_OPER, "ERROR :from %s via %s -- %s",
853 –                         source_p->name, get_client_name(client_p, MASK_IP), in_para);
854 –    sendto_realops_flags(UMODE_ALL, L_ADMIN, "ERROR :from %s via %s -- %s",
855 –                         source_p->name, get_client_name(client_p, HIDE_IP), in_para);
856 –  }
857 –
858 –  if (MyClient(source_p))
859 –    exit_client(source_p, source_p, "ERROR");
860 – }

Comparing:
ircd-hybrid-8/src/parse.c (property svn:keywords), Revision 1243 by michael, Fri Sep 30 10:47:53 2011 UTC vs.
ircd-hybrid/trunk/src/parse.c (property svn:keywords), Revision 9403 by michael, Sat May 23 14:18:18 2020 UTC

# Line 1 | Line 1
1 < Id Revision
1 > Id

Diff Legend

– Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)