xref: /illumos-gate/usr/src/lib/libc/port/inet/inet_ntoa.c (revision 8a7aa2a5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb620785Sraf  * Common Development and Distribution License (the "License").
6cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
266c740c0aSraf 
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
35b71b9f11Sja /*
36b71b9f11Sja  * Copyright (c) 1983, 1990, 1993
37b71b9f11Sja  *    The Regents of the University of California.  All rights reserved.
38b71b9f11Sja  *
39b71b9f11Sja  * Redistribution and use in source and binary forms, with or without
40b71b9f11Sja  * modification, are permitted provided that the following conditions
41b71b9f11Sja  * are met:
42b71b9f11Sja  * 1. Redistributions of source code must retain the above copyright
43b71b9f11Sja  *    notice, this list of conditions and the following disclaimer.
44b71b9f11Sja  * 2. Redistributions in binary form must reproduce the above copyright
45b71b9f11Sja  *    notice, this list of conditions and the following disclaimer in the
46b71b9f11Sja  *    documentation and/or other materials provided with the distribution.
47b71b9f11Sja  * 3. All advertising materials mentioning features or use of this software
48b71b9f11Sja  *    must display the following acknowledgement:
49*8a7aa2a5SYuri Pankov  *	This product includes software developed by the University of
50*8a7aa2a5SYuri Pankov  *	California, Berkeley and its contributors.
51b71b9f11Sja  * 4. Neither the name of the University nor the names of its contributors
52b71b9f11Sja  *    may be used to endorse or promote products derived from this software
53b71b9f11Sja  *    without specific prior written permission.
54b71b9f11Sja  *
55b71b9f11Sja  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56b71b9f11Sja  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57b71b9f11Sja  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58b71b9f11Sja  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59b71b9f11Sja  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60b71b9f11Sja  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61b71b9f11Sja  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62b71b9f11Sja  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63b71b9f11Sja  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64b71b9f11Sja  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65b71b9f11Sja  * SUCH DAMAGE.
66b71b9f11Sja  */
67b71b9f11Sja 
68b71b9f11Sja /*
69b71b9f11Sja  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
70b71b9f11Sja  *
71b71b9f11Sja  * Permission to use, copy, modify, and distribute this software for any
72b71b9f11Sja  * purpose with or without fee is hereby granted, provided that the above
73b71b9f11Sja  * copyright notice and this permission notice appear in all copies, and that
74b71b9f11Sja  * the name of Digital Equipment Corporation not be used in advertising or
75b71b9f11Sja  * publicity pertaining to distribution of the document or software without
76b71b9f11Sja  * specific, written prior permission.
77b71b9f11Sja  *
78b71b9f11Sja  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
79b71b9f11Sja  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
80b71b9f11Sja  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
81b71b9f11Sja  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
82b71b9f11Sja  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
83b71b9f11Sja  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
84b71b9f11Sja  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
85b71b9f11Sja  * SOFTWARE.
86b71b9f11Sja  */
87b71b9f11Sja 
88b71b9f11Sja /*
89b71b9f11Sja  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
90b71b9f11Sja  *
91b71b9f11Sja  * Permission to use, copy, modify, and distribute this software for any
92b71b9f11Sja  * purpose with or without fee is hereby granted, provided that the above
93b71b9f11Sja  * copyright notice and this permission notice appear in all copies.
94b71b9f11Sja  *
95b71b9f11Sja  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
96b71b9f11Sja  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
97b71b9f11Sja  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
98b71b9f11Sja  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
99b71b9f11Sja  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
100b71b9f11Sja  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
101b71b9f11Sja  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
102b71b9f11Sja  * SOFTWARE.
103b71b9f11Sja  */
104b71b9f11Sja 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Convert network-format internet address
1077c478bd9Sstevel@tonic-gate  * to base 256 d.d.d.d representation.
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * Reentrant interface
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate 
112*8a7aa2a5SYuri Pankov #include "lint.h"
113*8a7aa2a5SYuri Pankov #include "thr_uberdata.h"
114*8a7aa2a5SYuri Pankov 
1157c478bd9Sstevel@tonic-gate #include <sys/types.h>
116*8a7aa2a5SYuri Pankov 
1177c478bd9Sstevel@tonic-gate #include <netinet/in.h>
118*8a7aa2a5SYuri Pankov 
119*8a7aa2a5SYuri Pankov #include <ctype.h>
120*8a7aa2a5SYuri Pankov #include <errno.h>
1217c478bd9Sstevel@tonic-gate #include <stdio.h>
1227c478bd9Sstevel@tonic-gate #include <stdlib.h>
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate char *
inet_ntoa_r(struct in_addr in,char b[])125*8a7aa2a5SYuri Pankov inet_ntoa_r(struct in_addr in, char b[])
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	char	*p;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	p = (char *)&in;
1307c478bd9Sstevel@tonic-gate #define	UC(b)	(((int)b)&0xff)
13161961e0fSrobinson 	(void) sprintf(b, "%d.%d.%d.%d",
132*8a7aa2a5SYuri Pankov 	    UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
1337c478bd9Sstevel@tonic-gate 	return (b);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate char *
inet_ntoa(struct in_addr in)137*8a7aa2a5SYuri Pankov inet_ntoa(struct in_addr in)
1387c478bd9Sstevel@tonic-gate {
139*8a7aa2a5SYuri Pankov 	return (inet_ntoa_r(in, curthread->ul_ntoabuf));
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Check whether "cp" is a valid ascii representation
1447c478bd9Sstevel@tonic-gate  * of an Internet address and convert to a binary address.
1457c478bd9Sstevel@tonic-gate  * Returns 1 if the address is valid, 0 if not.
1467c478bd9Sstevel@tonic-gate  * This replaces inet_addr, the return value from which
1477c478bd9Sstevel@tonic-gate  * cannot distinguish between failure and a local broadcast address.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate int
inet_aton(const char * cp,struct in_addr * addr)1506c740c0aSraf inet_aton(const char *cp, struct in_addr *addr)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	uint32_t val;
1537c478bd9Sstevel@tonic-gate 	int base, n;
1547c478bd9Sstevel@tonic-gate 	char c;
1557c478bd9Sstevel@tonic-gate 	unsigned int parts[4];
15661961e0fSrobinson 	unsigned int *pp = parts;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	c = *cp;
1597c478bd9Sstevel@tonic-gate 	for (;;) {
1607c478bd9Sstevel@tonic-gate 		/*
1617c478bd9Sstevel@tonic-gate 		 * Collect number up to ``.''.
1627c478bd9Sstevel@tonic-gate 		 * Values are specified as for C:
1637c478bd9Sstevel@tonic-gate 		 * 0x=hex, 0=octal, isdigit=decimal.
1647c478bd9Sstevel@tonic-gate 		 */
1657c478bd9Sstevel@tonic-gate 		if (!isdigit(c))
1667c478bd9Sstevel@tonic-gate 			return (0);
1677c478bd9Sstevel@tonic-gate 		val = 0; base = 10;
1687c478bd9Sstevel@tonic-gate 		if (c == '0') {
1697c478bd9Sstevel@tonic-gate 			c = *++cp;
1707c478bd9Sstevel@tonic-gate 			if (c == 'x' || c == 'X')
1717c478bd9Sstevel@tonic-gate 				base = 16, c = *++cp;
1727c478bd9Sstevel@tonic-gate 			else
1737c478bd9Sstevel@tonic-gate 				base = 8;
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 		for (;;) {
1767c478bd9Sstevel@tonic-gate 			if (isascii(c) && isdigit(c)) {
1777c478bd9Sstevel@tonic-gate 				val = (val * base) + (c - '0');
1787c478bd9Sstevel@tonic-gate 				c = *++cp;
1797c478bd9Sstevel@tonic-gate 			} else if (base == 16 && isascii(c) && isxdigit(c)) {
1807c478bd9Sstevel@tonic-gate 				val = (val << 4) |
1817257d1b4Sraf 				    (c + 10 - (islower(c) ? 'a' : 'A'));
1827c478bd9Sstevel@tonic-gate 				c = *++cp;
1837c478bd9Sstevel@tonic-gate 			} else
1847c478bd9Sstevel@tonic-gate 				break;
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		if (c == '.') {
1877c478bd9Sstevel@tonic-gate 			/*
1887c478bd9Sstevel@tonic-gate 			 * Internet format:
1897c478bd9Sstevel@tonic-gate 			 *	a.b.c.d
1907c478bd9Sstevel@tonic-gate 			 *	a.b.c	(with c treated as 16 bits)
1917c478bd9Sstevel@tonic-gate 			 *	a.b	(with b treated as 24 bits)
1927c478bd9Sstevel@tonic-gate 			 */
1937c478bd9Sstevel@tonic-gate 			if (pp >= parts + 3)
1947c478bd9Sstevel@tonic-gate 				return (0);
1957c478bd9Sstevel@tonic-gate 			*pp++ = val;
1967c478bd9Sstevel@tonic-gate 			c = *++cp;
1977c478bd9Sstevel@tonic-gate 		} else
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	/*
2017c478bd9Sstevel@tonic-gate 	 * Check for trailing characters.
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	if (c != '\0' && (!isascii(c) || !isspace(c)))
2047c478bd9Sstevel@tonic-gate 		return (0);
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * Concoct the address according to
2077c478bd9Sstevel@tonic-gate 	 * the number of parts specified.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	n = pp - parts + 1;
2107c478bd9Sstevel@tonic-gate 	switch (n) {
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	case 0:
2137c478bd9Sstevel@tonic-gate 		return (0);		/* initial nondigit */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	case 1:				/* a -- 32 bits */
2167c478bd9Sstevel@tonic-gate 		break;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	case 2:				/* a.b -- 8.24 bits */
2197c478bd9Sstevel@tonic-gate 		if ((val > 0xffffff) || (parts[0] > 0xff))
2207c478bd9Sstevel@tonic-gate 			return (0);
2217c478bd9Sstevel@tonic-gate 		val |= parts[0] << 24;
2227c478bd9Sstevel@tonic-gate 		break;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	case 3:				/* a.b.c -- 8.8.16 bits */
2257c478bd9Sstevel@tonic-gate 		if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
2267c478bd9Sstevel@tonic-gate 			return (0);
2277c478bd9Sstevel@tonic-gate 		val |= (parts[0] << 24) | (parts[1] << 16);
2287c478bd9Sstevel@tonic-gate 		break;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
2317c478bd9Sstevel@tonic-gate 		if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) ||
2327c478bd9Sstevel@tonic-gate 		    (parts[2] > 0xff))
2337c478bd9Sstevel@tonic-gate 			return (0);
2347c478bd9Sstevel@tonic-gate 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
2357c478bd9Sstevel@tonic-gate 		break;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	if (addr)
2387c478bd9Sstevel@tonic-gate 		addr->s_addr = htonl(val);
2397c478bd9Sstevel@tonic-gate 	return (1);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Internet address interpretation routine.
2447c478bd9Sstevel@tonic-gate  * All the network library routines call this
2457c478bd9Sstevel@tonic-gate  * routine to interpret entries in the data bases
2467c478bd9Sstevel@tonic-gate  * which are expected to be an address.
2477c478bd9Sstevel@tonic-gate  * The value returned is in network order.
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate in_addr_t
inet_addr(const char * cp)25061961e0fSrobinson inet_addr(const char *cp)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	struct in_addr val;
2537c478bd9Sstevel@tonic-gate 
2546c740c0aSraf 	if (inet_aton(cp, &val))
2557c478bd9Sstevel@tonic-gate 		return (val.s_addr);
2567c478bd9Sstevel@tonic-gate 	return (INADDR_NONE);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * Return the network number from an internet
2617c478bd9Sstevel@tonic-gate  * address; handles class a/b/c network #'s.
2627c478bd9Sstevel@tonic-gate  */
263*8a7aa2a5SYuri Pankov in_addr_t
inet_netof(struct in_addr in)26461961e0fSrobinson inet_netof(struct in_addr in)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	uint32_t i = ntohl(in.s_addr);
2677c478bd9Sstevel@tonic-gate 
26861961e0fSrobinson 	if (IN_CLASSA(i))
26961961e0fSrobinson 		return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
27061961e0fSrobinson 	if (IN_CLASSB(i))
27161961e0fSrobinson 		return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
27261961e0fSrobinson 	return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
2737c478bd9Sstevel@tonic-gate }
274