1 | /* $OpenBSD: inet_pton.c,v 1.9 2015/01/16 16:48:51 deraadt Exp $ */
|
---|
2 | // from https://raw.githubusercontent.com/libressl-portable/portable/master/crypto/compat/inet_pton.c
|
---|
3 |
|
---|
4 | /* Copyright (c) 1996 by Internet Software Consortium.
|
---|
5 | *
|
---|
6 | * Permission to use, copy, modify, and distribute this software for any
|
---|
7 | * purpose with or without fee is hereby granted, provided that the above
|
---|
8 | * copyright notice and this permission notice appear in all copies.
|
---|
9 | *
|
---|
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
---|
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
---|
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
---|
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
---|
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
---|
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
---|
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
---|
17 | * SOFTWARE.
|
---|
18 | */
|
---|
19 | #ifdef _WIN32
|
---|
20 | #include <win32netcompat.h>
|
---|
21 | #include <sys/types.h>
|
---|
22 | #include <string.h>
|
---|
23 | #include <errno.h>
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * WARNING: Don't even consider trying to compile this on a system where
|
---|
27 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
|
---|
28 | */
|
---|
29 |
|
---|
30 | static int inet_pton4(const char *src, u_char *dst);
|
---|
31 | static int inet_pton6(const char *src, u_char *dst);
|
---|
32 |
|
---|
33 | /* int
|
---|
34 | * inet_pton(af, src, dst)
|
---|
35 | * convert from presentation format (which usually means ASCII printable)
|
---|
36 | * to network format (which is usually some kind of binary format).
|
---|
37 | * return:
|
---|
38 | * 1 if the address was valid for the specified address family
|
---|
39 | * 0 if the address wasn't valid (`dst' is untouched in this case)
|
---|
40 | * -1 if some other error occurred (`dst' is untouched in this case, too)
|
---|
41 | * author:
|
---|
42 | * Paul Vixie, 1996.
|
---|
43 | */
|
---|
44 | int
|
---|
45 | inet_pton(int af, const char *src, void *dst)
|
---|
46 | {
|
---|
47 | switch (af) {
|
---|
48 | case AF_INET:
|
---|
49 | return (inet_pton4(src, dst));
|
---|
50 | case AF_INET6:
|
---|
51 | return (inet_pton6(src, dst));
|
---|
52 | default:
|
---|
53 | errno = EAFNOSUPPORT;
|
---|
54 | return (-1);
|
---|
55 | }
|
---|
56 | /* NOTREACHED */
|
---|
57 | }
|
---|
58 |
|
---|
59 | /* int
|
---|
60 | * inet_pton4(src, dst)
|
---|
61 | * like inet_aton() but without all the hexadecimal and shorthand.
|
---|
62 | * return:
|
---|
63 | * 1 if `src' is a valid dotted quad, else 0.
|
---|
64 | * notice:
|
---|
65 | * does not touch `dst' unless it's returning 1.
|
---|
66 | * author:
|
---|
67 | * Paul Vixie, 1996.
|
---|
68 | */
|
---|
69 | static int
|
---|
70 | inet_pton4(const char *src, u_char *dst)
|
---|
71 | {
|
---|
72 | static const char digits[] = "0123456789";
|
---|
73 | int saw_digit, octets, ch;
|
---|
74 | u_char tmp[INADDRSZ], *tp;
|
---|
75 |
|
---|
76 | saw_digit = 0;
|
---|
77 | octets = 0;
|
---|
78 | *(tp = tmp) = 0;
|
---|
79 | while ((ch = *src++) != '\0') {
|
---|
80 | const char *pch;
|
---|
81 |
|
---|
82 | if ((pch = strchr(digits, ch)) != NULL) {
|
---|
83 | u_int new = *tp * 10 + (pch - digits);
|
---|
84 |
|
---|
85 | if (new > 255)
|
---|
86 | return (0);
|
---|
87 | if (! saw_digit) {
|
---|
88 | if (++octets > 4)
|
---|
89 | return (0);
|
---|
90 | saw_digit = 1;
|
---|
91 | }
|
---|
92 | *tp = new;
|
---|
93 | } else if (ch == '.' && saw_digit) {
|
---|
94 | if (octets == 4)
|
---|
95 | return (0);
|
---|
96 | *++tp = 0;
|
---|
97 | saw_digit = 0;
|
---|
98 | } else
|
---|
99 | return (0);
|
---|
100 | }
|
---|
101 | if (octets < 4)
|
---|
102 | return (0);
|
---|
103 |
|
---|
104 | memcpy(dst, tmp, INADDRSZ);
|
---|
105 | return (1);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* int
|
---|
109 | * inet_pton6(src, dst)
|
---|
110 | * convert presentation level address to network order binary form.
|
---|
111 | * return:
|
---|
112 | * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
|
---|
113 | * notice:
|
---|
114 | * does not touch `dst' unless it's returning 1.
|
---|
115 | * credit:
|
---|
116 | * inspired by Mark Andrews.
|
---|
117 | * author:
|
---|
118 | * Paul Vixie, 1996.
|
---|
119 | */
|
---|
120 | static int
|
---|
121 | inet_pton6(const char *src, u_char *dst)
|
---|
122 | {
|
---|
123 | static const char xdigits_l[] = "0123456789abcdef",
|
---|
124 | xdigits_u[] = "0123456789ABCDEF";
|
---|
125 | u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
|
---|
126 | const char *xdigits, *curtok;
|
---|
127 | int ch, saw_xdigit, count_xdigit;
|
---|
128 | u_int val;
|
---|
129 |
|
---|
130 | memset((tp = tmp), '\0', IN6ADDRSZ);
|
---|
131 | endp = tp + IN6ADDRSZ;
|
---|
132 | colonp = NULL;
|
---|
133 | /* Leading :: requires some special handling. */
|
---|
134 | if (*src == ':')
|
---|
135 | if (*++src != ':')
|
---|
136 | return (0);
|
---|
137 | curtok = src;
|
---|
138 | saw_xdigit = count_xdigit = 0;
|
---|
139 | val = 0;
|
---|
140 | while ((ch = *src++) != '\0') {
|
---|
141 | const char *pch;
|
---|
142 |
|
---|
143 | if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
|
---|
144 | pch = strchr((xdigits = xdigits_u), ch);
|
---|
145 | if (pch != NULL) {
|
---|
146 | if (count_xdigit >= 4)
|
---|
147 | return (0);
|
---|
148 | val <<= 4;
|
---|
149 | val |= (pch - xdigits);
|
---|
150 | if (val > 0xffff)
|
---|
151 | return (0);
|
---|
152 | saw_xdigit = 1;
|
---|
153 | count_xdigit++;
|
---|
154 | continue;
|
---|
155 | }
|
---|
156 | if (ch == ':') {
|
---|
157 | curtok = src;
|
---|
158 | if (!saw_xdigit) {
|
---|
159 | if (colonp)
|
---|
160 | return (0);
|
---|
161 | colonp = tp;
|
---|
162 | continue;
|
---|
163 | } else if (*src == '\0') {
|
---|
164 | return (0);
|
---|
165 | }
|
---|
166 | if (tp + INT16SZ > endp)
|
---|
167 | return (0);
|
---|
168 | *tp++ = (u_char) (val >> 8) & 0xff;
|
---|
169 | *tp++ = (u_char) val & 0xff;
|
---|
170 | saw_xdigit = 0;
|
---|
171 | count_xdigit = 0;
|
---|
172 | val = 0;
|
---|
173 | continue;
|
---|
174 | }
|
---|
175 | if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
|
---|
176 | inet_pton4(curtok, tp) > 0) {
|
---|
177 | tp += INADDRSZ;
|
---|
178 | saw_xdigit = 0;
|
---|
179 | count_xdigit = 0;
|
---|
180 | break; /* '\0' was seen by inet_pton4(). */
|
---|
181 | }
|
---|
182 | return (0);
|
---|
183 | }
|
---|
184 | if (saw_xdigit) {
|
---|
185 | if (tp + INT16SZ > endp)
|
---|
186 | return (0);
|
---|
187 | *tp++ = (u_char) (val >> 8) & 0xff;
|
---|
188 | *tp++ = (u_char) val & 0xff;
|
---|
189 | }
|
---|
190 | if (colonp != NULL) {
|
---|
191 | /*
|
---|
192 | * Since some memmove()'s erroneously fail to handle
|
---|
193 | * overlapping regions, we'll do the shift by hand.
|
---|
194 | */
|
---|
195 | const int n = tp - colonp;
|
---|
196 | int i;
|
---|
197 |
|
---|
198 | if (tp == endp)
|
---|
199 | return (0);
|
---|
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 | #endif
|
---|