Commit | Line | Data |
---|---|---|
8912421c LC |
1 | /* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form |
2 | ||
af07e104 | 3 | Copyright (C) 2006, 2008-2013 Free Software Foundation, Inc. |
8912421c LC |
4 | |
5 | This program is free software: you can redistribute it and/or modify | |
6 | it under the terms of the GNU Lesser General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | /* | |
19 | * Copyright (c) 1996,1999 by Internet Software Consortium. | |
20 | * | |
21 | * Permission to use, copy, modify, and distribute this software for any | |
22 | * purpose with or without fee is hereby granted, provided that the above | |
23 | * copyright notice and this permission notice appear in all copies. | |
24 | * | |
25 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | |
26 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
27 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | |
28 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
29 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
30 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
31 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
32 | * SOFTWARE. | |
33 | */ | |
34 | ||
35 | #include <config.h> | |
36 | ||
37 | /* Specification. */ | |
38 | #include <arpa/inet.h> | |
39 | ||
35428fb6 | 40 | #if HAVE_DECL_INET_PTON |
8912421c | 41 | |
35428fb6 LC |
42 | # undef inet_pton |
43 | ||
44 | int | |
45 | rpl_inet_pton (int af, const char *restrict src, void *restrict dst) | |
46 | { | |
47 | return inet_pton (af, src, dst); | |
48 | } | |
49 | ||
50 | #else | |
51 | ||
52 | # include <c-ctype.h> | |
53 | # include <string.h> | |
54 | # include <errno.h> | |
55 | ||
56 | # define NS_INADDRSZ 4 | |
57 | # define NS_IN6ADDRSZ 16 | |
58 | # define NS_INT16SZ 2 | |
8912421c LC |
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 (const char *src, unsigned char *dst); | |
35428fb6 | 66 | # if HAVE_IPV6 |
8912421c | 67 | static int inet_pton6 (const char *src, unsigned char *dst); |
35428fb6 | 68 | # endif |
8912421c LC |
69 | |
70 | /* int | |
71 | * inet_pton(af, src, dst) | |
1cd4fffc LC |
72 | * convert from presentation format (which usually means ASCII printable) |
73 | * to network format (which is usually some kind of binary format). | |
8912421c | 74 | * return: |
1cd4fffc | 75 | * 1 if the address was valid for the specified address family |
f0007cad LC |
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) | |
8912421c | 78 | * author: |
1cd4fffc | 79 | * Paul Vixie, 1996. |
8912421c LC |
80 | */ |
81 | int | |
82 | inet_pton (int af, const char *restrict src, void *restrict dst) | |
83 | { | |
84 | switch (af) | |
85 | { | |
86 | case AF_INET: | |
87 | return (inet_pton4 (src, dst)); | |
88 | ||
35428fb6 | 89 | # if HAVE_IPV6 |
8912421c LC |
90 | case AF_INET6: |
91 | return (inet_pton6 (src, dst)); | |
35428fb6 | 92 | # endif |
8912421c LC |
93 | |
94 | default: | |
95 | errno = EAFNOSUPPORT; | |
96 | return (-1); | |
97 | } | |
98 | /* NOTREACHED */ | |
99 | } | |
100 | ||
101 | /* int | |
102 | * inet_pton4(src, dst) | |
1cd4fffc LC |
103 | * like inet_aton() but without all the hexadecimal, octal (with the |
104 | * exception of 0) and shorthand. | |
8912421c | 105 | * return: |
f0007cad | 106 | * 1 if 'src' is a valid dotted quad, else 0. |
8912421c | 107 | * notice: |
f0007cad | 108 | * does not touch 'dst' unless it's returning 1. |
8912421c | 109 | * author: |
1cd4fffc | 110 | * Paul Vixie, 1996. |
8912421c LC |
111 | */ |
112 | static int | |
113 | inet_pton4 (const char *restrict src, unsigned char *restrict dst) | |
114 | { | |
115 | int saw_digit, octets, ch; | |
116 | unsigned char tmp[NS_INADDRSZ], *tp; | |
117 | ||
118 | saw_digit = 0; | |
119 | octets = 0; | |
120 | *(tp = tmp) = 0; | |
121 | while ((ch = *src++) != '\0') | |
122 | { | |
123 | ||
124 | if (ch >= '0' && ch <= '9') | |
1cd4fffc LC |
125 | { |
126 | unsigned new = *tp * 10 + (ch - '0'); | |
127 | ||
128 | if (saw_digit && *tp == 0) | |
129 | return (0); | |
130 | if (new > 255) | |
131 | return (0); | |
132 | *tp = new; | |
133 | if (!saw_digit) | |
134 | { | |
135 | if (++octets > 4) | |
136 | return (0); | |
137 | saw_digit = 1; | |
138 | } | |
139 | } | |
8912421c | 140 | else if (ch == '.' && saw_digit) |
1cd4fffc LC |
141 | { |
142 | if (octets == 4) | |
143 | return (0); | |
144 | *++tp = 0; | |
145 | saw_digit = 0; | |
146 | } | |
8912421c | 147 | else |
1cd4fffc | 148 | return (0); |
8912421c LC |
149 | } |
150 | if (octets < 4) | |
151 | return (0); | |
152 | memcpy (dst, tmp, NS_INADDRSZ); | |
153 | return (1); | |
154 | } | |
155 | ||
35428fb6 | 156 | # if HAVE_IPV6 |
8912421c LC |
157 | |
158 | /* int | |
159 | * inet_pton6(src, dst) | |
1cd4fffc | 160 | * convert presentation level address to network order binary form. |
8912421c | 161 | * return: |
f0007cad | 162 | * 1 if 'src' is a valid [RFC1884 2.2] address, else 0. |
8912421c | 163 | * notice: |
f0007cad | 164 | * (1) does not touch 'dst' unless it's returning 1. |
1cd4fffc | 165 | * (2) :: in a full address is silently ignored. |
8912421c | 166 | * credit: |
1cd4fffc | 167 | * inspired by Mark Andrews. |
8912421c | 168 | * author: |
1cd4fffc | 169 | * Paul Vixie, 1996. |
8912421c LC |
170 | */ |
171 | static int | |
172 | inet_pton6 (const char *restrict src, unsigned char *restrict dst) | |
173 | { | |
174 | static const char xdigits[] = "0123456789abcdef"; | |
175 | unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; | |
176 | const char *curtok; | |
177 | int ch, saw_xdigit; | |
178 | unsigned val; | |
179 | ||
180 | tp = memset (tmp, '\0', NS_IN6ADDRSZ); | |
181 | endp = tp + NS_IN6ADDRSZ; | |
182 | colonp = NULL; | |
183 | /* Leading :: requires some special handling. */ | |
184 | if (*src == ':') | |
185 | if (*++src != ':') | |
186 | return (0); | |
187 | curtok = src; | |
188 | saw_xdigit = 0; | |
189 | val = 0; | |
190 | while ((ch = c_tolower (*src++)) != '\0') | |
191 | { | |
192 | const char *pch; | |
193 | ||
194 | pch = strchr (xdigits, ch); | |
195 | if (pch != NULL) | |
1cd4fffc LC |
196 | { |
197 | val <<= 4; | |
198 | val |= (pch - xdigits); | |
199 | if (val > 0xffff) | |
200 | return (0); | |
201 | saw_xdigit = 1; | |
202 | continue; | |
203 | } | |
8912421c | 204 | if (ch == ':') |
1cd4fffc LC |
205 | { |
206 | curtok = src; | |
207 | if (!saw_xdigit) | |
208 | { | |
209 | if (colonp) | |
210 | return (0); | |
211 | colonp = tp; | |
212 | continue; | |
213 | } | |
214 | else if (*src == '\0') | |
215 | { | |
216 | return (0); | |
217 | } | |
218 | if (tp + NS_INT16SZ > endp) | |
219 | return (0); | |
220 | *tp++ = (u_char) (val >> 8) & 0xff; | |
221 | *tp++ = (u_char) val & 0xff; | |
222 | saw_xdigit = 0; | |
223 | val = 0; | |
224 | continue; | |
225 | } | |
8912421c | 226 | if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && |
1cd4fffc LC |
227 | inet_pton4 (curtok, tp) > 0) |
228 | { | |
229 | tp += NS_INADDRSZ; | |
230 | saw_xdigit = 0; | |
231 | break; /* '\0' was seen by inet_pton4(). */ | |
232 | } | |
8912421c LC |
233 | return (0); |
234 | } | |
235 | if (saw_xdigit) | |
236 | { | |
237 | if (tp + NS_INT16SZ > endp) | |
1cd4fffc | 238 | return (0); |
8912421c LC |
239 | *tp++ = (u_char) (val >> 8) & 0xff; |
240 | *tp++ = (u_char) val & 0xff; | |
241 | } | |
242 | if (colonp != NULL) | |
243 | { | |
244 | /* | |
245 | * Since some memmove()'s erroneously fail to handle | |
246 | * overlapping regions, we'll do the shift by hand. | |
247 | */ | |
248 | const int n = tp - colonp; | |
249 | int i; | |
250 | ||
251 | if (tp == endp) | |
1cd4fffc | 252 | return (0); |
8912421c | 253 | for (i = 1; i <= n; i++) |
1cd4fffc LC |
254 | { |
255 | endp[-i] = colonp[n - i]; | |
256 | colonp[n - i] = 0; | |
257 | } | |
8912421c LC |
258 | tp = endp; |
259 | } | |
260 | if (tp != endp) | |
261 | return (0); | |
262 | memcpy (dst, tmp, NS_IN6ADDRSZ); | |
263 | return (1); | |
264 | } | |
35428fb6 LC |
265 | |
266 | # endif | |
267 | ||
8912421c | 268 | #endif |