--- libelwix/src/net.c 2014/01/29 14:11:45 1.8.12.1 +++ libelwix/src/net.c 2014/11/26 00:30:37 1.10.10.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.8.12.1 2014/01/29 14:11:45 misho Exp $ +* $Id: net.c,v 1.10.10.4 2014/11/26 00:30:37 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -95,6 +95,25 @@ e_link_ntoa(const struct sockaddr_dl *sdl) } /* + * e_link_addr() - String ethernet address to link address + * + * @mac = ethernet address + * @sdl = link address + * return: -1 error or 0 ok + */ +int +e_link_addr(const char *mac, struct sockaddr_dl * __restrict sdl) +{ + if (!mac || !sdl) + return -1; + if (!sdl->sdl_len) + sdl->sdl_len = sizeof(struct sockaddr_dl); + + link_addr(mac, sdl); + return 0; +} + +/* * e_ether_ntoa() - Convert ethernet address to string * * @n = ethernet address structure, like struct ether_addr @@ -242,10 +261,7 @@ e_gethostbyname(const char *psHost, u_short port, sock if (*psHost != '/') { /* resolver */ - if (!addr->sa.sa_family) - host = gethostbyname(psHost); - else - host = gethostbyname2(psHost, addr->sa.sa_family); + host = gethostbyname2(psHost, !strchr(psHost, ':') ? AF_INET : AF_INET6); if (!host) { elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno)); return NULL; @@ -445,7 +461,7 @@ e_getnet(const char *net) * * @ifname = interface name * @addr = if addr is !=NULL then set this for new address - * return: NULL error or !=NULL get ethernet address + * return: NULL error or !=NULL get ethernet address should be e_free() */ ether_addr_t * e_ether_addr(const char *ifname, ether_addr_t * __restrict addr) @@ -472,4 +488,76 @@ e_ether_addr(const char *ifname, ether_addr_t * __rest freeifaddrs(ifa); return a; +} + +/* + * e_get1stiface() - Get first interface of host + * + * @szIface = interface string buffer + * @iflen = size of interface buffer + * return: -1 error or 0 ok + */ +int +e_get1stiface(char *szIface, int iflen) +{ + struct ifaddrs *ifa; + + if (!szIface || !iflen) + return -1; + + getifaddrs(&ifa); + strlcpy(szIface, ifa->ifa_name, iflen); + freeifaddrs(ifa); + return 0; +} + +/* + * e_getifacebyname() - Get interface and make network structure + * + * @psIface = Interface, if =NULL first interface + * @addr = Network address structure + * return: NULL error or !=NULL network structure + */ +sockaddr_t * +e_getifacebyname(const char *psIface, sockaddr_t * __restrict addr) +{ + char szIface[64] = { [0 ... 63] = 0 }; + struct ifaddrs *p, *ifa = NULL; + + if (!addr) + return NULL; + + memset(addr, 0, sizeof(sockaddr_t)); + getifaddrs(&ifa); + strlcpy(szIface, psIface ? psIface : ifa->ifa_name, sizeof szIface); + for (p = ifa; p && p->ifa_name; p++) + if (p->ifa_name && !strcmp(p->ifa_name, szIface) && p->ifa_addr && + p->ifa_addr->sa_family == AF_LINK) { + memcpy(&addr->sdl, p->ifa_addr, sizeof(struct sockaddr_dl)); + break; + } + freeifaddrs(ifa); + + return addr; +} + +/* + * e_getlinkbyname() - Get host ethernet address and make network structure + * + * @psHost = Host ethernet address + * @addr = Network address structure + * return: NULL error or !=NULL network structure + */ +sockaddr_t * +e_getlinkbyname(const char *psHost, sockaddr_t * __restrict addr) +{ + if (!addr) + return NULL; + else + memset(addr, 0, sizeof(sockaddr_t)); + + addr->sdl.sdl_len = sizeof(struct sockaddr_dl); + e_link_addr(psHost, &addr->sdl); + + return addr; }