/* From openssh4.3p2 compat/inet_aton.c */1/*2* Copyright (c) 1983, 1990, 19933* The Regents of the University of California. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13* 3. Neither the name of the University nor the names of its contributors14* may be used to endorse or promote products derived from this software15* without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28* -29* Portions Copyright (c) 1993 by Digital Equipment Corporation.30*31* Permission to use, copy, modify, and distribute this software for any32* purpose with or without fee is hereby granted, provided that the above33* copyright notice and this permission notice appear in all copies, and that34* the name of Digital Equipment Corporation not be used in advertising or35* publicity pertaining to distribution of the document or software without36* specific, written prior permission.37*38* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL39* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES40* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT41* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL42* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR43* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS44* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS45* SOFTWARE.46* -47* --Copyright--48*/4950/* OPENBSD ORIGINAL: lib/libc/net/inet_addr.c */5152#include <ldns/config.h>5354#if !defined(HAVE_INET_ATON)5556#include <sys/types.h>57#include <sys/param.h>58#ifdef HAVE_NETINET_IN_H59#include <netinet/in.h>60#endif61#ifdef HAVE_ARPA_INET_H62#include <arpa/inet.h>63#endif64#include <ctype.h>6566#if 067/*68* Ascii internet address interpretation routine.69* The value returned is in network order.70*/71in_addr_t72inet_addr(const char *cp)73{74struct in_addr val;7576if (inet_aton(cp, &val))77return (val.s_addr);78return (INADDR_NONE);79}80#endif8182/*83* Check whether "cp" is a valid ascii representation84* of an Internet address and convert to a binary address.85* Returns 1 if the address is valid, 0 if not.86* This replaces inet_addr, the return value from which87* cannot distinguish between failure and a local broadcast address.88*/89int90inet_aton(const char *cp, struct in_addr *addr)91{92uint32_t val;93int base, n;94char c;95unsigned int parts[4];96unsigned int *pp = parts;9798c = *cp;99for (;;) {100/*101* Collect number up to ``.''.102* Values are specified as for C:103* 0x=hex, 0=octal, isdigit=decimal.104*/105if (!isdigit((int) c))106return (0);107val = 0; base = 10;108if (c == '0') {109c = *++cp;110if (c == 'x' || c == 'X')111base = 16, c = *++cp;112else113base = 8;114}115for (;;) {116if (isascii((int) c) && isdigit((int) c)) {117val = (val * base) + (c - '0');118c = *++cp;119} else if (base == 16 && isascii((int) c) && isxdigit((int) c)) {120val = (val << 4) |121(c + 10 - (islower((int) c) ? 'a' : 'A'));122c = *++cp;123} else124break;125}126if (c == '.') {127/*128* Internet format:129* a.b.c.d130* a.b.c (with c treated as 16 bits)131* a.b (with b treated as 24 bits)132*/133if (pp >= parts + 3)134return (0);135*pp++ = val;136c = *++cp;137} else138break;139}140/*141* Check for trailing characters.142*/143if (c != '\0' && (!isascii((int) c) || !isspace((int) c)))144return (0);145/*146* Concoct the address according to147* the number of parts specified.148*/149n = pp - parts + 1;150switch (n) {151152case 0:153return (0); /* initial nondigit */154155case 1: /* a -- 32 bits */156break;157158case 2: /* a.b -- 8.24 bits */159if ((val > 0xffffff) || (parts[0] > 0xff))160return (0);161val |= parts[0] << 24;162break;163164case 3: /* a.b.c -- 8.8.16 bits */165if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))166return (0);167val |= (parts[0] << 24) | (parts[1] << 16);168break;169170case 4: /* a.b.c.d -- 8.8.8.8 bits */171if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))172return (0);173val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);174break;175}176if (addr)177addr->s_addr = htonl(val);178return (1);179}180181#endif /* !defined(HAVE_INET_ATON) */182183184