--- libelwix/src/net.c 2014/11/26 00:34:27 1.10.10.5 +++ libelwix/src/net.c 2014/12/17 01:12:34 1.10.10.7 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.10.10.5 2014/11/26 00:34:27 misho Exp $ +* $Id: net.c,v 1.10.10.7 2014/12/17 01:12:34 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -129,9 +129,9 @@ e_ether_ntoa(const ether_addr_t * __restrict n, char * memset(a, 0, len); 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) + n->octet[0], n->octet[1], + n->octet[2], n->octet[3], + n->octet[4], n->octet[5]) < 17) return NULL; return a; @@ -153,12 +153,12 @@ e_ether_aton(const char *a, ether_addr_t * __restrict return NULL; 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]); + &e->octet[0], + &e->octet[1], + &e->octet[2], + &e->octet[3], + &e->octet[4], + &e->octet[5]); if (i != 6) return NULL; @@ -551,14 +551,58 @@ e_getifacebyname(const char *psIface, sockaddr_t * __r sockaddr_t * e_getlinkbyname(const char *psHost, sockaddr_t * __restrict addr) { - if (!addr) + ait_val_t v; + sockaddr_t *a = addr; + + if (!psHost || !addr) return NULL; else memset(addr, 0, sizeof(sockaddr_t)); + AIT_INIT_VAL2(&v, string); + if (!strchr(psHost, '.')) + AIT_SET_STR(&v, ":"); + AIT_SET_STRCAT(&v, psHost); + addr->sdl.sdl_len = sizeof(struct sockaddr_dl); - if (e_link_addr(psHost, &addr->sdl)) + if (e_link_addr(AIT_GET_STR(&v), &addr->sdl)) + a = NULL; + + AIT_FREE_VAL(&v); + return a; +} + +/* + * e_getlinkbyether() - Get ethernet address and make network structure + * + * @mac = Ethernet address + * @idx = Interface index + * @type = Interface type + * @iface = Interface name + * @addr = Network address structure + * return: NULL error or !=NULL network structure + */ +sockaddr_t * +e_getlinkbyether(const ether_addr_t * __restrict mac, u_short idx, u_char type, + const char *iface, sockaddr_t * __restrict addr) +{ + sockaddr_t *a = addr; + + if (!addr) return NULL; + else + memset(addr, 0, sizeof(sockaddr_t)); - return addr; + addr->sdl.sdl_len = sizeof(struct sockaddr_dl); + addr->sdl.sdl_family = AF_LINK; + addr->sdl.sdl_index = idx; + addr->sdl.sdl_type = type; + if (iface && *iface) { + addr->sdl.sdl_nlen = strlen(iface); + memcpy(addr->sdl.sdl_data, iface, addr->sdl.sdl_nlen); + } + addr->sdl.sdl_alen = sizeof(ether_addr_t); + memcpy(LLADDR(&addr->sdl), mac, addr->sdl.sdl_alen); + + return a; }