1181834Sroberto/* 2181834Sroberto * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") 3181834Sroberto * Portions Copyright (C) 1996-2001 Internet Software Consortium. 4181834Sroberto * 5181834Sroberto * Permission to use, copy, modify, and distribute this software for any 6181834Sroberto * purpose with or without fee is hereby granted, provided that the above 7181834Sroberto * copyright notice and this permission notice appear in all copies. 8181834Sroberto * 9181834Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10181834Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11181834Sroberto * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12181834Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13181834Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14181834Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15181834Sroberto * PERFORMANCE OF THIS SOFTWARE. 16181834Sroberto */ 17181834Sroberto 18181834Sroberto/* 19181834Sroberto * Copyright (c) 1983, 1990, 1993 20181834Sroberto * The Regents of the University of California. All rights reserved. 21181834Sroberto * 22181834Sroberto * Redistribution and use in source and binary forms, with or without 23181834Sroberto * modification, are permitted provided that the following conditions 24181834Sroberto * are met: 25181834Sroberto * 1. Redistributions of source code must retain the above copyright 26181834Sroberto * notice, this list of conditions and the following disclaimer. 27181834Sroberto * 2. Redistributions in binary form must reproduce the above copyright 28181834Sroberto * notice, this list of conditions and the following disclaimer in the 29181834Sroberto * documentation and/or other materials provided with the distribution. 30181834Sroberto * 3. All advertising materials mentioning features or use of this software 31181834Sroberto * must display the following acknowledgement: 32181834Sroberto * This product includes software developed by the University of 33181834Sroberto * California, Berkeley and its contributors. 34181834Sroberto * 4. Neither the name of the University nor the names of its contributors 35181834Sroberto * may be used to endorse or promote products derived from this software 36181834Sroberto * without specific prior written permission. 37181834Sroberto * 38181834Sroberto * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 39181834Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40181834Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 41181834Sroberto * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 42181834Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 43181834Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 44181834Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 45181834Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 46181834Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47181834Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48181834Sroberto * SUCH DAMAGE. 49181834Sroberto */ 50181834Sroberto 51181834Sroberto/* 52181834Sroberto * Portions Copyright (c) 1993 by Digital Equipment Corporation. 53181834Sroberto * 54181834Sroberto * Permission to use, copy, modify, and distribute this software for any 55181834Sroberto * purpose with or without fee is hereby granted, provided that the above 56181834Sroberto * copyright notice and this permission notice appear in all copies, and that 57181834Sroberto * the name of Digital Equipment Corporation not be used in advertising or 58181834Sroberto * publicity pertaining to distribution of the document or software without 59181834Sroberto * specific, written prior permission. 60181834Sroberto * 61181834Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 62181834Sroberto * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 63181834Sroberto * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 64181834Sroberto * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 65181834Sroberto * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 66181834Sroberto * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 67181834Sroberto * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 68181834Sroberto * SOFTWARE. 69181834Sroberto */ 70181834Sroberto 71181834Sroberto#if defined(LIBC_SCCS) && !defined(lint) 72181834Srobertostatic char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 73181834Srobertostatic char rcsid[] = "$Id: inet_aton.c,v 1.15.12.3 2004/03/08 09:04:49 marka Exp $"; 74181834Sroberto#endif /* LIBC_SCCS and not lint */ 75181834Sroberto 76181834Sroberto#include <config.h> 77181834Sroberto 78181834Sroberto#include <ctype.h> 79181834Sroberto#include <stddef.h> /* Required for NULL. */ 80181834Sroberto 81181834Sroberto#include <isc/types.h> 82181834Sroberto#include <isc/net.h> 83181834Sroberto 84181834Sroberto/* 85181834Sroberto * Check whether "cp" is a valid ascii representation 86181834Sroberto * of an Internet address and convert to a binary address. 87181834Sroberto * Returns 1 if the address is valid, 0 if not. 88181834Sroberto * This replaces inet_addr, the return value from which 89181834Sroberto * cannot distinguish between failure and a local broadcast address. 90181834Sroberto */ 91181834Srobertoint 92181834Srobertoisc_net_aton(const char *cp, struct in_addr *addr) { 93181834Sroberto unsigned long val; 94181834Sroberto int base, n; 95181834Sroberto unsigned char c; 96181834Sroberto isc_uint8_t parts[4]; 97181834Sroberto isc_uint8_t *pp = parts; 98181834Sroberto int digit; 99181834Sroberto 100181834Sroberto c = *cp; 101181834Sroberto for (;;) { 102181834Sroberto /* 103181834Sroberto * Collect number up to ``.''. 104181834Sroberto * Values are specified as for C: 105181834Sroberto * 0x=hex, 0=octal, isdigit=decimal. 106181834Sroberto */ 107181834Sroberto if (!isdigit(c & 0xff)) 108181834Sroberto return (0); 109181834Sroberto val = 0; base = 10; digit = 0; 110181834Sroberto if (c == '0') { 111181834Sroberto c = *++cp; 112181834Sroberto if (c == 'x' || c == 'X') 113181834Sroberto base = 16, c = *++cp; 114181834Sroberto else { 115181834Sroberto base = 8; 116181834Sroberto digit = 1; 117181834Sroberto } 118181834Sroberto } 119181834Sroberto for (;;) { 120181834Sroberto /* 121181834Sroberto * isascii() is valid for all integer values, and 122181834Sroberto * when it is true, c is known to be in scope 123181834Sroberto * for isdigit(). No cast necessary. Similar 124181834Sroberto * comment applies for later ctype uses. 125181834Sroberto */ 126181834Sroberto if (isascii(c) && isdigit(c)) { 127181834Sroberto if (base == 8 && (c == '8' || c == '9')) 128181834Sroberto return (0); 129181834Sroberto val = (val * base) + (c - '0'); 130181834Sroberto c = *++cp; 131181834Sroberto digit = 1; 132181834Sroberto } else if (base == 16 && isascii(c) && isxdigit(c)) { 133181834Sroberto val = (val << 4) | 134181834Sroberto (c + 10 - (islower(c) ? 'a' : 'A')); 135181834Sroberto c = *++cp; 136181834Sroberto digit = 1; 137181834Sroberto } else 138181834Sroberto break; 139181834Sroberto } 140181834Sroberto if (c == '.') { 141181834Sroberto /* 142181834Sroberto * Internet format: 143181834Sroberto * a.b.c.d 144181834Sroberto * a.b.c (with c treated as 16 bits) 145181834Sroberto * a.b (with b treated as 24 bits) 146181834Sroberto */ 147181834Sroberto if (pp >= parts + 3 || val > 0xff) 148181834Sroberto return (0); 149181834Sroberto *pp++ = (isc_uint8_t)val; 150181834Sroberto c = *++cp; 151181834Sroberto } else 152181834Sroberto break; 153181834Sroberto } 154181834Sroberto /* 155181834Sroberto * Check for trailing characters. 156181834Sroberto */ 157181834Sroberto if (c != '\0' && (!isascii(c) || !isspace(c))) 158181834Sroberto return (0); 159181834Sroberto /* 160181834Sroberto * Did we get a valid digit? 161181834Sroberto */ 162181834Sroberto if (!digit) 163181834Sroberto return (0); 164181834Sroberto /* 165181834Sroberto * Concoct the address according to 166181834Sroberto * the number of parts specified. 167181834Sroberto */ 168181834Sroberto n = pp - parts + 1; 169181834Sroberto switch (n) { 170181834Sroberto case 1: /* a -- 32 bits */ 171181834Sroberto break; 172181834Sroberto 173181834Sroberto case 2: /* a.b -- 8.24 bits */ 174181834Sroberto if (val > 0xffffff) 175181834Sroberto return (0); 176181834Sroberto val |= parts[0] << 24; 177181834Sroberto break; 178181834Sroberto 179181834Sroberto case 3: /* a.b.c -- 8.8.16 bits */ 180181834Sroberto if (val > 0xffff) 181181834Sroberto return (0); 182181834Sroberto val |= (parts[0] << 24) | (parts[1] << 16); 183181834Sroberto break; 184181834Sroberto 185181834Sroberto case 4: /* a.b.c.d -- 8.8.8.8 bits */ 186181834Sroberto if (val > 0xff) 187181834Sroberto return (0); 188181834Sroberto val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 189181834Sroberto break; 190181834Sroberto } 191181834Sroberto if (addr != NULL) 192181834Sroberto addr->s_addr = htonl(val); 193181834Sroberto 194181834Sroberto return (1); 195181834Sroberto} 196