File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / net.c
Revision 1.22: download - view: text, annotated - select for diffs - revision graph
Mon Sep 26 19:39:23 2022 UTC (2 years, 1 month ago) by misho
Branches: MAIN
CVS tags: elwix6_6, elwix6_5, elwix6_4, elwix6_3, elwix6_2, elwix6_1, elwix5_9, elwix5_8, elwix5_7, elwix5_6, elwix5_12, elwix5_11, elwix5_10, HEAD, ELWIX6_5, ELWIX6_4, ELWIX6_2, ELWIX6_1, ELWIX6_0, ELWIX5_9, ELWIX5_8, ELWIX5_7, ELWIX5_6, ELWIX5_5, ELWIX5_11, ELWIX5_10
Version 5.5
 - Debian port

/*************************************************************************
* (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
*  by Michael Pounov <misho@elwix.org>
*
* $Author: misho $
* $Id: net.c,v 1.22 2022/09/26 19:39:23 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2022
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"


#ifndef __linux__
static char hexlist[] = "0123456789abcdef";

#ifndef HAVE_LINK_ADDR

/* States*/
#define NAMING	0
#define GOTONE	1
#define GOTTWO	2
#define RESET	3
/* Inputs */
#define	DIGIT	(4*0)
#define	END	(4*1)
#define DELIM	(4*2)
#define LETTER	(4*3)

void
link_addr(const char *addr, struct sockaddr_dl *sdl)
{
	char *cp = sdl->sdl_data;
	char *cplim = sdl->sdl_len + (char *)sdl;
	int byte = 0, state = NAMING, new = 0;

	bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1);
	sdl->sdl_family = AF_LINK;
	do {
		state &= ~LETTER;
		if ((*addr >= '0') && (*addr <= '9')) {
			new = *addr - '0';
		} else if ((*addr >= 'a') && (*addr <= 'f')) {
			new = *addr - 'a' + 10;
		} else if ((*addr >= 'A') && (*addr <= 'F')) {
			new = *addr - 'A' + 10;
		} else if (*addr == 0) {
			state |= END;
		} else if (state == NAMING &&
			   (((*addr >= 'A') && (*addr <= 'Z')) ||
			   ((*addr >= 'a') && (*addr <= 'z'))))
			state |= LETTER;
		else
			state |= DELIM;
		addr++;
		switch (state /* | INPUT */) {
		case NAMING | DIGIT:
		case NAMING | LETTER:
			*cp++ = addr[-1];
			continue;
		case NAMING | DELIM:
			state = RESET;
			sdl->sdl_nlen = cp - sdl->sdl_data;
			continue;
		case GOTTWO | DIGIT:
			*cp++ = byte;
			/* FALLTHROUGH */
		case RESET | DIGIT:
			state = GOTONE;
			byte = new;
			continue;
		case GOTONE | DIGIT:
			state = GOTTWO;
			byte = new + (byte << 4);
			continue;
		default: /* | DELIM */
			state = RESET;
			*cp++ = byte;
			byte = 0;
			continue;
		case GOTONE | END:
		case GOTTWO | END:
			*cp++ = byte;
			/* FALLTHROUGH */
		case RESET | END:
			break;
		}
		break;
	} while (cp < cplim);
	sdl->sdl_alen = cp - LLADDR(sdl);
	new = cp - (char *)sdl;
	if (new > sizeof(*sdl))
		sdl->sdl_len = new;
	return;
}
#endif


/*
 * 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
 * @a = string
 * @len = string length
 * return: NULL error or !=NULL string a
 */
char *
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, "%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;
}

/*
 * e_ether_aton() - Convert string to ethernet address
 *
 * @a = string
 * @e = ethernet address structure, like struct ether_addr
 * return: NULL error or !=NULL ethernet address structure
 */
ether_addr_t *
e_ether_aton(const char *a, ether_addr_t * __restrict e)
{                       
	int i;

	if (!a || !e)
		return NULL;

	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;

	return e;
}

/*
 * e_n2port() - Extract port from network structure
 *
 * @addr = Address
 * return: 0 not supported family type or port number
 */
u_short
e_n2port(sockaddr_t * __restrict addr)
{
	u_short port = 0;

	if (!addr)
		return port;

	switch (addr->sa.sa_family) {
		case AF_INET:
			return ntohs(addr->sin.sin_port);
		case AF_INET6:
			return ntohs(addr->sin6.sin6_port);
		default:
			break;
	}

	return port;
}

/*
 * e_n2addr() - Extract address from network structure
 *
 * @addr = Address
 * @val = Value for store string address
 * return: NULL error or !=NULL string address from val
 */
const char *
e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val)
{
#ifndef __linux__
	char *s;
#endif
	char str[INET6_ADDRSTRLEN] = { 0 };
	const char *ret = NULL;

	if (!addr || !val)
		return ret;

	AIT_INIT_VAL(val);
	switch (addr->sa.sa_family) {
		case AF_INET:
			if (!inet_ntop(AF_INET, &addr->sin.sin_addr, str, INET_ADDRSTRLEN)) {
				LOGERR;
				return ret;
			} else
				ret = str;
			break;
		case AF_INET6:
			if (!inet_ntop(AF_INET6, &addr->sin6.sin6_addr, str, INET6_ADDRSTRLEN)) {
				LOGERR;
				return ret;
			} else
				ret = str;
			break;
		case AF_LOCAL:
			ret = addr->sun.sun_path;
			break;
#ifndef __linux__
		case AF_LINK:
			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);
			return ret;
	}

	AIT_SET_STR(val, ret);
	return (const char*) AIT_GET_STR(val);
}

/*
 * e_gethostbyname() - Get host and port and make network structure
 *
 * @psHost = Hostname
 * @port = Port
 * @addr = Network address structure
 * return: 0 is error or >0 length of network structure
 */
socklen_t 
e_gethostbyname(const char *psHost, u_short port, sockaddr_t * __restrict addr)
{
	struct hostent *host = NULL;

	if (!psHost || !addr)
		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 0;
		} else {
			memset(addr, 0, sizeof(sockaddr_t));
			addr->sa.sa_family = host->h_addrtype;
		}
	} else {
		memset(addr, 0, sizeof(sockaddr_t));
		addr->sa.sa_family = AF_LOCAL;
	}
		

	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 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 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 sizeof addr->sun;
		default:
			elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family);
			break;
	}

	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
 * @b = 2nd address
 * @p = compare and ports, if family is AF_INET or AF_INET6
 * return: 0 is equal or !=0 is different
 */
int
e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __restrict b, int p)
{
	if (a && b && a->sa.sa_family == b->sa.sa_family)
		switch (a->sa.sa_family) {
			case AF_LOCAL:
				return strcmp(a->sun.sun_path, b->sun.sun_path);
			case AF_INET:
				if (p && (a->sin.sin_port - b->sin.sin_port))
					return (int) !!(a->sin.sin_port - b->sin.sin_port);
				else
					return memcmp(&a->sin.sin_addr, &b->sin.sin_addr, 
							sizeof a->sin.sin_addr);
			case AF_INET6:
				if (p && (a->sin6.sin6_port - b->sin6.sin6_port))
					return (int) !!(a->sin6.sin6_port - b->sin6.sin6_port);
				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);
}

/*
 * e_usleep() - usleep() replacement for ELWIX
 *
 * @usec = microseconds for sleep
 * return: -1 interrupted by signal or 0 ok
 */
int
e_usleep(u_int usec)
{
	struct timeval tv = { (time_t) (usec / 1000000), (long) (usec % 1000000) };

	return select(0, NULL, NULL, NULL, &tv);
}

/*
 * e_innet() - Test address match in network
 *
 * @net = network
 * @addr = address
 * return: -1 error, 0 match or 1 not match
 */
int
e_innet(netaddr_t * __restrict net, inaddr_t * __restrict addr)
{
	register int i;
	int ret = 0;

	if (!net || !addr)
		return -1;

	switch (net->addr.sa.sa_family) {
		case AF_INET:
			for (i = 0; i < sizeof(struct in_addr); i++) {
				ret = ((caddr_t) &net->addr.sin.sin_addr.s_addr)[i] & 
					net->mask.in4.s4_addr[i];
				ret -= addr->in4.s4_addr[i] & net->mask.in4.s4_addr[i];
				if (ret)
					break;
			}
			break;
		case AF_INET6:
			for (i = 0; i < sizeof(struct in6_addr); i++) {
				ret = net->addr.sin6.sin6_addr.s6_addr[i] & 
					net->mask.in6.s6_addr[i];
				ret -= addr->in6.s6_addr[i] & net->mask.in6.s6_addr[i];
				if (ret)
					break;
			}
			break;
		default:
			return -1;
	}

	return !!ret;
}

/*
 * e_getnet() - Get network from string
 *
 * @net = Network string (format: <net[/cidr]>)
 * return: NULL error or !=NULL network should be e_free()
 */
netaddr_t *
e_getnet(const char *net)
{
	netaddr_t *n;
	char *str, *wrk;
	struct hostent *host;

	n = e_malloc(sizeof(netaddr_t));
	if (!n) {
		LOGERR;
		return NULL;
	} else
		memset(n, 0, sizeof(netaddr_t));
	str = e_strdup(net);
	if (!str) {
		LOGERR;
		e_free(n);
		return NULL;
	}
	wrk = strchr(str, '/');
	if (wrk)
		*wrk++ = 0;

	host = gethostbyname2(str, strchr(str, ':') ? AF_INET6 : AF_INET);
	if (!host) {
		elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno));
		e_free(str);
		e_free(n);
		return NULL;
	}
	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 && strtol(wrk, NULL, 10) != 32)
				n->mask.in.s_addr = E_CIDRMASK(strtol(wrk, NULL, 10));
			else
				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 */
			break;
		default:
			elwix_SetErr(EINVAL, "Unsupported family #%d", host->h_addrtype);
			e_free(str);
			e_free(n);
			return NULL;
	}

	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 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;

	getifaddrs(&ifa);
	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) {
			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;
}

/*
 * 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;
}

#ifndef __linux__
/*
 * 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)
{
	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 = 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));
			break;
		}
	freeifaddrs(ifa);

	return addr;
}

/*
 * 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)
{
	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;
}

/*
 * 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)
{
	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;
}
#endif

/*
 * e_network() - Get network from address string
 *
 * @csAddr = Address string with CIDR mask /xx
 * @net = Network information structure
 * return: -1 error, 1 nothing for return or 0 ok
 */
int
e_network(const char *csAddr, netaddr_t * __restrict net)
{
	int ret = 0;
	u_char mask = 0;
	inaddr_t a;
	char *pos, szAddr[STRSIZ];
	register int i;

	if (!csAddr || !net)
		return -1;
	else
		strlcpy(szAddr, csAddr, sizeof szAddr);

	memset(net, 0, sizeof(netaddr_t));

	pos = strrchr(szAddr, '/');
	if (pos) {
		*pos++ = 0;
		mask = (u_char) strtol(pos, NULL, 10);
	} else
		return 1;

	if (strchr(szAddr, ':')) {
		if (mask > 128)
			return -1;
		else {
#ifndef __linux__
			for (i = 0; i < 4 && (mask / 32); i++, mask -= 32)
				net->mask.in6.__u6_addr.__u6_addr32[i] = 0xFFFFFFFF;
			if (mask)
				net->mask.in6.__u6_addr.__u6_addr32[i] = E_CIDRMASK(mask % 32);
#else
			for (i = 0; i < 4 && (mask / 32); i++, mask -= 32)
				net->mask.in6.__in6_u.__u6_addr32[i] = 0xFFFFFFFF;
			if (mask)
				net->mask.in6.__in6_u.__u6_addr32[i] = E_CIDRMASK(mask % 32);
#endif
		}

		inet_pton(AF_INET6, szAddr, &a.in6);

#ifndef __linux__
		net->addr.sin6.sin6_len = sizeof net->addr.sin6;
		for (i = 0; i < 4; i++)
			net->addr.sin6.sin6_addr.__u6_addr.__u6_addr32[i] = 
				a.in6.__u6_addr.__u6_addr32[i] & net->mask.in6.__u6_addr.__u6_addr32[i];
#else
		for (i = 0; i < 4; i++)
			net->addr.sin6.sin6_addr.__in6_u.__u6_addr32[i] = 
				a.in6.__in6_u.__u6_addr32[i] & net->mask.in6.__in6_u.__u6_addr32[i];
#endif
	} else {
		if (mask > 32)
			return -1;
		else {
			if (mask == 32)
				net->mask.in.s_addr = 0xFFFFFFFF;
			else
				net->mask.in.s_addr = E_CIDRMASK(mask);
		}

		inet_pton(AF_INET, szAddr, &a.in4);

#ifndef __linux__
		net->addr.sin.sin_len = sizeof net->addr.sin;
#endif
		net->addr.sin.sin_addr.s_addr = a.in.s_addr & net->mask.in.s_addr;
	}

	return ret;
}

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