1290001Sglebius/* 2290001Sglebius * Portions Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC") 3290001Sglebius * Portions Copyright (C) 1996-2001 Internet Software Consortium. 4290001Sglebius * 5290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any 6290001Sglebius * purpose with or without fee is hereby granted, provided that the above 7290001Sglebius * copyright notice and this permission notice appear in all copies. 8290001Sglebius * 9290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11290001Sglebius * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15290001Sglebius * PERFORMANCE OF THIS SOFTWARE. 16290001Sglebius */ 17290001Sglebius 18290001Sglebius/* 19290001Sglebius * Copyright (c) 1983, 1990, 1993 20290001Sglebius * The Regents of the University of California. All rights reserved. 21290001Sglebius * 22290001Sglebius * Redistribution and use in source and binary forms, with or without 23290001Sglebius * modification, are permitted provided that the following conditions 24290001Sglebius * are met: 25290001Sglebius * 1. Redistributions of source code must retain the above copyright 26290001Sglebius * notice, this list of conditions and the following disclaimer. 27290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright 28290001Sglebius * notice, this list of conditions and the following disclaimer in the 29290001Sglebius * documentation and/or other materials provided with the distribution. 30290001Sglebius * 3. All advertising materials mentioning features or use of this software 31290001Sglebius * must display the following acknowledgement: 32290001Sglebius * This product includes software developed by the University of 33290001Sglebius * California, Berkeley and its contributors. 34290001Sglebius * 4. Neither the name of the University nor the names of its contributors 35290001Sglebius * may be used to endorse or promote products derived from this software 36290001Sglebius * without specific prior written permission. 37290001Sglebius * 38290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 39290001Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40290001Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 41290001Sglebius * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 42290001Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 43290001Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 44290001Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 45290001Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 46290001Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47290001Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48290001Sglebius * SUCH DAMAGE. 49290001Sglebius */ 50290001Sglebius 51290001Sglebius/* 52290001Sglebius * Portions Copyright (c) 1993 by Digital Equipment Corporation. 53290001Sglebius * 54290001Sglebius * Permission to use, copy, modify, and distribute this software for any 55290001Sglebius * purpose with or without fee is hereby granted, provided that the above 56290001Sglebius * copyright notice and this permission notice appear in all copies, and that 57290001Sglebius * the name of Digital Equipment Corporation not be used in advertising or 58290001Sglebius * publicity pertaining to distribution of the document or software without 59290001Sglebius * specific, written prior permission. 60290001Sglebius * 61290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 62290001Sglebius * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 63290001Sglebius * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 64290001Sglebius * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 65290001Sglebius * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 66290001Sglebius * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 67290001Sglebius * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 68290001Sglebius * SOFTWARE. 69290001Sglebius */ 70290001Sglebius/*! \file */ 71290001Sglebius 72290001Sglebius#if defined(LIBC_SCCS) && !defined(lint) 73290001Sglebiusstatic char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 74290001Sglebiusstatic char rcsid[] = "$Id: inet_aton.c,v 1.23 2008/12/01 23:47:45 tbox Exp $"; 75290001Sglebius#endif /* LIBC_SCCS and not lint */ 76290001Sglebius 77290001Sglebius#include <config.h> 78290001Sglebius 79290001Sglebius#include <ctype.h> 80290001Sglebius#include <stddef.h> /* Required for NULL. */ 81290001Sglebius 82290001Sglebius#include <isc/types.h> 83290001Sglebius#include <isc/net.h> 84290001Sglebius 85290001Sglebius/*% 86290001Sglebius * Check whether "cp" is a valid ascii representation 87290001Sglebius * of an Internet address and convert to a binary address. 88290001Sglebius * Returns 1 if the address is valid, 0 if not. 89290001Sglebius * This replaces inet_addr, the return value from which 90290001Sglebius * cannot distinguish between failure and a local broadcast address. 91290001Sglebius */ 92290001Sglebiusint 93290001Sglebiusisc_net_aton(const char *cp, struct in_addr *addr) { 94290001Sglebius unsigned long val; 95293896Sglebius int base; 96290001Sglebius unsigned char c; 97290001Sglebius isc_uint8_t parts[4]; 98290001Sglebius isc_uint8_t *pp = parts; 99290001Sglebius int digit; 100290001Sglebius 101290001Sglebius c = *cp; 102290001Sglebius for (;;) { 103290001Sglebius /* 104290001Sglebius * Collect number up to ``.''. 105290001Sglebius * Values are specified as for C: 106290001Sglebius * 0x=hex, 0=octal, isdigit=decimal. 107290001Sglebius */ 108290001Sglebius if (!isdigit(c & 0xff)) 109290001Sglebius return (0); 110290001Sglebius val = 0; base = 10; digit = 0; 111290001Sglebius if (c == '0') { 112290001Sglebius c = *++cp; 113290001Sglebius if (c == 'x' || c == 'X') 114290001Sglebius base = 16, c = *++cp; 115290001Sglebius else { 116290001Sglebius base = 8; 117290001Sglebius digit = 1; 118290001Sglebius } 119290001Sglebius } 120290001Sglebius for (;;) { 121290001Sglebius /* 122290001Sglebius * isascii() is valid for all integer values, and 123290001Sglebius * when it is true, c is known to be in scope 124290001Sglebius * for isdigit(). No cast necessary. Similar 125290001Sglebius * comment applies for later ctype uses. 126290001Sglebius */ 127290001Sglebius if (isascii(c) && isdigit(c)) { 128290001Sglebius if (base == 8 && (c == '8' || c == '9')) 129290001Sglebius return (0); 130290001Sglebius val = (val * base) + (c - '0'); 131290001Sglebius c = *++cp; 132290001Sglebius digit = 1; 133290001Sglebius } else if (base == 16 && isascii(c) && isxdigit(c)) { 134290001Sglebius val = (val << 4) | 135290001Sglebius (c + 10 - (islower(c) ? 'a' : 'A')); 136290001Sglebius c = *++cp; 137290001Sglebius digit = 1; 138290001Sglebius } else 139290001Sglebius break; 140290001Sglebius } 141290001Sglebius if (c == '.') { 142290001Sglebius /* 143290001Sglebius * Internet format: 144290001Sglebius * a.b.c.d 145290001Sglebius * a.b.c (with c treated as 16 bits) 146290001Sglebius * a.b (with b treated as 24 bits) 147290001Sglebius */ 148290001Sglebius if (pp >= parts + 3 || val > 0xffU) 149290001Sglebius return (0); 150290001Sglebius *pp++ = (isc_uint8_t)val; 151290001Sglebius c = *++cp; 152290001Sglebius } else 153290001Sglebius break; 154290001Sglebius } 155290001Sglebius /* 156290001Sglebius * Check for trailing characters. 157290001Sglebius */ 158290001Sglebius if (c != '\0' && (!isascii(c) || !isspace(c))) 159290001Sglebius return (0); 160290001Sglebius /* 161290001Sglebius * Did we get a valid digit? 162290001Sglebius */ 163290001Sglebius if (!digit) 164290001Sglebius return (0); 165290001Sglebius /* 166290001Sglebius * Concoct the address according to 167290001Sglebius * the number of parts specified. 168290001Sglebius */ 169293896Sglebius switch (pp - parts + 1) { 170290001Sglebius case 1: /* a -- 32 bits */ 171290001Sglebius break; 172290001Sglebius 173290001Sglebius case 2: /* a.b -- 8.24 bits */ 174290001Sglebius if (val > 0xffffffU) 175290001Sglebius return (0); 176290001Sglebius val |= parts[0] << 24; 177290001Sglebius break; 178290001Sglebius 179290001Sglebius case 3: /* a.b.c -- 8.8.16 bits */ 180290001Sglebius if (val > 0xffffU) 181290001Sglebius return (0); 182290001Sglebius val |= (parts[0] << 24) | (parts[1] << 16); 183290001Sglebius break; 184290001Sglebius 185290001Sglebius case 4: /* a.b.c.d -- 8.8.8.8 bits */ 186290001Sglebius if (val > 0xffU) 187290001Sglebius return (0); 188290001Sglebius val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 189290001Sglebius break; 190290001Sglebius } 191290001Sglebius if (addr != NULL) 192290001Sglebius addr->s_addr = htonl(val); 193290001Sglebius 194290001Sglebius return (1); 195290001Sglebius} 196