summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/inet_addr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/inet_addr.c')
-rw-r--r--src/lib/libc/net/inet_addr.c111
1 files changed, 62 insertions, 49 deletions
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c
index b5b9d8302f..c962a03382 100644
--- a/src/lib/libc/net/inet_addr.c
+++ b/src/lib/libc/net/inet_addr.c
@@ -1,9 +1,11 @@
1/* $NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $ */ 1/* $OpenBSD: inet_addr.c,v 1.9 2005/08/06 20:30:03 espie Exp $ */
2 2
3/* 3/*
4 * ++Copyright++ 1983, 1990, 1993
5 * -
4 * Copyright (c) 1983, 1990, 1993 6 * Copyright (c) 1983, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -12,14 +14,10 @@
12 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. Neither the name of the University nor the names of its contributors
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 18 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 19 * without specific prior written permission.
22 * 20 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -31,16 +29,29 @@
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
34 */ 52 */
35 53
36#if defined(LIBC_SCCS) && !defined(lint) 54#include <sys/types.h>
37#if 0
38static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
39#else
40static char rcsid[] = "$NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */
43
44#include <sys/param.h> 55#include <sys/param.h>
45#include <netinet/in.h> 56#include <netinet/in.h>
46#include <arpa/inet.h> 57#include <arpa/inet.h>
@@ -50,9 +61,8 @@ static char rcsid[] = "$NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $"
50 * Ascii internet address interpretation routine. 61 * Ascii internet address interpretation routine.
51 * The value returned is in network order. 62 * The value returned is in network order.
52 */ 63 */
53u_long 64in_addr_t
54inet_addr(cp) 65inet_addr(const char *cp)
55 register const char *cp;
56{ 66{
57 struct in_addr val; 67 struct in_addr val;
58 68
@@ -69,60 +79,60 @@ inet_addr(cp)
69 * cannot distinguish between failure and a local broadcast address. 79 * cannot distinguish between failure and a local broadcast address.
70 */ 80 */
71int 81int
72inet_aton(cp, addr) 82inet_aton(const char *cp, struct in_addr *addr)
73 register const char *cp;
74 struct in_addr *addr;
75{ 83{
76 register u_long val; 84 in_addr_t val;
77 register int base, n; 85 int base, n;
78 register char c; 86 char c;
79 u_int parts[4]; 87 u_int parts[4];
80 register u_int *pp = parts; 88 u_int *pp = parts;
81 89
90 c = *cp;
82 for (;;) { 91 for (;;) {
83 /* 92 /*
84 * Collect number up to ``.''. 93 * Collect number up to ``.''.
85 * Values are specified as for C: 94 * Values are specified as for C:
86 * 0x=hex, 0=octal, other=decimal. 95 * 0x=hex, 0=octal, isdigit=decimal.
87 */ 96 */
97 if (!isdigit(c))
98 return (0);
88 val = 0; base = 10; 99 val = 0; base = 10;
89 if (*cp == '0') { 100 if (c == '0') {
90 if (*++cp == 'x' || *cp == 'X') 101 c = *++cp;
91 base = 16, cp++; 102 if (c == 'x' || c == 'X')
103 base = 16, c = *++cp;
92 else 104 else
93 base = 8; 105 base = 8;
94 } 106 }
95 while ((c = *cp) != '\0') { 107 for (;;) {
96 if (isascii(c) && isdigit(c)) { 108 if (isascii(c) && isdigit(c)) {
97 val = (val * base) + (c - '0'); 109 val = (val * base) + (c - '0');
98 cp++; 110 c = *++cp;
99 continue; 111 } else if (base == 16 && isascii(c) && isxdigit(c)) {
100 } 112 val = (val << 4) |
101 if (base == 16 && isascii(c) && isxdigit(c)) {
102 val = (val << 4) +
103 (c + 10 - (islower(c) ? 'a' : 'A')); 113 (c + 10 - (islower(c) ? 'a' : 'A'));
104 cp++; 114 c = *++cp;
105 continue; 115 } else
106 } 116 break;
107 break;
108 } 117 }
109 if (*cp == '.') { 118 if (c == '.') {
110 /* 119 /*
111 * Internet format: 120 * Internet format:
112 * a.b.c.d 121 * a.b.c.d
113 * a.b.c (with c treated as 16-bits) 122 * a.b.c (with c treated as 16 bits)
114 * a.b (with b treated as 24 bits) 123 * a.b (with b treated as 24 bits)
115 */ 124 */
116 if (pp >= parts + 3 || val > 0xff) 125 if (pp >= parts + 3)
117 return (0); 126 return (0);
118 *pp++ = val, cp++; 127 *pp++ = val;
128 c = *++cp;
119 } else 129 } else
120 break; 130 break;
121 } 131 }
122 /* 132 /*
123 * Check for trailing characters. 133 * Check for trailing characters.
124 */ 134 */
125 if (*cp && (!isascii(*cp) || !isspace(*cp))) 135 if (c != '\0' && (!isascii(c) || !isspace(c)))
126 return (0); 136 return (0);
127 /* 137 /*
128 * Concoct the address according to 138 * Concoct the address according to
@@ -131,23 +141,26 @@ inet_aton(cp, addr)
131 n = pp - parts + 1; 141 n = pp - parts + 1;
132 switch (n) { 142 switch (n) {
133 143
144 case 0:
145 return (0); /* initial nondigit */
146
134 case 1: /* a -- 32 bits */ 147 case 1: /* a -- 32 bits */
135 break; 148 break;
136 149
137 case 2: /* a.b -- 8.24 bits */ 150 case 2: /* a.b -- 8.24 bits */
138 if (val > 0xffffff) 151 if ((val > 0xffffff) || (parts[0] > 0xff))
139 return (0); 152 return (0);
140 val |= parts[0] << 24; 153 val |= parts[0] << 24;
141 break; 154 break;
142 155
143 case 3: /* a.b.c -- 8.8.16 bits */ 156 case 3: /* a.b.c -- 8.8.16 bits */
144 if (val > 0xffff) 157 if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
145 return (0); 158 return (0);
146 val |= (parts[0] << 24) | (parts[1] << 16); 159 val |= (parts[0] << 24) | (parts[1] << 16);
147 break; 160 break;
148 161
149 case 4: /* a.b.c.d -- 8.8.8.8 bits */ 162 case 4: /* a.b.c.d -- 8.8.8.8 bits */
150 if (val > 0xff) 163 if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
151 return (0); 164 return (0);
152 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 165 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
153 break; 166 break;