inet_aton.c revision dafcb997e390efa4423883dafd100c975c4095d6
1ca2cf024391992fe14b2df7d3ae0f575d074452Evan Hunt * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Portions Copyright (C) 1996-2001 Internet Software Consortium.
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.
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 * Copyright (c) 1983, 1990, 1993
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * The Regents of the University of California. All rights reserved.
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 * 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.
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 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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 * 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 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 * 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 Huntisc_net_aton(const char *cp, struct in_addr *addr) {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt unsigned long val;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews unsigned char c;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Collect number up to ``.''.
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * Values are specified as for C:
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * 0x=hex, 0=octal, isdigit=decimal.
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (c == '0') {
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.
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt } else if (base == 16 && isascii(c) && isxdigit(c)) {
b947e1a521c6931f787d6d1b3604d5b138170c3dMukund Sivaraman if (c == '.') {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Internet format:
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * a.b.c (with c treated as 16 bits)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * a.b (with b treated as 24 bits)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Check for trailing characters.
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Did we get a valid digit?
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Concoct the address according to
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * the number of parts specified.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews switch (n) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);