--- libelwix/src/net.c 2014/12/17 01:12:34 1.10.10.7 +++ libelwix/src/net.c 2016/08/02 11:23:31 1.13.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.10.10.7 2014/12/17 01:12:34 misho Exp $ +* $Id: net.c,v 1.13.2.1 2016/08/02 11:23:31 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 - 2014 +Copyright 2004 - 2016 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -46,6 +46,7 @@ SUCH DAMAGE. #include "global.h" +#ifndef __linux__ static char hexlist[] = "0123456789abcdef"; /* @@ -112,6 +113,7 @@ e_link_addr(const char *mac, struct sockaddr_dl * __re link_addr(mac, sdl); return 0; } +#endif /* * e_ether_ntoa() - Convert ethernet address to string @@ -201,7 +203,10 @@ e_n2port(sockaddr_t * __restrict addr) const char * e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val) { - char *s, str[INET6_ADDRSTRLEN] = { 0 }; +#ifndef __linux__ + char *s; +#endif + char str[INET6_ADDRSTRLEN] = { 0 }; const char *ret = NULL; if (!addr || !val) @@ -226,6 +231,7 @@ e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r case AF_LOCAL: ret = addr->sun.sun_path; break; +#ifndef __linux__ case AF_LINK: if (!(s = e_link_ntoa(&addr->sdl))) { LOGERR; @@ -233,6 +239,7 @@ e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r } else ret = s; break; +#endif default: elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family); @@ -249,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 error or >0 network structure length */ -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; @@ -277,29 +284,35 @@ e_gethostbyname(const char *psHost, u_short port, sock switch (addr->sa.sa_family) { case AF_INET: +#ifndef __linux__ addr->sin.sin_len = sizeof(struct sockaddr_in); +#endif 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); +#endif 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); +#endif 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; } /* @@ -329,9 +342,13 @@ e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __re else return memcmp(&a->sin6.sin6_addr, &b->sin6.sin6_addr, sizeof a->sin6.sin6_addr); +#ifndef __linux__ case AF_LINK: return memcmp(&a->sdl.sdl_data, &b->sdl.sdl_data, sizeof a->sdl.sdl_data); +#endif + case AF_UNSPEC: + return memcmp(a, b, sizeof(sockaddr_t)); } return (int) !!(a - b); @@ -431,7 +448,9 @@ e_getnet(const char *net) } switch (host->h_addrtype) { case AF_INET: +#ifndef __linux__ n->addr.sin.sin_len = sizeof(struct sockaddr_in); +#endif 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) @@ -440,7 +459,9 @@ e_getnet(const char *net) n->mask.in.s_addr = 0xFFFFFFFF; break; case AF_INET6: +#ifndef __linux__ n->addr.sin6.sin6_len = sizeof(struct sockaddr_in6); +#endif 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 */ @@ -460,31 +481,70 @@ 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; + struct ifreq req; + int s; + sockaddr_t sa = E_SOCKADDR_INIT; + memset(&req, 0, sizeof req); 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++) + for (p = ifa; p && p->ifa_name; p = p->ifa_next) { +#ifndef __linux__ 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)); + a = e_malloc(sizeof(ether_addr_t)); + if (a) + 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, 0); + if (s == -1) + break; + strlcpy(req.ifr_name, ifname, sizeof req.ifr_name); + if (!ioctl(s, SIOCGIFHWADDR, &req)) { + 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 + } freeifaddrs(ifa); return a; @@ -511,6 +571,7 @@ e_get1stiface(char *szIface, int iflen) return 0; } +#ifndef __linux__ /* * e_getifacebyname() - Get interface and make network structure * @@ -530,7 +591,7 @@ e_getifacebyname(const char *psIface, sockaddr_t * __r 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++) + for (p = ifa; p && p->ifa_name; p = p->ifa_next) 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)); @@ -606,3 +667,4 @@ e_getlinkbyether(const ether_addr_t * __restrict mac, return a; } +#endif