--- libelwix/src/net.c 2013/01/17 10:05:35 1.1.1.1 +++ libelwix/src/net.c 2013/06/03 13:59:26 1.2.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.1.1.1 2013/01/17 10:05:35 misho Exp $ +* $Id: net.c,v 1.2.2.3 2013/06/03 13:59:26 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -54,7 +54,7 @@ SUCH DAMAGE. * @len = string length * return: NULL error or !=NULL string a */ -inline char * +char * e_ether_ntoa(const struct e_ether_addr *n, char * __restrict a, int len) { if (!n || !a) @@ -77,7 +77,7 @@ e_ether_ntoa(const struct e_ether_addr *n, char * __re * @e = ethernet address structure, like struct ether_addr * return: NULL error or !=NULL ethernet address structure */ -inline struct e_ether_addr * +struct e_ether_addr * e_ether_aton(const char *a, struct e_ether_addr *e) { int i; @@ -106,7 +106,7 @@ e_ether_aton(const char *a, struct e_ether_addr *e) * @addr = Address * return: 0 not supported family type or port number */ -inline u_short +u_short e_n2port(sockaddr_t * __restrict addr) { u_short port = 0; @@ -274,10 +274,52 @@ e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __re * @usec = microseconds for sleep * return: -1 interrupted by signal or 0 ok */ -inline int +int 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: + for (i = 0; i < sizeof(struct in_addr); i++) { + ret = ((caddr_t) &net->addr.sin.sin_addr.s_addr)[i] & + net->mask.in.s4_addr[i]; + ret -= addr->in.s4_addr[i] & net->mask.in.s4_addr[i]; + if (ret) + break; + } + 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; }