inet_addr.c revision 156956
1156952Sume/* 2156952Sume * Copyright (c) 1983, 1990, 1993 3156952Sume * The Regents of the University of California. All rights reserved. 4156952Sume * 5156952Sume * Redistribution and use in source and binary forms, with or without 6156952Sume * modification, are permitted provided that the following conditions 7156952Sume * are met: 8156952Sume * 1. Redistributions of source code must retain the above copyright 9156952Sume * notice, this list of conditions and the following disclaimer. 10156952Sume * 2. Redistributions in binary form must reproduce the above copyright 11156952Sume * notice, this list of conditions and the following disclaimer in the 12156952Sume * documentation and/or other materials provided with the distribution. 13156952Sume * 3. All advertising materials mentioning features or use of this software 14156952Sume * must display the following acknowledgement: 15156952Sume * This product includes software developed by the University of 16156952Sume * California, Berkeley and its contributors. 17156952Sume * 4. Neither the name of the University nor the names of its contributors 18156952Sume * may be used to endorse or promote products derived from this software 19156952Sume * without specific prior written permission. 20156952Sume * 21156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31156952Sume * SUCH DAMAGE. 32156952Sume */ 33156952Sume 34156952Sume/* 35156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36156952Sume * 37156952Sume * Permission to use, copy, modify, and distribute this software for any 38156952Sume * purpose with or without fee is hereby granted, provided that the above 39156952Sume * copyright notice and this permission notice appear in all copies, and that 40156952Sume * the name of Digital Equipment Corporation not be used in advertising or 41156952Sume * publicity pertaining to distribution of the document or software without 42156952Sume * specific, written prior permission. 43156952Sume * 44156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 45156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 46156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 47156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 48156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 49156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 50156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51156952Sume * SOFTWARE. 52156952Sume */ 53156952Sume 54156952Sume/* 55156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 56156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 57156952Sume * 58156952Sume * Permission to use, copy, modify, and distribute this software for any 59156952Sume * purpose with or without fee is hereby granted, provided that the above 60156952Sume * copyright notice and this permission notice appear in all copies. 61156952Sume * 62156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 63156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 64156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 65156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 66156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 67156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 68156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 69156952Sume */ 70156952Sume 71156952Sume#if defined(LIBC_SCCS) && !defined(lint) 72156952Sumestatic const char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 73156952Sumestatic const char rcsid[] = "$Id: inet_addr.c,v 1.2.206.2 2004/03/17 00:29:45 marka Exp $"; 74156952Sume#endif /* LIBC_SCCS and not lint */ 75156956Sume#include <sys/cdefs.h> 76156956Sume__FBSDID("$FreeBSD: head/lib/libc/inet/inet_addr.c 156956 2006-03-21 15:37:16Z ume $"); 77156952Sume 78156952Sume#include "port_before.h" 79156952Sume 80156952Sume#include <sys/types.h> 81156952Sume#include <sys/param.h> 82156952Sume 83156952Sume#include <netinet/in.h> 84156952Sume#include <arpa/inet.h> 85156952Sume 86156952Sume#include <ctype.h> 87156952Sume 88156952Sume#include "port_after.h" 89156952Sume 90156952Sume/* 91156952Sume * Ascii internet address interpretation routine. 92156952Sume * The value returned is in network order. 93156952Sume */ 94156956Sumein_addr_t /* XXX should be struct in_addr :( */ 95156952Sumeinet_addr(const char *cp) { 96156952Sume struct in_addr val; 97156952Sume 98156952Sume if (inet_aton(cp, &val)) 99156952Sume return (val.s_addr); 100156952Sume return (INADDR_NONE); 101156952Sume} 102156952Sume 103156952Sume/* 104156952Sume * Check whether "cp" is a valid ascii representation 105156952Sume * of an Internet address and convert to a binary address. 106156952Sume * Returns 1 if the address is valid, 0 if not. 107156952Sume * This replaces inet_addr, the return value from which 108156952Sume * cannot distinguish between failure and a local broadcast address. 109156952Sume */ 110156952Sumeint 111156952Sumeinet_aton(const char *cp, struct in_addr *addr) { 112156952Sume u_long val; 113156952Sume int base, n; 114156952Sume char c; 115156952Sume u_int8_t parts[4]; 116156952Sume u_int8_t *pp = parts; 117156952Sume int digit; 118156952Sume 119156952Sume c = *cp; 120156952Sume for (;;) { 121156952Sume /* 122156952Sume * Collect number up to ``.''. 123156952Sume * Values are specified as for C: 124156952Sume * 0x=hex, 0=octal, isdigit=decimal. 125156952Sume */ 126156952Sume if (!isdigit((unsigned char)c)) 127156952Sume return (0); 128156952Sume val = 0; base = 10; digit = 0; 129156952Sume if (c == '0') { 130156952Sume c = *++cp; 131156952Sume if (c == 'x' || c == 'X') 132156952Sume base = 16, c = *++cp; 133156952Sume else { 134156952Sume base = 8; 135156952Sume digit = 1 ; 136156952Sume } 137156952Sume } 138156952Sume for (;;) { 139156952Sume if (isascii(c) && isdigit((unsigned char)c)) { 140156952Sume if (base == 8 && (c == '8' || c == '9')) 141156952Sume return (0); 142156952Sume val = (val * base) + (c - '0'); 143156952Sume c = *++cp; 144156952Sume digit = 1; 145156952Sume } else if (base == 16 && isascii(c) && 146156952Sume isxdigit((unsigned char)c)) { 147156952Sume val = (val << 4) | 148156952Sume (c + 10 - (islower((unsigned char)c) ? 'a' : 'A')); 149156952Sume c = *++cp; 150156952Sume digit = 1; 151156952Sume } else 152156952Sume break; 153156952Sume } 154156952Sume if (c == '.') { 155156952Sume /* 156156952Sume * Internet format: 157156952Sume * a.b.c.d 158156952Sume * a.b.c (with c treated as 16 bits) 159156952Sume * a.b (with b treated as 24 bits) 160156952Sume */ 161156952Sume if (pp >= parts + 3 || val > 0xffU) 162156952Sume return (0); 163156952Sume *pp++ = val; 164156952Sume c = *++cp; 165156952Sume } else 166156952Sume break; 167156952Sume } 168156952Sume /* 169156952Sume * Check for trailing characters. 170156952Sume */ 171156952Sume if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c))) 172156952Sume return (0); 173156952Sume /* 174156952Sume * Did we get a valid digit? 175156952Sume */ 176156952Sume if (!digit) 177156952Sume return (0); 178156952Sume /* 179156952Sume * Concoct the address according to 180156952Sume * the number of parts specified. 181156952Sume */ 182156952Sume n = pp - parts + 1; 183156952Sume switch (n) { 184156952Sume case 1: /* a -- 32 bits */ 185156952Sume break; 186156952Sume 187156952Sume case 2: /* a.b -- 8.24 bits */ 188156952Sume if (val > 0xffffffU) 189156952Sume return (0); 190156952Sume val |= parts[0] << 24; 191156952Sume break; 192156952Sume 193156952Sume case 3: /* a.b.c -- 8.8.16 bits */ 194156952Sume if (val > 0xffffU) 195156952Sume return (0); 196156952Sume val |= (parts[0] << 24) | (parts[1] << 16); 197156952Sume break; 198156952Sume 199156952Sume case 4: /* a.b.c.d -- 8.8.8.8 bits */ 200156952Sume if (val > 0xffU) 201156952Sume return (0); 202156952Sume val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 203156952Sume break; 204156952Sume } 205156952Sume if (addr != NULL) 206156952Sume addr->s_addr = htonl(val); 207156952Sume return (1); 208156952Sume} 209156956Sume 210156956Sume/* 211156956Sume * Weak aliases for applications that use certain private entry points, 212156956Sume * and fail to include <arpa/inet.h>. 213156956Sume */ 214156956Sume#undef inet_addr 215156956Sume__weak_reference(__inet_addr, inet_addr); 216156956Sume#undef inet_aton 217156956Sume__weak_reference(__inet_aton, inet_aton); 218