inet_aton.c revision dafcb997e390efa4423883dafd100c975c4095d6
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews/*
1ca2cf024391992fe14b2df7d3ae0f575d074452Evan Hunt * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Portions Copyright (C) 1996-2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * copyright notice and this permission notice appear in all copies.
f97d56e757b9a293ffbaa915ca4d792ae84ba85aTinderbox User *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * PERFORMANCE OF THIS SOFTWARE.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
6eba31815af00edc72a43ae1a79f84bd9eeb284eMark Andrews
8c9fba44a41e3ea23e7e8405029980aba672f7ceEvan Hunt/*
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Copyright (c) 1983, 1990, 1993
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * The Regents of the University of California. All rights reserved.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Redistribution and use in source and binary forms, with or without
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * modification, are permitted provided that the following conditions
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * are met:
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * 1. Redistributions of source code must retain the above copyright
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * notice, this list of conditions and the following disclaimer.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * 2. Redistributions in binary form must reproduce the above copyright
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * notice, this list of conditions and the following disclaimer in the
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * documentation and/or other materials provided with the distribution.
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * 3. All advertising materials mentioning features or use of this software
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * must display the following acknowledgement:
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * This product includes software developed by the University of
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * California, Berkeley and its contributors.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * 4. Neither the name of the University nor the names of its contributors
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * may be used to endorse or promote products derived from this software
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * without specific prior written permission.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b947e1a521c6931f787d6d1b3604d5b138170c3dMukund Sivaraman * SUCH DAMAGE.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews/*
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Portions Copyright (c) 1993 by Digital Equipment Corporation.
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt *
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Permission to use, copy, modify, and distribute this software for any
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * purpose with or without fee is hereby granted, provided that the above
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * copyright notice and this permission notice appear in all copies, and that
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * the name of Digital Equipment Corporation not be used in advertising or
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * publicity pertaining to distribution of the document or software without
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * specific, written prior permission.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * SOFTWARE.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#if defined(LIBC_SCCS) && !defined(lint)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsstatic char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsstatic char rcsid[] = "$Id: inet_aton.c,v 1.17 2004/03/05 05:10:45 marka Exp $";
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#endif /* LIBC_SCCS and not lint */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <config.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <ctype.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <stddef.h> /* Required for NULL. */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <isc/types.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <isc/net.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews/*
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Check whether "cp" is a valid ascii representation
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * of an Internet address and convert to a binary address.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Returns 1 if the address is valid, 0 if not.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * This replaces inet_addr, the return value from which
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * cannot distinguish between failure and a local broadcast address.
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt */
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntint
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntisc_net_aton(const char *cp, struct in_addr *addr) {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt unsigned long val;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt int base, n;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews unsigned char c;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_uint8_t parts[4];
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_uint8_t *pp = parts;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt int digit;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt c = *cp;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt for (;;) {
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews /*
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Collect number up to ``.''.
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * Values are specified as for C:
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * 0x=hex, 0=octal, isdigit=decimal.
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt */
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (!isdigit(c & 0xff))
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (0);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt val = 0; base = 10; digit = 0;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (c == '0') {
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt c = *++cp;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (c == 'x' || c == 'X')
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews base = 16, c = *++cp;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt else {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt base = 8;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt digit = 1;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews }
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt }
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt for (;;) {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt /*
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * isascii() is valid for all integer values, and
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * when it is true, c is known to be in scope
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * for isdigit(). No cast necessary. Similar
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * comment applies for later ctype uses.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (isascii(c) && isdigit(c)) {
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (base == 8 && (c == '8' || c == '9'))
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (0);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt val = (val * base) + (c - '0');
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt c = *++cp;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt digit = 1;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt } else if (base == 16 && isascii(c) && isxdigit(c)) {
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt val = (val << 4) |
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt (c + 10 - (islower(c) ? 'a' : 'A'));
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt c = *++cp;
b947e1a521c6931f787d6d1b3604d5b138170c3dMukund Sivaraman digit = 1;
b947e1a521c6931f787d6d1b3604d5b138170c3dMukund Sivaraman } else
b947e1a521c6931f787d6d1b3604d5b138170c3dMukund Sivaraman break;
b947e1a521c6931f787d6d1b3604d5b138170c3dMukund Sivaraman }
b947e1a521c6931f787d6d1b3604d5b138170c3dMukund Sivaraman if (c == '.') {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt /*
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Internet format:
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * a.b.c.d
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * a.b.c (with c treated as 16 bits)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * a.b (with b treated as 24 bits)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (pp >= parts + 3 || val > 0xff)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (0);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt *pp++ = (isc_uint8_t)val;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt c = *++cp;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt } else
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt break;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt }
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt /*
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Check for trailing characters.
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt */
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (c != '\0' && (!isascii(c) || !isspace(c)))
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (0);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt /*
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Did we get a valid digit?
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt */
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (!digit)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (0);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt /*
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Concoct the address according to
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * the number of parts specified.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver n = pp - parts + 1;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews switch (n) {
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt case 1: /* a -- 32 bits */
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt break;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews case 2: /* a.b -- 8.24 bits */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (val > 0xffffff)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews return (0);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews val |= parts[0] << 24;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews break;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews case 3: /* a.b.c -- 8.8.16 bits */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (val > 0xffff)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews return (0);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews val |= (parts[0] << 24) | (parts[1] << 16);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt break;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews case 4: /* a.b.c.d -- 8.8.8.8 bits */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (val > 0xff)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews return (0);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt break;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt }
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (addr != NULL)
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt addr->s_addr = htonl(val);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (1);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt}
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt