File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / net.c
Revision 1.12.4.1: download - view: text, annotated - select for diffs - revision graph
Sat May 14 11:31:38 2016 UTC (8 years, 5 months ago) by misho
Branches: elwix3_9
Diff to: branchpoint 1.12: preferred, unified
initial port for linux!

    1: /*************************************************************************
    2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: net.c,v 1.12.4.1 2016/05/14 11:31:38 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2015
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: #ifndef __linux__
   50: static char hexlist[] = "0123456789abcdef";
   51: 
   52: /*
   53:  * e_link_ntoa() - String ethernet address from link address
   54:  *
   55:  * @sdl = link address
   56:  * return: =NULL error or !=NULL ethernet address, should be e_free()
   57:  */
   58: char *
   59: e_link_ntoa(const struct sockaddr_dl *sdl)
   60: {
   61: 	static char obuf[64];
   62: 	char *out = obuf;
   63: 	int i;
   64: 	u_char *in = (u_char*) LLADDR(sdl);
   65: 	u_char *inlim = in + sdl->sdl_alen;
   66: 	int firsttime = 1;
   67: 
   68: 	if (sdl->sdl_nlen) {
   69: 		memcpy(obuf, sdl->sdl_data, sdl->sdl_nlen);
   70: 		out += sdl->sdl_nlen;
   71: 		if (sdl->sdl_alen)
   72: 			*out++ = '!';
   73: 	}
   74: 
   75: 	while (in < inlim) {
   76: 		if (firsttime)
   77: 			firsttime ^= firsttime;
   78: 		else
   79: 			*out++ = ':';
   80: 
   81: 		i = *in++;
   82: 		if (i > 0xf) {
   83: 			out[1] = hexlist[i & 0xf];
   84: 			i >>= 4;
   85: 		} else {
   86: 			out[1] = hexlist[i];
   87: 			i = 0;
   88: 		}
   89: 
   90: 		out[0] = hexlist[i];
   91: 		out += 2;
   92: 	}
   93: 
   94: 	*out = 0;
   95: 	return obuf;
   96: }
   97: 
   98: /*
   99:  * e_link_addr() - String ethernet address to link address
  100:  *
  101:  * @mac = ethernet address
  102:  * @sdl = link address
  103:  * return: -1 error or 0 ok
  104:  */
  105: int
  106: e_link_addr(const char *mac, struct sockaddr_dl * __restrict sdl)
  107: {
  108: 	if (!mac || !sdl)
  109: 		return -1;
  110: 	if (!sdl->sdl_len)
  111: 		sdl->sdl_len = sizeof(struct sockaddr_dl);
  112: 
  113: 	link_addr(mac, sdl);
  114: 	return 0;
  115: }
  116: #endif
  117: 
  118: /*
  119:  * e_ether_ntoa() - Convert ethernet address to string
  120:  *
  121:  * @n = ethernet address structure, like struct ether_addr
  122:  * @a = string
  123:  * @len = string length
  124:  * return: NULL error or !=NULL string a
  125:  */
  126: char *
  127: e_ether_ntoa(const ether_addr_t * __restrict n, char * __restrict a, int len)
  128: {
  129: 	if (!n || !a)
  130: 		return NULL;
  131: 
  132: 	memset(a, 0, len);
  133: 	if (snprintf(a, len, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 
  134: 			n->octet[0], n->octet[1], 
  135: 			n->octet[2], n->octet[3], 
  136: 			n->octet[4], n->octet[5]) < 17)
  137: 		return NULL;
  138: 
  139: 	return a;
  140: }
  141: 
  142: /*
  143:  * e_ether_aton() - Convert string to ethernet address
  144:  *
  145:  * @a = string
  146:  * @e = ethernet address structure, like struct ether_addr
  147:  * return: NULL error or !=NULL ethernet address structure
  148:  */
  149: ether_addr_t *
  150: e_ether_aton(const char *a, ether_addr_t * __restrict e)
  151: {                       
  152: 	int i;
  153: 
  154: 	if (!a || !e)
  155: 		return NULL;
  156: 
  157: 	i = sscanf(a, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 
  158: 			&e->octet[0], 
  159: 			&e->octet[1], 
  160: 			&e->octet[2], 
  161: 			&e->octet[3], 
  162: 			&e->octet[4], 
  163: 			&e->octet[5]);
  164: 	if (i != 6)
  165: 		return NULL;
  166: 
  167: 	return e;
  168: }
  169: 
  170: /*
  171:  * e_n2port() - Extract port from network structure
  172:  *
  173:  * @addr = Address
  174:  * return: 0 not supported family type or port number
  175:  */
  176: u_short
  177: e_n2port(sockaddr_t * __restrict addr)
  178: {
  179: 	u_short port = 0;
  180: 
  181: 	if (!addr)
  182: 		return port;
  183: 
  184: 	switch (addr->sa.sa_family) {
  185: 		case AF_INET:
  186: 			return ntohs(addr->sin.sin_port);
  187: 		case AF_INET6:
  188: 			return ntohs(addr->sin6.sin6_port);
  189: 		default:
  190: 			break;
  191: 	}
  192: 
  193: 	return port;
  194: }
  195: 
  196: /*
  197:  * e_n2addr() - Extract address from network structure
  198:  *
  199:  * @addr = Address
  200:  * @val = Value for store string address
  201:  * return: NULL error or !=NULL string address from val
  202:  */
  203: const char *
  204: e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val)
  205: {
  206: #ifndef __linux__
  207: 	char *s;
  208: #endif
  209: 	char str[INET6_ADDRSTRLEN] = { 0 };
  210: 	const char *ret = NULL;
  211: 
  212: 	if (!addr || !val)
  213: 		return ret;
  214: 
  215: 	AIT_INIT_VAL(val);
  216: 	switch (addr->sa.sa_family) {
  217: 		case AF_INET:
  218: 			if (!inet_ntop(AF_INET, &addr->sin.sin_addr, str, INET_ADDRSTRLEN)) {
  219: 				LOGERR;
  220: 				return ret;
  221: 			} else
  222: 				ret = str;
  223: 			break;
  224: 		case AF_INET6:
  225: 			if (!inet_ntop(AF_INET6, &addr->sin6.sin6_addr, str, INET6_ADDRSTRLEN)) {
  226: 				LOGERR;
  227: 				return ret;
  228: 			} else
  229: 				ret = str;
  230: 			break;
  231: 		case AF_LOCAL:
  232: 			ret = addr->sun.sun_path;
  233: 			break;
  234: #ifndef __linux__
  235: 		case AF_LINK:
  236: 			if (!(s = e_link_ntoa(&addr->sdl))) {
  237: 				LOGERR;
  238: 				return ret;
  239: 			} else
  240: 				ret = s;
  241: 			break;
  242: #endif
  243: 		default:
  244: 			elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", 
  245: 					addr->sa.sa_family);
  246: 			return ret;
  247: 	}
  248: 
  249: 	AIT_SET_STR(val, ret);
  250: 	return (const char*) AIT_GET_STR(val);
  251: }
  252: 
  253: /*
  254:  * e_gethostbyname() - Get host and port and make network structure
  255:  *
  256:  * @psHost = Hostname
  257:  * @port = Port
  258:  * @addr = Network address structure
  259:  * return: NULL error or !=NULL network structure
  260:  */
  261: sockaddr_t *
  262: e_gethostbyname(const char *psHost, u_short port, sockaddr_t * __restrict addr)
  263: {
  264: 	struct hostent *host = NULL;
  265: 
  266: 	if (!psHost || !addr)
  267: 		return NULL;
  268: 
  269: 	if (*psHost != '/') {
  270: 		/* resolver */
  271: 		host = gethostbyname2(psHost, !strchr(psHost, ':') ? AF_INET : AF_INET6);
  272: 		if (!host) {
  273: 			elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno));
  274: 			return NULL;
  275: 		} else {
  276: 			memset(addr, 0, sizeof(sockaddr_t));
  277: 			addr->sa.sa_family = host->h_addrtype;
  278: 		}
  279: 	} else {
  280: 		memset(addr, 0, sizeof(sockaddr_t));
  281: 		addr->sa.sa_family = AF_LOCAL;
  282: 	}
  283: 		
  284: 
  285: 	switch (addr->sa.sa_family) {
  286: 		case AF_INET:
  287: #ifndef __linux__
  288: 			addr->sin.sin_len = sizeof(struct sockaddr_in);
  289: #endif
  290: 			addr->sin.sin_family = AF_INET;
  291: 			addr->sin.sin_port = htons(port);
  292: 			memcpy(&addr->sin.sin_addr, host->h_addr, sizeof addr->sin.sin_addr);
  293: 			return addr;
  294: 		case AF_INET6:
  295: #ifndef __linux__
  296: 			addr->sin6.sin6_len = sizeof(struct sockaddr_in6);
  297: #endif
  298: 			addr->sin6.sin6_family = AF_INET6;
  299: 			addr->sin6.sin6_port = htons(port);
  300: 			memcpy(&addr->sin6.sin6_addr, host->h_addr, sizeof addr->sin6.sin6_addr);
  301: 			return addr;
  302: 		case AF_LOCAL:
  303: #ifndef __linux__
  304: 			addr->sun.sun_len = sizeof(struct sockaddr_un);
  305: #endif
  306: 			addr->sun.sun_family = AF_LOCAL;
  307: 			memset(addr->sun.sun_path, 0, sizeof addr->sun.sun_path);
  308: 			snprintf(addr->sun.sun_path, sizeof addr->sun.sun_path, "%s-%hu", psHost, port);
  309: 			return addr;
  310: 		default:
  311: 			elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family);
  312: 			break;
  313: 	}
  314: 
  315: 	return NULL;
  316: }
  317: 
  318: /*
  319:  * e_addrcmp() - Compare network addresses
  320:  *
  321:  * @a = 1st address
  322:  * @b = 2nd address
  323:  * @p = compare and ports, if family is AF_INET or AF_INET6
  324:  * return: 0 is equal or !=0 is different
  325:  */
  326: int
  327: e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __restrict b, int p)
  328: {
  329: 	if (a && b && a->sa.sa_family == b->sa.sa_family)
  330: 		switch (a->sa.sa_family) {
  331: 			case AF_LOCAL:
  332: 				return strcmp(a->sun.sun_path, b->sun.sun_path);
  333: 			case AF_INET:
  334: 				if (p && (a->sin.sin_port - b->sin.sin_port))
  335: 					return (int) !!(a->sin.sin_port - b->sin.sin_port);
  336: 				else
  337: 					return memcmp(&a->sin.sin_addr, &b->sin.sin_addr, 
  338: 							sizeof a->sin.sin_addr);
  339: 			case AF_INET6:
  340: 				if (p && (a->sin6.sin6_port - b->sin6.sin6_port))
  341: 					return (int) !!(a->sin6.sin6_port - b->sin6.sin6_port);
  342: 				else
  343: 					return memcmp(&a->sin6.sin6_addr, &b->sin6.sin6_addr, 
  344: 							sizeof a->sin6.sin6_addr);
  345: #ifndef __linux__
  346: 			case AF_LINK:
  347: 				return memcmp(&a->sdl.sdl_data, &b->sdl.sdl_data, 
  348: 						sizeof a->sdl.sdl_data);
  349: #endif
  350: 			case AF_UNSPEC:
  351: 				return memcmp(a, b, sizeof(sockaddr_t));
  352: 		}
  353: 
  354: 	return (int) !!(a - b);
  355: }
  356: 
  357: /*
  358:  * e_usleep() - usleep() replacement for ELWIX
  359:  *
  360:  * @usec = microseconds for sleep
  361:  * return: -1 interrupted by signal or 0 ok
  362:  */
  363: int
  364: e_usleep(u_int usec)
  365: {
  366: 	struct timeval tv = { (time_t) (usec / 1000000), (long) (usec % 1000000) };
  367: 
  368: 	return select(0, NULL, NULL, NULL, &tv);
  369: }
  370: 
  371: /*
  372:  * e_innet() - Test address match in network
  373:  *
  374:  * @net = network
  375:  * @addr = address
  376:  * return: -1 error, 0 match or 1 not match
  377:  */
  378: int
  379: e_innet(netaddr_t * __restrict net, inaddr_t * __restrict addr)
  380: {
  381: 	register int i;
  382: 	int ret = 0;
  383: 
  384: 	if (!net || !addr)
  385: 		return -1;
  386: 
  387: 	switch (net->addr.sa.sa_family) {
  388: 		case AF_INET:
  389: 			for (i = 0; i < sizeof(struct in_addr); i++) {
  390: 				ret = ((caddr_t) &net->addr.sin.sin_addr.s_addr)[i] & 
  391: 					net->mask.in.s4_addr[i];
  392: 				ret -= addr->in.s4_addr[i] & net->mask.in.s4_addr[i];
  393: 				if (ret)
  394: 					break;
  395: 			}
  396: 			break;
  397: 		case AF_INET6:
  398: 			for (i = 0; i < sizeof(struct in6_addr); i++) {
  399: 				ret = net->addr.sin6.sin6_addr.s6_addr[i] & 
  400: 					net->mask.in6.s6_addr[i];
  401: 				ret -= addr->in6.s6_addr[i] & net->mask.in6.s6_addr[i];
  402: 				if (ret)
  403: 					break;
  404: 			}
  405: 			break;
  406: 		default:
  407: 			return -1;
  408: 	}
  409: 
  410: 	return !!ret;
  411: }
  412: 
  413: /*
  414:  * e_getnet() - Get network from string
  415:  *
  416:  * @net = Network string (format: <net[/cidr]>)
  417:  * return: NULL error or !=NULL network should be e_free()
  418:  */
  419: netaddr_t *
  420: e_getnet(const char *net)
  421: {
  422: 	netaddr_t *n;
  423: 	char *str, *wrk;
  424: 	struct hostent *host;
  425: 
  426: 	n = e_malloc(sizeof(netaddr_t));
  427: 	if (!n) {
  428: 		LOGERR;
  429: 		return NULL;
  430: 	} else
  431: 		memset(n, 0, sizeof(netaddr_t));
  432: 	str = e_strdup(net);
  433: 	if (!str) {
  434: 		LOGERR;
  435: 		e_free(n);
  436: 		return NULL;
  437: 	}
  438: 	wrk = strchr(str, '/');
  439: 	if (wrk)
  440: 		*wrk++ = 0;
  441: 
  442: 	host = gethostbyname2(str, strchr(str, ':') ? AF_INET6 : AF_INET);
  443: 	if (!host) {
  444: 		elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno));
  445: 		e_free(str);
  446: 		e_free(n);
  447: 		return NULL;
  448: 	}
  449: 	switch (host->h_addrtype) {
  450: 		case AF_INET:
  451: #ifndef __linux__
  452: 			n->addr.sin.sin_len = sizeof(struct sockaddr_in);
  453: #endif
  454: 			n->addr.sin.sin_family = host->h_addrtype;
  455: 			memcpy(&n->addr.sin.sin_addr, host->h_addr, sizeof n->addr.sin.sin_addr);
  456: 			if (wrk)
  457: 				n->mask.in.s_addr = E_CIDRMASK(strtol(wrk, NULL, 10));
  458: 			else
  459: 				n->mask.in.s_addr = 0xFFFFFFFF;
  460: 			break;
  461: 		case AF_INET6:
  462: #ifndef __linux__
  463: 			n->addr.sin6.sin6_len = sizeof(struct sockaddr_in6);
  464: #endif
  465: 			n->addr.sin6.sin6_family = host->h_addrtype;
  466: 			memcpy(&n->addr.sin6.sin6_addr, host->h_addr, sizeof n->addr.sin6.sin6_addr);
  467: 			/* TODO: should support ipv6 mask */
  468: 			break;
  469: 		default:
  470: 			elwix_SetErr(EINVAL, "Unsupported family #%d", host->h_addrtype);
  471: 			e_free(str);
  472: 			e_free(n);
  473: 			return NULL;
  474: 	}
  475: 
  476: 	e_free(str);
  477: 	return n;
  478: }
  479: 
  480: /*
  481:  * e_ether_addr() - Get or set ethernet address from interface name
  482:  *
  483:  * @ifname = interface name
  484:  * @addr = if addr is !=NULL then set this for new address
  485:  * return: NULL error or !=NULL get ethernet address should be e_free()
  486:  */
  487: ether_addr_t *
  488: e_ether_addr(const char *ifname, ether_addr_t * __restrict addr)
  489: {
  490: 	ether_addr_t *a = NULL;
  491: 	struct ifaddrs *p, *ifa = NULL;
  492: #ifdef __linux__
  493: 	int s;
  494: 	struct ifreq req;
  495: 
  496: 	memset(&req, 0, sizeof req);
  497: #endif
  498: 
  499: 	if (!ifname)
  500: 		return NULL;
  501: 
  502: 	a = e_malloc(sizeof(ether_addr_t));
  503: 	if (!a)
  504: 		return NULL;
  505: 	else
  506: 		memset(a, 0, sizeof(ether_addr_t));
  507: 
  508: 	getifaddrs(&ifa);
  509: 	for (p = ifa; p && p->ifa_name; p++)
  510: #ifndef __linux__
  511: 		if (p->ifa_name && !strcmp(p->ifa_name, ifname) && p->ifa_addr && 
  512: 				p->ifa_addr->sa_family == AF_LINK) {
  513: 			memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), sizeof(ether_addr_t));
  514: 			break;
  515: 		}
  516: #else
  517: 		if (p->ifa_name && !strcmp(p->ifa_name, ifname)) {
  518: 			s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  519: 			if (s == -1) {
  520: 				e_free(a);
  521: 				a = NULL;
  522: 				break;
  523: 			} else
  524: 				strlcpy(req.ifr_name, ifname, sizeof req.ifr_name);
  525: 			if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
  526: 				e_free(a);
  527: 				a = NULL;
  528: 			} else
  529: 				memcpy(a, req.ifr_addr.sa_data, sizeof(ether_addr_t));
  530: 			break;
  531: 		}
  532: #endif
  533: 	freeifaddrs(ifa);
  534: 
  535: 	return a;
  536: }
  537: 
  538: /*
  539:  * e_get1stiface() - Get first interface of host
  540:  *
  541:  * @szIface = interface string buffer
  542:  * @iflen = size of interface buffer
  543:  * return: -1 error or 0 ok
  544:  */
  545: int
  546: e_get1stiface(char *szIface, int iflen)
  547: {
  548: 	struct ifaddrs *ifa;
  549: 
  550: 	if (!szIface || !iflen)
  551: 		return -1;
  552: 
  553: 	getifaddrs(&ifa);
  554: 	strlcpy(szIface, ifa->ifa_name, iflen);
  555: 	freeifaddrs(ifa);
  556: 	return 0;
  557: }
  558: 
  559: /*
  560:  * e_getifacebyname() - Get interface and make network structure
  561:  *
  562:  * @psIface = Interface, if =NULL first interface
  563:  * @addr = Network address structure
  564:  * return: NULL error or !=NULL network structure
  565:  */
  566: sockaddr_t *
  567: e_getifacebyname(const char *psIface, sockaddr_t * __restrict addr)
  568: {
  569: #ifndef __linux__
  570: 	char szIface[64] = { [0 ... 63] = 0 };
  571: 	struct ifaddrs *p, *ifa = NULL;
  572: 
  573: 	if (!addr)
  574: 		return NULL;
  575: 
  576: 	memset(addr, 0, sizeof(sockaddr_t));
  577: 	getifaddrs(&ifa);
  578: 	strlcpy(szIface, psIface ? psIface : ifa->ifa_name, sizeof szIface);
  579: 	for (p = ifa; p && p->ifa_name; p++)
  580: 		if (p->ifa_name && !strcmp(p->ifa_name, szIface) && p->ifa_addr && 
  581: 				p->ifa_addr->sa_family == AF_LINK) {
  582: 			memcpy(&addr->sdl, p->ifa_addr, sizeof(struct sockaddr_dl));
  583: 			break;
  584: 		}
  585: 	freeifaddrs(ifa);
  586: 
  587: 	return addr;
  588: #else
  589: 	elwix_SetErr(ENOSYS, "Unsuported routine on linux");
  590: 	return NULL;
  591: #endif
  592: }
  593: 
  594: /*
  595:  * e_getlinkbyname() - Get host ethernet address and make network structure
  596:  *
  597:  * @psHost = Host ethernet address
  598:  * @addr = Network address structure
  599:  * return: NULL error or !=NULL network structure
  600:  */
  601: sockaddr_t *
  602: e_getlinkbyname(const char *psHost, sockaddr_t * __restrict addr)
  603: {
  604: #ifndef __linux__
  605: 	ait_val_t v;
  606: 	sockaddr_t *a = addr;
  607: 
  608: 	if (!psHost || !addr)
  609: 		return NULL;
  610: 	else
  611: 		memset(addr, 0, sizeof(sockaddr_t));
  612: 
  613: 	AIT_INIT_VAL2(&v, string);
  614: 	if (!strchr(psHost, '.'))
  615: 		AIT_SET_STR(&v, ":");
  616: 	AIT_SET_STRCAT(&v, psHost);
  617: 
  618: 	addr->sdl.sdl_len = sizeof(struct sockaddr_dl);
  619: 	if (e_link_addr(AIT_GET_STR(&v), &addr->sdl))
  620: 		a = NULL;
  621: 
  622: 	AIT_FREE_VAL(&v);
  623: 	return a;
  624: #else
  625: 	elwix_SetErr(ENOSYS, "Unsuported routine on linux");
  626: 	return NULL;
  627: #endif
  628: }
  629: 
  630: /*
  631:  * e_getlinkbyether() - Get ethernet address and make network structure
  632:  *
  633:  * @mac = Ethernet address
  634:  * @idx = Interface index
  635:  * @type = Interface type
  636:  * @iface = Interface name
  637:  * @addr = Network address structure
  638:  * return: NULL error or !=NULL network structure
  639:  */
  640: sockaddr_t *
  641: e_getlinkbyether(const ether_addr_t * __restrict mac, u_short idx, u_char type, 
  642: 		const char *iface, sockaddr_t * __restrict addr)
  643: {
  644: #ifndef __linux__
  645: 	sockaddr_t *a = addr;
  646: 
  647: 	if (!addr)
  648: 		return NULL;
  649: 	else
  650: 		memset(addr, 0, sizeof(sockaddr_t));
  651: 
  652: 	addr->sdl.sdl_len = sizeof(struct sockaddr_dl);
  653: 	addr->sdl.sdl_family = AF_LINK;
  654: 	addr->sdl.sdl_index = idx;
  655: 	addr->sdl.sdl_type = type;
  656: 	if (iface && *iface) {
  657: 		addr->sdl.sdl_nlen = strlen(iface);
  658: 		memcpy(addr->sdl.sdl_data, iface, addr->sdl.sdl_nlen);
  659: 	}
  660: 	addr->sdl.sdl_alen = sizeof(ether_addr_t);
  661: 	memcpy(LLADDR(&addr->sdl), mac, addr->sdl.sdl_alen);
  662: 
  663: 	return a;
  664: #else
  665: 	elwix_SetErr(ENOSYS, "Unsuported routine on linux");
  666: 	return NULL;
  667: #endif
  668: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>