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.c93
1 files changed, 59 insertions, 34 deletions
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c
index b5b9d8302f..716dcb6f80 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.7 2003/06/02 20:18:35 millert 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,38 @@
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#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 55#if 0
38static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 56static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
57static char rcsid[] = "$From: inet_addr.c,v 8.5 1996/08/05 08:31:35 vixie Exp $";
39#else 58#else
40static char rcsid[] = "$NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $"; 59static char rcsid[] = "$OpenBSD: inet_addr.c,v 1.7 2003/06/02 20:18:35 millert Exp $";
41#endif 60#endif
42#endif /* LIBC_SCCS and not lint */ 61#endif /* LIBC_SCCS and not lint */
43 62
63#include <sys/types.h>
44#include <sys/param.h> 64#include <sys/param.h>
45#include <netinet/in.h> 65#include <netinet/in.h>
46#include <arpa/inet.h> 66#include <arpa/inet.h>
@@ -50,7 +70,7 @@ static char rcsid[] = "$NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $"
50 * Ascii internet address interpretation routine. 70 * Ascii internet address interpretation routine.
51 * The value returned is in network order. 71 * The value returned is in network order.
52 */ 72 */
53u_long 73in_addr_t
54inet_addr(cp) 74inet_addr(cp)
55 register const char *cp; 75 register const char *cp;
56{ 76{
@@ -73,56 +93,58 @@ inet_aton(cp, addr)
73 register const char *cp; 93 register const char *cp;
74 struct in_addr *addr; 94 struct in_addr *addr;
75{ 95{
76 register u_long val; 96 register in_addr_t val;
77 register int base, n; 97 register int base, n;
78 register char c; 98 register char c;
79 u_int parts[4]; 99 u_int parts[4];
80 register u_int *pp = parts; 100 register u_int *pp = parts;
81 101
102 c = *cp;
82 for (;;) { 103 for (;;) {
83 /* 104 /*
84 * Collect number up to ``.''. 105 * Collect number up to ``.''.
85 * Values are specified as for C: 106 * Values are specified as for C:
86 * 0x=hex, 0=octal, other=decimal. 107 * 0x=hex, 0=octal, isdigit=decimal.
87 */ 108 */
109 if (!isdigit(c))
110 return (0);
88 val = 0; base = 10; 111 val = 0; base = 10;
89 if (*cp == '0') { 112 if (c == '0') {
90 if (*++cp == 'x' || *cp == 'X') 113 c = *++cp;
91 base = 16, cp++; 114 if (c == 'x' || c == 'X')
115 base = 16, c = *++cp;
92 else 116 else
93 base = 8; 117 base = 8;
94 } 118 }
95 while ((c = *cp) != '\0') { 119 for (;;) {
96 if (isascii(c) && isdigit(c)) { 120 if (isascii(c) && isdigit(c)) {
97 val = (val * base) + (c - '0'); 121 val = (val * base) + (c - '0');
98 cp++; 122 c = *++cp;
99 continue; 123 } else if (base == 16 && isascii(c) && isxdigit(c)) {
100 } 124 val = (val << 4) |
101 if (base == 16 && isascii(c) && isxdigit(c)) {
102 val = (val << 4) +
103 (c + 10 - (islower(c) ? 'a' : 'A')); 125 (c + 10 - (islower(c) ? 'a' : 'A'));
104 cp++; 126 c = *++cp;
105 continue; 127 } else
106 } 128 break;
107 break;
108 } 129 }
109 if (*cp == '.') { 130 if (c == '.') {
110 /* 131 /*
111 * Internet format: 132 * Internet format:
112 * a.b.c.d 133 * a.b.c.d
113 * a.b.c (with c treated as 16-bits) 134 * a.b.c (with c treated as 16 bits)
114 * a.b (with b treated as 24 bits) 135 * a.b (with b treated as 24 bits)
115 */ 136 */
116 if (pp >= parts + 3 || val > 0xff) 137 if (pp >= parts + 3)
117 return (0); 138 return (0);
118 *pp++ = val, cp++; 139 *pp++ = val;
140 c = *++cp;
119 } else 141 } else
120 break; 142 break;
121 } 143 }
122 /* 144 /*
123 * Check for trailing characters. 145 * Check for trailing characters.
124 */ 146 */
125 if (*cp && (!isascii(*cp) || !isspace(*cp))) 147 if (c != '\0' && (!isascii(c) || !isspace(c)))
126 return (0); 148 return (0);
127 /* 149 /*
128 * Concoct the address according to 150 * Concoct the address according to
@@ -131,23 +153,26 @@ inet_aton(cp, addr)
131 n = pp - parts + 1; 153 n = pp - parts + 1;
132 switch (n) { 154 switch (n) {
133 155
156 case 0:
157 return (0); /* initial nondigit */
158
134 case 1: /* a -- 32 bits */ 159 case 1: /* a -- 32 bits */
135 break; 160 break;
136 161
137 case 2: /* a.b -- 8.24 bits */ 162 case 2: /* a.b -- 8.24 bits */
138 if (val > 0xffffff) 163 if ((val > 0xffffff) || (parts[0] > 0xff))
139 return (0); 164 return (0);
140 val |= parts[0] << 24; 165 val |= parts[0] << 24;
141 break; 166 break;
142 167
143 case 3: /* a.b.c -- 8.8.16 bits */ 168 case 3: /* a.b.c -- 8.8.16 bits */
144 if (val > 0xffff) 169 if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
145 return (0); 170 return (0);
146 val |= (parts[0] << 24) | (parts[1] << 16); 171 val |= (parts[0] << 24) | (parts[1] << 16);
147 break; 172 break;
148 173
149 case 4: /* a.b.c.d -- 8.8.8.8 bits */ 174 case 4: /* a.b.c.d -- 8.8.8.8 bits */
150 if (val > 0xff) 175 if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
151 return (0); 176 return (0);
152 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 177 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
153 break; 178 break;