--- libelwix/src/net.c 2013/05/30 09:07:33 1.2 +++ libelwix/src/net.c 2013/06/03 11:48:26 1.2.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.2 2013/05/30 09:07:33 misho Exp $ +* $Id: net.c,v 1.2.2.2 2013/06/03 11:48:26 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -280,4 +280,40 @@ e_usleep(u_int usec) struct timeval tv = { (time_t) (usec / 1000000), (long) (usec % 1000000) }; return select(0, NULL, NULL, NULL, &tv); +} + +/* + * e_innet() - Test address match in network + * + * @net = network + * @addr = address + * return: -1 error, 0 match or 1 not match + */ +int +e_innet(netaddr_t * __restrict net, inaddr_t * __restrict addr) +{ + register int i; + int ret = 0; + + if (!net || !addr) + return -1; + + switch (net->addr.sa.sa_family) { + case AF_INET: + ret = net->addr.sin.sin_addr.s_addr & net->mask.in.s_addr; + ret -= addr->in.s_addr & net->mask.in.s_addr; + break; + case AF_INET6: + for (i = 0; i < sizeof(struct in6_addr); i++) { + ret = net->addr.sin6.sin6_addr.s6_addr[i] & net->mask.in6.s6_addr[i]; + ret -= addr->in6.s6_addr[i] & net->mask.in6.s6_addr[i]; + if (ret) + break; + } + break; + default: + return -1; + } + + return !!ret; }