--- libelwix/src/net.c 2013/06/03 22:42:25 1.3.2.2 +++ libelwix/src/net.c 2014/02/10 16:36:08 1.9.6.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.3.2.2 2013/06/03 22:42:25 misho Exp $ +* $Id: net.c,v 1.9.6.1 2014/02/10 16:36:08 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2014 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -46,7 +46,55 @@ 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[i]; + i = 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 @@ -55,13 +103,13 @@ SUCH DAMAGE. * return: NULL error or !=NULL string a */ char * -e_ether_ntoa(const struct e_ether_addr *n, char * __restrict a, int len) +e_ether_ntoa(const ether_addr_t * __restrict n, char * __restrict a, int len) { if (!n || !a) return NULL; memset(a, 0, len); - if (snprintf(a, len, "%02x:%02x:%02x:%02x:%02x:%02x", + if (snprintf(a, len, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", n->ether_addr_octet[0], n->ether_addr_octet[1], n->ether_addr_octet[2], n->ether_addr_octet[3], n->ether_addr_octet[4], n->ether_addr_octet[5]) < 17) @@ -77,26 +125,24 @@ 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 */ -struct e_ether_addr * -e_ether_aton(const char *a, struct e_ether_addr *e) +ether_addr_t * +e_ether_aton(const char *a, ether_addr_t * __restrict e) { int i; - u_int o0, o1, o2, o3, o4, o5; if (!a || !e) return NULL; - i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5); + i = sscanf(a, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", + &e->ether_addr_octet[0], + &e->ether_addr_octet[1], + &e->ether_addr_octet[2], + &e->ether_addr_octet[3], + &e->ether_addr_octet[4], + &e->ether_addr_octet[5]); if (i != 6) return NULL; - e->ether_addr_octet[0] = o0; - e->ether_addr_octet[1] = o1; - e->ether_addr_octet[2] = o2; - e->ether_addr_octet[3] = o3; - e->ether_addr_octet[4] = o4; - e->ether_addr_octet[5] = o5; - return e; } @@ -136,7 +182,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 +207,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); @@ -190,7 +243,7 @@ e_gethostbyname(const char *psHost, u_short port, sock if (*psHost != '/') { /* resolver */ if (!addr->sa.sa_family) - host = gethostbyname(psHost); + host = gethostbyname2(psHost, !strchr(psHost, ':') ? AF_INET : AF_INET6); else host = gethostbyname2(psHost, addr->sa.sa_family); if (!host) { @@ -385,4 +438,38 @@ e_getnet(const char *net) e_free(str); return n; +} + +/* + * e_ether_addr() - Get or set ethernet address from interface name + * + * @ifname = interface name + * @addr = if addr is !=NULL then set this for new address + * return: NULL error or !=NULL get ethernet address + */ +ether_addr_t * +e_ether_addr(const char *ifname, ether_addr_t * __restrict addr) +{ + ether_addr_t *a = NULL; + struct ifaddrs *p, *ifa = NULL; + + if (!ifname) + return NULL; + + a = e_malloc(sizeof(ether_addr_t)); + if (!a) + return NULL; + else + memset(a, 0, sizeof(ether_addr_t)); + + getifaddrs(&ifa); + for (p = ifa; p && p->ifa_name; p++) + if (p->ifa_name && !strcmp(p->ifa_name, ifname) && p->ifa_addr && + p->ifa_addr->sa_family == AF_LINK) { + memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), sizeof(ether_addr_t)); + break; + } + freeifaddrs(ifa); + + return a; }