1139823Simp/* 21541Srgrimes * Portions Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC") 31541Srgrimes * Portions Copyright (C) 1996-2001 Internet Software Consortium. 41541Srgrimes * 51541Srgrimes * Permission to use, copy, modify, and/or distribute this software for any 61541Srgrimes * purpose with or without fee is hereby granted, provided that the above 71541Srgrimes * copyright notice and this permission notice appear in all copies. 81541Srgrimes * 91541Srgrimes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 101541Srgrimes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 111541Srgrimes * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 121541Srgrimes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 131541Srgrimes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 141541Srgrimes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 151541Srgrimes * PERFORMANCE OF THIS SOFTWARE. 161541Srgrimes */ 171541Srgrimes 181541Srgrimes/* 191541Srgrimes * Copyright (c) 1983, 1990, 1993 201541Srgrimes * The Regents of the University of California. All rights reserved. 211541Srgrimes * 221541Srgrimes * Redistribution and use in source and binary forms, with or without 231541Srgrimes * modification, are permitted provided that the following conditions 241541Srgrimes * are met: 251541Srgrimes * 1. Redistributions of source code must retain the above copyright 261541Srgrimes * notice, this list of conditions and the following disclaimer. 271541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 281541Srgrimes * notice, this list of conditions and the following disclaimer in the 2985050Sru * documentation and/or other materials provided with the distribution. 3050477Speter * 3. All advertising materials mentioning features or use of this software 311541Srgrimes * must display the following acknowledgement: 321541Srgrimes * This product includes software developed by the University of 3331778Seivind * California, Berkeley and its contributors. 3454263Sshin * 4. Neither the name of the University nor the names of its contributors 3555276Sshin * may be used to endorse or promote products derived from this software 3631778Seivind * without specific prior written permission. 371541Srgrimes * 38135568Sbrooks * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 3984106Sjlemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4029024Sbde * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 41135568Sbrooks * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 4284817Sjlemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 431541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 441541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 45164033Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 461541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 471541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 481541Srgrimes * SUCH DAMAGE. 491541Srgrimes */ 501541Srgrimes 51185808Sbz/* 52191367Srwatson * Portions Copyright (c) 1993 by Digital Equipment Corporation. 53191816Szec * 54185808Sbz * Permission to use, copy, modify, and distribute this software for any 5524204Sbde * purpose with or without fee is hereby granted, provided that the above 5610957Swollman * copyright notice and this permission notice appear in all copies, and that 5712942Swollman * the name of Digital Equipment Corporation not be used in advertising or 58132712Srwatson * publicity pertaining to distribution of the document or software without 59121161Sume * specific, written prior permission. 6072786Srwatson * 61223735Sbz * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 62223735Sbz * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 63103900Sbrooks * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 64186119Sqingli * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 651541Srgrimes * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 661541Srgrimes * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 67184726Sbz * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 68130933Sbrooks * SOFTWARE. 691541Srgrimes */ 7064651Sarchie/*! \file */ 7179103Sbrooks 723419Sphk#if defined(LIBC_SCCS) && !defined(lint) 7354728Simpstatic char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 74185571Sbzstatic char rcsid[] = "$Id: inet_aton.c,v 1.23 2008/12/01 23:47:45 tbox Exp $"; 751541Srgrimes#endif /* LIBC_SCCS and not lint */ 7655276Sshin 77265292Srmacklem#include <config.h> 7853541Sshin 7955276Sshin#include <ctype.h> 80252781Sandre#include <stddef.h> /* Required for NULL. */ 81211193Swill 82252781Sandre#include <isc/types.h> 83252781Sandre#include <isc/net.h> 84252781Sandre 8556938Sshin/*% 8663241Sitojun * Check whether "cp" is a valid ascii representation 8763241Sitojun * of an Internet address and convert to a binary address. 88252781Sandre * Returns 1 if the address is valid, 0 if not. 89252781Sandre * This replaces inet_addr, the return value from which 9053541Sshin * cannot distinguish between failure and a local broadcast address. 91163606Srwatson */ 92163606Srwatsonint 93214136Spluknetisc_net_aton(const char *cp, struct in_addr *addr) { 94214136Spluknet unsigned long val; 95214136Spluknet int base; 96214136Spluknet unsigned char c; 97214136Spluknet isc_uint8_t parts[4]; 98195175Sbrooks isc_uint8_t *pp = parts; 99195175Sbrooks int digit; 100195175Sbrooks 101195175Sbrooks c = *cp; 102143464Sglebius for (;;) { 103143464Sglebius /* 104143464Sglebius * Collect number up to ``.''. 105207554Ssobomax * Values are specified as for C: 106217322Smdf * 0x=hex, 0=octal, isdigit=decimal. 107207554Ssobomax */ 108207554Ssobomax if (!isdigit(c & 0xff)) 109143464Sglebius return (0); 110143464Sglebius val = 0; base = 10; digit = 0; 111143464Sglebius if (c == '0') { 112143464Sglebius c = *++cp; 113143464Sglebius if (c == 'x' || c == 'X') 114143464Sglebius base = 16, c = *++cp; 115143464Sglebius else { 116203052Sdelphij base = 8; 117203052Sdelphij digit = 1; 118203052Sdelphij } 119203052Sdelphij } 120203052Sdelphij for (;;) { 121203052Sdelphij /* 122249132Smav * isascii() is valid for all integer values, and 123203052Sdelphij * when it is true, c is known to be in scope 124203052Sdelphij * for isdigit(). No cast necessary. Similar 125203052Sdelphij * comment applies for later ctype uses. 126203052Sdelphij */ 127203052Sdelphij if (isascii(c) && isdigit(c)) { 128236051Sthompsa if (base == 8 && (c == '8' || c == '9')) 129139903Sglebius return (0); 130168793Sthompsa val = (val * base) + (c - '0'); 131211193Swill c = *++cp; 132211157Swill digit = 1; 133211193Swill } else if (base == 16 && isascii(c) && isxdigit(c)) { 134211193Swill val = (val << 4) | 135211193Swill (c + 10 - (islower(c) ? 'a' : 'A')); 136211193Swill c = *++cp; 137211193Swill digit = 1; 138211193Swill } else 139211193Swill break; 140211193Swill } 141211193Swill if (c == '.') { 142211193Swill /* 143211193Swill * Internet format: 144211193Swill * a.b.c.d 145211193Swill * a.b.c (with c treated as 16 bits) 146211193Swill * a.b (with b treated as 24 bits) 147139903Sglebius */ 148130508Smlaier if (pp >= parts + 3 || val > 0xffU) 149130508Smlaier return (0); 150167729Sbms *pp++ = (isc_uint8_t)val; 151167729Sbms c = *++cp; 152167729Sbms } else 153167729Sbms break; 154167729Sbms } 155121161Sume /* 156121161Sume * Check for trailing characters. 15783129Sjlemon */ 158167729Sbms if (c != '\0' && (!isascii(c) || !isspace(c))) 15983130Sjlemon return (0); 160196510Srwatson /* 161128407Smlaier * Did we get a valid digit? 162147986Syar */ 163185162Skmacy if (!digit) 164128407Smlaier return (0); 16585074Sru /* 16683129Sjlemon * Concoct the address according to 16784106Sjlemon * the number of parts specified. 168167729Sbms */ 169145320Sglebius switch (pp - parts + 1) { 170159781Smlaier case 1: /* a -- 32 bits */ 171159781Smlaier break; 172190903Smlaier 173192605Szec case 2: /* a.b -- 8.24 bits */ 174192605Szec if (val > 0xffffffU) 175185162Skmacy return (0); 17653541Sshin val |= parts[0] << 24; 1771541Srgrimes break; 17853541Sshin 17953541Sshin case 3: /* a.b.c -- 8.8.16 bits */ 18053541Sshin if (val > 0xffffU) 18192725Salfred return (0); 18253541Sshin val |= (parts[0] << 24) | (parts[1] << 16); 18353541Sshin break; 184207369Sbz 185207369Sbz case 4: /* a.b.c.d -- 8.8.8.8 bits */ 186195699Srwatson if (val > 0xffU) 187195699Srwatson return (0); 188207369Sbz val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 189215701Sdim break; 190195699Srwatson } 191196481Srwatson if (addr != NULL) 192214333Sbz addr->s_addr = htonl(val); 193185088Szec 194195727Srwatson return (1); 195195727Srwatson} 196195699Srwatson