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 3003 by michael, Wed Feb 19 10:05:57 2014 UTC vs.
Revision 4565 by michael, Sun Aug 24 10:27:40 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 57 | Line 57
57     Also a bug in DigitParse above.
58     -Gozem 2002-07-19 gozem@linux.nu
59   */
60 #ifdef IPV6
60   static int
61   try_parse_v6_netmask(const char *text, struct irc_ssaddr *addr, int *b)
62   {
64  const char *p;
63    char c;
64    int d[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
65    int dp = 0;
# Line 72 | Line 70 | try_parse_v6_netmask(const char *text, s
70    short dc[8];
71    struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
72  
73 <  for (p = text; (c = *p); p++)
73 >  for (const char *p = text; (c = *p); ++p)
74    {
75      if (IsXDigit(c))
76      {
# Line 111 | Line 109 | try_parse_v6_netmask(const char *text, s
109        char *after;
110  
111        d[dp] = d[dp] >> 4 * nyble;
112 <      dp++;
112 >      ++dp;
113        bits = strtoul(p + 1, &after, 10);
114  
115        if (bits < 0 || *after)
# Line 127 | Line 125 | try_parse_v6_netmask(const char *text, s
125    d[dp] = d[dp] >> 4 * nyble;
126  
127    if (c == 0)
128 <    dp++;
128 >    ++dp;
129    if (finsert < 0 && bits == 0)
130      bits = dp * 16;
131  
# Line 135 | Line 133 | try_parse_v6_netmask(const char *text, s
133    deficit = bits / 16 + ((bits % 16) ? 1 : 0) - dp;
134  
135    /* Now fill in the gaps(from ::) in the copied table... -A1kmm */
136 <  for (dp = 0, nyble = 0; dp < 8; dp++)
136 >  for (dp = 0, nyble = 0; dp < 8; ++dp)
137    {
138      if (nyble == finsert && deficit)
139      {
# Line 149 | Line 147 | try_parse_v6_netmask(const char *text, s
147    /* Set unused bits to 0... -A1kmm */
148    if (bits < 128 && (bits % 16 != 0))
149      dc[bits / 16] &= ~((1 << (15 - bits % 16)) - 1);
150 <  for (dp = bits / 16 + (bits % 16 ? 1 : 0); dp < 8; dp++)
150 >  for (dp = bits / 16 + (bits % 16 ? 1 : 0); dp < 8; ++dp)
151      dc[dp] = 0;
152  
153    /* And assign... -A1kmm */
154    if (addr)
155 <    for (dp = 0; dp < 8; dp++)
155 >    for (dp = 0; dp < 8; ++dp)
156        /* The cast is a kludge to make netbsd work. */
157        ((unsigned short *)&v6->sin6_addr)[dp] = htons(dc[dp]);
158  
159 <  if (b != NULL)
159 >  if (b)
160      *b = bits;
161    return HM_IPV6;
162   }
165 #endif
163  
164   /* int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *);
165   * Input: A possible IPV4 address as a string.
# Line 174 | Line 171 | try_parse_v6_netmask(const char *text, s
171   static int
172   try_parse_v4_netmask(const char *text, struct irc_ssaddr *addr, int *b)
173   {
177  const char *p;
174    const char *digits[4];
175    unsigned char addb[4];
176    int n = 0, bits = 0;
# Line 183 | Line 179 | try_parse_v4_netmask(const char *text, s
179  
180    digits[n++] = text;
181  
182 <  for (p = text; (c = *p); p++)
182 >  for (const char *p = text; (c = *p); ++p)
183    {
184      if (c >= '0' && c <= '9')   /* empty */
185        ;
# Line 224 | Line 220 | try_parse_v4_netmask(const char *text, s
220      while (n < 4)
221        digits[n++] = "0";
222  
223 <  for (n = 0; n < 4; n++)
223 >  for (n = 0; n < 4; ++n)
224      addb[n] = strtoul(digits[n], NULL, 10);
225  
226    if (bits == 0)
# Line 233 | Line 229 | try_parse_v4_netmask(const char *text, s
229    /* Set unused bits to 0... -A1kmm */
230    if (bits < 32 && bits % 8)
231      addb[bits / 8] &= ~((1 << (8 - bits % 8)) - 1);
232 <  for (n = bits / 8 + (bits % 8 ? 1 : 0); n < 4; n++)
232 >  for (n = bits / 8 + (bits % 8 ? 1 : 0); n < 4; ++n)
233      addb[n] = 0;
234    if (addr)
235      v4->sin_addr.s_addr =
236        htonl(addb[0] << 24 | addb[1] << 16 | addb[2] << 8 | addb[3]);
237 <  if (b != NULL)
237 >  if (b)
238      *b = bits;
239    return HM_IPV4;
240   }
# Line 255 | Line 251 | parse_netmask(const char *text, struct i
251   {
252    if (strchr(text, '.'))
253      return try_parse_v4_netmask(text, addr, b);
258 #ifdef IPV6
254    if (strchr(text, ':'))
255      return try_parse_v6_netmask(text, addr, b);
256 < #endif
256 >
257    return HM_HOST;
258   }
259  
# Line 268 | Line 263 | parse_netmask(const char *text, struct i
263   * Output: if match, -1 else 0
264   * Side effects: None
265   */
271 #ifdef IPV6
266   int
267   match_ipv6(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
268   {
# Line 276 | Line 270 | match_ipv6(const struct irc_ssaddr *addr
270    const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
271    const struct sockaddr_in6 *v6mask = (const struct sockaddr_in6 *)mask;
272  
273 <  for (i = 0; i < n; i++)
273 >  for (i = 0; i < n; ++i)
274      if (v6->sin6_addr.s6_addr[i] != v6mask->sin6_addr.s6_addr[i])
275        return 0;
276 +
277    if ((m = bits % 8) == 0)
278      return -1;
279    if ((v6->sin6_addr.s6_addr[n] & ~((1 << (8 - m)) - 1)) ==
# Line 286 | Line 281 | match_ipv6(const struct irc_ssaddr *addr
281      return -1;
282    return 0;
283   }
289 #endif
284  
285   /* int match_ipv4(struct irc_ssaddr *, struct irc_ssaddr *, int)
286   * Input: An IP address, an IP mask, the number of bits in the mask.
# Line 317 | Line 311 | void
311   mask_addr(struct irc_ssaddr *ip, int bits)
312   {
313    int mask;
320 #ifdef IPV6
314    struct sockaddr_in6 *v6_base_ip;
315    int i, m, n;
323 #endif
316    struct sockaddr_in *v4_base_ip;
317  
318 < #ifdef IPV6
327 <  if (ip->ss.ss_family != AF_INET6)
328 < #endif
318 >  if (ip->ss.ss_family == AF_INET)
319    {
320 +    uint32_t tmp = 0;
321      v4_base_ip = (struct sockaddr_in *)ip;
322  
323      mask = ~((1 << (32 - bits)) - 1);
324 <    v4_base_ip->sin_addr.s_addr = htonl(ntohl(v4_base_ip->sin_addr.s_addr) & mask);
324 >    tmp = ntohl(v4_base_ip->sin_addr.s_addr);
325 >    v4_base_ip->sin_addr.s_addr = htonl(tmp & mask);
326    }
335 #ifdef IPV6
327    else
328    {
329      n = bits / 8;
# Line 345 | Line 336 | mask_addr(struct irc_ssaddr *ip, int bit
336      for (i = n + 1; i < 16; i++)
337        v6_base_ip->sin6_addr.s6_addr[i] = 0;
338    }
348 #endif
339   }
340  
341 < /* Hashtable stuff...now external as its used in m_stats.c */
341 > /* Hashtable stuff...now external as it's used in m_stats.c */
342   dlink_list atable[ATABLE_SIZE];
343  
344   void
# Line 381 | Line 371 | hash_ipv4(const struct irc_ssaddr *addr,
371   * Output: A hash value of the IP address.
372   * Side effects: None
373   */
384 #ifdef IPV6
374   static uint32_t
375   hash_ipv6(const struct irc_ssaddr *addr, int bits)
376   {
377    uint32_t v = 0, n;
378    const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
379  
380 <  for (n = 0; n < 16; n++)
380 >  for (n = 0; n < 16; ++n)
381    {
382      if (bits >= 8)
383      {
# Line 403 | Line 392 | hash_ipv6(const struct irc_ssaddr *addr,
392      else
393        return v & (ATABLE_SIZE - 1);
394    }
395 +
396    return v & (ATABLE_SIZE - 1);
397   }
408 #endif
398  
399   /* int hash_text(const char *start)
400   * Input: The start of the text to hash.
# Line 415 | Line 404 | hash_ipv6(const struct irc_ssaddr *addr,
404   static uint32_t
405   hash_text(const char *start)
406   {
418  const char *p = start;
407    uint32_t h = 0;
408  
409 <  for (; *p; ++p)
409 >  for (const char *p = start; *p; ++p)
410      h = (h << 4) - (h + ToLower(*p));
411  
412    return h & (ATABLE_SIZE - 1);
# Line 435 | Line 423 | get_mask_hash(const char *text)
423   {
424    const char *hp = "", *p;
425  
426 <  for (p = text + strlen(text) - 1; p >= text; p--)
426 >  for (p = text + strlen(text) - 1; p >= text; --p)
427      if (IsMWildChar(*p))
428        return hash_text(hp);
429      else if (*p == '.')
# Line 464 | Line 452 | find_conf_by_address(const char *name, s
452    int b;
453    int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp;
454  
467  if (password == NULL)
468    password = "";
469
455    if (addr)
456    {
457      /* Check for IPV6 matches... */
473 #ifdef IPV6
458      if (fam == AF_INET6)
459      {
460        for (b = 128; b >= 0; b -= 16)
# Line 494 | Line 478 | find_conf_by_address(const char *name, s
478          }
479        }
480      }
481 <    else
498 < #endif
499 <    if (fam == AF_INET)
481 >    else if (fam == AF_INET)
482      {
483        for (b = 32; b >= 0; b -= 8)
484        {
# Line 521 | Line 503 | find_conf_by_address(const char *name, s
503      }
504    }
505  
506 <  if (name != NULL)
506 >  if (name)
507    {
508      const char *p = name;
509  
# Line 542 | Line 524 | find_conf_by_address(const char *name, s
524            hprec = arec->conf;
525          }
526        }
527 <      p = strchr(p, '.');
528 <      if (p == NULL)
527 >
528 >      if ((p = strchr(p, '.')) == NULL)
529          break;
530 <      p++;
530 >      ++p;
531      }
532  
533      DLINK_FOREACH(ptr, atable[0].head)
# Line 576 | Line 558 | find_conf_by_address(const char *name, s
558   * Side-effects: None
559   */
560   struct MaskItem *
561 < find_address_conf(const char *host, const char *user,
562 <                  struct irc_ssaddr *ip, int aftype, char *password)
561 > find_address_conf(const char *host, const char *user, struct irc_ssaddr *ip,
562 >                  int aftype, const char *password)
563   {
564    struct MaskItem *authcnf = NULL, *killcnf = NULL;
565  
# Line 597 | Line 579 | find_address_conf(const char *host, cons
579     * If they are K-lined, return the K-line. Otherwise, return the
580     * auth{} block. -A1kmm
581     */
582 <  if (killcnf != NULL)
582 >  if (killcnf)
583      return killcnf;
584  
585    if (IsConfExemptGline(authcnf))
586      return authcnf;
587  
588    killcnf = find_conf_by_address(host, ip, CONF_GLINE, aftype, user, NULL, 1);
589 <  if (killcnf != NULL)
589 >  if (killcnf)
590      return killcnf;
591  
592    return authcnf;
# Line 622 | Line 604 | find_dline_conf(struct irc_ssaddr *addr,
604    struct MaskItem *eline;
605  
606    eline = find_conf_by_address(NULL, addr, CONF_EXEMPT, aftype, NULL, NULL, 1);
607 <  if (eline != NULL)
607 >  if (eline)
608      return eline;
609  
610    return find_conf_by_address(NULL, addr, CONF_DLINE, aftype, NULL, NULL, 1);
# Line 644 | Line 626 | add_conf_by_address(const unsigned int t
626  
627    assert(type && !EmptyString(hostname));
628  
629 <  arec = MyMalloc(sizeof(struct AddressRec));
629 >  arec = MyCalloc(sizeof(struct AddressRec));
630    arec->masktype = parse_netmask(hostname, &arec->Mask.ipa.addr, &bits);
631    arec->Mask.ipa.bits = bits;
632    arec->username = username;
# Line 659 | Line 641 | add_conf_by_address(const unsigned int t
641        bits -= bits % 8;
642        dlinkAdd(arec, &arec->node, &atable[hash_ipv4(&arec->Mask.ipa.addr, bits)]);
643        break;
662 #ifdef IPV6
644      case HM_IPV6:
645        /* We have to do this, since we do not re-hash for every bit -A1kmm. */
646        bits -= bits % 16;
647        dlinkAdd(arec, &arec->node, &atable[hash_ipv6(&arec->Mask.ipa.addr, bits)]);
648        break;
668 #endif
649      default: /* HM_HOST */
650        arec->Mask.hostname = hostname;
651        dlinkAdd(arec, &arec->node, &atable[get_mask_hash(hostname)]);
# Line 686 | Line 666 | delete_one_address_conf(const char *addr
666   {
667    int bits = 0;
668    uint32_t hv = 0;
669 <  dlink_node *ptr = NULL, *ptr_next = NULL;
669 >  dlink_node *ptr = NULL;
670    struct irc_ssaddr addr;
671  
672    switch (parse_netmask(address, &addr, &bits))
# Line 696 | Line 676 | delete_one_address_conf(const char *addr
676        bits -= bits % 8;
677        hv = hash_ipv4(&addr, bits);
678        break;
699 #ifdef IPV6
679      case HM_IPV6:
680        /* We have to do this, since we do not re-hash for every bit -A1kmm. */
681        bits -= bits % 16;
682        hv = hash_ipv6(&addr, bits);
683        break;
705 #endif
684      default: /* HM_HOST */
685        hv = get_mask_hash(address);
686        break;
687    }
688  
689 <  DLINK_FOREACH_SAFE(ptr, ptr_next, atable[hv].head)
689 >  DLINK_FOREACH(ptr, atable[hv].head)
690    {
691      struct AddressRec *arec = ptr->data;
692  
# Line 735 | Line 713 | delete_one_address_conf(const char *addr
713   void
714   clear_out_address_conf(void)
715   {
738  unsigned int i = 0;
716    dlink_node *ptr = NULL, *ptr_next = NULL;
717  
718 <  for (i = 0; i < ATABLE_SIZE; ++i)
718 >  for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
719    {
720      DLINK_FOREACH_SAFE(ptr, ptr_next, atable[i].head)
721      {
# Line 752 | Line 729 | clear_out_address_conf(void)
729          continue;
730  
731        dlinkDelete(&arec->node, &atable[i]);
732 +      arec->conf->active = 0;
733  
734        if (!arec->conf->ref_count)
735          conf_free(arec->conf);
# Line 765 | Line 743 | hostmask_send_expiration(struct AddressR
743   {
744    char ban_type = '\0';
745  
746 <  if (!ConfigFileEntry.tkline_expire_notices)
746 >  if (!ConfigGeneral.tkline_expire_notices)
747      return;
748  
749    switch (arec->type)
# Line 791 | Line 769 | hostmask_send_expiration(struct AddressR
769   void
770   hostmask_expire_temporary(void)
771   {
794  unsigned int i = 0;
772    dlink_node *ptr = NULL, *ptr_next = NULL;
773  
774 <  for (i = 0; i < ATABLE_SIZE; ++i)
774 >  for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
775    {
776      DLINK_FOREACH_SAFE(ptr, ptr_next, atable[i].head)
777      {

Diff Legend

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