/*-1* SPDX-License-Identifier: (BSD-3-Clause AND ISC)2*3* Copyright (c) 1983, 1990, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031/*32* Portions Copyright (c) 1993 by Digital Equipment Corporation.33*34* Permission to use, copy, modify, and distribute this software for any35* purpose with or without fee is hereby granted, provided that the above36* copyright notice and this permission notice appear in all copies, and that37* the name of Digital Equipment Corporation not be used in advertising or38* publicity pertaining to distribution of the document or software without39* specific, written prior permission.40*41* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL42* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES43* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT44* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL45* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR46* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS47* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS48* SOFTWARE.49*/5051/*52* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")53* Portions Copyright (c) 1996-1999 by Internet Software Consortium.54*55* Permission to use, copy, modify, and distribute this software for any56* purpose with or without fee is hereby granted, provided that the above57* copyright notice and this permission notice appear in all copies.58*59* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES60* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF61* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR62* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES63* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN64* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT65* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.66*/6768#include "port_before.h"6970#include <sys/param.h>7172#include <netinet/in.h>73#include <arpa/inet.h>7475#include <ctype.h>7677#include "port_after.h"7879/*%80* Ascii internet address interpretation routine.81* The value returned is in network order.82*/83in_addr_t /* XXX should be struct in_addr :( */84inet_addr(const char *cp) {85struct in_addr val;8687if (inet_aton(cp, &val))88return (val.s_addr);89return (INADDR_NONE);90}9192/*%93* Check whether "cp" is a valid ascii representation94* of an Internet address and convert to a binary address.95* Returns 1 if the address is valid, 0 if not.96* This replaces inet_addr, the return value from which97* cannot distinguish between failure and a local broadcast address.98*/99int100inet_aton(const char *cp, struct in_addr *addr) {101u_long val;102int base, n;103char c;104u_int8_t parts[4];105u_int8_t *pp = parts;106int digit;107108c = *cp;109for (;;) {110/*111* Collect number up to ``.''.112* Values are specified as for C:113* 0x=hex, 0=octal, isdigit=decimal.114*/115if (!isdigit((unsigned char)c))116return (0);117val = 0; base = 10; digit = 0;118if (c == '0') {119c = *++cp;120if (c == 'x' || c == 'X')121base = 16, c = *++cp;122else {123base = 8;124digit = 1 ;125}126}127for (;;) {128if (isascii(c) && isdigit((unsigned char)c)) {129if (base == 8 && (c == '8' || c == '9'))130return (0);131val = (val * base) + (c - '0');132c = *++cp;133digit = 1;134} else if (base == 16 && isascii(c) &&135isxdigit((unsigned char)c)) {136val = (val << 4) |137(c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));138c = *++cp;139digit = 1;140} else141break;142}143if (c == '.') {144/*145* Internet format:146* a.b.c.d147* a.b.c (with c treated as 16 bits)148* a.b (with b treated as 24 bits)149*/150if (pp >= parts + 3 || val > 0xffU)151return (0);152*pp++ = val;153c = *++cp;154} else155break;156}157/*158* Check for trailing characters.159*/160if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))161return (0);162/*163* Did we get a valid digit?164*/165if (!digit)166return (0);167/*168* Concoct the address according to169* the number of parts specified.170*/171n = pp - parts + 1;172switch (n) {173case 1: /*%< a -- 32 bits */174break;175176case 2: /*%< a.b -- 8.24 bits */177if (val > 0xffffffU)178return (0);179val |= (uint32_t)parts[0] << 24;180break;181182case 3: /*%< a.b.c -- 8.8.16 bits */183if (val > 0xffffU)184return (0);185val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16);186break;187188case 4: /*%< a.b.c.d -- 8.8.8.8 bits */189if (val > 0xffU)190return (0);191val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16) |192(parts[2] << 8);193break;194}195if (addr != NULL)196addr->s_addr = htonl(val);197return (1);198}199200/*201* Weak aliases for applications that use certain private entry points,202* and fail to include <arpa/inet.h>.203*/204#undef inet_addr205__weak_reference(__inet_addr, inet_addr);206#undef inet_aton207__weak_reference(__inet_aton, inet_aton);208209/*! \file */210211212