int match_cidr(const char *s1, const char *s2) { struct rb_sockaddr_storage ipaddr, maskaddr; char mask[BUFSIZE]; char address[NICKLEN + USERLEN + HOSTLEN + 6]; char *ipmask; char *ip; char *len; void *ipptr, *maskptr; int cidrlen, aftype; strcpy(mask, s1); strcpy(address, s2); ipmask = strrchr(mask, '@'); if(ipmask == NULL) return 0; *ipmask++ = '\0'; ip = strrchr(address, '@'); if(ip == NULL) return 0; *ip++ = '\0'; len = strrchr(ipmask, '/'); if(len == NULL) return 0; *len++ = '\0'; cidrlen = atoi(len); if(cidrlen == 0) return 0; if(strchr(ip, ':') && strchr(ipmask, ':')) { aftype = AF_INET6; ipptr = &((struct sockaddr_in6 *) &ipaddr)->sin6_addr; maskptr = &((struct sockaddr_in6 *) &maskaddr)->sin6_addr; } else if(!strchr(ip, ':') && !strchr(ipmask, ':')) { aftype = AF_INET; ipptr = &((struct sockaddr_in *) &ipaddr)->sin_addr; maskptr = &((struct sockaddr_in *) &maskaddr)->sin_addr; } else return 0; rb_inet_pton(aftype, ip, ipptr); rb_inet_pton(aftype, ipmask, maskptr); if(comp_with_mask(ipptr, maskptr, cidrlen) && match(mask, address)) return 1; else return 0; }
/* * match_ips() * * Input - cidr ip mask, address */ int match_ips(const char *s1, const char *s2) { struct rb_sockaddr_storage ipaddr, maskaddr; char mask[IRCD_BUFSIZE]; char address[HOSTLEN + 1]; char *len; void *ipptr, *maskptr; int cidrlen, aftype; rb_strlcpy(mask, s1, sizeof(mask)); rb_strlcpy(address, s2, sizeof(address)); len = strrchr(mask, '/'); if(len == NULL) return 0; *len++ = '\0'; cidrlen = atoi(len); if(cidrlen <= 0) return 0; #ifdef RB_IPV6 if(strchr(mask, ':') && strchr(address, ':')) { if(cidrlen > 128) return 0; aftype = AF_INET6; ipptr = &((struct sockaddr_in6 *)&ipaddr)->sin6_addr; maskptr = &((struct sockaddr_in6 *)&maskaddr)->sin6_addr; } else #endif if(!strchr(mask, ':') && !strchr(address, ':')) { if(cidrlen > 32) return 0; aftype = AF_INET; ipptr = &((struct sockaddr_in *)&ipaddr)->sin_addr; maskptr = &((struct sockaddr_in *)&maskaddr)->sin_addr; } else return 0; if(rb_inet_pton(aftype, address, ipptr) <= 0) return 0; if(rb_inet_pton(aftype, mask, maskptr) <= 0) return 0; if(comp_with_mask(ipptr, maskptr, cidrlen)) return 1; else return 0; }
static void set_bind(char **parv) { char *ipv4 = parv[2]; #ifdef RB_IPV6 char *ipv6 = parv[3]; #endif if(!strcmp(ipv4, "0")) ipv4_addr.s_addr = INADDR_ANY; else rb_inet_pton(AF_INET, ipv4, &ipv4_addr); #ifdef RB_IPV6 if(!strcmp(ipv6, "0")) memcpy(&ipv6_addr, &in6addr_any, sizeof(ipv6_addr)); else rb_inet_pton(AF_INET6, ipv6, &ipv6_addr); #endif }
/* * add_listener- create a new listener * port - the port number to listen on * vhost_ip - if non-null must contain a valid IP address string in * the format "255.255.255.255" */ void add_listener(int port, const char *vhost_ip, int family, int ssl) { struct Listener *listener; struct rb_sockaddr_storage vaddr; /* * if no port in conf line, don't bother */ if(port == 0) return; memset(&vaddr, 0, sizeof(vaddr)); vaddr.ss_family = family; if(vhost_ip != NULL) { if(family == AF_INET) { if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0) return; } #ifdef RB_IPV6 else { if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0) return; } #endif } else { switch(family) { case AF_INET: ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY; break; #ifdef RB_IPV6 case AF_INET6: memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); break; default: return; #endif } } switch(family) { case AF_INET: SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in)); ((struct sockaddr_in *)&vaddr)->sin_port = htons(port); break; #ifdef RB_IPV6 case AF_INET6: SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6)); ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port); break; #endif default: break; } if((listener = find_listener(&vaddr))) { if(listener->F != NULL) return; } else { listener = make_listener(&vaddr); listener->next = ListenerPollList; ListenerPollList = listener; } listener->F = NULL; listener->ssl = ssl; if(inetport(listener)) listener->active = 1; else close_listener(listener); }