Libinfo-173.tar.gz
[apple/libinfo.git] / gen.subproj / inet_pton.c
1 /*
2 * Copyright (c) 1996,1999 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 char rcsid[] = "$Id: inet_pton.c,v 1.3 2003/04/10 18:53:29 majka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <arpa/nameser.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <errno.h>
35
36 #define IN6ADDRSZ 16
37
38 #if 0
39 #ifndef HAVE_PORTABLE_PROTOTYPE
40 #include "cdecl_ext.h"
41 #endif
42
43 #ifndef HAVE_U_INT16_T
44 #include "bittypes.h"
45 #endif
46 #if !(defined(HAVE_INADDRSZ) && defined(HAVE_IN6ADDRSZ))
47 #include "addrsize.h"
48 #endif
49 #endif
50 #ifndef NS_INADDRSZ
51 #define NS_INADDRSZ INADDRSZ
52 #endif
53 #ifndef NS_IN6ADDRSZ
54 #define NS_IN6ADDRSZ IN6ADDRSZ
55 #endif
56 #ifndef NS_INT16SZ
57 #define NS_INT16SZ sizeof(u_int16_t)
58 #endif
59
60 /*
61 * WARNING: Don't even consider trying to compile this on a system where
62 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
63 */
64
65 static int inet_pton4 __P((const char *src, u_char *dst));
66 #ifdef INET6
67 static int inet_pton6 __P((const char *src, u_char *dst));
68 #endif
69
70 /* int
71 * inet_pton(af, src, dst)
72 * convert from presentation format (which usually means ASCII printable)
73 * to network format (which is usually some kind of binary format).
74 * return:
75 * 1 if the address was valid for the specified address family
76 * 0 if the address wasn't valid (`dst' is untouched in this case)
77 * -1 if some other error occurred (`dst' is untouched in this case, too)
78 * author:
79 * Paul Vixie, 1996.
80 */
81 int
82 inet_pton(int af, const char *src, void *dst)
83 {
84 int status;
85 char *p, *s;
86
87 switch (af)
88 {
89 case AF_INET: return (inet_pton4(src, dst));
90
91 #ifdef INET6
92 case AF_INET6:
93 /* Ignore trailing %xxx (interface specification) */
94
95 p = NULL;
96 s = (char *)src;
97
98 if (src != NULL) p = strrchr(src, '%');
99 if (p != NULL)
100 {
101 s = strdup(src);
102 s[p - src] = '\0';
103 }
104
105 status = inet_pton6(s, dst);
106 if (p != NULL) free(s);
107 return status;
108 #endif
109
110 default:
111 errno = EAFNOSUPPORT;
112 return -1;
113 }
114 /* NOTREACHED */
115 }
116
117 /* int
118 * inet_pton4(src, dst)
119 * like inet_aton() but without all the hexadecimal and shorthand.
120 * return:
121 * 1 if `src' is a valid dotted quad, else 0.
122 * notice:
123 * does not touch `dst' unless it's returning 1.
124 * author:
125 * Paul Vixie, 1996.
126 */
127 static int
128 inet_pton4(src, dst)
129 const char *src;
130 u_char *dst;
131 {
132 static const char digits[] = "0123456789";
133 int saw_digit, octets, ch;
134 u_char tmp[NS_INADDRSZ], *tp;
135
136 saw_digit = 0;
137 octets = 0;
138 *(tp = tmp) = 0;
139 while ((ch = *src++) != '\0') {
140 const char *pch;
141
142 if ((pch = strchr(digits, ch)) != NULL) {
143 u_int new = *tp * 10 + (pch - digits);
144
145 if (new > 255)
146 return (0);
147 *tp = new;
148 if (! saw_digit) {
149 if (++octets > 4)
150 return (0);
151 saw_digit = 1;
152 }
153 } else if (ch == '.' && saw_digit) {
154 if (octets == 4)
155 return (0);
156 *++tp = 0;
157 saw_digit = 0;
158 } else
159 return (0);
160 }
161 if (octets < 4)
162 return (0);
163 memcpy(dst, tmp, NS_INADDRSZ);
164 return (1);
165 }
166
167 #ifdef INET6
168 /* int
169 * inet_pton6(src, dst)
170 * convert presentation level address to network order binary form.
171 * return:
172 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
173 * notice:
174 * (1) does not touch `dst' unless it's returning 1.
175 * (2) :: in a full address is silently ignored.
176 * credit:
177 * inspired by Mark Andrews.
178 * author:
179 * Paul Vixie, 1996.
180 */
181 static int
182 inet_pton6(src, dst)
183 const char *src;
184 u_char *dst;
185 {
186 static const char xdigits_l[] = "0123456789abcdef",
187 xdigits_u[] = "0123456789ABCDEF";
188 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
189 const char *xdigits, *curtok;
190 int ch, saw_xdigit;
191 u_int val;
192
193 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
194 endp = tp + NS_IN6ADDRSZ;
195 colonp = NULL;
196 /* Leading :: requires some special handling. */
197 if (*src == ':')
198 if (*++src != ':')
199 return (0);
200 curtok = src;
201 saw_xdigit = 0;
202 val = 0;
203 while ((ch = *src++) != '\0') {
204 const char *pch;
205
206 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
207 pch = strchr((xdigits = xdigits_u), ch);
208 if (pch != NULL) {
209 val <<= 4;
210 val |= (pch - xdigits);
211 if (val > 0xffff)
212 return (0);
213 saw_xdigit = 1;
214 continue;
215 }
216 if (ch == ':') {
217 curtok = src;
218 if (!saw_xdigit) {
219 if (colonp)
220 return (0);
221 colonp = tp;
222 continue;
223 } else if (*src == '\0') {
224 return (0);
225 }
226 if (tp + NS_INT16SZ > endp)
227 return (0);
228 *tp++ = (u_char) (val >> 8) & 0xff;
229 *tp++ = (u_char) val & 0xff;
230 saw_xdigit = 0;
231 val = 0;
232 continue;
233 }
234 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
235 inet_pton4(curtok, tp) > 0) {
236 tp += NS_INADDRSZ;
237 saw_xdigit = 0;
238 break; /* '\0' was seen by inet_pton4(). */
239 }
240 return (0);
241 }
242 if (saw_xdigit) {
243 if (tp + NS_INT16SZ > endp)
244 return (0);
245 *tp++ = (u_char) (val >> 8) & 0xff;
246 *tp++ = (u_char) val & 0xff;
247 }
248 if (colonp != NULL) {
249 /*
250 * Since some memmove()'s erroneously fail to handle
251 * overlapping regions, we'll do the shift by hand.
252 */
253 const int n = tp - colonp;
254 int i;
255
256 if (tp == endp)
257 return (0);
258 for (i = 1; i <= n; i++) {
259 endp[- i] = colonp[n - i];
260 colonp[n - i] = 0;
261 }
262 tp = endp;
263 }
264 if (tp != endp)
265 return (0);
266 memcpy(dst, tmp, NS_IN6ADDRSZ);
267 return (1);
268 }
269 #endif /*INET6*/