--- libelwix/src/net.c 2013/06/03 11:48:26 1.2.2.2 +++ libelwix/src/net.c 2013/06/10 14:49:48 1.4.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.2.2.2 2013/06/03 11:48:26 misho Exp $ +* $Id: net.c,v 1.4.2.1 2013/06/10 14:49:48 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -136,7 +136,7 @@ e_n2port(sockaddr_t * __restrict addr) const char * e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val) { - char str[INET6_ADDRSTRLEN] = { 0 }; + char *s, str[INET6_ADDRSTRLEN] = { 0 }; const char *ret = NULL; if (!addr || !val) @@ -161,6 +161,13 @@ e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r case AF_LOCAL: ret = addr->sun.sun_path; break; + case AF_LINK: + if (!(s = link_ntoa(&addr->sdl))) { + LOGERR; + return ret; + } else + ret = s; + break; default: elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family); @@ -300,12 +307,18 @@ e_innet(netaddr_t * __restrict net, inaddr_t * __restr 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; + 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 = 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; @@ -316,4 +329,67 @@ e_innet(netaddr_t * __restrict net, inaddr_t * __restr } return !!ret; +} + +/* + * e_getnet() - Get network from string + * + * @net = Network string (format: ) + * return: NULL error or !=NULL network should be e_free() + */ +netaddr_t * +e_getnet(const char *net) +{ + netaddr_t *n; + char *str, *wrk; + struct hostent *host; + + n = e_malloc(sizeof(netaddr_t)); + if (!n) { + LOGERR; + return NULL; + } else + memset(n, 0, sizeof(netaddr_t)); + str = e_strdup(net); + if (!str) { + LOGERR; + e_free(n); + return NULL; + } + wrk = strchr(str, '/'); + if (wrk) + *wrk++ = 0; + + host = gethostbyname2(str, strchr(str, ':') ? AF_INET6 : AF_INET); + if (!host) { + elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno)); + e_free(str); + e_free(n); + return NULL; + } + switch (host->h_addrtype) { + case AF_INET: + n->addr.sin.sin_len = sizeof(struct sockaddr_in); + n->addr.sin.sin_family = host->h_addrtype; + memcpy(&n->addr.sin.sin_addr, host->h_addr, sizeof n->addr.sin.sin_addr); + if (wrk) + n->mask.in.s_addr = E_CIDRMASK(strtol(wrk, NULL, 10)); + else + n->mask.in.s_addr = 0xFFFFFFFF; + break; + case AF_INET6: + n->addr.sin6.sin6_len = sizeof(struct sockaddr_in6); + n->addr.sin6.sin6_family = host->h_addrtype; + memcpy(&n->addr.sin6.sin6_addr, host->h_addr, sizeof n->addr.sin6.sin6_addr); + /* TODO: should support ipv6 mask */ + break; + default: + elwix_SetErr(EINVAL, "Unsupported family #%d", host->h_addrtype); + e_free(str); + e_free(n); + return NULL; + } + + e_free(str); + return n; }