Flesh out <arpa/inet.h>.
Use the upstream OpenBSD implementations of these functions. Also ensure we have symbols for htonl, htons, ntohl, and ntohs. gtest doesn't like us using the macro versions in ASSERT_EQ. Bug: 14840760 Change-Id: I68720e9aca14838df457d2bb27b999d5818ac2b5
This commit is contained in:
		
							
								
								
									
										21
									
								
								libc/upstream-openbsd/lib/libc/net/htonl.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								libc/upstream-openbsd/lib/libc/net/htonl.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
/*	$OpenBSD: htonl.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Written by J.T. Conklin <jtc@netbsd.org>.
 | 
			
		||||
 * Public domain.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <machine/endian.h>
 | 
			
		||||
 | 
			
		||||
#undef htonl
 | 
			
		||||
 | 
			
		||||
u_int32_t
 | 
			
		||||
htonl(u_int32_t x)
 | 
			
		||||
{
 | 
			
		||||
#if BYTE_ORDER == LITTLE_ENDIAN
 | 
			
		||||
	u_char *s = (u_char *)&x;
 | 
			
		||||
	return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
 | 
			
		||||
#else
 | 
			
		||||
	return x;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								libc/upstream-openbsd/lib/libc/net/htons.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								libc/upstream-openbsd/lib/libc/net/htons.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
/*	$OpenBSD: htons.c,v 1.8 2005/08/06 20:30:03 espie Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Written by J.T. Conklin <jtc@netbsd.org>.
 | 
			
		||||
 * Public domain.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <machine/endian.h>
 | 
			
		||||
 | 
			
		||||
#undef htons
 | 
			
		||||
 | 
			
		||||
u_int16_t
 | 
			
		||||
htons(u_int16_t x)
 | 
			
		||||
{
 | 
			
		||||
#if BYTE_ORDER == LITTLE_ENDIAN
 | 
			
		||||
	u_char *s = (u_char *) &x;
 | 
			
		||||
	return (u_int16_t)(s[0] << 8 | s[1]);
 | 
			
		||||
#else
 | 
			
		||||
	return x;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										175
									
								
								libc/upstream-openbsd/lib/libc/net/inet_addr.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										175
									
								
								libc/upstream-openbsd/lib/libc/net/inet_addr.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,175 @@
 | 
			
		||||
/*	$OpenBSD: inet_addr.c,v 1.10 2013/11/24 23:51:28 deraadt Exp $	*/
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * ++Copyright++ 1983, 1990, 1993
 | 
			
		||||
 * -
 | 
			
		||||
 * Copyright (c) 1983, 1990, 1993
 | 
			
		||||
 *    The Regents of the University of California.  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. Neither the name of the University 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 THE REGENTS 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.
 | 
			
		||||
 * -
 | 
			
		||||
 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
 | 
			
		||||
 * 
 | 
			
		||||
 * Permission to use, copy, modify, and distribute this software for any
 | 
			
		||||
 * purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
 * copyright notice and this permission notice appear in all copies, and that
 | 
			
		||||
 * the name of Digital Equipment Corporation not be used in advertising or
 | 
			
		||||
 * publicity pertaining to distribution of the document or software without
 | 
			
		||||
 * specific, written prior permission.
 | 
			
		||||
 * 
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
 | 
			
		||||
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
 | 
			
		||||
 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
 | 
			
		||||
 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 | 
			
		||||
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 | 
			
		||||
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 | 
			
		||||
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 | 
			
		||||
 * SOFTWARE.
 | 
			
		||||
 * -
 | 
			
		||||
 * --Copyright--
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/param.h>
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Ascii internet address interpretation routine.
 | 
			
		||||
 * The value returned is in network order.
 | 
			
		||||
 */
 | 
			
		||||
in_addr_t
 | 
			
		||||
inet_addr(const char *cp)
 | 
			
		||||
{
 | 
			
		||||
	struct in_addr val;
 | 
			
		||||
 | 
			
		||||
	if (inet_aton(cp, &val))
 | 
			
		||||
		return (val.s_addr);
 | 
			
		||||
	return (INADDR_NONE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 
 | 
			
		||||
 * Check whether "cp" is a valid ascii representation
 | 
			
		||||
 * of an Internet address and convert to a binary address.
 | 
			
		||||
 * Returns 1 if the address is valid, 0 if not.
 | 
			
		||||
 * This replaces inet_addr, the return value from which
 | 
			
		||||
 * cannot distinguish between failure and a local broadcast address.
 | 
			
		||||
 */
 | 
			
		||||
int
 | 
			
		||||
inet_aton(const char *cp, struct in_addr *addr)
 | 
			
		||||
{
 | 
			
		||||
	in_addr_t val;
 | 
			
		||||
	int base, n;
 | 
			
		||||
	char c;
 | 
			
		||||
	u_int parts[4];
 | 
			
		||||
	u_int *pp = parts;
 | 
			
		||||
 | 
			
		||||
	c = *cp;
 | 
			
		||||
	for (;;) {
 | 
			
		||||
		/*
 | 
			
		||||
		 * Collect number up to ``.''.
 | 
			
		||||
		 * Values are specified as for C:
 | 
			
		||||
		 * 0x=hex, 0=octal, isdigit=decimal.
 | 
			
		||||
		 */
 | 
			
		||||
		if (!isdigit((unsigned char)c))
 | 
			
		||||
			return (0);
 | 
			
		||||
		val = 0; base = 10;
 | 
			
		||||
		if (c == '0') {
 | 
			
		||||
			c = *++cp;
 | 
			
		||||
			if (c == 'x' || c == 'X')
 | 
			
		||||
				base = 16, c = *++cp;
 | 
			
		||||
			else
 | 
			
		||||
				base = 8;
 | 
			
		||||
		}
 | 
			
		||||
		for (;;) {
 | 
			
		||||
			if (isascii((unsigned char)c) &&
 | 
			
		||||
			    isdigit((unsigned char)c)) {
 | 
			
		||||
				val = (val * base) + (c - '0');
 | 
			
		||||
				c = *++cp;
 | 
			
		||||
			} else if (base == 16 &&
 | 
			
		||||
			    isascii((unsigned char)c) &&
 | 
			
		||||
			    isxdigit((unsigned char)c)) {
 | 
			
		||||
				val = (val << 4) |
 | 
			
		||||
				    (c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
 | 
			
		||||
				c = *++cp;
 | 
			
		||||
			} else
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
		if (c == '.') {
 | 
			
		||||
			/*
 | 
			
		||||
			 * Internet format:
 | 
			
		||||
			 *	a.b.c.d
 | 
			
		||||
			 *	a.b.c	(with c treated as 16 bits)
 | 
			
		||||
			 *	a.b	(with b treated as 24 bits)
 | 
			
		||||
			 */
 | 
			
		||||
			if (pp >= parts + 3)
 | 
			
		||||
				return (0);
 | 
			
		||||
			*pp++ = val;
 | 
			
		||||
			c = *++cp;
 | 
			
		||||
		} else
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
	/*
 | 
			
		||||
	 * Check for trailing characters.
 | 
			
		||||
	 */
 | 
			
		||||
	if (c != '\0' &&
 | 
			
		||||
	    (!isascii((unsigned char)c) || !isspace((unsigned char)c)))
 | 
			
		||||
		return (0);
 | 
			
		||||
	/*
 | 
			
		||||
	 * Concoct the address according to
 | 
			
		||||
	 * the number of parts specified.
 | 
			
		||||
	 */
 | 
			
		||||
	n = pp - parts + 1;
 | 
			
		||||
	switch (n) {
 | 
			
		||||
 | 
			
		||||
	case 0:
 | 
			
		||||
		return (0);		/* initial nondigit */
 | 
			
		||||
 | 
			
		||||
	case 1:				/* a -- 32 bits */
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case 2:				/* a.b -- 8.24 bits */
 | 
			
		||||
		if ((val > 0xffffff) || (parts[0] > 0xff))
 | 
			
		||||
			return (0);
 | 
			
		||||
		val |= parts[0] << 24;
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case 3:				/* a.b.c -- 8.8.16 bits */
 | 
			
		||||
		if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
 | 
			
		||||
			return (0);
 | 
			
		||||
		val |= (parts[0] << 24) | (parts[1] << 16);
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
 | 
			
		||||
		if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
 | 
			
		||||
			return (0);
 | 
			
		||||
		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
	if (addr)
 | 
			
		||||
		addr->s_addr = htonl(val);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										51
									
								
								libc/upstream-openbsd/lib/libc/net/inet_lnaof.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								libc/upstream-openbsd/lib/libc/net/inet_lnaof.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
/*	$OpenBSD: inet_lnaof.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 1983, 1993
 | 
			
		||||
 *	The Regents of the University of California.  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. Neither the name of the University 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 THE REGENTS 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 <sys/param.h>
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Return the local network address portion of an
 | 
			
		||||
 * internet address; handles class a/b/c network
 | 
			
		||||
 * number formats.
 | 
			
		||||
 */
 | 
			
		||||
in_addr_t
 | 
			
		||||
inet_lnaof(struct in_addr in)
 | 
			
		||||
{
 | 
			
		||||
	in_addr_t i = ntohl(in.s_addr);
 | 
			
		||||
 | 
			
		||||
	if (IN_CLASSA(i))
 | 
			
		||||
		return ((i)&IN_CLASSA_HOST);
 | 
			
		||||
	else if (IN_CLASSB(i))
 | 
			
		||||
		return ((i)&IN_CLASSB_HOST);
 | 
			
		||||
	else
 | 
			
		||||
		return ((i)&IN_CLASSC_HOST);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										54
									
								
								libc/upstream-openbsd/lib/libc/net/inet_makeaddr.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								libc/upstream-openbsd/lib/libc/net/inet_makeaddr.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
/*	$OpenBSD: inet_makeaddr.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 1983, 1993
 | 
			
		||||
 *	The Regents of the University of California.  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. Neither the name of the University 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 THE REGENTS 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 <sys/param.h>
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Formulate an Internet address from network + host.  Used in
 | 
			
		||||
 * building addresses stored in the ifnet structure.
 | 
			
		||||
 */
 | 
			
		||||
struct in_addr
 | 
			
		||||
inet_makeaddr(in_addr_t net, in_addr_t host)
 | 
			
		||||
{
 | 
			
		||||
	in_addr_t addr;
 | 
			
		||||
 | 
			
		||||
	if (net < 128)
 | 
			
		||||
		addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
 | 
			
		||||
	else if (net < 65536)
 | 
			
		||||
		addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
 | 
			
		||||
	else if (net < 16777216L)
 | 
			
		||||
		addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
 | 
			
		||||
	else
 | 
			
		||||
		addr = net | host;
 | 
			
		||||
	addr = htonl(addr);
 | 
			
		||||
	return (*(struct in_addr *)&addr);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								libc/upstream-openbsd/lib/libc/net/inet_netof.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								libc/upstream-openbsd/lib/libc/net/inet_netof.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
			
		||||
/*	$OpenBSD: inet_netof.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 1983, 1993
 | 
			
		||||
 *	The Regents of the University of California.  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. Neither the name of the University 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 THE REGENTS 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 <sys/param.h>
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Return the network number from an internet
 | 
			
		||||
 * address; handles class a/b/c network #'s.
 | 
			
		||||
 */
 | 
			
		||||
in_addr_t
 | 
			
		||||
inet_netof(struct in_addr in)
 | 
			
		||||
{
 | 
			
		||||
	in_addr_t i = ntohl(in.s_addr);
 | 
			
		||||
 | 
			
		||||
	if (IN_CLASSA(i))
 | 
			
		||||
		return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
 | 
			
		||||
	else if (IN_CLASSB(i))
 | 
			
		||||
		return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
 | 
			
		||||
	else
 | 
			
		||||
		return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										84
									
								
								libc/upstream-openbsd/lib/libc/net/inet_network.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								libc/upstream-openbsd/lib/libc/net/inet_network.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
			
		||||
/*	$OpenBSD: inet_network.c,v 1.11 2013/11/25 17:29:19 deraadt Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 1983, 1993
 | 
			
		||||
 *	The Regents of the University of California.  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. Neither the name of the University 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 THE REGENTS 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 <sys/types.h>
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Internet network address interpretation routine.
 | 
			
		||||
 * The library routines call this routine to interpret
 | 
			
		||||
 * network numbers.
 | 
			
		||||
 */
 | 
			
		||||
in_addr_t
 | 
			
		||||
inet_network(const char *cp)
 | 
			
		||||
{
 | 
			
		||||
	in_addr_t val, base, n;
 | 
			
		||||
	u_char c;
 | 
			
		||||
	in_addr_t parts[4], *pp = parts;
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
again:
 | 
			
		||||
	val = 0; base = 10;
 | 
			
		||||
	if (*cp == '0')
 | 
			
		||||
		base = 8, cp++;
 | 
			
		||||
	if (*cp == 'x' || *cp == 'X')
 | 
			
		||||
		base = 16, cp++;
 | 
			
		||||
	while ((c = *cp)) {
 | 
			
		||||
		if (isdigit(c)) {
 | 
			
		||||
			val = (val * base) + (c - '0');
 | 
			
		||||
			cp++;
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		if (base == 16 && isxdigit(c)) {
 | 
			
		||||
			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
 | 
			
		||||
			cp++;
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
	if (*cp == '.') {
 | 
			
		||||
		if (pp >= parts + 3)
 | 
			
		||||
			return (INADDR_NONE);
 | 
			
		||||
		*pp++ = val, cp++;
 | 
			
		||||
		goto again;
 | 
			
		||||
	}
 | 
			
		||||
	if (*cp && !isspace(*cp))
 | 
			
		||||
		return (INADDR_NONE);
 | 
			
		||||
	*pp++ = val;
 | 
			
		||||
	n = pp - parts;
 | 
			
		||||
	for (val = 0, i = 0; i < 4; i++) {
 | 
			
		||||
		val <<= 8;
 | 
			
		||||
		if (i < n)
 | 
			
		||||
			val |= parts[i] & 0xff;
 | 
			
		||||
	}
 | 
			
		||||
	return (val);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										51
									
								
								libc/upstream-openbsd/lib/libc/net/inet_ntoa.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								libc/upstream-openbsd/lib/libc/net/inet_ntoa.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
/*	$OpenBSD: inet_ntoa.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 1983, 1993
 | 
			
		||||
 *	The Regents of the University of California.  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. Neither the name of the University 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 THE REGENTS 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.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Convert network-format internet address
 | 
			
		||||
 * to base 256 d.d.d.d representation.
 | 
			
		||||
 */
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
char *
 | 
			
		||||
inet_ntoa(struct in_addr in)
 | 
			
		||||
{
 | 
			
		||||
	static char b[18];
 | 
			
		||||
	char *p;
 | 
			
		||||
 | 
			
		||||
	p = (char *)∈
 | 
			
		||||
#define	UC(b)	(((int)b)&0xff)
 | 
			
		||||
	(void)snprintf(b, sizeof(b),
 | 
			
		||||
	    "%u.%u.%u.%u", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
 | 
			
		||||
	return (b);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										205
									
								
								libc/upstream-openbsd/lib/libc/net/inet_ntop.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										205
									
								
								libc/upstream-openbsd/lib/libc/net/inet_ntop.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,205 @@
 | 
			
		||||
/*	$OpenBSD: inet_ntop.c,v 1.9 2014/02/05 14:20:43 millert Exp $	*/
 | 
			
		||||
 | 
			
		||||
/* Copyright (c) 1996 by Internet Software Consortium.
 | 
			
		||||
 *
 | 
			
		||||
 * Permission to use, copy, modify, and distribute this software for any
 | 
			
		||||
 * purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
 * copyright notice and this permission notice appear in all copies.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 | 
			
		||||
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 | 
			
		||||
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 | 
			
		||||
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 | 
			
		||||
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 | 
			
		||||
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 | 
			
		||||
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 | 
			
		||||
 * SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <sys/param.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/socket.h>
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
#include <arpa/nameser.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * WARNING: Don't even consider trying to compile this on a system where
 | 
			
		||||
 * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
 | 
			
		||||
static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
 | 
			
		||||
 | 
			
		||||
/* const char *
 | 
			
		||||
 * inet_ntop(af, src, dst, size)
 | 
			
		||||
 *	convert a network format address to presentation format.
 | 
			
		||||
 * return:
 | 
			
		||||
 *	pointer to presentation format address (`dst'), or NULL (see errno).
 | 
			
		||||
 * author:
 | 
			
		||||
 *	Paul Vixie, 1996.
 | 
			
		||||
 */
 | 
			
		||||
const char *
 | 
			
		||||
inet_ntop(int af, const void *src, char *dst, socklen_t size)
 | 
			
		||||
{
 | 
			
		||||
	switch (af) {
 | 
			
		||||
	case AF_INET:
 | 
			
		||||
		return (inet_ntop4(src, dst, (size_t)size));
 | 
			
		||||
	case AF_INET6:
 | 
			
		||||
		return (inet_ntop6(src, dst, (size_t)size));
 | 
			
		||||
	default:
 | 
			
		||||
		errno = EAFNOSUPPORT;
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	/* NOTREACHED */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* const char *
 | 
			
		||||
 * inet_ntop4(src, dst, size)
 | 
			
		||||
 *	format an IPv4 address, more or less like inet_ntoa()
 | 
			
		||||
 * return:
 | 
			
		||||
 *	`dst' (as a const)
 | 
			
		||||
 * notes:
 | 
			
		||||
 *	(1) uses no statics
 | 
			
		||||
 *	(2) takes a u_char* not an in_addr as input
 | 
			
		||||
 * author:
 | 
			
		||||
 *	Paul Vixie, 1996.
 | 
			
		||||
 */
 | 
			
		||||
static const char *
 | 
			
		||||
inet_ntop4(const u_char *src, char *dst, size_t size)
 | 
			
		||||
{
 | 
			
		||||
	static const char fmt[] = "%u.%u.%u.%u";
 | 
			
		||||
	char tmp[sizeof "255.255.255.255"];
 | 
			
		||||
	int l;
 | 
			
		||||
 | 
			
		||||
	l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
 | 
			
		||||
	if (l <= 0 || l >= size) {
 | 
			
		||||
		errno = ENOSPC;
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	strlcpy(dst, tmp, size);
 | 
			
		||||
	return (dst);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* const char *
 | 
			
		||||
 * inet_ntop6(src, dst, size)
 | 
			
		||||
 *	convert IPv6 binary address into presentation (printable) format
 | 
			
		||||
 * author:
 | 
			
		||||
 *	Paul Vixie, 1996.
 | 
			
		||||
 */
 | 
			
		||||
static const char *
 | 
			
		||||
inet_ntop6(const u_char *src, char *dst, size_t size)
 | 
			
		||||
{
 | 
			
		||||
	/*
 | 
			
		||||
	 * Note that int32_t and int16_t need only be "at least" large enough
 | 
			
		||||
	 * to contain a value of the specified size.  On some systems, like
 | 
			
		||||
	 * Crays, there is no such thing as an integer variable with 16 bits.
 | 
			
		||||
	 * Keep this in mind if you think this function should have been coded
 | 
			
		||||
	 * to use pointer overlays.  All the world's not a VAX.
 | 
			
		||||
	 */
 | 
			
		||||
	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
 | 
			
		||||
	char *tp, *ep;
 | 
			
		||||
	struct { int base, len; } best, cur;
 | 
			
		||||
	u_int words[IN6ADDRSZ / INT16SZ];
 | 
			
		||||
	int i;
 | 
			
		||||
	int advance;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Preprocess:
 | 
			
		||||
	 *	Copy the input (bytewise) array into a wordwise array.
 | 
			
		||||
	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
 | 
			
		||||
	 */
 | 
			
		||||
	memset(words, '\0', sizeof words);
 | 
			
		||||
	for (i = 0; i < IN6ADDRSZ; i++)
 | 
			
		||||
		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
 | 
			
		||||
	best.base = -1;
 | 
			
		||||
	cur.base = -1;
 | 
			
		||||
	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
 | 
			
		||||
		if (words[i] == 0) {
 | 
			
		||||
			if (cur.base == -1)
 | 
			
		||||
				cur.base = i, cur.len = 1;
 | 
			
		||||
			else
 | 
			
		||||
				cur.len++;
 | 
			
		||||
		} else {
 | 
			
		||||
			if (cur.base != -1) {
 | 
			
		||||
				if (best.base == -1 || cur.len > best.len)
 | 
			
		||||
					best = cur;
 | 
			
		||||
				cur.base = -1;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (cur.base != -1) {
 | 
			
		||||
		if (best.base == -1 || cur.len > best.len)
 | 
			
		||||
			best = cur;
 | 
			
		||||
	}
 | 
			
		||||
	if (best.base != -1 && best.len < 2)
 | 
			
		||||
		best.base = -1;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Format the result.
 | 
			
		||||
	 */
 | 
			
		||||
	tp = tmp;
 | 
			
		||||
	ep = tmp + sizeof(tmp);
 | 
			
		||||
	for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
 | 
			
		||||
		/* Are we inside the best run of 0x00's? */
 | 
			
		||||
		if (best.base != -1 && i >= best.base &&
 | 
			
		||||
		    i < (best.base + best.len)) {
 | 
			
		||||
			if (i == best.base) {
 | 
			
		||||
				if (tp + 1 >= ep) {
 | 
			
		||||
					errno = ENOSPC;
 | 
			
		||||
					return (NULL);
 | 
			
		||||
				}
 | 
			
		||||
				*tp++ = ':';
 | 
			
		||||
			}
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		/* Are we following an initial run of 0x00s or any real hex? */
 | 
			
		||||
		if (i != 0) {
 | 
			
		||||
			if (tp + 1 >= ep) {
 | 
			
		||||
				errno = ENOSPC;
 | 
			
		||||
				return (NULL);
 | 
			
		||||
			}
 | 
			
		||||
			*tp++ = ':';
 | 
			
		||||
		}
 | 
			
		||||
		/* Is this address an encapsulated IPv4? */
 | 
			
		||||
		if (i == 6 && best.base == 0 &&
 | 
			
		||||
		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
 | 
			
		||||
			if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
 | 
			
		||||
				return (NULL);
 | 
			
		||||
			tp += strlen(tp);
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
		advance = snprintf(tp, ep - tp, "%x", words[i]);
 | 
			
		||||
		if (advance <= 0 || advance >= ep - tp) {
 | 
			
		||||
			errno = ENOSPC;
 | 
			
		||||
			return (NULL);
 | 
			
		||||
		}
 | 
			
		||||
		tp += advance;
 | 
			
		||||
	}
 | 
			
		||||
	/* Was it a trailing run of 0x00's? */
 | 
			
		||||
	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
 | 
			
		||||
		if (tp + 1 >= ep) {
 | 
			
		||||
			errno = ENOSPC;
 | 
			
		||||
			return (NULL);
 | 
			
		||||
		}
 | 
			
		||||
		*tp++ = ':';
 | 
			
		||||
	}
 | 
			
		||||
	if (tp + 1 >= ep) {
 | 
			
		||||
		errno = ENOSPC;
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	*tp++ = '\0';
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Check for overflow, copy, and we're done.
 | 
			
		||||
	 */
 | 
			
		||||
	if ((size_t)(tp - tmp) > size) {
 | 
			
		||||
		errno = ENOSPC;
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	strlcpy(dst, tmp, size);
 | 
			
		||||
	return (dst);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										213
									
								
								libc/upstream-openbsd/lib/libc/net/inet_pton.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										213
									
								
								libc/upstream-openbsd/lib/libc/net/inet_pton.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,213 @@
 | 
			
		||||
/*	$OpenBSD: inet_pton.c,v 1.8 2010/05/06 15:47:14 claudio Exp $	*/
 | 
			
		||||
 | 
			
		||||
/* Copyright (c) 1996 by Internet Software Consortium.
 | 
			
		||||
 *
 | 
			
		||||
 * Permission to use, copy, modify, and distribute this software for any
 | 
			
		||||
 * purpose with or without fee is hereby granted, provided that the above
 | 
			
		||||
 * copyright notice and this permission notice appear in all copies.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 | 
			
		||||
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 | 
			
		||||
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 | 
			
		||||
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 | 
			
		||||
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 | 
			
		||||
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 | 
			
		||||
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 | 
			
		||||
 * SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <sys/param.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/socket.h>
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
#include <arpa/nameser.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * WARNING: Don't even consider trying to compile this on a system where
 | 
			
		||||
 * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
static int	inet_pton4(const char *src, u_char *dst);
 | 
			
		||||
static int	inet_pton6(const char *src, u_char *dst);
 | 
			
		||||
 | 
			
		||||
/* int
 | 
			
		||||
 * inet_pton(af, src, dst)
 | 
			
		||||
 *	convert from presentation format (which usually means ASCII printable)
 | 
			
		||||
 *	to network format (which is usually some kind of binary format).
 | 
			
		||||
 * return:
 | 
			
		||||
 *	1 if the address was valid for the specified address family
 | 
			
		||||
 *	0 if the address wasn't valid (`dst' is untouched in this case)
 | 
			
		||||
 *	-1 if some other error occurred (`dst' is untouched in this case, too)
 | 
			
		||||
 * author:
 | 
			
		||||
 *	Paul Vixie, 1996.
 | 
			
		||||
 */
 | 
			
		||||
int
 | 
			
		||||
inet_pton(int af, const char *src, void *dst)
 | 
			
		||||
{
 | 
			
		||||
	switch (af) {
 | 
			
		||||
	case AF_INET:
 | 
			
		||||
		return (inet_pton4(src, dst));
 | 
			
		||||
	case AF_INET6:
 | 
			
		||||
		return (inet_pton6(src, dst));
 | 
			
		||||
	default:
 | 
			
		||||
		errno = EAFNOSUPPORT;
 | 
			
		||||
		return (-1);
 | 
			
		||||
	}
 | 
			
		||||
	/* NOTREACHED */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* int
 | 
			
		||||
 * inet_pton4(src, dst)
 | 
			
		||||
 *	like inet_aton() but without all the hexadecimal and shorthand.
 | 
			
		||||
 * return:
 | 
			
		||||
 *	1 if `src' is a valid dotted quad, else 0.
 | 
			
		||||
 * notice:
 | 
			
		||||
 *	does not touch `dst' unless it's returning 1.
 | 
			
		||||
 * author:
 | 
			
		||||
 *	Paul Vixie, 1996.
 | 
			
		||||
 */
 | 
			
		||||
static int
 | 
			
		||||
inet_pton4(const char *src, u_char *dst)
 | 
			
		||||
{
 | 
			
		||||
	static const char digits[] = "0123456789";
 | 
			
		||||
	int saw_digit, octets, ch;
 | 
			
		||||
	u_char tmp[INADDRSZ], *tp;
 | 
			
		||||
 | 
			
		||||
	saw_digit = 0;
 | 
			
		||||
	octets = 0;
 | 
			
		||||
	*(tp = tmp) = 0;
 | 
			
		||||
	while ((ch = *src++) != '\0') {
 | 
			
		||||
		const char *pch;
 | 
			
		||||
 | 
			
		||||
		if ((pch = strchr(digits, ch)) != NULL) {
 | 
			
		||||
			u_int new = *tp * 10 + (pch - digits);
 | 
			
		||||
 | 
			
		||||
			if (new > 255)
 | 
			
		||||
				return (0);
 | 
			
		||||
			if (! saw_digit) {
 | 
			
		||||
				if (++octets > 4)
 | 
			
		||||
					return (0);
 | 
			
		||||
				saw_digit = 1;
 | 
			
		||||
			}
 | 
			
		||||
			*tp = new;
 | 
			
		||||
		} else if (ch == '.' && saw_digit) {
 | 
			
		||||
			if (octets == 4)
 | 
			
		||||
				return (0);
 | 
			
		||||
			*++tp = 0;
 | 
			
		||||
			saw_digit = 0;
 | 
			
		||||
		} else
 | 
			
		||||
			return (0);
 | 
			
		||||
	}
 | 
			
		||||
	if (octets < 4)
 | 
			
		||||
		return (0);
 | 
			
		||||
 | 
			
		||||
	memcpy(dst, tmp, INADDRSZ);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* int
 | 
			
		||||
 * inet_pton6(src, dst)
 | 
			
		||||
 *	convert presentation level address to network order binary form.
 | 
			
		||||
 * return:
 | 
			
		||||
 *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
 | 
			
		||||
 * notice:
 | 
			
		||||
 *	does not touch `dst' unless it's returning 1.
 | 
			
		||||
 * credit:
 | 
			
		||||
 *	inspired by Mark Andrews.
 | 
			
		||||
 * author:
 | 
			
		||||
 *	Paul Vixie, 1996.
 | 
			
		||||
 */
 | 
			
		||||
static int
 | 
			
		||||
inet_pton6(const char *src, u_char *dst)
 | 
			
		||||
{
 | 
			
		||||
	static const char xdigits_l[] = "0123456789abcdef",
 | 
			
		||||
			  xdigits_u[] = "0123456789ABCDEF";
 | 
			
		||||
	u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
 | 
			
		||||
	const char *xdigits, *curtok;
 | 
			
		||||
	int ch, saw_xdigit, count_xdigit;
 | 
			
		||||
	u_int val;
 | 
			
		||||
 | 
			
		||||
	memset((tp = tmp), '\0', IN6ADDRSZ);
 | 
			
		||||
	endp = tp + IN6ADDRSZ;
 | 
			
		||||
	colonp = NULL;
 | 
			
		||||
	/* Leading :: requires some special handling. */
 | 
			
		||||
	if (*src == ':')
 | 
			
		||||
		if (*++src != ':')
 | 
			
		||||
			return (0);
 | 
			
		||||
	curtok = src;
 | 
			
		||||
	saw_xdigit = count_xdigit = 0;
 | 
			
		||||
	val = 0;
 | 
			
		||||
	while ((ch = *src++) != '\0') {
 | 
			
		||||
		const char *pch;
 | 
			
		||||
 | 
			
		||||
		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
 | 
			
		||||
			pch = strchr((xdigits = xdigits_u), ch);
 | 
			
		||||
		if (pch != NULL) {
 | 
			
		||||
			if (count_xdigit >= 4)
 | 
			
		||||
				return (0);
 | 
			
		||||
			val <<= 4;
 | 
			
		||||
			val |= (pch - xdigits);
 | 
			
		||||
			if (val > 0xffff)
 | 
			
		||||
				return (0);
 | 
			
		||||
			saw_xdigit = 1;
 | 
			
		||||
			count_xdigit++;
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		if (ch == ':') {
 | 
			
		||||
			curtok = src;
 | 
			
		||||
			if (!saw_xdigit) {
 | 
			
		||||
				if (colonp)
 | 
			
		||||
					return (0);
 | 
			
		||||
				colonp = tp;
 | 
			
		||||
				continue;
 | 
			
		||||
			} else if (*src == '\0') {
 | 
			
		||||
				return (0);
 | 
			
		||||
			}
 | 
			
		||||
			if (tp + INT16SZ > endp)
 | 
			
		||||
				return (0);
 | 
			
		||||
			*tp++ = (u_char) (val >> 8) & 0xff;
 | 
			
		||||
			*tp++ = (u_char) val & 0xff;
 | 
			
		||||
			saw_xdigit = 0;
 | 
			
		||||
			count_xdigit = 0;
 | 
			
		||||
			val = 0;
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
 | 
			
		||||
		    inet_pton4(curtok, tp) > 0) {
 | 
			
		||||
			tp += INADDRSZ;
 | 
			
		||||
			saw_xdigit = 0;
 | 
			
		||||
			count_xdigit = 0;
 | 
			
		||||
			break;	/* '\0' was seen by inet_pton4(). */
 | 
			
		||||
		}
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	if (saw_xdigit) {
 | 
			
		||||
		if (tp + INT16SZ > endp)
 | 
			
		||||
			return (0);
 | 
			
		||||
		*tp++ = (u_char) (val >> 8) & 0xff;
 | 
			
		||||
		*tp++ = (u_char) val & 0xff;
 | 
			
		||||
	}
 | 
			
		||||
	if (colonp != NULL) {
 | 
			
		||||
		/*
 | 
			
		||||
		 * Since some memmove()'s erroneously fail to handle
 | 
			
		||||
		 * overlapping regions, we'll do the shift by hand.
 | 
			
		||||
		 */
 | 
			
		||||
		const int n = tp - colonp;
 | 
			
		||||
		int i;
 | 
			
		||||
 | 
			
		||||
		if (tp == endp)
 | 
			
		||||
			return (0);
 | 
			
		||||
		for (i = 1; i <= n; i++) {
 | 
			
		||||
			endp[- i] = colonp[n - i];
 | 
			
		||||
			colonp[n - i] = 0;
 | 
			
		||||
		}
 | 
			
		||||
		tp = endp;
 | 
			
		||||
	}
 | 
			
		||||
	if (tp != endp)
 | 
			
		||||
		return (0);
 | 
			
		||||
	memcpy(dst, tmp, IN6ADDRSZ);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								libc/upstream-openbsd/lib/libc/net/ntohl.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								libc/upstream-openbsd/lib/libc/net/ntohl.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
/*	$OpenBSD: ntohl.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Written by J.T. Conklin <jtc@netbsd.org>.
 | 
			
		||||
 * Public domain.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <machine/endian.h>
 | 
			
		||||
 | 
			
		||||
#undef ntohl
 | 
			
		||||
 | 
			
		||||
u_int32_t
 | 
			
		||||
ntohl(u_int32_t x)
 | 
			
		||||
{
 | 
			
		||||
#if BYTE_ORDER == LITTLE_ENDIAN
 | 
			
		||||
	u_char *s = (u_char *)&x;
 | 
			
		||||
	return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
 | 
			
		||||
#else
 | 
			
		||||
	return x;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								libc/upstream-openbsd/lib/libc/net/ntohs.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								libc/upstream-openbsd/lib/libc/net/ntohs.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
/*	$OpenBSD: ntohs.c,v 1.8 2005/08/06 20:30:03 espie Exp $ */
 | 
			
		||||
/*
 | 
			
		||||
 * Written by J.T. Conklin <jtc@netbsd.org>.
 | 
			
		||||
 * Public domain.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <machine/endian.h>
 | 
			
		||||
 | 
			
		||||
#undef ntohs
 | 
			
		||||
 | 
			
		||||
u_int16_t
 | 
			
		||||
ntohs(u_int16_t x)
 | 
			
		||||
{
 | 
			
		||||
#if BYTE_ORDER == LITTLE_ENDIAN
 | 
			
		||||
	u_char *s = (u_char *) &x;
 | 
			
		||||
	return (u_int16_t)(s[0] << 8 | s[1]);
 | 
			
		||||
#else
 | 
			
		||||
	return x;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user