Blame resolv/inet_addr.c

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 3e830d
/*
Packit Service 3e830d
 * Ascii internet address interpretation routine.
Packit Service 3e830d
 * The value returned is in network order.
Packit Service 3e830d
 */
Packit Service 3e830d
in_addr_t
Packit Service 3e830d
__inet_addr(const char *cp) {
Packit Service 3e830d
	struct in_addr val;
Packit Service bcfbfc
Packit Service 3e830d
	if (__inet_aton(cp, &val))
Packit Service 3e830d
		return (val.s_addr);
Packit Service 3e830d
	return (INADDR_NONE);
Packit Service bcfbfc
}
Packit Service 3e830d
weak_alias (__inet_addr, inet_addr)
Packit Service bcfbfc
Packit Service 3e830d
/*
Packit Service 3e830d
 * Check whether "cp" is a valid ascii representation
Packit Service 3e830d
 * of an Internet address and convert to a binary address.
Packit Service 3e830d
 * Returns 1 if the address is valid, 0 if not.
Packit Service 3e830d
 * This replaces inet_addr, the return value from which
Packit Service 3e830d
 * cannot distinguish between failure and a local broadcast address.
Packit Service 3e830d
 */
Packit Service bcfbfc
int
Packit Service 3e830d
__inet_aton(const char *cp, struct in_addr *addr)
Packit Service bcfbfc
{
Packit Service 3e830d
	static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
Packit Service 3e830d
	in_addr_t val;
Packit Service 3e830d
	char c;
Packit Service 3e830d
	union iaddr {
Packit Service 3e830d
	  uint8_t bytes[4];
Packit Service 3e830d
	  uint32_t word;
Packit Service 3e830d
	} res;
Packit Service 3e830d
	uint8_t *pp = res.bytes;
Packit Service 3e830d
	int digit;
Packit Service 3e830d
Packit Service 3e830d
	int saved_errno = errno;
Packit Service 3e830d
	__set_errno (0);
Packit Service 3e830d
Packit Service 3e830d
	res.word = 0;
Packit Service 3e830d
Packit Service 3e830d
	c = *cp;
Packit Service 3e830d
	for (;;) {
Packit Service 3e830d
		/*
Packit Service 3e830d
		 * Collect number up to ``.''.
Packit Service 3e830d
		 * Values are specified as for C:
Packit Service 3e830d
		 * 0x=hex, 0=octal, isdigit=decimal.
Packit Service 3e830d
		 */
Packit Service 3e830d
		if (!isdigit(c))
Packit Service 3e830d
			goto ret_0;
Packit Service 3e830d
		{
Packit Service 3e830d
			char *endp;
Packit Service 3e830d
			unsigned long ul = strtoul (cp, (char **) &endp, 0);
Packit Service 3e830d
			if (ul == ULONG_MAX && errno == ERANGE)
Packit Service 3e830d
				goto ret_0;
Packit Service 3e830d
			if (ul > 0xfffffffful)
Packit Service 3e830d
				goto ret_0;
Packit Service 3e830d
			val = ul;
Packit Service 3e830d
			digit = cp != endp;
Packit Service 3e830d
			cp = endp;
Packit Service 3e830d
		}
Packit Service 3e830d
		c = *cp;
Packit Service 3e830d
		if (c == '.') {
Packit Service 3e830d
			/*
Packit Service 3e830d
			 * Internet format:
Packit Service 3e830d
			 *	a.b.c.d
Packit Service 3e830d
			 *	a.b.c	(with c treated as 16 bits)
Packit Service 3e830d
			 *	a.b	(with b treated as 24 bits)
Packit Service 3e830d
			 */
Packit Service 3e830d
			if (pp > res.bytes + 2 || val > 0xff)
Packit Service 3e830d
				goto ret_0;
Packit Service 3e830d
			*pp++ = val;
Packit Service 3e830d
			c = *++cp;
Packit Service 3e830d
		} else
Packit Service 3e830d
			break;
Packit Service 3e830d
	}
Packit Service 3e830d
	/*
Packit Service 3e830d
	 * Check for trailing characters.
Packit Service 3e830d
	 */
Packit Service 3e830d
	if (c != '\0' && (!isascii(c) || !isspace(c)))
Packit Service 3e830d
		goto ret_0;
Packit Service 3e830d
	/*
Packit Service 3e830d
	 * Did we get a valid digit?
Packit Service 3e830d
	 */
Packit Service 3e830d
	if (!digit)
Packit Service 3e830d
		goto ret_0;
Packit Service 3e830d
Packit Service 3e830d
	/* Check whether the last part is in its limits depending on
Packit Service 3e830d
	   the number of parts in total.  */
Packit Service 3e830d
	if (val > max[pp - res.bytes])
Packit Service 3e830d
	  goto ret_0;
Packit Service bcfbfc
Packit Service 3e830d
	if (addr != NULL)
Packit Service 3e830d
		addr->s_addr = res.word | htonl (val);
Packit Service 3e830d
Packit Service 3e830d
	__set_errno (saved_errno);
Packit Service 3e830d
	return (1);
Packit Service 3e830d
Packit Service 3e830d
ret_0:
Packit Service 3e830d
	__set_errno (saved_errno);
Packit Service 3e830d
	return (0);
Packit Service bcfbfc
}
Packit Service 3e830d
weak_alias (__inet_aton, inet_aton)
Packit Service 3e830d
libc_hidden_def (__inet_aton)
Packit Service 3e830d
libc_hidden_weak (inet_aton)