--- libelwix/src/net.c 2013/06/19 00:11:16 1.5 +++ libelwix/src/net.c 2016/05/14 11:31:38 1.12.4.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: net.c,v 1.5 2013/06/19 00:11:16 misho Exp $ +* $Id: net.c,v 1.12.4.1 2016/05/14 11:31:38 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2015 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -46,7 +46,76 @@ SUCH DAMAGE. #include "global.h" +#ifndef __linux__ +static char hexlist[] = "0123456789abcdef"; + /* + * e_link_ntoa() - String ethernet address from link address + * + * @sdl = link address + * return: =NULL error or !=NULL ethernet address, should be e_free() + */ +char * +e_link_ntoa(const struct sockaddr_dl *sdl) +{ + static char obuf[64]; + char *out = obuf; + int i; + u_char *in = (u_char*) LLADDR(sdl); + u_char *inlim = in + sdl->sdl_alen; + int firsttime = 1; + + if (sdl->sdl_nlen) { + memcpy(obuf, sdl->sdl_data, sdl->sdl_nlen); + out += sdl->sdl_nlen; + if (sdl->sdl_alen) + *out++ = '!'; + } + + while (in < inlim) { + if (firsttime) + firsttime ^= firsttime; + else + *out++ = ':'; + + i = *in++; + if (i > 0xf) { + out[1] = hexlist[i & 0xf]; + i >>= 4; + } else { + out[1] = hexlist[i]; + i = 0; + } + + out[0] = hexlist[i]; + out += 2; + } + + *out = 0; + return obuf; +} + +/* + * 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; +} +#endif + +/* * e_ether_ntoa() - Convert ethernet address to string * * @n = ethernet address structure, like struct ether_addr @@ -55,16 +124,16 @@ SUCH DAMAGE. * return: NULL error or !=NULL string a */ char * -e_ether_ntoa(const struct e_ether_addr *n, char * __restrict a, int len) +e_ether_ntoa(const ether_addr_t * __restrict n, char * __restrict a, int len) { if (!n || !a) return NULL; memset(a, 0, len); - if (snprintf(a, len, "%02x:%02x:%02x:%02x:%02x:%02x", - 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) + if (snprintf(a, len, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", + n->octet[0], n->octet[1], + n->octet[2], n->octet[3], + n->octet[4], n->octet[5]) < 17) return NULL; return a; @@ -77,26 +146,24 @@ e_ether_ntoa(const struct e_ether_addr *n, char * __re * @e = ethernet address structure, like struct ether_addr * return: NULL error or !=NULL ethernet address structure */ -struct e_ether_addr * -e_ether_aton(const char *a, struct e_ether_addr *e) +ether_addr_t * +e_ether_aton(const char *a, ether_addr_t * __restrict e) { int i; - u_int o0, o1, o2, o3, o4, o5; if (!a || !e) return NULL; - i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5); + i = sscanf(a, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", + &e->octet[0], + &e->octet[1], + &e->octet[2], + &e->octet[3], + &e->octet[4], + &e->octet[5]); if (i != 6) return NULL; - e->ether_addr_octet[0] = o0; - e->ether_addr_octet[1] = o1; - e->ether_addr_octet[2] = o2; - e->ether_addr_octet[3] = o3; - e->ether_addr_octet[4] = o4; - e->ether_addr_octet[5] = o5; - return e; } @@ -136,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) @@ -161,13 +231,15 @@ 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 = link_ntoa(&addr->sdl))) { + if (!(s = e_link_ntoa(&addr->sdl))) { LOGERR; return ret; } else ret = s; break; +#endif default: elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family); @@ -196,10 +268,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; @@ -215,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); @@ -267,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); @@ -369,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) @@ -378,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 */ @@ -392,4 +475,194 @@ e_getnet(const char *net) e_free(str); return n; +} + +/* + * 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() + */ +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; + + 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++) +#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)); + break; + } +#else + if (p->ifa_name && !strcmp(p->ifa_name, ifname)) { + s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + if (s == -1) { + e_free(a); + a = NULL; + break; + } else + strlcpy(req.ifr_name, ifname, sizeof req.ifr_name); + if (ioctl(s, SIOCGIFHWADDR, &req) == -1) { + e_free(a); + a = NULL; + } else + memcpy(a, req.ifr_addr.sa_data, sizeof(ether_addr_t)); + break; + } +#endif + 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) +{ +#ifndef __linux__ + 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; +#else + elwix_SetErr(ENOSYS, "Unsuported routine on linux"); + return NULL; +#endif +} + +/* + * 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) +{ +#ifndef __linux__ + 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(AIT_GET_STR(&v), &addr->sdl)) + a = NULL; + + AIT_FREE_VAL(&v); + return a; +#else + elwix_SetErr(ENOSYS, "Unsuported routine on linux"); + return NULL; +#endif +} + +/* + * 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) +{ +#ifndef __linux__ + sockaddr_t *a = addr; + + if (!addr) + return NULL; + else + memset(addr, 0, sizeof(sockaddr_t)); + + 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; +#else + elwix_SetErr(ENOSYS, "Unsuported routine on linux"); + return NULL; +#endif }