Compatibility text updated.
[gnutls.git] / gl / inet_pton.c
blob8b38e8091dfd4c020ae27762583ac5ea5d190bd2
1 /* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form
3 Copyright (C) 2006, 2008-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU 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.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
35 #include <config.h>
37 /* Specification. */
38 #include <arpa/inet.h>
40 #include <c-ctype.h>
41 #include <string.h>
42 #include <errno.h>
44 #define NS_INADDRSZ 4
45 #define NS_IN6ADDRSZ 16
46 #define NS_INT16SZ 2
49 * WARNING: Don't even consider trying to compile this on a system where
50 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
53 static int inet_pton4 (const char *src, unsigned char *dst);
54 #if HAVE_IPV6
55 static int inet_pton6 (const char *src, unsigned char *dst);
56 #endif
58 /* int
59 * inet_pton(af, src, dst)
60 * convert from presentation format (which usually means ASCII printable)
61 * to network format (which is usually some kind of binary format).
62 * return:
63 * 1 if the address was valid for the specified address family
64 * 0 if the address wasn't valid (`dst' is untouched in this case)
65 * -1 if some other error occurred (`dst' is untouched in this case, too)
66 * author:
67 * Paul Vixie, 1996.
69 int
70 inet_pton (int af, const char *restrict src, void *restrict dst)
72 switch (af)
74 case AF_INET:
75 return (inet_pton4 (src, dst));
77 #if HAVE_IPV6
78 case AF_INET6:
79 return (inet_pton6 (src, dst));
80 #endif
82 default:
83 errno = EAFNOSUPPORT;
84 return (-1);
86 /* NOTREACHED */
89 /* int
90 * inet_pton4(src, dst)
91 * like inet_aton() but without all the hexadecimal, octal (with the
92 * exception of 0) and shorthand.
93 * return:
94 * 1 if `src' is a valid dotted quad, else 0.
95 * notice:
96 * does not touch `dst' unless it's returning 1.
97 * author:
98 * Paul Vixie, 1996.
100 static int
101 inet_pton4 (const char *restrict src, unsigned char *restrict dst)
103 int saw_digit, octets, ch;
104 unsigned char tmp[NS_INADDRSZ], *tp;
106 saw_digit = 0;
107 octets = 0;
108 *(tp = tmp) = 0;
109 while ((ch = *src++) != '\0')
112 if (ch >= '0' && ch <= '9')
114 unsigned new = *tp * 10 + (ch - '0');
116 if (saw_digit && *tp == 0)
117 return (0);
118 if (new > 255)
119 return (0);
120 *tp = new;
121 if (!saw_digit)
123 if (++octets > 4)
124 return (0);
125 saw_digit = 1;
128 else if (ch == '.' && saw_digit)
130 if (octets == 4)
131 return (0);
132 *++tp = 0;
133 saw_digit = 0;
135 else
136 return (0);
138 if (octets < 4)
139 return (0);
140 memcpy (dst, tmp, NS_INADDRSZ);
141 return (1);
144 #if HAVE_IPV6
146 /* int
147 * inet_pton6(src, dst)
148 * convert presentation level address to network order binary form.
149 * return:
150 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
151 * notice:
152 * (1) does not touch `dst' unless it's returning 1.
153 * (2) :: in a full address is silently ignored.
154 * credit:
155 * inspired by Mark Andrews.
156 * author:
157 * Paul Vixie, 1996.
159 static int
160 inet_pton6 (const char *restrict src, unsigned char *restrict dst)
162 static const char xdigits[] = "0123456789abcdef";
163 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
164 const char *curtok;
165 int ch, saw_xdigit;
166 unsigned val;
168 tp = memset (tmp, '\0', NS_IN6ADDRSZ);
169 endp = tp + NS_IN6ADDRSZ;
170 colonp = NULL;
171 /* Leading :: requires some special handling. */
172 if (*src == ':')
173 if (*++src != ':')
174 return (0);
175 curtok = src;
176 saw_xdigit = 0;
177 val = 0;
178 while ((ch = c_tolower (*src++)) != '\0')
180 const char *pch;
182 pch = strchr (xdigits, ch);
183 if (pch != NULL)
185 val <<= 4;
186 val |= (pch - xdigits);
187 if (val > 0xffff)
188 return (0);
189 saw_xdigit = 1;
190 continue;
192 if (ch == ':')
194 curtok = src;
195 if (!saw_xdigit)
197 if (colonp)
198 return (0);
199 colonp = tp;
200 continue;
202 else if (*src == '\0')
204 return (0);
206 if (tp + NS_INT16SZ > endp)
207 return (0);
208 *tp++ = (u_char) (val >> 8) & 0xff;
209 *tp++ = (u_char) val & 0xff;
210 saw_xdigit = 0;
211 val = 0;
212 continue;
214 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
215 inet_pton4 (curtok, tp) > 0)
217 tp += NS_INADDRSZ;
218 saw_xdigit = 0;
219 break; /* '\0' was seen by inet_pton4(). */
221 return (0);
223 if (saw_xdigit)
225 if (tp + NS_INT16SZ > endp)
226 return (0);
227 *tp++ = (u_char) (val >> 8) & 0xff;
228 *tp++ = (u_char) val & 0xff;
230 if (colonp != NULL)
233 * Since some memmove()'s erroneously fail to handle
234 * overlapping regions, we'll do the shift by hand.
236 const int n = tp - colonp;
237 int i;
239 if (tp == endp)
240 return (0);
241 for (i = 1; i <= n; i++)
243 endp[-i] = colonp[n - i];
244 colonp[n - i] = 0;
246 tp = endp;
248 if (tp != endp)
249 return (0);
250 memcpy (dst, tmp, NS_IN6ADDRSZ);
251 return (1);
253 #endif