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/trunk/src/parse.c (file contents):
Revision 3872 by michael, Fri Jun 6 13:20:05 2014 UTC vs.
Revision 4926 by michael, Tue Nov 25 16:43:49 2014 UTC

# Line 15 | 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  
# Line 72 | Line 72
72   * Diane Bruce (Dianora), June 6 2003
73   */
74  
75 + /*
76 + * Must be a power of 2, and larger than 26 [a-z]|[A-Z]. It's used to allocate
77 + * the set of pointers at each node of the tree.
78 + * There are MAXPTRLEN pointers at each node. Obviously, there have to be more
79 + * pointers than ASCII letters. 32 is a nice number since there is then no need
80 + * to shift 'A'/'a' to base 0 index, at the expense of a few never used
81 + * pointers.
82 + * For a small parser like this, this is a good compromise and does
83 + * make it somewhat faster. - Dianora
84 + */
85   #define MAXPTRLEN 32
76 –                                /* Must be a power of 2, and
77 –                                 * larger than 26 [a-z]|[A-Z]
78 –                                 * its used to allocate the set
79 –                                 * of pointers at each node of the tree
80 –                                 * There are MAXPTRLEN pointers at each node.
81 –                                 * Obviously, there have to be more pointers
82 –                                 * Than ASCII letters. 32 is a nice number
83 –                                 * since there is then no need to shift
84 –                                 * 'A'/'a' to base 0 index, at the expense
85 –                                 * of a few never used pointers. For a small
86 –                                 * parser like this, this is a good compromise
87 –                                 * and does make it somewhat faster.
88 –                                 *
89 –                                 * - Dianora
90 –                                 */
86  
87 < struct MessageTree
87 >
88 > static struct MessageTree
89   {
90    int links; /* Count of all pointers (including msg) at this node
91                * used as reference count for deletion of _this_ node.
92                */
93    struct Message *msg;
94    struct MessageTree *pointers[MAXPTRLEN];
95 < };
95 > } msg_tree;
96 >
97 >
98 > /* remove_unknown()
99 > *
100 > * inputs       -
101 > * output       -
102 > * side effects -
103 > */
104 > static void
105 > parse_remove_unknown(struct Client *client_p, const char *lsender, char *lbuffer)
106 > {
107 >  /*
108 >   * Do kill if it came from a server because it means there is a ghost
109 >   * user on the other server which needs to be removed. -avalon
110 >   * Tell opers about this. -Taner
111 >   */
112 >  /*
113 >   * '[0-9]something'  is an ID      (KILL/SQUIT depending on its length)
114 >   * 'nodots'          is a nickname (KILL)
115 >   * 'no.dot.at.start' is a server   (SQUIT)
116 >   */
117 >  if ((IsDigit(*lsender) && strlen(lsender) <= IRC_MAXSID) || strchr(lsender, '.'))
118 >  {
119 >    sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
120 >                         "Unknown prefix (%s) from %s, Squitting %s",
121 >                         lbuffer, get_client_name(client_p, SHOW_IP), lsender);
122 >    sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
123 >                         "Unknown prefix (%s) from %s, Squitting %s",
124 >                         lbuffer, client_p->name, lsender);
125 >    sendto_one(client_p, ":%s SQUIT %s :(Unknown prefix (%s) from %s)",
126 >               me.id, lsender, lbuffer, client_p->name);
127 >  }
128 >  else
129 >    sendto_one(client_p, ":%s KILL %s :%s (Unknown Client)",
130 >               me.id, lsender, me.name);
131 > }
132 >
133 > /*
134 > *
135 > *      parc    number of arguments ('sender' counted as one!)
136 > *      parv[0] pointer to 'sender' (may point to empty string) (not used)
137 > *      parv[1]..parv[parc-1]
138 > *              pointers to additional parameters, this is a NULL
139 > *              terminated list (parv[parc] == NULL).
140 > *
141 > * *WARNING*
142 > *      Numerics are mostly error reports. If there is something
143 > *      wrong with the message, just *DROP* it! Don't even think of
144 > *      sending back a neat error message -- big danger of creating
145 > *      a ping pong error message...
146 > *
147 > * Rewritten by Nemesi, Jan 1999, to support numeric nicks in parv[1]
148 > *
149 > * Called when we get a numeric message from a remote _server_ and we are
150 > * supposed to forward it somewhere. Note that we always ignore numerics sent
151 > * to 'me' and simply drop the message if we can't handle with this properly:
152 > * the savvy approach is NEVER generate an error in response to an... error :)
153 > */
154 > static void
155 > parse_handle_numeric(unsigned int numeric, struct Client *source_p, int parc, char *parv[])
156 > {
157 >  struct Client *target_p = NULL;
158 >  struct Channel *chptr = NULL;
159 >
160 >  /*
161 >   * Avoid trash, we need it to come from a server and have a target
162 >   */
163 >  if (parc < 2 || !IsServer(source_p))
164 >    return;
165 >
166 >  /*
167 >   * Who should receive this message ? Will we do something with it ?
168 >   * Note that we use findUser functions, so the target can't be neither
169 >   * a server, nor a channel (?) nor a list of targets (?) .. u2.10
170 >   * should never generate numeric replies to non-users anyway
171 >   * Ahem... it can be a channel actually, csc bots use it :\ --Nem
172 >   */
173 >  if (IsChanPrefix(*parv[1]))
174 >    chptr = hash_find_channel(parv[1]);
175 >  else
176 >    target_p = find_person(source_p, parv[1]);
177 >
178 >  if (((!target_p) || (target_p->from == source_p->from)) && !chptr)
179 >    return;
180 >
181 >  /*
182 >   * Remap low number numerics, not that I understand WHY.. --Nemesi
183 >   */
184 >  /*
185 >   * Numerics below 100 talk about the current 'connection', you're not
186 >   * connected to a remote server so it doesn't make sense to send them
187 >   * remotely - but the information they contain may be useful, so we
188 >   * remap them up. Weird, but true.  -- Isomer
189 >   */
190 >  if (numeric < 100)
191 >    numeric += 100;
192 >
193 >  if (target_p)
194 >  {
195 >    /* Fake it for server hiding, if it's our client */
196 >    if ((ConfigServerHide.hide_servers || IsHidden(source_p)) && MyConnect(target_p) &&
197 >        !HasUMode(target_p, UMODE_OPER))
198 >      sendto_one_numeric(target_p, &me, numeric|SND_EXPLICIT, "%s", parv[2]);
199 >    else
200 >      sendto_one_numeric(target_p, source_p, numeric|SND_EXPLICIT, "%s", parv[2]);
201 >  }
202 >  else
203 >    sendto_channel_butone(source_p, source_p, chptr, 0, "%u %s %s",
204 >                          numeric, chptr->name, parv[2]);
205 > }
206  
207 < static struct MessageTree msg_tree;
207 > /* handle_command()
208 > *
209 > * inputs       - pointer to message block
210 > *              - pointer to client
211 > *              - pointer to client message is from
212 > *              - count of number of args
213 > *              - pointer to argv[] array
214 > * output       - -1 if error from server
215 > * side effects -
216 > */
217 > static void
218 > parse_handle_command(struct Message *mptr, struct Client *source_p,
219 >                     unsigned int i, char *para[])
220 > {
221 >  if (IsServer(source_p->from))
222 >    ++mptr->rcount;
223  
224 < static void cancel_clients(struct Client *, struct Client *, char *);
104 < static void remove_unknown(struct Client *, char *, char *);
105 < static void handle_numeric(unsigned int, struct Client *, int, char *[]);
106 < static void handle_command(struct Message *, struct Client *, unsigned int, char *[]);
224 >  ++mptr->count;
225  
226 +  /* Check right amount of parameters is passed... --is */
227 +  if (i < mptr->args_min)
228 +    sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, mptr->cmd);
229 +  else
230 +    mptr->handlers[source_p->from->handler](source_p, i, para);
231 + }
232  
233   /*
234   * parse a buffer.
# Line 126 | Line 250 | parse(struct Client *client_p, char *pbu
250    if (IsDefunct(client_p))
251      return;
252  
253 <  assert(client_p->localClient->fd.flags.open);
253 >  assert(client_p->connection->fd.flags.open);
254    assert((bufend - pbuffer) < IRCD_BUFSIZE);
255  
256    for (ch = pbuffer; *ch == ' '; ++ch)  /* Skip spaces */
# Line 138 | Line 262 | parse(struct Client *client_p, char *pbu
262       * Copy the prefix to 'sender' assuming it terminates
263       * with SPACE (or NULL, which is an error, though).
264       */
265 <    char *sender = ++ch;
265 >    const char *const sender = ++ch;
266  
267      if ((s = strchr(ch, ' ')))
268      {
# Line 148 | Line 272 | parse(struct Client *client_p, char *pbu
272  
273      if (*sender && IsServer(client_p))
274      {
275 <      if ((from = find_person(client_p, sender)) == NULL)
276 <        from = hash_find_server(sender);
275 >      if ((from = hash_find_id(sender)) == NULL)
276 >        from = hash_find_client(sender);
277  
278        /*
279         * Hmm! If the client corresponding to the prefix is not found--what is
# Line 159 | Line 283 | parse(struct Client *client_p, char *pbu
283        if (from == NULL)
284        {
285          ++ServerStats.is_unpf;
286 <        remove_unknown(client_p, sender, pbuffer);
286 >        parse_remove_unknown(client_p, sender, pbuffer);
287          return;
288        }
289  
290        if (from->from != client_p)
291        {
292          ++ServerStats.is_wrdi;
293 <        cancel_clients(client_p, from, pbuffer);
293 >        sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
294 >                             "Fake direction: dropped message from %s[%s] via %s",
295 >                             from->name, from->from->name,
296 >                             get_client_name(client_p, SHOW_IP));
297 >        sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
298 >                             "Fake direction: dropped message fro %s[%s] via %s",
299 >                             from->name, from->from->name,
300 >                             get_client_name(client_p, MASK_IP));
301          return;
302        }
303      }
# Line 190 | Line 321 | parse(struct Client *client_p, char *pbu
321     */
322  
323    /* EOB is 3 characters long but is not a numeric */
324 <  if (*(ch + 3) == ' ' &&  /* Ok, lets see if its a possible numeric.. */
324 >  if (*(ch + 3) == ' ' &&  /* Ok, let's see if it's a possible numeric */
325        IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
326    {
327      numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
# Line 201 | Line 332 | parse(struct Client *client_p, char *pbu
332    }
333    else
334    {
335 <    unsigned int ii = 0;
335 >    unsigned int length = 0;
336  
337      if ((s = strchr(ch, ' ')))
338        *s++ = '\0';
# Line 230 | Line 361 | parse(struct Client *client_p, char *pbu
361      assert(msg_ptr->cmd);
362  
363      paramcount = msg_ptr->args_max;
364 <    ii = bufend - ((s) ? s : ch);
365 <    msg_ptr->bytes += ii;
364 >    length = bufend - ((s) ? s : ch);
365 >    msg_ptr->bytes += length;
366    }
367  
368    /*
# Line 245 | Line 376 | parse(struct Client *client_p, char *pbu
376  
377    para[parc] = ch;
378  
379 +  if (msg_ptr && (msg_ptr->flags & MFLG_EXTRA))
380 +  {
381 +    /*
382 +     * XXX: This will have to go away after the command handler rewrite
383 +     */
384 +    para[++parc] = msg_ptr->extra;
385 +  }
386 +
387    if (s)
388    {
389      if (paramcount > MAXPARA)
# Line 260 | Line 399 | parse(struct Client *client_p, char *pbu
399  
400         if (*s == ':')
401         {
402 <         /* The rest is a single parameter */
402 >         /* The rest is single parameter--can include blanks also. */
403           para[++parc] = s + (!numeric);  /* Keep the colon if it's a numeric */
404           break;
405         }
# Line 278 | Line 417 | parse(struct Client *client_p, char *pbu
417    para[++parc] = NULL;
418  
419    if (msg_ptr)
420 <    handle_command(msg_ptr, from, parc, para);
282 <  else
283 <    handle_numeric(numeric, from, parc, para);
284 < }
285 <
286 < /* handle_command()
287 < *
288 < * inputs       - pointer to message block
289 < *              - pointer to client
290 < *              - pointer to client message is from
291 < *              - count of number of args
292 < *              - pointer to argv[] array
293 < * output       - -1 if error from server
294 < * side effects -
295 < */
296 < static void
297 < handle_command(struct Message *mptr, struct Client *source_p,
298 <               unsigned int i, char *para[])
299 < {
300 <  if (IsServer(source_p->from))
301 <    ++mptr->rcount;
302 <
303 <  ++mptr->count;
304 <
305 <  /* Check right amount of parameters is passed... --is */
306 <  if (i < mptr->args_min)
307 <    sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, mptr->cmd);
420 >    parse_handle_command(msg_ptr, from, parc, para);
421    else
422 <    mptr->handlers[source_p->from->handler](source_p, i, para);
422 >    parse_handle_numeric(numeric, from, parc, para);
423   }
424  
425   /* add_msg_element()
# Line 378 | Line 491 | add_msg_element(struct MessageTree *mtre
491   *
492   * Then it pops up the recurse stack. As it comes back up the recurse
493   * The code checks to see if the child now has no pointers or msg
494 < * i.e. the links count has gone to zero. If its no longer used, the
494 > * i.e. the links count has gone to zero. If it's no longer used, the
495   * child struct MessageTree can be deleted. The parent reference
496   * to this child is then removed and the parents link count goes down.
497   * Thus, we continue to go back up removing all unused MessageTree(s)
# Line 505 | Line 618 | recurse_report_messages(struct Client *s
618   void
619   report_messages(struct Client *source_p)
620   {
621 <  const struct MessageTree *mtree = &msg_tree;
621 >  const struct MessageTree *const mtree = &msg_tree;
622  
623    for (unsigned int i = 0; i < MAXPTRLEN; ++i)
624      if (mtree->pointers[i])
625        recurse_report_messages(source_p, mtree->pointers[i]);
626   }
627  
515 – /* cancel_clients()
516 – *
517 – * inputs       -
518 – * output       -
519 – * side effects -
520 – */
521 – static void
522 – cancel_clients(struct Client *client_p, struct Client *source_p, char *cmd)
523 – {
524 –  if (IsServer(source_p) || IsMe(source_p))
525 –  {
526 –    sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
527 –                         "Message for %s[%s] from %s",
528 –                         source_p->name, source_p->from->name,
529 –                         get_client_name(client_p, SHOW_IP));
530 –    sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
531 –                         "Message for %s[%s] from %s",
532 –                         source_p->name, source_p->from->name,
533 –                         get_client_name(client_p, MASK_IP));
534 –    sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
535 –                         "Not dropping server %s (%s) for Fake Direction",
536 –                         client_p->name, source_p->name);
537 –    return;
538 –  }
539 –
540 –  sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
541 –                       "Message for %s[%s@%s!%s] from %s (TS, ignored)",
542 –                       source_p->name, source_p->username, source_p->host,
543 –                       source_p->from->name, get_client_name(client_p, SHOW_IP));
544 –  sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
545 –                       "Message for %s[%s@%s!%s] from %s (TS, ignored)",
546 –                       source_p->name, source_p->username, source_p->host,
547 –                       source_p->from->name, get_client_name(client_p, MASK_IP));
548 – }
549 –
550 – /* remove_unknown()
551 – *
552 – * inputs       -
553 – * output       -
554 – * side effects -
555 – */
556 – static void
557 – remove_unknown(struct Client *client_p, char *lsender, char *lbuffer)
558 – {
559 –  /*
560 –   * Do kill if it came from a server because it means there is a ghost
561 –   * user on the other server which needs to be removed. -avalon
562 –   * Tell opers about this. -Taner
563 –   */
564 –  /*
565 –   * '[0-9]something'  is an ID      (KILL/SQUIT depending on its length)
566 –   * 'nodots'          is a nickname (KILL)
567 –   * 'no.dot.at.start' is a server   (SQUIT)
568 –   */
569 –  if ((IsDigit(*lsender) && strlen(lsender) <= IRC_MAXSID) || strchr(lsender, '.'))
570 –  {
571 –    sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
572 –                         "Unknown prefix (%s) from %s, Squitting %s",
573 –                         lbuffer, get_client_name(client_p, SHOW_IP), lsender);
574 –    sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
575 –                         "Unknown prefix (%s) from %s, Squitting %s",
576 –                         lbuffer, client_p->name, lsender);
577 –    sendto_one(client_p, ":%s SQUIT %s :(Unknown prefix (%s) from %s)",
578 –               me.id, lsender, lbuffer, client_p->name);
579 –  }
580 –  else
581 –    sendto_one(client_p, ":%s KILL %s :%s (Unknown Client)",
582 –               me.id, lsender, me.name);
583 – }
584 –
585 – /*
586 – *
587 – *      parc    number of arguments ('sender' counted as one!)
588 – *      parv[0] pointer to 'sender' (may point to empty string) (not used)
589 – *      parv[1]..parv[parc-1]
590 – *              pointers to additional parameters, this is a NULL
591 – *              terminated list (parv[parc] == NULL).
592 – *
593 – * *WARNING*
594 – *      Numerics are mostly error reports. If there is something
595 – *      wrong with the message, just *DROP* it! Don't even think of
596 – *      sending back a neat error message -- big danger of creating
597 – *      a ping pong error message...
598 – *
599 – * Rewritten by Nemesi, Jan 1999, to support numeric nicks in parv[1]
600 – *
601 – * Called when we get a numeric message from a remote _server_ and we are
602 – * supposed to forward it somewhere. Note that we always ignore numerics sent
603 – * to 'me' and simply drop the message if we can't handle with this properly:
604 – * the savvy approach is NEVER generate an error in response to an... error :)
605 – */
606 – static void
607 – handle_numeric(unsigned int numeric, struct Client *source_p, int parc, char *parv[])
608 – {
609 –  struct Client *target_p = NULL;
610 –  struct Channel *chptr = NULL;
611 –
612 –  /*
613 –   * Avoid trash, we need it to come from a server and have a target
614 –   */
615 –  if (parc < 2 || !IsServer(source_p))
616 –    return;
617 –
618 –  /*
619 –   * Who should receive this message ? Will we do something with it ?
620 –   * Note that we use findUser functions, so the target can't be neither
621 –   * a server, nor a channel (?) nor a list of targets (?) .. u2.10
622 –   * should never generate numeric replies to non-users anyway
623 –   * Ahem... it can be a channel actually, csc bots use it :\ --Nem
624 –   */
625 –  if (IsChanPrefix(*parv[1]))
626 –    chptr = hash_find_channel(parv[1]);
627 –  else
628 –    target_p = find_person(source_p, parv[1]);
629 –
630 –  if (((!target_p) || (target_p->from == source_p->from)) && !chptr)
631 –    return;
632 –
633 –  /*
634 –   * Remap low number numerics, not that I understand WHY.. --Nemesi
635 –   */
636 –  /*
637 –   * Numerics below 100 talk about the current 'connection', you're not
638 –   * connected to a remote server so it doesn't make sense to send them
639 –   * remotely - but the information they contain may be useful, so we
640 –   * remap them up. Weird, but true.  -- Isomer
641 –   */
642 –  if (numeric < 100)
643 –    numeric += 100;
644 –
645 –  if (target_p)
646 –  {
647 –    /* Fake it for server hiding, if it's our client */
648 –    if (ConfigServerHide.hide_servers && MyConnect(target_p) &&
649 –        !HasUMode(target_p, UMODE_OPER))
650 –      sendto_one_numeric(target_p, &me, numeric|SND_EXPLICIT, "%s", parv[2]);
651 –    else
652 –      sendto_one_numeric(target_p, source_p, numeric|SND_EXPLICIT, "%s", parv[2]);
653 –  }
654 –  else
655 –    sendto_channel_butone(source_p, source_p, chptr, 0, "%u %s %s",
656 –                          numeric, chptr->chname, parv[2]);
657 – }
658 –
628   /* m_not_oper()
629   * inputs       -
630   * output       -

Diff Legend

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