]>
Commit | Line | Data |
---|---|---|
df21c858 UD |
1 | /* |
2 | * Copyright (c) 1996 by Internet Software Consortium. | |
3 | * | |
4 | * Permission to use, copy, modify, and distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | |
9 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
10 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | |
11 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
12 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
13 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
14 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
15 | * SOFTWARE. | |
16 | */ | |
17 | ||
18 | #if defined(LIBC_SCCS) && !defined(lint) | |
19 | static const char rcsid[] = "$Id$"; | |
20 | #endif | |
21 | ||
22 | #include <sys/types.h> | |
23 | #include <sys/socket.h> | |
24 | #include <netinet/in.h> | |
25 | #include <arpa/inet.h> | |
26 | ||
27 | #include <assert.h> | |
28 | #include <ctype.h> | |
29 | #include <errno.h> | |
30 | #include <stdio.h> | |
31 | #include <string.h> | |
32 | #include <stdlib.h> | |
33 | ||
34 | #ifdef SPRINTF_CHAR | |
35 | # define SPRINTF(x) strlen(sprintf/**/x) | |
36 | #else | |
37 | # define SPRINTF(x) ((size_t)sprintf x) | |
38 | #endif | |
39 | ||
40 | static int inet_net_pton_ipv4 __P((const char *src, u_char *dst, | |
41 | size_t size)); | |
42 | ||
43 | /* | |
44 | * static int | |
45 | * inet_net_pton(af, src, dst, size) | |
46 | * convert network number from presentation to network format. | |
47 | * accepts hex octets, hex strings, decimal octets, and /CIDR. | |
48 | * "size" is in bytes and describes "dst". | |
49 | * return: | |
50 | * number of bits, either imputed classfully or specified with /CIDR, | |
51 | * or -1 if some failure occurred (check errno). ENOENT means it was | |
52 | * not a valid network specification. | |
53 | * author: | |
54 | * Paul Vixie (ISC), June 1996 | |
55 | */ | |
56 | int | |
57 | inet_net_pton(af, src, dst, size) | |
58 | int af; | |
59 | const char *src; | |
60 | void *dst; | |
61 | size_t size; | |
62 | { | |
63 | switch (af) { | |
64 | case AF_INET: | |
65 | return (inet_net_pton_ipv4(src, dst, size)); | |
66 | default: | |
c4029823 | 67 | __set_errno (EAFNOSUPPORT); |
df21c858 UD |
68 | return (-1); |
69 | } | |
70 | } | |
71 | ||
72 | /* | |
73 | * static int | |
74 | * inet_net_pton_ipv4(src, dst, size) | |
75 | * convert IPv4 network number from presentation to network format. | |
76 | * accepts hex octets, hex strings, decimal octets, and /CIDR. | |
77 | * "size" is in bytes and describes "dst". | |
78 | * return: | |
79 | * number of bits, either imputed classfully or specified with /CIDR, | |
80 | * or -1 if some failure occurred (check errno). ENOENT means it was | |
81 | * not an IPv4 network specification. | |
82 | * note: | |
83 | * network byte order assumed. this means 192.5.5.240/28 has | |
84 | * 0x11110000 in its fourth octet. | |
85 | * author: | |
86 | * Paul Vixie (ISC), June 1996 | |
87 | */ | |
88 | static int | |
89 | inet_net_pton_ipv4(src, dst, size) | |
90 | const char *src; | |
91 | u_char *dst; | |
92 | size_t size; | |
93 | { | |
94 | static const char | |
95 | xdigits[] = "0123456789abcdef", | |
96 | digits[] = "0123456789"; | |
97 | int n, ch, tmp, dirty, bits; | |
98 | const u_char *odst = dst; | |
99 | ||
100 | bits = -1; | |
101 | ch = *src++; | |
102 | ||
103 | if (ch == '0' && (src[0] == 'x' || src[0] == 'X') | |
104 | && isascii(src[1]) && isxdigit(src[1])) { | |
105 | /* Hexadecimal: Eat nybble string. */ | |
106 | if (size <= 0) | |
107 | goto emsgsize; | |
108 | *dst = 0, dirty = 0; | |
109 | src++; /* skip x or X. */ | |
110 | while ((ch = *src++) != '\0' && | |
111 | isascii(ch) && isxdigit(ch)) { | |
112 | if (isupper(ch)) | |
113 | ch = tolower(ch); | |
114 | n = strchr(xdigits, ch) - xdigits; | |
115 | assert(n >= 0 && n <= 15); | |
116 | *dst |= n; | |
117 | if (!dirty++) | |
118 | *dst <<= 4; | |
119 | else if (size-- > 0) | |
120 | *++dst = 0, dirty = 0; | |
121 | else | |
122 | goto emsgsize; | |
123 | } | |
124 | if (dirty) | |
125 | size--; | |
126 | } else if (isascii(ch) && isdigit(ch)) { | |
127 | /* Decimal: eat dotted digit string. */ | |
128 | for (;;) { | |
129 | tmp = 0; | |
130 | do { | |
131 | n = strchr(digits, ch) - digits; | |
132 | assert(n >= 0 && n <= 9); | |
133 | tmp *= 10; | |
134 | tmp += n; | |
135 | if (tmp > 255) | |
136 | goto enoent; | |
137 | } while ((ch = *src++) != '\0' && | |
138 | isascii(ch) && isdigit(ch)); | |
139 | if (size-- <= 0) | |
140 | goto emsgsize; | |
141 | *dst++ = (u_char) tmp; | |
142 | if (ch == '\0' || ch == '/') | |
143 | break; | |
144 | if (ch != '.') | |
145 | goto enoent; | |
146 | ch = *src++; | |
147 | if (!isascii(ch) || !isdigit(ch)) | |
148 | goto enoent; | |
149 | } | |
150 | } else | |
151 | goto enoent; | |
152 | ||
153 | if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) { | |
154 | /* CIDR width specifier. Nothing can follow it. */ | |
155 | ch = *src++; /* Skip over the /. */ | |
156 | bits = 0; | |
157 | do { | |
158 | n = strchr(digits, ch) - digits; | |
159 | assert(n >= 0 && n <= 9); | |
160 | bits *= 10; | |
161 | bits += n; | |
162 | } while ((ch = *src++) != '\0' && | |
163 | isascii(ch) && isdigit(ch)); | |
164 | if (ch != '\0') | |
165 | goto enoent; | |
166 | } | |
167 | ||
168 | /* Firey death and destruction unless we prefetched EOS. */ | |
169 | if (ch != '\0') | |
170 | goto enoent; | |
171 | ||
172 | /* If nothing was written to the destination, we found no address. */ | |
173 | if (dst == odst) | |
174 | goto enoent; | |
175 | /* If no CIDR spec was given, infer width from net class. */ | |
176 | if (bits == -1) { | |
177 | if (*odst >= 224) | |
178 | bits = 4; | |
179 | else if (*odst >= 192) | |
180 | bits = 24; | |
181 | else if (*odst >= 128) | |
182 | bits = 16; | |
183 | else | |
184 | bits = 8; | |
185 | } | |
186 | /* Extend network to cover the actual mask. */ | |
187 | while (bits > ((dst - odst) * 8)) { | |
188 | if (size-- <= 0) | |
189 | goto emsgsize; | |
190 | *dst++ = '\0'; | |
191 | } | |
192 | return (bits); | |
193 | ||
194 | enoent: | |
c4029823 | 195 | __set_errno (ENOENT); |
df21c858 UD |
196 | return (-1); |
197 | ||
198 | emsgsize: | |
c4029823 | 199 | __set_errno (EMSGSIZE); |
df21c858 UD |
200 | return (-1); |
201 | } |