1156952Sume/*
2156952Sume * Copyright (c) 1983, 1993
3156952Sume *	The Regents of the University of California.  All rights reserved.
4156952Sume *
5156952Sume * Redistribution and use in source and binary forms, with or without
6156952Sume * modification, are permitted provided that the following conditions
7156952Sume * are met:
8156952Sume * 1. Redistributions of source code must retain the above copyright
9156952Sume *    notice, this list of conditions and the following disclaimer.
10156952Sume * 2. Redistributions in binary form must reproduce the above copyright
11156952Sume *    notice, this list of conditions and the following disclaimer in the
12156952Sume *    documentation and/or other materials provided with the distribution.
13156952Sume * 4. Neither the name of the University nor the names of its contributors
14156952Sume *    may be used to endorse or promote products derived from this software
15156952Sume *    without specific prior written permission.
16156952Sume *
17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20156952Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27156952Sume * SUCH DAMAGE.
28156952Sume */
29156952Sume
30156952Sume#if defined(LIBC_SCCS) && !defined(lint)
31156952Sumestatic const char sccsid[] = "@(#)inet_network.c	8.1 (Berkeley) 6/4/93";
32156952Sume#endif /* LIBC_SCCS and not lint */
33156956Sume#include <sys/cdefs.h>
34156956Sume__FBSDID("$FreeBSD$");
35156952Sume
36156952Sume#include "port_before.h"
37156952Sume
38156952Sume#include <sys/types.h>
39156952Sume#include <netinet/in.h>
40156952Sume#include <arpa/inet.h>
41156952Sume#include <ctype.h>
42156952Sume
43156952Sume#include "port_after.h"
44156952Sume
45170244Sume/*%
46156952Sume * Internet network address interpretation routine.
47156952Sume * The library routines call this routine to interpret
48156952Sume * network numbers.
49156952Sume */
50156956Sumein_addr_t
51156952Sumeinet_network(cp)
52156956Sume	const char *cp;
53156952Sume{
54156956Sume	in_addr_t val, base, n;
55156956Sume	char c;
56156956Sume	in_addr_t parts[4], *pp = parts;
57156956Sume	int i, digit;
58156952Sume
59156952Sumeagain:
60156952Sume	val = 0; base = 10; digit = 0;
61156952Sume	if (*cp == '0')
62156952Sume		digit = 1, base = 8, cp++;
63156952Sume	if (*cp == 'x' || *cp == 'X')
64156952Sume		base = 16, cp++;
65156952Sume	while ((c = *cp) != 0) {
66156952Sume		if (isdigit((unsigned char)c)) {
67156952Sume			if (base == 8U && (c == '8' || c == '9'))
68156952Sume				return (INADDR_NONE);
69156952Sume			val = (val * base) + (c - '0');
70156952Sume			cp++;
71156952Sume			digit = 1;
72156952Sume			continue;
73156952Sume		}
74156952Sume		if (base == 16U && isxdigit((unsigned char)c)) {
75156952Sume			val = (val << 4) +
76156952Sume			      (c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
77156952Sume			cp++;
78156952Sume			digit = 1;
79156952Sume			continue;
80156952Sume		}
81156952Sume		break;
82156952Sume	}
83156952Sume	if (!digit)
84156952Sume		return (INADDR_NONE);
85175330Scperciva	if (pp >= parts + 4 || val > 0xffU)
86175330Scperciva		return (INADDR_NONE);
87156952Sume	if (*cp == '.') {
88156952Sume		*pp++ = val, cp++;
89156952Sume		goto again;
90156952Sume	}
91156952Sume	if (*cp && !isspace(*cp&0xff))
92156952Sume		return (INADDR_NONE);
93156952Sume	*pp++ = val;
94156952Sume	n = pp - parts;
95156952Sume	if (n > 4U)
96156952Sume		return (INADDR_NONE);
97156952Sume	for (val = 0, i = 0; i < n; i++) {
98156952Sume		val <<= 8;
99156952Sume		val |= parts[i] & 0xff;
100156952Sume	}
101156952Sume	return (val);
102156952Sume}
103156956Sume
104156956Sume/*
105156956Sume * Weak aliases for applications that use certain private entry points,
106156956Sume * and fail to include <arpa/inet.h>.
107156956Sume */
108156956Sume#undef inet_network
109156956Sume__weak_reference(__inet_network, inet_network);
110170244Sume
111170244Sume/*! \file */
112