1 /* $NetBSD: inet_addr.c,v 1.6 2001/04/18 15:42:28 thorpej Exp $ */
2
3 /* Copyright (c) 1996 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 * SOFTWARE.
17 */
18
19 /*
20 * Derived from:
21 * src/lib/libc/net/inet_pton.c
22 */
23
24 #include <sys/types.h>
25
26 #if !defined(_KERNEL) && !defined(_STANDALONE)
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <ctype.h>
31 #else
32 #include <lib/libkern/libkern.h>
33 #endif
34
35 /*
36 * Ascii internet address interpretation routine.
37 * The value returned is in network order.
38 */
39 #if !defined(_KERNEL) && !defined(_STANDALONE)
40 u_long /* XXX to comply with the prototype in <arpa/inet.h> */
41 #else
42 u_int32_t
43 #endif
44 inet_addr(src)
45 const char *src;
46 {
47 u_int32_t val;
48 int base, n;
49 unsigned char c;
50 u_int parts[4];
51 u_int *pp = parts;
52
53 c = *src;
54 for (;;) {
55 /*
56 * Collect number up to ``.''.
57 * Values are specified as for C:
58 * 0x=hex, 0=octal, isdigit=decimal.
59 */
60 if (!isdigit(c))
61 return (0);
62 val = 0; base = 10;
63 if (c == '') {
64 c = *++src;
65 if (toupper(c) == 'X')
66 base = 16, c = *++src;
67 else
68 base = 8;
69 }
70 for (;;) {
71 if (isdigit(c)) {
72 val = (val * base) + (c - '');
73 c = *++src;
74 } else if (base == 16 && isxdigit(toupper(c))) {
75 val = (val << 4) |
76 (toupper(c) + 10 - 'A');
77 c = *++src;
78 } else
79 break;
80 }
81 if (c == '.') {
82 /*
83 * Internet format:
84 * a.b.c.d
85 * a.b.c (with c treated as 16 bits)
86 * a.b (with b treated as 24 bits)
87 */
88 if (pp >= parts + 3)
89 return (0);
90 *pp++ = val;
91 c = *++src;
92 } else
93 break;
94 }
95 /*
96 * Check for trailing characters.
97 */
98 if (c != '\0' && !isspace(c))
99 return (0);
100 /*
101 * Concoct the address according to
102 * the number of parts specified.
103 */
104 n = pp - parts + 1;
105 switch (n) {
106
107 case 0:
108 return (0); /* initial nondigit */
109
110 case 1: /* a -- 32 bits */
111 break;
112
113 case 2: /* a.b -- 8.24 bits */
114 if (val > 0xffffff)
115 return (0);
116 val |= parts[0] << 24;
117 break;
118
119 case 3: /* a.b.c -- 8.8.16 bits */
120 if (val > 0xffff)
121 return (0);
122 val |= (parts[0] << 24) | (parts[1] << 16);
123 break;
124
125 case 4: /* a.b.c.d -- 8.8.8.8 bits */
126 if (val > 0xff)
127 return (0);
128 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
129 break;
130 }
131
132 val = htonl(val);
133 return (val);
134 }
Cache object: 1b110357dcaf37013a904e0e27b504e5
|