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