1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * ++Copyright++ 1983, 1990, 1993 3*0Sstevel@tonic-gate * - 4*0Sstevel@tonic-gate * Copyright (c) 1983, 1990, 1993 5*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 6*0Sstevel@tonic-gate * 7*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 8*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 9*0Sstevel@tonic-gate * are met: 10*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 11*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 12*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 14*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 15*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 16*0Sstevel@tonic-gate * must display the following acknowledgement: 17*0Sstevel@tonic-gate * This product includes software developed by the University of 18*0Sstevel@tonic-gate * California, Berkeley and its contributors. 19*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 20*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 21*0Sstevel@tonic-gate * without specific prior written permission. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33*0Sstevel@tonic-gate * SUCH DAMAGE. 34*0Sstevel@tonic-gate * - 35*0Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36*0Sstevel@tonic-gate * 37*0Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 38*0Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 39*0Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that 40*0Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or 41*0Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without 42*0Sstevel@tonic-gate * specific, written prior permission. 43*0Sstevel@tonic-gate * 44*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 45*0Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 46*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 47*0Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 48*0Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 49*0Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 50*0Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51*0Sstevel@tonic-gate * SOFTWARE. 52*0Sstevel@tonic-gate * - 53*0Sstevel@tonic-gate * --Copyright-- 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #if !defined(lint) 57*0Sstevel@tonic-gate static const char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 58*0Sstevel@tonic-gate static const char rcsid[] = "@(#)$Id: inet_addr.c,v 1.4 2002/02/21 09:25:42 darrenr Exp $"; 59*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #include <sys/param.h> 62*0Sstevel@tonic-gate #include <netinet/in.h> 63*0Sstevel@tonic-gate #include <arpa/inet.h> 64*0Sstevel@tonic-gate #include <ctype.h> 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #ifndef __P 67*0Sstevel@tonic-gate # ifdef __STDC__ 68*0Sstevel@tonic-gate # define __P(x) x 69*0Sstevel@tonic-gate # else 70*0Sstevel@tonic-gate # define __P(x) () 71*0Sstevel@tonic-gate # endif 72*0Sstevel@tonic-gate #endif 73*0Sstevel@tonic-gate int inet_aton __P((const char *, struct in_addr *)); 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate /* 76*0Sstevel@tonic-gate * Check whether "cp" is a valid ascii representation 77*0Sstevel@tonic-gate * of an Internet address and convert to a binary address. 78*0Sstevel@tonic-gate * Returns 1 if the address is valid, 0 if not. 79*0Sstevel@tonic-gate * This replaces inet_addr, the return value from which 80*0Sstevel@tonic-gate * cannot distinguish between failure and a local broadcast address. 81*0Sstevel@tonic-gate */ 82*0Sstevel@tonic-gate int 83*0Sstevel@tonic-gate inet_aton(cp, addr) 84*0Sstevel@tonic-gate register const char *cp; 85*0Sstevel@tonic-gate struct in_addr *addr; 86*0Sstevel@tonic-gate { 87*0Sstevel@tonic-gate register u_long val; 88*0Sstevel@tonic-gate register int base, n; 89*0Sstevel@tonic-gate register char c; 90*0Sstevel@tonic-gate u_int parts[4]; 91*0Sstevel@tonic-gate register u_int *pp = parts; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate c = *cp; 94*0Sstevel@tonic-gate for (;;) { 95*0Sstevel@tonic-gate /* 96*0Sstevel@tonic-gate * Collect number up to ``.''. 97*0Sstevel@tonic-gate * Values are specified as for C: 98*0Sstevel@tonic-gate * 0x=hex, 0=octal, isdigit=decimal. 99*0Sstevel@tonic-gate */ 100*0Sstevel@tonic-gate if (!isdigit(c)) 101*0Sstevel@tonic-gate return (0); 102*0Sstevel@tonic-gate val = 0; base = 10; 103*0Sstevel@tonic-gate if (c == '0') { 104*0Sstevel@tonic-gate c = *++cp; 105*0Sstevel@tonic-gate if (c == 'x' || c == 'X') 106*0Sstevel@tonic-gate base = 16, c = *++cp; 107*0Sstevel@tonic-gate else 108*0Sstevel@tonic-gate base = 8; 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate for (;;) { 111*0Sstevel@tonic-gate if (isascii(c) && isdigit(c)) { 112*0Sstevel@tonic-gate val = (val * base) + (c - '0'); 113*0Sstevel@tonic-gate c = *++cp; 114*0Sstevel@tonic-gate } else if (base == 16 && isascii(c) && isxdigit(c)) { 115*0Sstevel@tonic-gate val = (val << 4) | 116*0Sstevel@tonic-gate (c + 10 - (islower(c) ? 'a' : 'A')); 117*0Sstevel@tonic-gate c = *++cp; 118*0Sstevel@tonic-gate } else 119*0Sstevel@tonic-gate break; 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate if (c == '.') { 122*0Sstevel@tonic-gate /* 123*0Sstevel@tonic-gate * Internet format: 124*0Sstevel@tonic-gate * a.b.c.d 125*0Sstevel@tonic-gate * a.b.c (with c treated as 16 bits) 126*0Sstevel@tonic-gate * a.b (with b treated as 24 bits) 127*0Sstevel@tonic-gate */ 128*0Sstevel@tonic-gate if (pp >= parts + 3) 129*0Sstevel@tonic-gate return (0); 130*0Sstevel@tonic-gate *pp++ = val; 131*0Sstevel@tonic-gate c = *++cp; 132*0Sstevel@tonic-gate } else 133*0Sstevel@tonic-gate break; 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate /* 136*0Sstevel@tonic-gate * Check for trailing characters. 137*0Sstevel@tonic-gate */ 138*0Sstevel@tonic-gate if (c != '\0' && (!isascii(c) || !isspace(c))) 139*0Sstevel@tonic-gate return (0); 140*0Sstevel@tonic-gate /* 141*0Sstevel@tonic-gate * Concoct the address according to 142*0Sstevel@tonic-gate * the number of parts specified. 143*0Sstevel@tonic-gate */ 144*0Sstevel@tonic-gate n = pp - parts + 1; 145*0Sstevel@tonic-gate switch (n) { 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate case 0: 148*0Sstevel@tonic-gate return (0); /* initial nondigit */ 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate case 1: /* a -- 32 bits */ 151*0Sstevel@tonic-gate break; 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate case 2: /* a.b -- 8.24 bits */ 154*0Sstevel@tonic-gate if (val > 0xffffff) 155*0Sstevel@tonic-gate return (0); 156*0Sstevel@tonic-gate val |= parts[0] << 24; 157*0Sstevel@tonic-gate break; 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate case 3: /* a.b.c -- 8.8.16 bits */ 160*0Sstevel@tonic-gate if (val > 0xffff) 161*0Sstevel@tonic-gate return (0); 162*0Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16); 163*0Sstevel@tonic-gate break; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate case 4: /* a.b.c.d -- 8.8.8.8 bits */ 166*0Sstevel@tonic-gate if (val > 0xff) 167*0Sstevel@tonic-gate return (0); 168*0Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 169*0Sstevel@tonic-gate break; 170*0Sstevel@tonic-gate } 171*0Sstevel@tonic-gate if (addr) 172*0Sstevel@tonic-gate addr->s_addr = htonl(val); 173*0Sstevel@tonic-gate return (1); 174*0Sstevel@tonic-gate } 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate /* these are compatibility routines, not needed on recent BSD releases */ 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate /* 179*0Sstevel@tonic-gate * Ascii internet address interpretation routine. 180*0Sstevel@tonic-gate * The value returned is in network order. 181*0Sstevel@tonic-gate */ 182*0Sstevel@tonic-gate #if (defined(SOLARIS2) && (SOLARIS2 > 5)) || defined(__hpux) || \ 183*0Sstevel@tonic-gate defined(__osf__) || (defined(IRIX) && (IRIX >= 605)) || \ 184*0Sstevel@tonic-gate (defined(__FreeBSD__) && (__FreeBSD__ >= 4)) || \ 185*0Sstevel@tonic-gate defined(__OpenBSD__) 186*0Sstevel@tonic-gate in_addr_t 187*0Sstevel@tonic-gate #else 188*0Sstevel@tonic-gate u_long 189*0Sstevel@tonic-gate #endif 190*0Sstevel@tonic-gate inet_addr(cp) 191*0Sstevel@tonic-gate register const char *cp; 192*0Sstevel@tonic-gate { 193*0Sstevel@tonic-gate struct in_addr val; 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate if (inet_aton(cp, &val)) 196*0Sstevel@tonic-gate return (val.s_addr); 197*0Sstevel@tonic-gate return (0xffffffff); 198*0Sstevel@tonic-gate } 199