--- libelwix/src/net.c 2013/05/26 20:03:19 1.1.1.1.6.1 +++ libelwix/src/net.c 2013/06/20 14:54:34 1.5.4.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.1.1.1.6.1 2013/05/26 20:03:19 misho Exp $ +* $Id: net.c,v 1.5.4.1 2013/06/20 14:54:34 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -46,7 +46,52 @@ SUCH DAMAGE. #include "global.h" +static char hexlist[] = "0123456789abcdef"; + /* + * e_link_ntoa() - String ethernet address from link address + * + * @sdl = link address + * return: =NULL error or !=NULL ethernet address, should be e_free() + */ +char * +e_link_ntoa(const struct sockaddr_dl *sdl) +{ + static char obuf[64]; + char *out = obuf; + int i; + u_char *in = (u_char*) LLADDR(sdl); + u_char *inlim = in + sdl->sdl_alen; + int firsttime = 1; + + if (sdl->sdl_nlen) { + memcpy(obuf, sdl->sdl_data, sdl->sdl_nlen); + out += sdl->sdl_nlen; + if (sdl->sdl_alen) + *out++ = '!'; + } + + while (in < inlim) { + if (firsttime) + firsttime ^= firsttime; + else + *out++ = ':'; + i = *in++; + if (i > 0xf) { + out[1] = hexlist[i & 0xf]; + i >>= 4; + } else + out[1] = hexlist[0]; + + out[0] = hexlist[i]; + out += 2; + } + + *out = 0; + return obuf; +} + +/* * e_ether_ntoa() - Convert ethernet address to string * * @n = ethernet address structure, like struct ether_addr @@ -136,7 +181,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 +206,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 = e_link_ntoa(&addr->sdl))) { + LOGERR; + return ret; + } else + ret = s; + break; default: elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family); @@ -280,4 +332,109 @@ 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; +} + +/* + * 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; }