1 /* $OpenBSD: inet_pton.c,v 1.6 2005/08/06 20:30:03 espie 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 #include <sys/param.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <arpa/nameser.h>
25 #include <string.h>
26 #include <errno.h>
27
28 __RCSID("$MirOS: src/lib/libc/net/inet_pton.c,v 1.2 2010/01/07 22:34:52 tg Exp $");
29
30 extern const uint8_t mbsd_digits_dec[11];
31 extern const uint8_t mbsd_digits_hex[17];
32 extern const uint8_t mbsd_digits_HEX[17];
33
34 /*
35 * WARNING: Don't even consider trying to compile this on a system where
36 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
37 */
38
39 static int inet_pton4(const char *src, u_char *dst);
40 static int inet_pton6(const char *src, u_char *dst);
41
42 /* int
43 * inet_pton(af, src, dst)
44 * convert from presentation format (which usually means ASCII printable)
45 * to network format (which is usually some kind of binary format).
46 * return:
47 * 1 if the address was valid for the specified address family
48 * 0 if the address wasn't valid (`dst' is untouched in this case)
49 * -1 if some other error occurred (`dst' is untouched in this case, too)
50 * author:
51 * Paul Vixie, 1996.
52 */
53 int
inet_pton(int af,const char * src,void * dst)54 inet_pton(int af, const char *src, void *dst)
55 {
56 switch (af) {
57 case AF_INET:
58 return (inet_pton4(src, dst));
59 case AF_INET6:
60 return (inet_pton6(src, dst));
61 default:
62 errno = EAFNOSUPPORT;
63 return (-1);
64 }
65 /* NOTREACHED */
66 }
67
68 /* int
69 * inet_pton4(src, dst)
70 * like inet_aton() but without all the hexadecimal and shorthand.
71 * return:
72 * 1 if `src' is a valid dotted quad, else 0.
73 * notice:
74 * does not touch `dst' unless it's returning 1.
75 * author:
76 * Paul Vixie, 1996.
77 */
78 static int
inet_pton4(const char * src,u_char * dst)79 inet_pton4(const char *src, u_char *dst)
80 {
81 int saw_digit, octets, ch;
82 u_char tmp[INADDRSZ], *tp;
83
84 saw_digit = 0;
85 octets = 0;
86 *(tp = tmp) = 0;
87 while ((ch = *src++) != '\0') {
88 const uint8_t *pch;
89
90 if ((pch = strchr(mbsd_digits_dec, ch)) != NULL) {
91 u_int new = *tp * 10 + (pch - &mbsd_digits_dec[0]);
92
93 if (new > 255)
94 return (0);
95 if (! saw_digit) {
96 if (++octets > 4)
97 return (0);
98 saw_digit = 1;
99 }
100 *tp = new;
101 } else if (ch == '.' && saw_digit) {
102 if (octets == 4)
103 return (0);
104 *++tp = 0;
105 saw_digit = 0;
106 } else
107 return (0);
108 }
109 if (octets < 4)
110 return (0);
111
112 memcpy(dst, tmp, INADDRSZ);
113 return (1);
114 }
115
116 /* int
117 * inet_pton6(src, dst)
118 * convert presentation level address to network order binary form.
119 * return:
120 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
121 * notice:
122 * (1) does not touch `dst' unless it's returning 1.
123 * (2) :: in a full address is silently ignored.
124 * credit:
125 * inspired by Mark Andrews.
126 * author:
127 * Paul Vixie, 1996.
128 */
129 static int
inet_pton6(const char * src,u_char * dst)130 inet_pton6(const char *src, u_char *dst)
131 {
132 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
133 const char *xdigits, *curtok;
134 int ch, saw_xdigit;
135 u_int val;
136
137 memset((tp = tmp), '\0', IN6ADDRSZ);
138 endp = tp + IN6ADDRSZ;
139 colonp = NULL;
140 /* Leading :: requires some special handling. */
141 if (*src == ':')
142 if (*++src != ':')
143 return (0);
144 curtok = src;
145 saw_xdigit = 0;
146 val = 0;
147 while ((ch = *src++) != '\0') {
148 const char *pch;
149
150 if ((pch = strchr((xdigits = mbsd_digits_hex), ch)) == NULL)
151 pch = strchr((xdigits = mbsd_digits_HEX), ch);
152 if (pch != NULL) {
153 val <<= 4;
154 val |= (pch - xdigits);
155 if (val > 0xffff)
156 return (0);
157 saw_xdigit = 1;
158 continue;
159 }
160 if (ch == ':') {
161 curtok = src;
162 if (!saw_xdigit) {
163 if (colonp)
164 return (0);
165 colonp = tp;
166 continue;
167 } else if (*src == '\0') {
168 return (0);
169 }
170 if (tp + INT16SZ > endp)
171 return (0);
172 *tp++ = (u_char) (val >> 8) & 0xff;
173 *tp++ = (u_char) val & 0xff;
174 saw_xdigit = 0;
175 val = 0;
176 continue;
177 }
178 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
179 inet_pton4(curtok, tp) > 0) {
180 tp += INADDRSZ;
181 saw_xdigit = 0;
182 break; /* '\0' was seen by inet_pton4(). */
183 }
184 return (0);
185 }
186 if (saw_xdigit) {
187 if (tp + INT16SZ > endp)
188 return (0);
189 *tp++ = (u_char) (val >> 8) & 0xff;
190 *tp++ = (u_char) val & 0xff;
191 }
192 if (colonp != NULL) {
193 /*
194 * Since some memmove()'s erroneously fail to handle
195 * overlapping regions, we'll do the shift by hand.
196 */
197 const int n = tp - colonp;
198 int i;
199
200 for (i = 1; i <= n; i++) {
201 endp[- i] = colonp[n - i];
202 colonp[n - i] = 0;
203 }
204 tp = endp;
205 }
206 if (tp != endp)
207 return (0);
208 memcpy(dst, tmp, IN6ADDRSZ);
209 return (1);
210 }
211