--- libelwix/src/net.c 2016/05/14 13:26:04 1.12.4.3 +++ libelwix/src/net.c 2016/08/02 12:03:56 1.16 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.12.4.3 2016/05/14 13:26:04 misho Exp $ +* $Id: net.c,v 1.16 2016/08/02 12:03:56 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -256,22 +256,22 @@ e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r * @psHost = Hostname * @port = Port * @addr = Network address structure - * return: NULL error or !=NULL network structure + * return: 0 is error or >0 length of network structure */ -sockaddr_t * +socklen_t e_gethostbyname(const char *psHost, u_short port, sockaddr_t * __restrict addr) { struct hostent *host = NULL; if (!psHost || !addr) - return NULL; + return 0; if (*psHost != '/') { /* resolver */ host = gethostbyname2(psHost, !strchr(psHost, ':') ? AF_INET : AF_INET6); if (!host) { elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno)); - return NULL; + return 0; } else { memset(addr, 0, sizeof(sockaddr_t)); addr->sa.sa_family = host->h_addrtype; @@ -290,7 +290,7 @@ e_gethostbyname(const char *psHost, u_short port, sock addr->sin.sin_family = AF_INET; addr->sin.sin_port = htons(port); memcpy(&addr->sin.sin_addr, host->h_addr, sizeof addr->sin.sin_addr); - return addr; + return sizeof addr->sin; case AF_INET6: #ifndef __linux__ addr->sin6.sin6_len = sizeof(struct sockaddr_in6); @@ -298,7 +298,7 @@ e_gethostbyname(const char *psHost, u_short port, sock addr->sin6.sin6_family = AF_INET6; addr->sin6.sin6_port = htons(port); memcpy(&addr->sin6.sin6_addr, host->h_addr, sizeof addr->sin6.sin6_addr); - return addr; + return sizeof addr->sin6; case AF_LOCAL: #ifndef __linux__ addr->sun.sun_len = sizeof(struct sockaddr_un); @@ -306,16 +306,44 @@ e_gethostbyname(const char *psHost, u_short port, sock addr->sun.sun_family = AF_LOCAL; memset(addr->sun.sun_path, 0, sizeof addr->sun.sun_path); snprintf(addr->sun.sun_path, sizeof addr->sun.sun_path, "%s-%hu", psHost, port); - return addr; + return sizeof addr->sun; default: elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family); break; } - return NULL; + return 0; } /* + * e_addrlen() - Get address length from network structure + * + * @addr = address + * return: 0 is error or >0 length of network structure + */ +socklen_t +e_addrlen(const sockaddr_t *addr) +{ + if (!addr) + return 0; + + switch (addr->sa.sa_family) { + case AF_INET: + return sizeof addr->sin; + case AF_INET6: + return sizeof addr->sin6; + case AF_LOCAL: + return sizeof addr->sun; +#ifndef __linux__ + case AF_LINK: + return sizeof addr->sdl; +#endif + } + + return E_SOCKADDR_MAX; +} + +/* * e_addrcmp() - Compare network addresses * * @a = 1st address @@ -481,21 +509,19 @@ e_getnet(const char *net) * 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 should be e_free() + * @addr = if addr is !=NULL then set new ethernet address + * return: NULL error or !=NULL get current ethernet address should be e_free() */ ether_addr_t * e_ether_addr(const char *ifname, ether_addr_t * __restrict addr) { ether_addr_t *a = NULL; struct ifaddrs *p, *ifa = NULL; -#ifdef __linux__ - int s; struct ifreq req; + int s; + sockaddr_t sa = E_SOCKADDR_INIT; memset(&req, 0, sizeof req); -#endif - if (!ifname) return NULL; @@ -506,12 +532,24 @@ e_ether_addr(const char *ifname, ether_addr_t * __rest p->ifa_addr->sa_family == AF_LINK) { a = e_malloc(sizeof(ether_addr_t)); if (a) - memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), sizeof(ether_addr_t)); + memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), + sizeof(ether_addr_t)); + + /* should set mac address */ + if (addr && (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) > 0) { + strlcpy(req.ifr_name, ifname, sizeof req.ifr_name); + sa.sa.sa_family = AF_LINK; + sa.sa.sa_len = sizeof(ether_addr_t); + memcpy(sa.sa.sa_data, addr, sizeof(ether_addr_t)); + req.ifr_ifru.ifru_addr = sa.sa; + ioctl(s, SIOCSIFLLADDR, &req); + close(s); + } break; } #else if (p->ifa_name && !strcmp(p->ifa_name, ifname)) { - s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + s = socket(AF_INET, SOCK_DGRAM, 0); if (s == -1) break; strlcpy(req.ifr_name, ifname, sizeof req.ifr_name); @@ -519,7 +557,18 @@ e_ether_addr(const char *ifname, ether_addr_t * __rest a = e_malloc(sizeof(ether_addr_t)); if (a) memcpy(a, req.ifr_addr.sa_data, sizeof(ether_addr_t)); + + /* should set mac address */ + if (addr) { + memset(&req, 0, sizeof req); + strlcpy(req.ifr_name, ifname, sizeof req.ifr_name); + sa.sa.sa_family = ARPHRD_ETHER; + memcpy(sa.sa.sa_data, addr, sizeof(ether_addr_t)); + req.ifr_hwaddr = sa.sa; + ioctl(s, SIOCSIFHWADDR, &req); + } } + close(s); break; } #endif