|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* Copyright (c) 1983, 1990, 1993
|
|
Packit |
6c4009 |
* The Regents of the University of California. All rights reserved.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
6c4009 |
* modification, are permitted provided that the following conditions
|
|
Packit |
6c4009 |
* are met:
|
|
Packit |
6c4009 |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
6c4009 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
6c4009 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
6c4009 |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
6c4009 |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
6c4009 |
* 4. Neither the name of the University nor the names of its contributors
|
|
Packit |
6c4009 |
* may be used to endorse or promote products derived from this software
|
|
Packit |
6c4009 |
* without specific prior written permission.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
6c4009 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
6c4009 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
6c4009 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
Packit |
6c4009 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
6c4009 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
6c4009 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
6c4009 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
6c4009 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
6c4009 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
6c4009 |
* SUCH DAMAGE.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* Portions Copyright (c) 1993 by Digital Equipment Corporation.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* Permission to use, copy, modify, and distribute this software for any
|
|
Packit |
6c4009 |
* purpose with or without fee is hereby granted, provided that the above
|
|
Packit |
6c4009 |
* copyright notice and this permission notice appear in all copies, and that
|
|
Packit |
6c4009 |
* the name of Digital Equipment Corporation not be used in advertising or
|
|
Packit |
6c4009 |
* publicity pertaining to distribution of the document or software without
|
|
Packit |
6c4009 |
* specific, written prior permission.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
|
|
Packit |
6c4009 |
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
|
|
Packit |
6c4009 |
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
|
|
Packit |
6c4009 |
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
Packit |
6c4009 |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
Packit |
6c4009 |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
Packit |
6c4009 |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
Packit |
6c4009 |
* SOFTWARE.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* Portions Copyright (c) 1996-1999 by Internet Software Consortium.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* Permission to use, copy, modify, and distribute this software for any
|
|
Packit |
6c4009 |
* purpose with or without fee is hereby granted, provided that the above
|
|
Packit |
6c4009 |
* copyright notice and this permission notice appear in all copies.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
Packit |
6c4009 |
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
Packit |
6c4009 |
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
Packit |
6c4009 |
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
Packit |
6c4009 |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
Packit |
6c4009 |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
Packit |
6c4009 |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
Packit |
6c4009 |
* SOFTWARE.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <sys/types.h>
|
|
Packit |
6c4009 |
#include <sys/param.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <netinet/in.h>
|
|
Packit |
6c4009 |
#include <arpa/inet.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <ctype.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <endian.h>
|
|
Packit |
6c4009 |
#include <stdint.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <limits.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
|
|
Packit Service |
c82647 |
/*
|
|
Packit Service |
c82647 |
* Ascii internet address interpretation routine.
|
|
Packit Service |
c82647 |
* The value returned is in network order.
|
|
Packit Service |
c82647 |
*/
|
|
Packit Service |
c82647 |
in_addr_t
|
|
Packit Service |
c82647 |
__inet_addr(const char *cp) {
|
|
Packit Service |
c82647 |
struct in_addr val;
|
|
Packit Service |
fd7b69 |
|
|
Packit Service |
c82647 |
if (__inet_aton(cp, &val))
|
|
Packit Service |
c82647 |
return (val.s_addr);
|
|
Packit Service |
c82647 |
return (INADDR_NONE);
|
|
Packit Service |
fd7b69 |
}
|
|
Packit Service |
c82647 |
weak_alias (__inet_addr, inet_addr)
|
|
Packit Service |
fd7b69 |
|
|
Packit Service |
c82647 |
/*
|
|
Packit Service |
c82647 |
* Check whether "cp" is a valid ascii representation
|
|
Packit Service |
c82647 |
* of an Internet address and convert to a binary address.
|
|
Packit Service |
c82647 |
* Returns 1 if the address is valid, 0 if not.
|
|
Packit Service |
c82647 |
* This replaces inet_addr, the return value from which
|
|
Packit Service |
c82647 |
* cannot distinguish between failure and a local broadcast address.
|
|
Packit Service |
c82647 |
*/
|
|
Packit Service |
fd7b69 |
int
|
|
Packit Service |
c82647 |
__inet_aton(const char *cp, struct in_addr *addr)
|
|
Packit Service |
fd7b69 |
{
|
|
Packit Service |
c82647 |
static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
|
|
Packit Service |
c82647 |
in_addr_t val;
|
|
Packit Service |
c82647 |
char c;
|
|
Packit Service |
c82647 |
union iaddr {
|
|
Packit Service |
c82647 |
uint8_t bytes[4];
|
|
Packit Service |
c82647 |
uint32_t word;
|
|
Packit Service |
c82647 |
} res;
|
|
Packit Service |
c82647 |
uint8_t *pp = res.bytes;
|
|
Packit Service |
c82647 |
int digit;
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
int saved_errno = errno;
|
|
Packit Service |
c82647 |
__set_errno (0);
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
res.word = 0;
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
c = *cp;
|
|
Packit Service |
c82647 |
for (;;) {
|
|
Packit Service |
c82647 |
/*
|
|
Packit Service |
c82647 |
* Collect number up to ``.''.
|
|
Packit Service |
c82647 |
* Values are specified as for C:
|
|
Packit Service |
c82647 |
* 0x=hex, 0=octal, isdigit=decimal.
|
|
Packit Service |
c82647 |
*/
|
|
Packit Service |
c82647 |
if (!isdigit(c))
|
|
Packit Service |
c82647 |
goto ret_0;
|
|
Packit Service |
c82647 |
{
|
|
Packit Service |
c82647 |
char *endp;
|
|
Packit Service |
c82647 |
unsigned long ul = strtoul (cp, (char **) &endp, 0);
|
|
Packit Service |
c82647 |
if (ul == ULONG_MAX && errno == ERANGE)
|
|
Packit Service |
c82647 |
goto ret_0;
|
|
Packit Service |
c82647 |
if (ul > 0xfffffffful)
|
|
Packit Service |
c82647 |
goto ret_0;
|
|
Packit Service |
c82647 |
val = ul;
|
|
Packit Service |
c82647 |
digit = cp != endp;
|
|
Packit Service |
c82647 |
cp = endp;
|
|
Packit Service |
c82647 |
}
|
|
Packit Service |
c82647 |
c = *cp;
|
|
Packit Service |
c82647 |
if (c == '.') {
|
|
Packit Service |
c82647 |
/*
|
|
Packit Service |
c82647 |
* Internet format:
|
|
Packit Service |
c82647 |
* a.b.c.d
|
|
Packit Service |
c82647 |
* a.b.c (with c treated as 16 bits)
|
|
Packit Service |
c82647 |
* a.b (with b treated as 24 bits)
|
|
Packit Service |
c82647 |
*/
|
|
Packit Service |
c82647 |
if (pp > res.bytes + 2 || val > 0xff)
|
|
Packit Service |
c82647 |
goto ret_0;
|
|
Packit Service |
c82647 |
*pp++ = val;
|
|
Packit Service |
c82647 |
c = *++cp;
|
|
Packit Service |
c82647 |
} else
|
|
Packit Service |
c82647 |
break;
|
|
Packit Service |
c82647 |
}
|
|
Packit Service |
c82647 |
/*
|
|
Packit Service |
c82647 |
* Check for trailing characters.
|
|
Packit Service |
c82647 |
*/
|
|
Packit Service |
c82647 |
if (c != '\0' && (!isascii(c) || !isspace(c)))
|
|
Packit Service |
c82647 |
goto ret_0;
|
|
Packit Service |
c82647 |
/*
|
|
Packit Service |
c82647 |
* Did we get a valid digit?
|
|
Packit Service |
c82647 |
*/
|
|
Packit Service |
c82647 |
if (!digit)
|
|
Packit Service |
c82647 |
goto ret_0;
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
/* Check whether the last part is in its limits depending on
|
|
Packit Service |
c82647 |
the number of parts in total. */
|
|
Packit Service |
c82647 |
if (val > max[pp - res.bytes])
|
|
Packit Service |
c82647 |
goto ret_0;
|
|
Packit Service |
fd7b69 |
|
|
Packit Service |
c82647 |
if (addr != NULL)
|
|
Packit Service |
c82647 |
addr->s_addr = res.word | htonl (val);
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
__set_errno (saved_errno);
|
|
Packit Service |
c82647 |
return (1);
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
ret_0:
|
|
Packit Service |
c82647 |
__set_errno (saved_errno);
|
|
Packit Service |
c82647 |
return (0);
|
|
Packit Service |
fd7b69 |
}
|
|
Packit Service |
c82647 |
weak_alias (__inet_aton, inet_aton)
|
|
Packit Service |
c82647 |
libc_hidden_def (__inet_aton)
|
|
Packit Service |
c82647 |
libc_hidden_weak (inet_aton)
|