From: Gunnar Beutner Date: Mon, 5 Aug 2013 21:56:15 +0000 (+0200) Subject: Fix ns-cidr and implement IPv6 support. X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/commitdiff_plain/b0733e585e2364c89d9cec5690f20f1f2726240d Fix ns-cidr and implement IPv6 support. --- diff --git a/newsearch/ns-cidr.c b/newsearch/ns-cidr.c index e57267a7..99d12dcc 100644 --- a/newsearch/ns-cidr.c +++ b/newsearch/ns-cidr.c @@ -6,14 +6,15 @@ #include #include +#include #include "../irc/irc_config.h" #include "../lib/irc_string.h" #include "../lib/irc_ipv6.h" struct cidr_localdata { - unsigned int ip; - unsigned int mask; + struct irc_in_addr ip; + unsigned char bits; }; void *cidr_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); @@ -22,8 +23,8 @@ void cidr_free(searchCtx *ctx, struct searchNode *thenode); struct searchNode *cidr_parse(searchCtx *ctx, int argc, char **argv) { struct searchNode *thenode, *convsn; struct cidr_localdata *c; - unsigned char mask; struct irc_in_addr ip; + unsigned char bits; char *p; int ret; @@ -35,7 +36,7 @@ struct searchNode *cidr_parse(searchCtx *ctx, int argc, char **argv) { if (!(convsn=argtoconststr("cidr", ctx, argv[0], &p))) return NULL; - ret = ipmask_parse(p, &ip, &mask); + ret = ipmask_parse(p, &ip, &bits); convsn->free(ctx, convsn); if(!ret) { @@ -43,18 +44,6 @@ struct searchNode *cidr_parse(searchCtx *ctx, int argc, char **argv) { return NULL; } - if(!irc_in_addr_is_ipv4(&ip)) { - parseError = "cidr: sorry, no IPv6 yet"; - return NULL; - } - - /* ??? */ - mask-=(128-32); - if(mask > 32) { - parseError = "cidr: bad mask supplied"; - return NULL; - } - if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) { parseError = "malloc: could not allocate memory for this search."; return NULL; @@ -68,13 +57,8 @@ struct searchNode *cidr_parse(searchCtx *ctx, int argc, char **argv) { return NULL; } - if(!mask) { - c->mask = 0; - } else if(mask < 32) { - c->mask = 0xffffffff << (32 - mask); - } - - c->ip = irc_in_addr_v4_to_int(&ip) & c->mask; + memcpy(&c->ip, &ip, sizeof(struct irc_in_addr)); + c->bits = bits; thenode->localdata = (void *)c; thenode->free = cidr_free; @@ -86,18 +70,11 @@ struct searchNode *cidr_parse(searchCtx *ctx, int argc, char **argv) { void *cidr_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { nick *np = (nick *)theinput; struct cidr_localdata *c = thenode->localdata; - unsigned int ip; - struct irc_in_addr *sin; - sin = &np->ipnode->prefix->sin; - if(!irc_in_addr_is_ipv4(sin)) + if(!ipmask_check(&np->p_ipaddr, &c->ip, c->bits)) return (void *)0; - ip = irc_in_addr_v4_to_int(sin); - if((ip & c->mask) == c->ip) - return (void *)1; - - return (void *)0; + return (void *)1; } void cidr_free(searchCtx *ctx, struct searchNode *thenode) {