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