lwinetaton.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
6ae232055d4d8a97267517c5e50074c2c819941and/*
6ae232055d4d8a97267517c5e50074c2c819941and * Portions Copyright (C) 1996-2001, 2003-2005, 2007, 2012-2014, 2016 Internet Systems Consortium, Inc. ("ISC")
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * This Source Code Form is subject to the terms of the Mozilla Public
6ae232055d4d8a97267517c5e50074c2c819941and * License, v. 2.0. If a copy of the MPL was not distributed with this
6ae232055d4d8a97267517c5e50074c2c819941and * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and/*
6ae232055d4d8a97267517c5e50074c2c819941and * Copyright (c) 1983, 1990, 1993
6ae232055d4d8a97267517c5e50074c2c819941and * The Regents of the University of California. All rights reserved.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Redistribution and use in source and binary forms, with or without
6ae232055d4d8a97267517c5e50074c2c819941and * modification, are permitted provided that the following conditions
6ae232055d4d8a97267517c5e50074c2c819941and * are met:
6ae232055d4d8a97267517c5e50074c2c819941and * 1. Redistributions of source code must retain the above copyright
6ae232055d4d8a97267517c5e50074c2c819941and * notice, this list of conditions and the following disclaimer.
6ae232055d4d8a97267517c5e50074c2c819941and * 2. Redistributions in binary form must reproduce the above copyright
6ae232055d4d8a97267517c5e50074c2c819941and * notice, this list of conditions and the following disclaimer in the
6ae232055d4d8a97267517c5e50074c2c819941and * documentation and/or other materials provided with the distribution.
6ae232055d4d8a97267517c5e50074c2c819941and * 3. Neither the name of the University nor the names of its contributors
4b3a8afbfcea8b265d179a122bf40dfedd1ce280takashi * may be used to endorse or promote products derived from this software
6ae232055d4d8a97267517c5e50074c2c819941and * without specific prior written permission.
af84459fbf938e508fd10b01cb8d699c79083813takashi *
6ae232055d4d8a97267517c5e50074c2c819941and * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6ae232055d4d8a97267517c5e50074c2c819941and * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6ae232055d4d8a97267517c5e50074c2c819941and * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6ae232055d4d8a97267517c5e50074c2c819941and * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6ae232055d4d8a97267517c5e50074c2c819941and * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6ae232055d4d8a97267517c5e50074c2c819941and * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6ae232055d4d8a97267517c5e50074c2c819941and * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6ae232055d4d8a97267517c5e50074c2c819941and * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6ae232055d4d8a97267517c5e50074c2c819941and * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6ae232055d4d8a97267517c5e50074c2c819941and * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6ae232055d4d8a97267517c5e50074c2c819941and * SUCH DAMAGE.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and/*
6ae232055d4d8a97267517c5e50074c2c819941and * Portions Copyright (c) 1993 by Digital Equipment Corporation.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Permission to use, copy, modify, and distribute this software for any
6ae232055d4d8a97267517c5e50074c2c819941and * purpose with or without fee is hereby granted, provided that the above
6ae232055d4d8a97267517c5e50074c2c819941and * copyright notice and this permission notice appear in all copies, and that
6ae232055d4d8a97267517c5e50074c2c819941and * the name of Digital Equipment Corporation not be used in advertising or
6ae232055d4d8a97267517c5e50074c2c819941and * publicity pertaining to distribution of the document or software without
6ae232055d4d8a97267517c5e50074c2c819941and * specific, written prior permission.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
6ae232055d4d8a97267517c5e50074c2c819941and * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
6ae232055d4d8a97267517c5e50074c2c819941and * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
6ae232055d4d8a97267517c5e50074c2c819941and * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
6ae232055d4d8a97267517c5e50074c2c819941and * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
6ae232055d4d8a97267517c5e50074c2c819941and * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
6ae232055d4d8a97267517c5e50074c2c819941and * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
6ae232055d4d8a97267517c5e50074c2c819941and * SOFTWARE.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and/*! \file lwinetaton.c
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and#if defined(LIBC_SCCS) && !defined(lint)
6ae232055d4d8a97267517c5e50074c2c819941andstatic char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
6ae232055d4d8a97267517c5e50074c2c819941andstatic char rcsid[] = "$Id: lwinetaton.c,v 1.16 2007/06/19 23:47:22 tbox Exp $";
6ae232055d4d8a97267517c5e50074c2c819941and#endif /* LIBC_SCCS and not lint */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include <config.h>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include <ctype.h>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include <stddef.h>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include <lwres/int.h>
6ae232055d4d8a97267517c5e50074c2c819941and#include <lwres/net.h>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "assert_p.h"
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and/*!
6ae232055d4d8a97267517c5e50074c2c819941and * Check whether "cp" is a valid ascii representation
6ae232055d4d8a97267517c5e50074c2c819941and * of an Internet address and convert to a binary address.
6ae232055d4d8a97267517c5e50074c2c819941and * Returns 1 if the address is valid, 0 if not.
6ae232055d4d8a97267517c5e50074c2c819941and * This replaces inet_addr, the return value from which
6ae232055d4d8a97267517c5e50074c2c819941and * cannot distinguish between failure and a local broadcast address.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941andint
6ae232055d4d8a97267517c5e50074c2c819941andlwres_net_aton(const char *cp, struct in_addr *addr) {
6ae232055d4d8a97267517c5e50074c2c819941and lwres_uint32_t val;
6ae232055d4d8a97267517c5e50074c2c819941and int base;
6ae232055d4d8a97267517c5e50074c2c819941and ptrdiff_t n;
6ae232055d4d8a97267517c5e50074c2c819941and unsigned char c;
6ae232055d4d8a97267517c5e50074c2c819941and lwres_uint8_t parts[4];
6ae232055d4d8a97267517c5e50074c2c819941and lwres_uint8_t *pp = parts;
6ae232055d4d8a97267517c5e50074c2c819941and int digit;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and REQUIRE(cp != NULL);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and c = *cp;
6ae232055d4d8a97267517c5e50074c2c819941and for (;;) {
6ae232055d4d8a97267517c5e50074c2c819941and /*
6ae232055d4d8a97267517c5e50074c2c819941and * Collect number up to ``.''.
6ae232055d4d8a97267517c5e50074c2c819941and * Values are specified as for C:
6ae232055d4d8a97267517c5e50074c2c819941and * 0x=hex, 0=octal, isdigit=decimal.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and if (!isdigit(c & 0xff))
6ae232055d4d8a97267517c5e50074c2c819941and return (0);
6ae232055d4d8a97267517c5e50074c2c819941and val = 0;
6ae232055d4d8a97267517c5e50074c2c819941and base = 10;
6ae232055d4d8a97267517c5e50074c2c819941and digit = 0;
6ae232055d4d8a97267517c5e50074c2c819941and if (c == '0') {
6ae232055d4d8a97267517c5e50074c2c819941and c = *++cp;
6ae232055d4d8a97267517c5e50074c2c819941and if (c == 'x' || c == 'X') {
6ae232055d4d8a97267517c5e50074c2c819941and base = 16;
6ae232055d4d8a97267517c5e50074c2c819941and c = *++cp;
6ae232055d4d8a97267517c5e50074c2c819941and } else {
6ae232055d4d8a97267517c5e50074c2c819941and base = 8;
6ae232055d4d8a97267517c5e50074c2c819941and digit = 1;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and for (;;) {
6ae232055d4d8a97267517c5e50074c2c819941and /*
6ae232055d4d8a97267517c5e50074c2c819941and * isascii() is valid for all integer values, and
6ae232055d4d8a97267517c5e50074c2c819941and * when it is true, c is known to be in scope
6ae232055d4d8a97267517c5e50074c2c819941and * for isdigit(). No cast necessary. Similar
6ae232055d4d8a97267517c5e50074c2c819941and * comment applies for later ctype uses.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and if (isascii(c) && isdigit(c)) {
6ae232055d4d8a97267517c5e50074c2c819941and if (base == 8 && (c == '8' || c == '9'))
6ae232055d4d8a97267517c5e50074c2c819941and return (0);
6ae232055d4d8a97267517c5e50074c2c819941and val = (val * base) + (c - '0');
6ae232055d4d8a97267517c5e50074c2c819941and c = *++cp;
6ae232055d4d8a97267517c5e50074c2c819941and digit = 1;
6ae232055d4d8a97267517c5e50074c2c819941and } else if (base == 16 && isascii(c) && isxdigit(c)) {
6ae232055d4d8a97267517c5e50074c2c819941and val = (val << 4) |
6ae232055d4d8a97267517c5e50074c2c819941and (c + 10 - (islower(c) ? 'a' : 'A'));
6ae232055d4d8a97267517c5e50074c2c819941and c = *++cp;
6ae232055d4d8a97267517c5e50074c2c819941and digit = 1;
6ae232055d4d8a97267517c5e50074c2c819941and } else
6ae232055d4d8a97267517c5e50074c2c819941and break;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and if (c == '.') {
6ae232055d4d8a97267517c5e50074c2c819941and /*
6ae232055d4d8a97267517c5e50074c2c819941and * Internet format:
6ae232055d4d8a97267517c5e50074c2c819941and * a.b.c.d
6ae232055d4d8a97267517c5e50074c2c819941and * a.b.c (with c treated as 16 bits)
6ae232055d4d8a97267517c5e50074c2c819941and * a.b (with b treated as 24 bits)
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and if (pp >= parts + 3 || val > 0xffU)
6ae232055d4d8a97267517c5e50074c2c819941and return (0);
6ae232055d4d8a97267517c5e50074c2c819941and *pp++ = (lwres_uint8_t)val;
6ae232055d4d8a97267517c5e50074c2c819941and c = *++cp;
6ae232055d4d8a97267517c5e50074c2c819941and } else
6ae232055d4d8a97267517c5e50074c2c819941and break;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and /*
6ae232055d4d8a97267517c5e50074c2c819941and * Check for trailing characters.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and if (c != '\0' && (!isascii(c) || !isspace(c)))
6ae232055d4d8a97267517c5e50074c2c819941and return (0);
6ae232055d4d8a97267517c5e50074c2c819941and /*
6ae232055d4d8a97267517c5e50074c2c819941and * Did we get a valid digit?
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and if (!digit)
6ae232055d4d8a97267517c5e50074c2c819941and return (0);
6ae232055d4d8a97267517c5e50074c2c819941and /*
6ae232055d4d8a97267517c5e50074c2c819941and * Concoct the address according to
6ae232055d4d8a97267517c5e50074c2c819941and * the number of parts specified.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and n = pp - parts + 1;
4b3a8afbfcea8b265d179a122bf40dfedd1ce280takashi switch (n) {
6ae232055d4d8a97267517c5e50074c2c819941and case 1: /* a -- 32 bits */
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and case 2: /* a.b -- 8.24 bits */
6ae232055d4d8a97267517c5e50074c2c819941and if (val > 0xffffffU)
6ae232055d4d8a97267517c5e50074c2c819941and return (0);
6ae232055d4d8a97267517c5e50074c2c819941and val |= parts[0] << 24;
6ae232055d4d8a97267517c5e50074c2c819941and break;
6ae232055d4d8a97267517c5e50074c2c819941and
case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffffU)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16);
break;
case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xffU)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
}
if (addr != NULL)
addr->s_addr = htonl(val);
return (1);
}