10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * ++Copyright++ 1983, 1990, 1993 30Sstevel@tonic-gate * - 40Sstevel@tonic-gate * Copyright (c) 1983, 1990, 1993 50Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 6*2393Syz155240 * 70Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 80Sstevel@tonic-gate * modification, are permitted provided that the following conditions 90Sstevel@tonic-gate * are met: 100Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 110Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 120Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 140Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 150Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 160Sstevel@tonic-gate * must display the following acknowledgement: 170Sstevel@tonic-gate * This product includes software developed by the University of 180Sstevel@tonic-gate * California, Berkeley and its contributors. 190Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 200Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 210Sstevel@tonic-gate * without specific prior written permission. 22*2393Syz155240 * 230Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 240Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 250Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 260Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 270Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 280Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 290Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 300Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 310Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 320Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 330Sstevel@tonic-gate * SUCH DAMAGE. 340Sstevel@tonic-gate * - 350Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36*2393Syz155240 * 370Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 380Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 390Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that 400Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or 410Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without 420Sstevel@tonic-gate * specific, written prior permission. 43*2393Syz155240 * 440Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 450Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 460Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 470Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 480Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 490Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 500Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 510Sstevel@tonic-gate * SOFTWARE. 520Sstevel@tonic-gate * - 530Sstevel@tonic-gate * --Copyright-- 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate 560Sstevel@tonic-gate #if !defined(lint) 570Sstevel@tonic-gate static const char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 58*2393Syz155240 static const char rcsid[] = "@(#)$Id: inet_addr.c,v 1.8.2.3 2004/12/09 19:41:20 darrenr Exp $"; 590Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 600Sstevel@tonic-gate 610Sstevel@tonic-gate #include <sys/param.h> 620Sstevel@tonic-gate #include <netinet/in.h> 630Sstevel@tonic-gate #include <arpa/inet.h> 640Sstevel@tonic-gate #include <ctype.h> 650Sstevel@tonic-gate 660Sstevel@tonic-gate #ifndef __P 670Sstevel@tonic-gate # ifdef __STDC__ 680Sstevel@tonic-gate # define __P(x) x 690Sstevel@tonic-gate # else 700Sstevel@tonic-gate # define __P(x) () 710Sstevel@tonic-gate # endif 720Sstevel@tonic-gate #endif 73*2393Syz155240 #ifndef linux 740Sstevel@tonic-gate int inet_aton __P((const char *, struct in_addr *)); 750Sstevel@tonic-gate 76*2393Syz155240 /* 77*2393Syz155240 * Because the ctype(3) posix definition, if used "safely" in code everywhere, 78*2393Syz155240 * would mean all normal code that walks through strings needed casts. Yuck. 79*2393Syz155240 */ 80*2393Syz155240 #define ISALNUM(x) isalnum((u_char)(x)) 81*2393Syz155240 #define ISALPHA(x) isalpha((u_char)(x)) 82*2393Syz155240 #define ISASCII(x) isascii((u_char)(x)) 83*2393Syz155240 #define ISDIGIT(x) isdigit((u_char)(x)) 84*2393Syz155240 #define ISPRINT(x) isprint((u_char)(x)) 85*2393Syz155240 #define ISSPACE(x) isspace((u_char)(x)) 86*2393Syz155240 #define ISUPPER(x) isupper((u_char)(x)) 87*2393Syz155240 #define ISXDIGIT(x) isxdigit((u_char)(x)) 88*2393Syz155240 #define ISLOWER(x) islower((u_char)(x)) 89*2393Syz155240 90*2393Syz155240 /* 910Sstevel@tonic-gate * Check whether "cp" is a valid ascii representation 920Sstevel@tonic-gate * of an Internet address and convert to a binary address. 930Sstevel@tonic-gate * Returns 1 if the address is valid, 0 if not. 940Sstevel@tonic-gate * This replaces inet_addr, the return value from which 950Sstevel@tonic-gate * cannot distinguish between failure and a local broadcast address. 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate int 980Sstevel@tonic-gate inet_aton(cp, addr) 990Sstevel@tonic-gate register const char *cp; 1000Sstevel@tonic-gate struct in_addr *addr; 1010Sstevel@tonic-gate { 1020Sstevel@tonic-gate register u_long val; 1030Sstevel@tonic-gate register int base, n; 1040Sstevel@tonic-gate register char c; 1050Sstevel@tonic-gate u_int parts[4]; 1060Sstevel@tonic-gate register u_int *pp = parts; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate c = *cp; 1090Sstevel@tonic-gate for (;;) { 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * Collect number up to ``.''. 1120Sstevel@tonic-gate * Values are specified as for C: 1130Sstevel@tonic-gate * 0x=hex, 0=octal, isdigit=decimal. 1140Sstevel@tonic-gate */ 115*2393Syz155240 if (!ISDIGIT(c)) 1160Sstevel@tonic-gate return (0); 1170Sstevel@tonic-gate val = 0; base = 10; 1180Sstevel@tonic-gate if (c == '0') { 1190Sstevel@tonic-gate c = *++cp; 1200Sstevel@tonic-gate if (c == 'x' || c == 'X') 1210Sstevel@tonic-gate base = 16, c = *++cp; 1220Sstevel@tonic-gate else 1230Sstevel@tonic-gate base = 8; 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate for (;;) { 126*2393Syz155240 if (ISASCII(c) && ISDIGIT(c)) { 1270Sstevel@tonic-gate val = (val * base) + (c - '0'); 1280Sstevel@tonic-gate c = *++cp; 129*2393Syz155240 } else if (base == 16 && ISASCII(c) && ISXDIGIT(c)) { 1300Sstevel@tonic-gate val = (val << 4) | 131*2393Syz155240 (c + 10 - (ISLOWER(c) ? 'a' : 'A')); 1320Sstevel@tonic-gate c = *++cp; 1330Sstevel@tonic-gate } else 1340Sstevel@tonic-gate break; 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate if (c == '.') { 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * Internet format: 1390Sstevel@tonic-gate * a.b.c.d 1400Sstevel@tonic-gate * a.b.c (with c treated as 16 bits) 1410Sstevel@tonic-gate * a.b (with b treated as 24 bits) 1420Sstevel@tonic-gate */ 1430Sstevel@tonic-gate if (pp >= parts + 3) 1440Sstevel@tonic-gate return (0); 1450Sstevel@tonic-gate *pp++ = val; 1460Sstevel@tonic-gate c = *++cp; 1470Sstevel@tonic-gate } else 1480Sstevel@tonic-gate break; 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * Check for trailing characters. 1520Sstevel@tonic-gate */ 153*2393Syz155240 if (c != '\0' && (!ISASCII(c) || !ISSPACE(c))) 1540Sstevel@tonic-gate return (0); 1550Sstevel@tonic-gate /* 1560Sstevel@tonic-gate * Concoct the address according to 1570Sstevel@tonic-gate * the number of parts specified. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate n = pp - parts + 1; 1600Sstevel@tonic-gate switch (n) { 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate case 0: 1630Sstevel@tonic-gate return (0); /* initial nondigit */ 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate case 1: /* a -- 32 bits */ 1660Sstevel@tonic-gate break; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate case 2: /* a.b -- 8.24 bits */ 1690Sstevel@tonic-gate if (val > 0xffffff) 1700Sstevel@tonic-gate return (0); 1710Sstevel@tonic-gate val |= parts[0] << 24; 1720Sstevel@tonic-gate break; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate case 3: /* a.b.c -- 8.8.16 bits */ 1750Sstevel@tonic-gate if (val > 0xffff) 1760Sstevel@tonic-gate return (0); 1770Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16); 1780Sstevel@tonic-gate break; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate case 4: /* a.b.c.d -- 8.8.8.8 bits */ 1810Sstevel@tonic-gate if (val > 0xff) 1820Sstevel@tonic-gate return (0); 1830Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 1840Sstevel@tonic-gate break; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate if (addr) 1870Sstevel@tonic-gate addr->s_addr = htonl(val); 1880Sstevel@tonic-gate return (1); 1890Sstevel@tonic-gate } 190*2393Syz155240 #endif 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* these are compatibility routines, not needed on recent BSD releases */ 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* 1950Sstevel@tonic-gate * Ascii internet address interpretation routine. 1960Sstevel@tonic-gate * The value returned is in network order. 1970Sstevel@tonic-gate */ 198*2393Syz155240 #if 0 1990Sstevel@tonic-gate inet_addr(cp) 200*2393Syz155240 const char *cp; 2010Sstevel@tonic-gate { 2020Sstevel@tonic-gate struct in_addr val; 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate if (inet_aton(cp, &val)) 2050Sstevel@tonic-gate return (val.s_addr); 2060Sstevel@tonic-gate return (0xffffffff); 2070Sstevel@tonic-gate } 208*2393Syz155240 #endif 209