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

Comparing ircd-hybrid/trunk/src/hostmask.c (file contents):
Revision 3469 by michael, Sat May 3 17:55:53 2014 UTC vs.
Revision 7672 by michael, Wed Jul 20 18:47:20 2016 UTC

# Line 1 | Line 1
1   /*
2   *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (c) 2001-2014 ircd-hybrid development team
4 > *  Copyright (c) 2001-2016 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 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 44 | Line 44
44                           ch = ch - 'a' + 10; \
45                         } while (0);
46  
47 + /* Hashtable stuff...now external as it's used in m_stats.c */
48 + dlink_list atable[ATABLE_SIZE];
49 +
50   /* The mask parser/type determination code... */
51  
52   /* int try_parse_v6_netmask(const char *, struct irc_ssaddr *, int *);
# Line 53 | Line 56
56   * Side effects: None
57   * Comments: Called from parse_netmask
58   */
56 /* Fixed so ::/0 (any IPv6 address) is valid
57   Also a bug in DigitParse above.
58   -Gozem 2002-07-19 gozem@linux.nu
59 */
60 #ifdef IPV6
59   static int
60   try_parse_v6_netmask(const char *text, struct irc_ssaddr *addr, int *b)
61   {
# Line 69 | Line 67 | try_parse_v6_netmask(const char *text, s
67    int bits = 128;
68    int deficit = 0;
69    short dc[8];
70 <  struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
70 >  struct sockaddr_in6 *const v6 = (struct sockaddr_in6 *)addr;
71  
72    for (const char *p = text; (c = *p); ++p)
73    {
# Line 90 | Line 88 | try_parse_v6_netmask(const char *text, s
88        }
89        else
90        {
91 <        /* If there were less than 4 hex digits, e.g. :ABC: shift right
92 <         * so we don't interpret it as ABC0 -A1kmm */
91 >        /*
92 >         * If there were less than 4 hex digits, e.g. :ABC: shift right
93 >         * so we don't interpret it as ABC0 -A1kmm
94 >         */
95          d[dp] = d[dp] >> 4 * nyble;
96          nyble = 4;
97 +
98          if (++dp >= 8)
99            return HM_HOST;
100        }
# Line 161 | Line 162 | try_parse_v6_netmask(const char *text, s
162      *b = bits;
163    return HM_IPV6;
164   }
164 #endif
165  
166   /* int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *);
167   * Input: A possible IPV4 address as a string.
# Line 177 | Line 177 | try_parse_v4_netmask(const char *text, s
177    unsigned char addb[4];
178    int n = 0, bits = 0;
179    char c;
180 <  struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
180 >  struct sockaddr_in *const v4 = (struct sockaddr_in *)addr;
181  
182    digits[n++] = text;
183  
# Line 194 | Line 194 | try_parse_v4_netmask(const char *text, s
194      }
195      else if (c == '*')
196      {
197 <      if (*(p + 1) || n == 0 || *(p - 1) != '.')
197 >      if (*(p + 1) || n == 1 || *(p - 1) != '.')
198          return HM_HOST;
199  
200        bits = (n - 1) * 8;
# Line 253 | Line 253 | parse_netmask(const char *text, struct i
253   {
254    if (strchr(text, '.'))
255      return try_parse_v4_netmask(text, addr, b);
256 #ifdef IPV6
256    if (strchr(text, ':'))
257      return try_parse_v6_netmask(text, addr, b);
258 < #endif
258 >
259    return HM_HOST;
260   }
261  
# Line 266 | Line 265 | parse_netmask(const char *text, struct i
265   * Output: if match, -1 else 0
266   * Side effects: None
267   */
269 #ifdef IPV6
268   int
269   match_ipv6(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
270   {
271    int i, m, n = bits / 8;
272 <  const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
273 <  const struct sockaddr_in6 *v6mask = (const struct sockaddr_in6 *)mask;
272 >  const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr;
273 >  const struct sockaddr_in6 *const v6mask = (const struct sockaddr_in6 *)mask;
274  
275    for (i = 0; i < n; ++i)
276      if (v6->sin6_addr.s6_addr[i] != v6mask->sin6_addr.s6_addr[i])
# Line 285 | Line 283 | match_ipv6(const struct irc_ssaddr *addr
283      return -1;
284    return 0;
285   }
288 #endif
286  
287   /* int match_ipv4(struct irc_ssaddr *, struct irc_ssaddr *, int)
288   * Input: An IP address, an IP mask, the number of bits in the mask.
# Line 295 | Line 292 | match_ipv6(const struct irc_ssaddr *addr
292   int
293   match_ipv4(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
294   {
295 <  const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
296 <  const struct sockaddr_in *v4mask = (const struct sockaddr_in *)mask;
295 >  const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr;
296 >  const struct sockaddr_in *const v4mask = (const struct sockaddr_in *)mask;
297  
298    if ((ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1)) !=
299        ntohl(v4mask->sin_addr.s_addr))
# Line 316 | Line 313 | void
313   mask_addr(struct irc_ssaddr *ip, int bits)
314   {
315    int mask;
319 #ifdef IPV6
316    struct sockaddr_in6 *v6_base_ip;
317    int i, m, n;
322 #endif
318    struct sockaddr_in *v4_base_ip;
319  
320 < #ifdef IPV6
326 <  if (ip->ss.ss_family != AF_INET6)
327 < #endif
320 >  if (ip->ss.ss_family == AF_INET)
321    {
322 +    uint32_t tmp = 0;
323      v4_base_ip = (struct sockaddr_in *)ip;
324  
325      mask = ~((1 << (32 - bits)) - 1);
326 <    v4_base_ip->sin_addr.s_addr = htonl(ntohl(v4_base_ip->sin_addr.s_addr) & mask);
326 >    tmp = ntohl(v4_base_ip->sin_addr.s_addr);
327 >    v4_base_ip->sin_addr.s_addr = htonl(tmp & mask);
328    }
334 #ifdef IPV6
329    else
330    {
331      n = bits / 8;
332      m = bits % 8;
333      v6_base_ip = (struct sockaddr_in6 *)ip;
334  
335 <    mask = ~((1 << (8 - m)) -1 );
335 >    mask = ~((1 << (8 - m)) - 1);
336      v6_base_ip->sin6_addr.s6_addr[n] = v6_base_ip->sin6_addr.s6_addr[n] & mask;
337  
338      for (i = n + 1; i < 16; i++)
339        v6_base_ip->sin6_addr.s6_addr[i] = 0;
340    }
347 #endif
348 }
349
350 /* Hashtable stuff...now external as its used in m_stats.c */
351 dlink_list atable[ATABLE_SIZE];
352
353 void
354 init_host_hash(void)
355 {
356  memset(&atable, 0, sizeof(atable));
341   }
342  
343   /* unsigned long hash_ipv4(struct irc_ssaddr*)
# Line 366 | Line 350 | hash_ipv4(const struct irc_ssaddr *addr,
350   {
351    if (bits != 0)
352    {
353 <    const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
353 >    const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr;
354      uint32_t av = ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1);
355  
356      return (av ^ (av >> 12) ^ (av >> 24)) & (ATABLE_SIZE - 1);
# Line 380 | Line 364 | hash_ipv4(const struct irc_ssaddr *addr,
364   * Output: A hash value of the IP address.
365   * Side effects: None
366   */
383 #ifdef IPV6
367   static uint32_t
368   hash_ipv6(const struct irc_ssaddr *addr, int bits)
369   {
370    uint32_t v = 0, n;
371 <  const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
371 >  const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr;
372  
373    for (n = 0; n < 16; ++n)
374    {
# Line 402 | Line 385 | hash_ipv6(const struct irc_ssaddr *addr,
385      else
386        return v & (ATABLE_SIZE - 1);
387    }
388 +
389    return v & (ATABLE_SIZE - 1);
390   }
407 #endif
391  
392   /* int hash_text(const char *start)
393   * Input: The start of the text to hash.
# Line 452 | Line 435 | get_mask_hash(const char *text)
435   * should always be true (i.e. conf->flags & CONF_FLAGS_NEED_PASSWORD == 0)
436   */
437   struct MaskItem *
438 < find_conf_by_address(const char *name, struct irc_ssaddr *addr, unsigned int type,
438 > find_conf_by_address(const char *name, const struct irc_ssaddr *addr, unsigned int type,
439                       int fam, const char *username, const char *password, int do_match)
440   {
441    unsigned int hprecv = 0;
442 <  dlink_node *ptr = NULL;
442 >  dlink_node *node = NULL;
443    struct MaskItem *hprec = NULL;
444    struct AddressRec *arec = NULL;
445    int b;
# Line 465 | Line 448 | find_conf_by_address(const char *name, s
448    if (addr)
449    {
450      /* Check for IPV6 matches... */
468 #ifdef IPV6
451      if (fam == AF_INET6)
452      {
453        for (b = 128; b >= 0; b -= 16)
454        {
455 <        DLINK_FOREACH(ptr, atable[hash_ipv6(addr, b)].head)
455 >        DLINK_FOREACH(node, atable[hash_ipv6(addr, b)].head)
456          {
457 <          arec = ptr->data;
457 >          arec = node->data;
458  
459            if ((arec->type == type) &&
460                arec->precedence > hprecv &&
# Line 489 | Line 471 | find_conf_by_address(const char *name, s
471          }
472        }
473      }
474 <    else
493 < #endif
494 <    if (fam == AF_INET)
474 >    else if (fam == AF_INET)
475      {
476        for (b = 32; b >= 0; b -= 8)
477        {
478 <        DLINK_FOREACH(ptr, atable[hash_ipv4(addr, b)].head)
478 >        DLINK_FOREACH(node, atable[hash_ipv4(addr, b)].head)
479          {
480 <          arec = ptr->data;
480 >          arec = node->data;
481  
482            if ((arec->type == type) &&
483                arec->precedence > hprecv &&
# Line 522 | Line 502 | find_conf_by_address(const char *name, s
502  
503      while (1)
504      {
505 <        DLINK_FOREACH(ptr, atable[hash_text(p)].head)
505 >        DLINK_FOREACH(node, atable[hash_text(p)].head)
506          {
507 <          arec = ptr->data;
507 >          arec = node->data;
508            if ((arec->type == type) &&
509              arec->precedence > hprecv &&
510              (arec->masktype == HM_HOST) &&
# Line 543 | Line 523 | find_conf_by_address(const char *name, s
523        ++p;
524      }
525  
526 <    DLINK_FOREACH(ptr, atable[0].head)
526 >    DLINK_FOREACH(node, atable[0].head)
527      {
528 <      arec = ptr->data;
528 >      arec = node->data;
529  
530        if (arec->type == type &&
531            arec->precedence > hprecv &&
# Line 571 | Line 551 | find_conf_by_address(const char *name, s
551   * Side-effects: None
552   */
553   struct MaskItem *
554 < find_address_conf(const char *host, const char *user,
555 <                  struct irc_ssaddr *ip, int aftype, char *password)
554 > find_address_conf(const char *host, const char *user, const struct irc_ssaddr *ip,
555 >                  int aftype, const char *password)
556   {
557    struct MaskItem *authcnf = NULL, *killcnf = NULL;
558  
# Line 590 | Line 570 | find_address_conf(const char *host, cons
570  
571    /*
572     * If they are K-lined, return the K-line. Otherwise, return the
573 <   * auth{} block. -A1kmm
573 >   * auth {} block. -A1kmm
574     */
575    if (killcnf)
576      return killcnf;
577  
598  if (IsConfExemptGline(authcnf))
599    return authcnf;
600
601  killcnf = find_conf_by_address(host, ip, CONF_GLINE, aftype, user, NULL, 1);
602  if (killcnf)
603    return killcnf;
604
578    return authcnf;
579   }
580  
# Line 612 | Line 585 | find_address_conf(const char *host, cons
585   * Side effects: None.
586   */
587   struct MaskItem *
588 < find_dline_conf(struct irc_ssaddr *addr, int aftype)
588 > find_dline_conf(const struct irc_ssaddr *addr, int aftype)
589   {
590    struct MaskItem *eline;
591  
# Line 631 | Line 604 | find_dline_conf(struct irc_ssaddr *addr,
604   struct AddressRec *
605   add_conf_by_address(const unsigned int type, struct MaskItem *conf)
606   {
607 <  const char *hostname = conf->host;
608 <  const char *username = conf->user;
609 <  static unsigned int prec_value = 0xFFFFFFFF;
607 >  const char *const hostname = conf->host;
608 >  const char *const username = conf->user;
609 >  static unsigned int prec_value = UINT_MAX;
610    int bits = 0;
638  struct AddressRec *arec = NULL;
611  
612    assert(type && !EmptyString(hostname));
613  
614 <  arec = MyMalloc(sizeof(struct AddressRec));
614 >  struct AddressRec *arec = xcalloc(sizeof(struct AddressRec));
615    arec->masktype = parse_netmask(hostname, &arec->Mask.ipa.addr, &bits);
616    arec->Mask.ipa.bits = bits;
617    arec->username = username;
# Line 654 | Line 626 | add_conf_by_address(const unsigned int t
626        bits -= bits % 8;
627        dlinkAdd(arec, &arec->node, &atable[hash_ipv4(&arec->Mask.ipa.addr, bits)]);
628        break;
657 #ifdef IPV6
629      case HM_IPV6:
630        /* We have to do this, since we do not re-hash for every bit -A1kmm. */
631        bits -= bits % 16;
632        dlinkAdd(arec, &arec->node, &atable[hash_ipv6(&arec->Mask.ipa.addr, bits)]);
633        break;
663 #endif
634      default: /* HM_HOST */
635        arec->Mask.hostname = hostname;
636        dlinkAdd(arec, &arec->node, &atable[get_mask_hash(hostname)]);
# Line 681 | Line 651 | delete_one_address_conf(const char *addr
651   {
652    int bits = 0;
653    uint32_t hv = 0;
654 <  dlink_node *ptr = NULL, *ptr_next = NULL;
654 >  dlink_node *node = NULL;
655    struct irc_ssaddr addr;
656  
657    switch (parse_netmask(address, &addr, &bits))
# Line 691 | Line 661 | delete_one_address_conf(const char *addr
661        bits -= bits % 8;
662        hv = hash_ipv4(&addr, bits);
663        break;
694 #ifdef IPV6
664      case HM_IPV6:
665        /* We have to do this, since we do not re-hash for every bit -A1kmm. */
666        bits -= bits % 16;
667        hv = hash_ipv6(&addr, bits);
668        break;
700 #endif
669      default: /* HM_HOST */
670        hv = get_mask_hash(address);
671        break;
672    }
673  
674 <  DLINK_FOREACH_SAFE(ptr, ptr_next, atable[hv].head)
674 >  DLINK_FOREACH(node, atable[hv].head)
675    {
676 <    struct AddressRec *arec = ptr->data;
676 >    struct AddressRec *arec = node->data;
677  
678      if (arec->conf == conf)
679      {
# Line 714 | Line 682 | delete_one_address_conf(const char *addr
682        if (!conf->ref_count)
683          conf_free(conf);
684  
685 <      MyFree(arec);
685 >      xfree(arec);
686        return;
687      }
688    }
# Line 730 | Line 698 | delete_one_address_conf(const char *addr
698   void
699   clear_out_address_conf(void)
700   {
701 <  dlink_node *ptr = NULL, *ptr_next = NULL;
701 >  dlink_node *node = NULL, *node_next = NULL;
702  
703    for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
704    {
705 <    DLINK_FOREACH_SAFE(ptr, ptr_next, atable[i].head)
705 >    DLINK_FOREACH_SAFE(node, node_next, atable[i].head)
706      {
707 <      struct AddressRec *arec = ptr->data;
707 >      struct AddressRec *arec = node->data;
708  
709        /*
710 <       * We keep the temporary K-lines and destroy the permanent ones,
743 <       * just to be confusing :) -A1kmm
710 >       * Destroy the ircd.conf items and keep those that are in the databases
711         */
712 <      if (arec->conf->until || IsConfDatabase(arec->conf))
712 >      if (IsConfDatabase(arec->conf))
713          continue;
714  
715        dlinkDelete(&arec->node, &atable[i]);
716 +      arec->conf->active = 0;
717  
718        if (!arec->conf->ref_count)
719          conf_free(arec->conf);
720 <      MyFree(arec);
720 >      xfree(arec);
721      }
722    }
723   }
724  
725   static void
726 < hostmask_send_expiration(struct AddressRec *arec)
726 > hostmask_send_expiration(const struct AddressRec *const arec)
727   {
728 <  char ban_type = '\0';
728 >  char ban_type = '?';
729  
730 <  if (!ConfigFileEntry.tkline_expire_notices)
730 >  if (!ConfigGeneral.tkline_expire_notices)
731      return;
732  
733    switch (arec->type)
# Line 770 | Line 738 | hostmask_send_expiration(struct AddressR
738      case CONF_DLINE:
739        ban_type = 'D';
740        break;
773    case CONF_GLINE:
774      ban_type = 'G';
775      break;
741      default: break;
742    }
743  
744 <  sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
744 >  sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
745                         "Temporary %c-line for [%s@%s] expired", ban_type,
746                         (arec->conf->user) ? arec->conf->user : "*",
747                         (arec->conf->host) ? arec->conf->host : "*");
# Line 785 | Line 750 | hostmask_send_expiration(struct AddressR
750   void
751   hostmask_expire_temporary(void)
752   {
753 <  dlink_node *ptr = NULL, *ptr_next = NULL;
753 >  dlink_node *node = NULL, *node_next = NULL;
754  
755    for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
756    {
757 <    DLINK_FOREACH_SAFE(ptr, ptr_next, atable[i].head)
757 >    DLINK_FOREACH_SAFE(node, node_next, atable[i].head)
758      {
759 <      struct AddressRec *arec = ptr->data;
759 >      struct AddressRec *arec = node->data;
760  
761        if (!arec->conf->until || arec->conf->until > CurrentTime)
762          continue;
# Line 800 | Line 765 | hostmask_expire_temporary(void)
765        {
766          case CONF_KLINE:
767          case CONF_DLINE:
803        case CONF_GLINE:
768            hostmask_send_expiration(arec);
769  
770            dlinkDelete(&arec->node, &atable[i]);
771            conf_free(arec->conf);
772 <          MyFree(arec);
772 >          xfree(arec);
773            break;
774          default: break;
775        }

Comparing ircd-hybrid/trunk/src/hostmask.c (property svn:keywords):
Revision 3469 by michael, Sat May 3 17:55:53 2014 UTC vs.
Revision 7672 by michael, Wed Jul 20 18:47:20 2016 UTC

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

Diff Legend

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