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