--- libelwix/src/net.c 2015/06/25 17:53:50 1.12 +++ libelwix/src/net.c 2016/05/14 13:26:04 1.12.4.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.12 2015/06/25 17:53:50 misho Exp $ +* $Id: net.c,v 1.12.4.3 2016/05/14 13:26:04 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 - 2015 +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); @@ -277,19 +284,25 @@ 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; 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; 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); @@ -329,9 +342,11 @@ 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)); } @@ -433,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) @@ -442,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 */ @@ -470,23 +489,41 @@ e_ether_addr(const char *ifname, ether_addr_t * __rest { ether_addr_t *a = NULL; struct ifaddrs *p, *ifa = NULL; +#ifdef __linux__ + int s; + struct ifreq req; + memset(&req, 0, sizeof req); +#endif + 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)); break; } +#else + if (p->ifa_name && !strcmp(p->ifa_name, ifname)) { + s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + 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)); + } + break; + } +#endif + } freeifaddrs(ifa); return a; @@ -513,6 +550,7 @@ e_get1stiface(char *szIface, int iflen) return 0; } +#ifndef __linux__ /* * e_getifacebyname() - Get interface and make network structure * @@ -532,7 +570,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)); @@ -608,3 +646,4 @@ e_getlinkbyether(const ether_addr_t * __restrict mac, return a; } +#endif