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 /* 23*7257d1b4Sraf * 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 35b71b9f11Sja97890 /* 36b71b9f11Sja97890 * Copyright (c) 1983, 1990, 1993 37b71b9f11Sja97890 * The Regents of the University of California. All rights reserved. 38b71b9f11Sja97890 * 39b71b9f11Sja97890 * Redistribution and use in source and binary forms, with or without 40b71b9f11Sja97890 * modification, are permitted provided that the following conditions 41b71b9f11Sja97890 * are met: 42b71b9f11Sja97890 * 1. Redistributions of source code must retain the above copyright 43b71b9f11Sja97890 * notice, this list of conditions and the following disclaimer. 44b71b9f11Sja97890 * 2. Redistributions in binary form must reproduce the above copyright 45b71b9f11Sja97890 * notice, this list of conditions and the following disclaimer in the 46b71b9f11Sja97890 * documentation and/or other materials provided with the distribution. 47b71b9f11Sja97890 * 3. All advertising materials mentioning features or use of this software 48b71b9f11Sja97890 * must display the following acknowledgement: 49b71b9f11Sja97890 * This product includes software developed by the University of 50b71b9f11Sja97890 * California, Berkeley and its contributors. 51b71b9f11Sja97890 * 4. Neither the name of the University nor the names of its contributors 52b71b9f11Sja97890 * may be used to endorse or promote products derived from this software 53b71b9f11Sja97890 * without specific prior written permission. 54b71b9f11Sja97890 * 55b71b9f11Sja97890 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56b71b9f11Sja97890 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57b71b9f11Sja97890 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58b71b9f11Sja97890 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59b71b9f11Sja97890 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60b71b9f11Sja97890 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61b71b9f11Sja97890 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62b71b9f11Sja97890 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63b71b9f11Sja97890 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64b71b9f11Sja97890 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65b71b9f11Sja97890 * SUCH DAMAGE. 66b71b9f11Sja97890 */ 67b71b9f11Sja97890 68b71b9f11Sja97890 /* 69b71b9f11Sja97890 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 70b71b9f11Sja97890 * 71b71b9f11Sja97890 * Permission to use, copy, modify, and distribute this software for any 72b71b9f11Sja97890 * purpose with or without fee is hereby granted, provided that the above 73b71b9f11Sja97890 * copyright notice and this permission notice appear in all copies, and that 74b71b9f11Sja97890 * the name of Digital Equipment Corporation not be used in advertising or 75b71b9f11Sja97890 * publicity pertaining to distribution of the document or software without 76b71b9f11Sja97890 * specific, written prior permission. 77b71b9f11Sja97890 * 78b71b9f11Sja97890 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 79b71b9f11Sja97890 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 80b71b9f11Sja97890 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 81b71b9f11Sja97890 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 82b71b9f11Sja97890 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 83b71b9f11Sja97890 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 84b71b9f11Sja97890 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 85b71b9f11Sja97890 * SOFTWARE. 86b71b9f11Sja97890 */ 87b71b9f11Sja97890 88b71b9f11Sja97890 /* 89b71b9f11Sja97890 * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 90b71b9f11Sja97890 * 91b71b9f11Sja97890 * Permission to use, copy, modify, and distribute this software for any 92b71b9f11Sja97890 * purpose with or without fee is hereby granted, provided that the above 93b71b9f11Sja97890 * copyright notice and this permission notice appear in all copies. 94b71b9f11Sja97890 * 95b71b9f11Sja97890 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 96b71b9f11Sja97890 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 97b71b9f11Sja97890 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 98b71b9f11Sja97890 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 99b71b9f11Sja97890 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 100b71b9f11Sja97890 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 101b71b9f11Sja97890 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 102b71b9f11Sja97890 * SOFTWARE. 103b71b9f11Sja97890 */ 104b71b9f11Sja97890 1057c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * Convert network-format internet address 1097c478bd9Sstevel@tonic-gate * to base 256 d.d.d.d representation. 1107c478bd9Sstevel@tonic-gate * 1117c478bd9Sstevel@tonic-gate * Reentrant interface 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate #include "mt.h" 1157c478bd9Sstevel@tonic-gate #include "rpc_mt.h" 1167c478bd9Sstevel@tonic-gate #include <errno.h> 1177c478bd9Sstevel@tonic-gate #include <sys/types.h> 1187c478bd9Sstevel@tonic-gate #include <ctype.h> 1197c478bd9Sstevel@tonic-gate #include <netinet/in.h> 1207c478bd9Sstevel@tonic-gate #include <stdio.h> 1217c478bd9Sstevel@tonic-gate #include <stdlib.h> 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate char * 1257c478bd9Sstevel@tonic-gate inet_ntoa_r(in, b) 1267c478bd9Sstevel@tonic-gate struct in_addr in; 1277c478bd9Sstevel@tonic-gate char b[]; /* Assumed >= 18 bytes */ 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate char *p; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate p = (char *)∈ 1327c478bd9Sstevel@tonic-gate #define UC(b) (((int)b)&0xff) 13361961e0fSrobinson (void) sprintf(b, "%d.%d.%d.%d", 13461961e0fSrobinson UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); 1357c478bd9Sstevel@tonic-gate return (b); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate char * 1397c478bd9Sstevel@tonic-gate inet_ntoa(in) 1407c478bd9Sstevel@tonic-gate struct in_addr in; 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate char *b; 1437c478bd9Sstevel@tonic-gate static char b_main[18]; 144cb620785Sraf static pthread_key_t ntoa_key = PTHREAD_ONCE_KEY_NP; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate if (thr_main()) 1477c478bd9Sstevel@tonic-gate b = b_main; 1487c478bd9Sstevel@tonic-gate else if ((b = thr_get_storage(&ntoa_key, 18, free)) == NULL) 1497c478bd9Sstevel@tonic-gate b = b_main; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate return (inet_ntoa_r(in, b)); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * Check whether "cp" is a valid ascii representation 1567c478bd9Sstevel@tonic-gate * of an Internet address and convert to a binary address. 1577c478bd9Sstevel@tonic-gate * Returns 1 if the address is valid, 0 if not. 1587c478bd9Sstevel@tonic-gate * This replaces inet_addr, the return value from which 1597c478bd9Sstevel@tonic-gate * cannot distinguish between failure and a local broadcast address. 1607c478bd9Sstevel@tonic-gate */ 1617c478bd9Sstevel@tonic-gate int 1626c740c0aSraf inet_aton(const char *cp, struct in_addr *addr) 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate uint32_t val; 1657c478bd9Sstevel@tonic-gate int base, n; 1667c478bd9Sstevel@tonic-gate char c; 1677c478bd9Sstevel@tonic-gate unsigned int parts[4]; 16861961e0fSrobinson unsigned int *pp = parts; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate c = *cp; 1727c478bd9Sstevel@tonic-gate for (;;) { 1737c478bd9Sstevel@tonic-gate /* 1747c478bd9Sstevel@tonic-gate * Collect number up to ``.''. 1757c478bd9Sstevel@tonic-gate * Values are specified as for C: 1767c478bd9Sstevel@tonic-gate * 0x=hex, 0=octal, isdigit=decimal. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate if (!isdigit(c)) 1797c478bd9Sstevel@tonic-gate return (0); 1807c478bd9Sstevel@tonic-gate val = 0; base = 10; 1817c478bd9Sstevel@tonic-gate if (c == '0') { 1827c478bd9Sstevel@tonic-gate c = *++cp; 1837c478bd9Sstevel@tonic-gate if (c == 'x' || c == 'X') 1847c478bd9Sstevel@tonic-gate base = 16, c = *++cp; 1857c478bd9Sstevel@tonic-gate else 1867c478bd9Sstevel@tonic-gate base = 8; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate for (;;) { 1897c478bd9Sstevel@tonic-gate if (isascii(c) && isdigit(c)) { 1907c478bd9Sstevel@tonic-gate val = (val * base) + (c - '0'); 1917c478bd9Sstevel@tonic-gate c = *++cp; 1927c478bd9Sstevel@tonic-gate } else if (base == 16 && isascii(c) && isxdigit(c)) { 1937c478bd9Sstevel@tonic-gate val = (val << 4) | 1947c478bd9Sstevel@tonic-gate (c + 10 - (islower(c) ? 'a' : 'A')); 1957c478bd9Sstevel@tonic-gate c = *++cp; 1967c478bd9Sstevel@tonic-gate } else 1977c478bd9Sstevel@tonic-gate break; 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate if (c == '.') { 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * Internet format: 2027c478bd9Sstevel@tonic-gate * a.b.c.d 2037c478bd9Sstevel@tonic-gate * a.b.c (with c treated as 16 bits) 2047c478bd9Sstevel@tonic-gate * a.b (with b treated as 24 bits) 2057c478bd9Sstevel@tonic-gate */ 2067c478bd9Sstevel@tonic-gate if (pp >= parts + 3) 2077c478bd9Sstevel@tonic-gate return (0); 2087c478bd9Sstevel@tonic-gate *pp++ = val; 2097c478bd9Sstevel@tonic-gate c = *++cp; 2107c478bd9Sstevel@tonic-gate } else 2117c478bd9Sstevel@tonic-gate break; 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * Check for trailing characters. 2157c478bd9Sstevel@tonic-gate */ 2167c478bd9Sstevel@tonic-gate if (c != '\0' && (!isascii(c) || !isspace(c))) 2177c478bd9Sstevel@tonic-gate return (0); 2187c478bd9Sstevel@tonic-gate /* 2197c478bd9Sstevel@tonic-gate * Concoct the address according to 2207c478bd9Sstevel@tonic-gate * the number of parts specified. 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate n = pp - parts + 1; 2237c478bd9Sstevel@tonic-gate switch (n) { 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate case 0: 2267c478bd9Sstevel@tonic-gate return (0); /* initial nondigit */ 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate case 1: /* a -- 32 bits */ 2297c478bd9Sstevel@tonic-gate break; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate case 2: /* a.b -- 8.24 bits */ 2327c478bd9Sstevel@tonic-gate if ((val > 0xffffff) || (parts[0] > 0xff)) 2337c478bd9Sstevel@tonic-gate return (0); 2347c478bd9Sstevel@tonic-gate val |= parts[0] << 24; 2357c478bd9Sstevel@tonic-gate break; 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate case 3: /* a.b.c -- 8.8.16 bits */ 2387c478bd9Sstevel@tonic-gate if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff)) 2397c478bd9Sstevel@tonic-gate return (0); 2407c478bd9Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16); 2417c478bd9Sstevel@tonic-gate break; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate case 4: /* a.b.c.d -- 8.8.8.8 bits */ 2447c478bd9Sstevel@tonic-gate if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || 2457c478bd9Sstevel@tonic-gate (parts[2] > 0xff)) 2467c478bd9Sstevel@tonic-gate return (0); 2477c478bd9Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 2487c478bd9Sstevel@tonic-gate break; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate if (addr) 2517c478bd9Sstevel@tonic-gate addr->s_addr = htonl(val); 2527c478bd9Sstevel@tonic-gate return (1); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * Internet address interpretation routine. 2577c478bd9Sstevel@tonic-gate * All the network library routines call this 2587c478bd9Sstevel@tonic-gate * routine to interpret entries in the data bases 2597c478bd9Sstevel@tonic-gate * which are expected to be an address. 2607c478bd9Sstevel@tonic-gate * The value returned is in network order. 2617c478bd9Sstevel@tonic-gate */ 2627c478bd9Sstevel@tonic-gate in_addr_t 26361961e0fSrobinson inet_addr(const char *cp) 2647c478bd9Sstevel@tonic-gate { 2657c478bd9Sstevel@tonic-gate struct in_addr val; 2667c478bd9Sstevel@tonic-gate 2676c740c0aSraf if (inet_aton(cp, &val)) 2687c478bd9Sstevel@tonic-gate return (val.s_addr); 2697c478bd9Sstevel@tonic-gate return (INADDR_NONE); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate /* 2737c478bd9Sstevel@tonic-gate * Return the network number from an internet 2747c478bd9Sstevel@tonic-gate * address; handles class a/b/c network #'s. 2757c478bd9Sstevel@tonic-gate */ 27661961e0fSrobinson uint_t 27761961e0fSrobinson inet_netof(struct in_addr in) 2787c478bd9Sstevel@tonic-gate { 2797c478bd9Sstevel@tonic-gate uint32_t i = ntohl(in.s_addr); 2807c478bd9Sstevel@tonic-gate 28161961e0fSrobinson if (IN_CLASSA(i)) 28261961e0fSrobinson return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); 28361961e0fSrobinson if (IN_CLASSB(i)) 28461961e0fSrobinson return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); 28561961e0fSrobinson return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); 2867c478bd9Sstevel@tonic-gate } 287