1 /** @file 2 Convert a string internet address into an integer (32-bit) address. 3 4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR> 5 This program and the accompanying materials are licensed and made available under 6 the terms and conditions of the BSD License that accompanies this distribution. 7 The full text of the license may be found at 8 http://opensource.org/licenses/bsd-license. 9 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 12 13 * Copyright (c) 1983, 1990, 1993 14 * The Regents of the University of California. All rights reserved. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 44 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 45 * 46 * Permission to use, copy, modify, and distribute this software for any 47 * purpose with or without fee is hereby granted, provided that the above 48 * copyright notice and this permission notice appear in all copies, and that 49 * the name of Digital Equipment Corporation not be used in advertising or 50 * publicity pertaining to distribution of the document or software without 51 * specific, written prior permission. 52 * 53 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 54 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 55 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 56 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 57 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 58 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 59 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 60 * SOFTWARE. 61 62 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 63 * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 64 * 65 * Permission to use, copy, modify, and distribute this software for any 66 * purpose with or without fee is hereby granted, provided that the above 67 * copyright notice and this permission notice appear in all copies. 68 * 69 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 70 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 71 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 72 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 73 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 74 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 75 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 76 77 NetBSD: inet_addr.c,v 1.1 2005/12/20 19:28:51 christos Exp 78 inet_addr.c 8.1 (Berkeley) 6/17/93 79 inet_addr.c,v 1.2.206.2 2004/03/17 00:29:45 marka Exp 80 **/ 81 82 #if !defined(_KERNEL) && !defined(_STANDALONE) 83 #include <LibConfig.h> 84 85 //#include "port_before.h" 86 87 #include <namespace.h> 88 #include <sys/types.h> 89 #include <sys/param.h> 90 91 #include <netinet/in.h> 92 #include <arpa/inet.h> 93 94 #include <ctype.h> 95 96 //#include "port_after.h" 97 98 #ifdef __weak_alias 99 __weak_alias(inet_aton,_inet_aton) 100 #endif 101 102 #else // NOT (!defined(_KERNEL) && !defined(_STANDALONE)) 103 #include <lib/libkern/libkern.h> 104 #include <netinet/in.h> 105 #endif 106 107 /* 108 * Ascii internet address interpretation routine. 109 * The value returned is in network order. 110 */ 111 u_int32_t 112 inet_addr(const char *cp) { 113 struct in_addr val; 114 115 if (inet_aton(cp, &val)) 116 return (val.s_addr); 117 return (INADDR_NONE); 118 } 119 120 /* 121 * Check whether "cp" is a valid ascii representation 122 * of an Internet address and convert to a binary address. 123 * Returns 1 if the address is valid, 0 if not. 124 * This replaces inet_addr, the return value from which 125 * cannot distinguish between failure and a local broadcast address. 126 */ 127 int 128 inet_aton(const char *cp, struct in_addr *addr) { 129 u_int32_t val; 130 int base, n; 131 char c; 132 u_int8_t parts[4]; 133 u_int8_t *pp = parts; 134 int digit; 135 136 c = *cp; 137 for (;;) { 138 /* 139 * Collect number up to ``.''. 140 * Values are specified as for C: 141 * 0x=hex, 0=octal, isdigit=decimal. 142 */ 143 if (!isdigit((unsigned char)c)) 144 return (0); 145 val = 0; base = 10; digit = 0; 146 if (c == '0') { 147 c = *++cp; 148 if (c == 'x' || c == 'X') 149 base = 16, c = *++cp; 150 else { 151 base = 8; 152 digit = 1 ; 153 } 154 } 155 for (;;) { 156 if (isascii(c) && isdigit((unsigned char)c)) { 157 if (base == 8 && (c == '8' || c == '9')) 158 return (0); 159 val = (val * base) + (c - '0'); 160 c = *++cp; 161 digit = 1; 162 } else if (base == 16 && isascii(c) && 163 isxdigit((unsigned char)c)) { 164 val = (val << 4) | 165 (c + 10 - (islower((unsigned char)c) ? 'a' : 'A')); 166 c = *++cp; 167 digit = 1; 168 } else 169 break; 170 } 171 if (c == '.') { 172 /* 173 * Internet format: 174 * a.b.c.d 175 * a.b.c (with c treated as 16 bits) 176 * a.b (with b treated as 24 bits) 177 */ 178 if (pp >= parts + 3 || val > 0xffU) 179 return (0); 180 *pp++ = (u_int8_t)val; 181 c = *++cp; 182 } else 183 break; 184 } 185 /* 186 * Check for trailing characters. 187 */ 188 if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c))) 189 return (0); 190 /* 191 * Did we get a valid digit? 192 */ 193 if (!digit) 194 return (0); 195 /* 196 * Concoct the address according to 197 * the number of parts specified. 198 */ 199 n = (int)(pp - parts + 1); 200 switch (n) { 201 case 1: /* a -- 32 bits */ 202 break; 203 204 case 2: /* a.b -- 8.24 bits */ 205 if (val > 0xffffffU) 206 return (0); 207 val |= parts[0] << 24; 208 break; 209 210 case 3: /* a.b.c -- 8.8.16 bits */ 211 if (val > 0xffffU) 212 return (0); 213 val |= (parts[0] << 24) | (parts[1] << 16); 214 break; 215 216 case 4: /* a.b.c.d -- 8.8.8.8 bits */ 217 if (val > 0xffU) 218 return (0); 219 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 220 break; 221 } 222 if (addr != NULL) 223 addr->s_addr = htonl(val); 224 return (1); 225 } 226