/[svn]/hopm/trunk/src/patricia.c
ViewVC logotype

Diff of /hopm/trunk/src/patricia.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 8752 by michael, Sat Sep 22 21:44:16 2018 UTC revision 8753 by michael, Tue Jan 1 13:56:48 2019 UTC
# Line 65  comp_with_mask(void *addr, void *dest, u Line 65  comp_with_mask(void *addr, void *dest, u
65    if ( /* mask/8 == 0 || */ memcmp(addr, dest, mask / 8) == 0)    if ( /* mask/8 == 0 || */ memcmp(addr, dest, mask / 8) == 0)
66    {    {
67      int n = mask / 8;      int n = mask / 8;
68      int m = ((-1) << (8 - (mask % 8)));      int m = ~((1 << (8 - (mask % 8))) - 1);
69    
70      if (mask % 8 == 0 || (((unsigned char *)addr)[n] & m) == (((unsigned char *)dest)[n] & m))      if (mask % 8 == 0 || (((unsigned char *)addr)[n] & m) == (((unsigned char *)dest)[n] & m))
71        return 1;        return 1;
# Line 113  New_Prefix2(int family, void *dest, int Line 113  New_Prefix2(int family, void *dest, int
113    
114    if (prefix == NULL)    if (prefix == NULL)
115    {    {
116      prefix = xcalloc(sizeof(prefix_t));      prefix = xcalloc(sizeof(*prefix));
117      dynamic_allocated = 1;      dynamic_allocated = 1;
118    }    }
119    
# Line 137  New_Prefix(int family, void *dest, int b Line 137  New_Prefix(int family, void *dest, int b
137  static prefix_t *  static prefix_t *
138  ascii2prefix(int family, const char *string)  ascii2prefix(int family, const char *string)
139  {  {
140      char save[INET6_ADDRSTRLEN];
141    int bitlen, maxbitlen = 0;    int bitlen, maxbitlen = 0;
142    union    union
143    {    {
# Line 151  ascii2prefix(int family, const char *str Line 152  ascii2prefix(int family, const char *str
152    {    {
153      family = AF_INET;      family = AF_INET;
154    
155      if (strchr (string, ':'))      if (strchr(string, ':'))
156        family = AF_INET6;        family = AF_INET6;
157    }    }
158    
# Line 163  ascii2prefix(int family, const char *str Line 164  ascii2prefix(int family, const char *str
164    const char *const cp = strchr(string, '/');    const char *const cp = strchr(string, '/');
165    if (cp)    if (cp)
166    {    {
167      char save[MAXLINE];      size_t length = cp - string;
168    
169        if (length >= sizeof(save))
170          return NULL;
171    
172      bitlen = atoi(cp + 1);      bitlen = atoi(cp + 1);
173    
     /* *cp = '\0'; */  
174      /* Copy the string to save. Avoid destroying the string */      /* Copy the string to save. Avoid destroying the string */
175      assert(cp - string < MAXLINE);      memcpy(save, string, length);
176      memcpy(save, string, cp - string);      save[length] = '\0';
177    
     save[cp - string] = '\0';  
178      string = save;      string = save;
179    
180      if (bitlen < 0 || bitlen > maxbitlen)      if (bitlen < 0 || bitlen > maxbitlen)
# Line 222  Deref_Prefix(prefix_t *prefix) Line 224  Deref_Prefix(prefix_t *prefix)
224  patricia_tree_t *  patricia_tree_t *
225  patricia_new(unsigned int maxbits)  patricia_new(unsigned int maxbits)
226  {  {
227    patricia_tree_t *patricia = xcalloc(sizeof *patricia);    patricia_tree_t *patricia = xcalloc(sizeof(*patricia));
228    patricia->maxbits = maxbits;    patricia->maxbits = maxbits;
229    
230    assert(maxbits <= PATRICIA_MAXBITS);  /* XXX */    assert(maxbits <= PATRICIA_MAXBITS);  /* XXX */
# Line 288  patricia_clear(patricia_tree_t *patricia Line 290  patricia_clear(patricia_tree_t *patricia
290      }      }
291    }    }
292    
293      patricia->head = NULL;
294    
295    assert(patricia->num_active_node == 0);    assert(patricia->num_active_node == 0);
   /* xfree (patricia); */  
296  }  }
297    
298  void  void
# Line 504  patricia_lookup(patricia_tree_t *patrici Line 507  patricia_lookup(patricia_tree_t *patrici
507    
508    if (patricia->head == NULL)    if (patricia->head == NULL)
509    {    {
510      node = xcalloc(sizeof *node);      node = xcalloc(sizeof(*node));
511      node->bit = prefix->bitlen;      node->bit = prefix->bitlen;
512      node->prefix = Ref_Prefix (prefix);      node->prefix = Ref_Prefix(prefix);
513      patricia->head = node;      patricia->head = node;
514  #ifdef PATRICIA_DEBUG  #ifdef PATRICIA_DEBUG
515      fprintf(stderr, "patricia_lookup: new_node #0 %s/%d (head)\n",      fprintf(stderr, "patricia_lookup: new_node #0 %s/%d (head)\n",
# Line 629  patricia_lookup(patricia_tree_t *patrici Line 632  patricia_lookup(patricia_tree_t *patrici
632      return node;      return node;
633    }    }
634    
635    new_node = xcalloc(sizeof *new_node);    new_node = xcalloc(sizeof(*new_node));
636    new_node->bit = prefix->bitlen;    new_node->bit = prefix->bitlen;
637    new_node->prefix = Ref_Prefix(prefix);    new_node->prefix = Ref_Prefix(prefix);
638    patricia->num_active_node++;    patricia->num_active_node++;
# Line 690  patricia_lookup(patricia_tree_t *patrici Line 693  patricia_lookup(patricia_tree_t *patrici
693    }    }
694    else    else
695    {    {
696      glue = xcalloc(sizeof *glue);      glue = xcalloc(sizeof(*glue));
697      glue->bit = differ_bit;      glue->bit = differ_bit;
698      glue->parent = node->parent;      glue->parent = node->parent;
699      patricia->num_active_node++;      patricia->num_active_node++;
# Line 866  patricia_make_and_lookup(patricia_tree_t Line 869  patricia_make_and_lookup(patricia_tree_t
869    if (prefix)    if (prefix)
870    {    {
871      patricia_node_t *node = patricia_lookup(tree, prefix);      patricia_node_t *node = patricia_lookup(tree, prefix);
872        Deref_Prefix(prefix);
873        return node;
874      }
875    
876      return NULL;
877    }
878    
879    patricia_node_t *
880    patricia_make_and_lookup_addr(patricia_tree_t *tree, struct sockaddr *addr, int bitlen)
881    {
882      int family;
883      void *dest;
884    
885      if (addr->sa_family == AF_INET6)
886      {
887        if (bitlen == 0 || bitlen > 128)
888          bitlen = 128;
889        family = AF_INET6;
890        dest = &((struct sockaddr_in6 *)addr)->sin6_addr;
891      }
892      else
893      {
894        if (bitlen == 0 || bitlen > 32)
895          bitlen = 32;
896        family = AF_INET;
897        dest = &((struct sockaddr_in *)addr)->sin_addr;
898      }
899    
900      prefix_t *prefix = New_Prefix(family, dest, bitlen);
901      if (prefix)
902      {
903        patricia_node_t *node = patricia_lookup(tree, prefix);
904      Deref_Prefix(prefix);      Deref_Prefix(prefix);
905      return node;      return node;
906    }    }

Legend:
Removed from v.8752  
changed lines
  Added in v.8753

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28