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-7.2/src/hostmask.c (file contents), Revision 34 by lusky, Sun Oct 2 21:05:51 2005 UTC vs.
ircd-hybrid-8/src/hostmask.c (file contents), Revision 1343 by michael, Sat Apr 7 18:46:29 2012 UTC

# Line 25 | Line 25
25   #include "stdinc.h"
26   #include "memory.h"
27   #include "ircd_defs.h"
28 < #include "tools.h"
29 < #include "s_conf.h"
28 > #include "list.h"
29 > #include "conf.h"
30   #include "hostmask.h"
31   #include "numeric.h"
32   #include "send.h"
# Line 34 | Line 34
34  
35   #ifdef IPV6
36   static int try_parse_v6_netmask(const char *, struct irc_ssaddr *, int *);
37 < static unsigned long hash_ipv6(struct irc_ssaddr *, int);
37 > static uint32_t hash_ipv6(struct irc_ssaddr *, int);
38   #endif
39   static int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *);
40 < static unsigned long hash_ipv4(struct irc_ssaddr *, int);
40 > static uint32_t hash_ipv4(struct irc_ssaddr *, int);
41  
42   #define DigitParse(ch) do { \
43                         if (ch >= '0' && ch <= '9') \
# Line 46 | Line 46 | static unsigned long hash_ipv4(struct ir
46                           ch = ch - 'A' + 10; \
47                         else if (ch >= 'a' && ch <= 'f') \
48                           ch = ch - 'a' + 10; \
49 <                       } while(0);
49 >                       } while (0);
50  
51   /* The mask parser/type determination code... */
52  
# Line 74 | Line 74 | try_parse_v6_netmask(const char *text, s
74    int bits = 128;
75    int deficit = 0;
76    short dc[8];
77 <  struct sockaddr_in6 *v6 = (struct sockaddr_in6*) addr;
77 >  struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
78  
79    for (p = text; (c = *p); p++)
80      if (IsXDigit(c))
# Line 116 | Line 116 | try_parse_v6_netmask(const char *text, s
116        d[dp] = d[dp] >> 4 * nyble;
117        dp++;
118        bits = strtoul(p + 1, &after, 10);
119 +
120        if (bits < 0 || *after)
121          return HM_HOST;
122        if (bits > dp * 4 && !(finsert >= 0 && bits <= 128))
# Line 126 | Line 127 | try_parse_v6_netmask(const char *text, s
127        return HM_HOST;
128  
129    d[dp] = d[dp] >> 4 * nyble;
130 +
131    if (c == 0)
132      dp++;
133    if (finsert < 0 && bits == 0)
134      bits = dp * 16;
135 +
136    /* How many words are missing? -A1kmm */
137    deficit = bits / 16 + ((bits % 16) ? 1 : 0) - dp;
138 +
139    /* Now fill in the gaps(from ::) in the copied table... -A1kmm */
140    for (dp = 0, nyble = 0; dp < 8; dp++)
141    {
# Line 143 | Line 147 | try_parse_v6_netmask(const char *text, s
147      else
148        dc[dp] = d[nyble++];
149    }
150 +
151    /* Set unused bits to 0... -A1kmm */
152    if (bits < 128 && (bits % 16 != 0))
153      dc[bits / 16] &= ~((1 << (15 - bits % 16)) - 1);
154    for (dp = bits / 16 + (bits % 16 ? 1 : 0); dp < 8; dp++)
155      dc[dp] = 0;
156 +
157    /* And assign... -A1kmm */
158    if (addr)
159      for (dp = 0; dp < 8; dp++)
160        /* The cast is a kludge to make netbsd work. */
161        ((unsigned short *)&v6->sin6_addr)[dp] = htons(dc[dp]);
162 +
163    if (b != NULL)
164      *b = bits;
165    return HM_IPV6;
# Line 174 | Line 181 | try_parse_v4_netmask(const char *text, s
181    unsigned char addb[4];
182    int n = 0, bits = 0;
183    char c;
184 <  struct sockaddr_in *v4 = (struct sockaddr_in*) addr;
184 >  struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
185  
186    digits[n++] = text;
187  
# Line 185 | Line 192 | try_parse_v4_netmask(const char *text, s
192      {
193        if (n >= 4)
194          return HM_HOST;
195 +
196        digits[n++] = p + 1;
197      }
198      else if (c == '*')
199      {
200        if (*(p + 1) || n == 0 || *(p - 1) != '.')
201          return HM_HOST;
202 +
203        bits = (n - 1) * 8;
204        break;
205      }
# Line 198 | Line 207 | try_parse_v4_netmask(const char *text, s
207      {
208        char *after;
209        bits = strtoul(p + 1, &after, 10);
210 +
211        if (!bits || *after)
212          return HM_HOST;
213        if (bits > n * 8)
214          return HM_HOST;
215 +
216        break;
217      }
218      else
# Line 212 | Line 223 | try_parse_v4_netmask(const char *text, s
223    if (bits)
224      while (n < 4)
225        digits[n++] = "0";
226 +
227    for (n = 0; n < 4; n++)
228      addb[n] = strtoul(digits[n], NULL, 10);
229 +
230    if (bits == 0)
231      bits = 32;
232 +
233    /* Set unused bits to 0... -A1kmm */
234    if (bits < 32 && bits % 8)
235      addb[bits / 8] &= ~((1 << (8 - bits % 8)) - 1);
# Line 240 | Line 254 | int
254   parse_netmask(const char *text, struct irc_ssaddr *addr, int *b)
255   {
256   #ifdef IPV6
257 <    if (strchr(text, ':'))
257 >  if (strchr(text, ':'))
258      return try_parse_v6_netmask(text, addr, b);
259   #endif
260    if (strchr(text, '.'))
# Line 259 | Line 273 | int
273   match_ipv6(struct irc_ssaddr *addr, struct irc_ssaddr *mask, int bits)
274   {
275    int i, m, n = bits / 8;
276 <  struct sockaddr_in6 *v6 = (struct sockaddr_in6*)addr;
277 <  struct sockaddr_in6 *v6mask = (struct sockaddr_in6*)mask;
276 >  struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
277 >  struct sockaddr_in6 *v6mask = (struct sockaddr_in6 *)mask;
278  
279    for (i = 0; i < n; i++)
280      if (v6->sin6_addr.s6_addr[i] != v6mask->sin6_addr.s6_addr[i])
# Line 281 | Line 295 | match_ipv6(struct irc_ssaddr *addr, stru
295   int
296   match_ipv4(struct irc_ssaddr *addr, struct irc_ssaddr *mask, int bits)
297   {
298 <  struct sockaddr_in *v4 = (struct sockaddr_in*) addr;
299 <  struct sockaddr_in *v4mask = (struct sockaddr_in*) mask;
298 >  struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
299 >  struct sockaddr_in *v4mask = (struct sockaddr_in *)mask;
300 >
301    if ((ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1)) !=
302        ntohl(v4mask->sin_addr.s_addr))
303      return 0;
# Line 311 | Line 326 | mask_addr(struct irc_ssaddr *ip, int bit
326    if (ip->ss.ss_family != AF_INET6)
327   #endif
328    {
329 <    v4_base_ip = (struct sockaddr_in*)ip;
329 >    v4_base_ip = (struct sockaddr_in *)ip;
330 >
331      mask = ~((1 << (32 - bits)) - 1);
332 <    v4_base_ip->sin_addr.s_addr =
317 <      htonl(ntohl(v4_base_ip->sin_addr.s_addr) & mask);
332 >    v4_base_ip->sin_addr.s_addr = htonl(ntohl(v4_base_ip->sin_addr.s_addr) & mask);
333    }
334   #ifdef IPV6
335    else
336    {
337      n = bits / 8;
338      m = bits % 8;
339 <    v6_base_ip = (struct sockaddr_in6*)ip;
339 >    v6_base_ip = (struct sockaddr_in6 *)ip;
340  
341      mask = ~((1 << (8 - m)) -1 );
342      v6_base_ip->sin6_addr.s6_addr[n] = v6_base_ip->sin6_addr.s6_addr[n] & mask;
343 <    for (i = n + 1; n < 16; i++)
344 <      v6_base_ip->sin6_addr.s6_addr[n] = 0;
343 >
344 >    for (i = n + 1; i < 16; i++)
345 >      v6_base_ip->sin6_addr.s6_addr[i] = 0;
346    }
347   #endif
348   }
# Line 345 | Line 361 | init_host_hash(void)
361   * Output: A hash value of the IP address.
362   * Side effects: None
363   */
364 < static unsigned long
364 > static uint32_t
365   hash_ipv4(struct irc_ssaddr *addr, int bits)
366   {
367    if (bits != 0)
368    {
369      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
370 <    unsigned long av = ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1);
371 <    return((av ^ (av >> 12) ^ (av >> 24)) & (ATABLE_SIZE - 1));
370 >    uint32_t av = ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1);
371 >
372 >    return (av ^ (av >> 12) ^ (av >> 24)) & (ATABLE_SIZE - 1);
373    }
374  
375 <  return(0);
375 >  return 0;
376   }
377  
378   /* unsigned long hash_ipv6(struct irc_ssaddr*)
# Line 364 | Line 381 | hash_ipv4(struct irc_ssaddr *addr, int b
381   * Side effects: None
382   */
383   #ifdef IPV6
384 < static unsigned long
384 > static uint32_t
385   hash_ipv6(struct irc_ssaddr *addr, int bits)
386   {
387 <  unsigned long v = 0, n;
388 <  struct sockaddr_in6 *v6 = (struct sockaddr_in6*) addr;
389 <  
387 >  uint32_t v = 0, n;
388 >  struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
389 >
390    for (n = 0; n < 16; n++)
391    {
392      if (bits >= 8)
# Line 394 | Line 411 | hash_ipv6(struct irc_ssaddr *addr, int b
411   * Output: The hash of the string between 1 and (TH_MAX-1)
412   * Side-effects: None.
413   */
414 < static int
414 > static uint32_t
415   hash_text(const char *start)
416   {
417    const char *p = start;
418 <  unsigned long h = 0;
418 >  uint32_t h = 0;
419  
420    while (*p)
404  {
421      h = (h << 4) - (h + (unsigned char)ToLower(*p++));
422 <  }
423 <  return (h & (ATABLE_SIZE - 1));
422 >
423 >  return h & (ATABLE_SIZE - 1);
424   }
425  
426   /* unsigned long get_hash_mask(const char *)
# Line 413 | Line 429 | hash_text(const char *start)
429   *         wildcard in the string.
430   * Side-effects: None.
431   */
432 < static unsigned long
432 > static uint32_t
433   get_mask_hash(const char *text)
434   {
435    const char *hp = "", *p;
436  
437    for (p = text + strlen(text) - 1; p >= text; p--)
438 <    if (*p == '*' || *p == '?')
438 >    if (IsMWildChar(*p))
439        return hash_text(hp);
440      else if (*p == '.')
441        hp = p + 1;
# Line 440 | Line 456 | struct AccessItem *
456   find_conf_by_address(const char *name, struct irc_ssaddr *addr, int type,
457                       int fam, const char *username, const char *password)
458   {
459 <  unsigned long hprecv = 0;
459 >  unsigned int hprecv = 0;
460    struct AccessItem *hprec = NULL;
461    struct AddressRec *arec;
462    int b;
# Line 551 | Line 567 | find_address_conf(const char *host, cons
567    /* Find the best I-line... If none, return NULL -A1kmm */
568    if ((iconf = find_conf_by_address(host, ip, CONF_CLIENT, aftype, user,
569                                      password)) == NULL)
570 <    return(NULL);
570 >    return NULL;
571  
572    /* If they are exempt from K-lines, return the best I-line. -A1kmm */
573    if (IsConfExemptKline(iconf))
574 <    return(iconf);
574 >    return iconf;
575  
576    /* Find the best K-line... -A1kmm */
577    kconf = find_conf_by_address(host, ip, CONF_KILL, aftype, user, NULL);
# Line 563 | Line 579 | find_address_conf(const char *host, cons
579    /* If they are K-lined, return the K-line. Otherwise, return the
580     * I-line. -A1kmm */
581    if (kconf != NULL)
582 <    return(kconf);
582 >    return kconf;
583  
584    kconf = find_conf_by_address(host, ip, CONF_GLINE, aftype, user, NULL);
585    if (kconf != NULL && !IsConfExemptGline(iconf))
586 <    return(kconf);
586 >    return kconf;
587  
588 <  return(iconf);
588 >  return iconf;
589   }
590  
591   struct AccessItem *
# Line 581 | Line 597 | find_gline_conf(const char *host, const
597    eline = find_conf_by_address(host, ip, CONF_EXEMPTKLINE, aftype,
598                                 user, NULL);
599    if (eline != NULL)
600 <    return(eline);
600 >    return eline;
601  
602 <  return(find_conf_by_address(host, ip, CONF_GLINE, aftype, user, NULL));
602 >  return find_conf_by_address(host, ip, CONF_GLINE, aftype, user, NULL);
603   }
604  
605   /* find_kline_conf
# Line 596 | Line 612 | find_gline_conf(const char *host, const
612   */
613   struct AccessItem *
614   find_kline_conf(const char *host, const char *user,
615 <                struct irc_ssaddr *ip, int aftype)
615 >                struct irc_ssaddr *ip, int aftype)
616   {
617    struct AccessItem *eline;
618  
619    eline = find_conf_by_address(host, ip, CONF_EXEMPTKLINE, aftype,
620                                 user, NULL);
621    if (eline != NULL)
622 <    return(eline);
622 >    return eline;
623  
624 <  return(find_conf_by_address(host, ip, CONF_KILL, aftype, user, NULL));
624 >  return find_conf_by_address(host, ip, CONF_KILL, aftype, user, NULL);
625   }
626  
627   /* struct AccessItem* find_dline_conf(struct irc_ssaddr*, int)
# Line 622 | Line 638 | find_dline_conf(struct irc_ssaddr *addr,
638    eline = find_conf_by_address(NULL, addr, CONF_EXEMPTDLINE | 1, aftype,
639                                 NULL, NULL);
640    if (eline != NULL)
641 <    return(eline);
642 <  return(find_conf_by_address(NULL, addr, CONF_DLINE | 1, aftype, NULL, NULL));
641 >    return eline;
642 >
643 >  return find_conf_by_address(NULL, addr, CONF_DLINE | 1, aftype, NULL, NULL);
644   }
645  
646   /* void add_conf_by_address(int, struct AccessItem *aconf)
# Line 636 | Line 653 | add_conf_by_address(int type, struct Acc
653   {
654    const char *address;
655    const char *username;
656 <  static unsigned long prec_value = 0xFFFFFFFF;
656 >  static unsigned int prec_value = 0xFFFFFFFF;
657    int masktype, bits;
658 <  unsigned long hv;
658 >  uint32_t hv;
659    struct AddressRec *arec;
660  
661    address = aconf->host;
# Line 654 | Line 671 | add_conf_by_address(int type, struct Acc
671    masktype = parse_netmask(address, &arec->Mask.ipa.addr, &bits);
672    arec->Mask.ipa.bits = bits;
673    arec->masktype = masktype;
674 +
675   #ifdef IPV6
676    if (masktype == HM_IPV6)
677    {
# Line 677 | Line 695 | add_conf_by_address(int type, struct Acc
695      arec->next = atable[(hv = get_mask_hash(address))];
696      atable[hv] = arec;
697    }
698 +
699    arec->username = username;
700    arec->aconf = aconf;
701    arec->precedence = prec_value--;
# Line 693 | Line 712 | void
712   delete_one_address_conf(const char *address, struct AccessItem *aconf)
713   {
714    int masktype, bits;
715 <  unsigned long hv;
715 >  uint32_t hv;
716    struct AddressRec *arec, *arecl = NULL;
717    struct irc_ssaddr addr;
718 +
719    masktype = parse_netmask(address, &addr, &bits);
720  
721   #ifdef IPV6
# Line 715 | Line 735 | delete_one_address_conf(const char *addr
735    }
736    else
737      hv = get_mask_hash(address);
738 +
739    for (arec = atable[hv]; arec; arec = arec->next)
740    {
741      if (arec->aconf == aconf)
# Line 723 | Line 744 | delete_one_address_conf(const char *addr
744          arecl->next = arec->next;
745        else
746          atable[hv] = arec->next;
747 +
748        aconf->status |= CONF_ILLEGAL;
749 +
750        if (aconf->clients == 0)
751          free_access_item(aconf);
752 +
753        MyFree(arec);
754        return;
755      }
# Line 751 | Line 775 | clear_out_address_conf(void)
775    for (i = 0; i < ATABLE_SIZE; i++)
776    {
777      last_arec = NULL;
778 +
779      for (arec = atable[i]; arec; arec = next_arec)
780      {
781        /* We keep the temporary K-lines and destroy the
# Line 776 | Line 801 | clear_out_address_conf(void)
801          }
802  
803          arec->aconf->status |= CONF_ILLEGAL;
804 +
805          if (arec->aconf->clients == 0)
806            free_access_item(arec->aconf);
807          MyFree(arec);
# Line 797 | Line 823 | char *
823   show_iline_prefix(struct Client *sptr, struct AccessItem *aconf, const char *name)
824   {
825    static char prefix_of_host[USERLEN + 14];
826 <  char *prefix_ptr;
826 >  char *prefix_ptr = prefix_of_host;
827  
802  prefix_ptr = prefix_of_host;
828    if (IsNoTilde(aconf))
829      *prefix_ptr++ = '-';
830    if (IsLimitIp(aconf))
# Line 820 | Line 845 | show_iline_prefix(struct Client *sptr, s
845      *prefix_ptr++ = '_';
846    if (MyOper(sptr) && IsConfExemptLimits(aconf))
847      *prefix_ptr++ = '>';
823  if (MyOper(sptr) && IsConfIdlelined(aconf))
824    *prefix_ptr++ = '<';
848    if (IsConfCanFlood(aconf))
849      *prefix_ptr++ = '|';
827  strlcpy(prefix_ptr, name, USERLEN);
850  
851 <  return(prefix_of_host);
851 >  strlcpy(prefix_ptr, name, USERLEN+1);
852 >
853 >  return prefix_of_host;
854   }
855  
856   /* report_auth()
# Line 910 | Line 934 | report_Klines(struct Client *client_p, i
934              (!tkline && ((aconf = arec->aconf)->flags & CONF_FLAGS_TEMPORARY)))
935            continue;
936  
937 <        if (IsOper(client_p))
938 <          sendto_one(client_p, form_str(RPL_STATSKLINE), me.name,
937 >        if (HasUMode(client_p, UMODE_OPER))
938 >          sendto_one(client_p, form_str(RPL_STATSKLINE), me.name,
939                       client_p->name, p, aconf->host, aconf->user,
940 <                     aconf->reason, aconf->oper_reason ? aconf->oper_reason : "");
941 <        else
940 >                     aconf->reason, aconf->oper_reason ? aconf->oper_reason : "");
941 >        else
942            sendto_one(client_p, form_str(RPL_STATSKLINE), me.name,
943                       client_p->name, p, aconf->host, aconf->user,
944 <                     aconf->reason, "");
944 >                     aconf->reason, "");
945        }
946      }
947    }

Diff Legend

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