Merge from vendor branch FILE:
[dragonfly.git] / lib / libc / net / inet_pton.c
1 /*      $KAME: inet_pton.c,v 1.5 2001/08/20 02:32:40 itojun 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  * $FreeBSD: src/lib/libc/net/inet_pton.c,v 1.6.2.1 2002/04/28 05:40:24 suz Exp $
19  * $DragonFly: src/lib/libc/net/inet_pton.c,v 1.4 2005/11/13 02:04:47 swildner Exp $
20  */
21
22 #include <sys/param.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <arpa/nameser.h>
28 #include <string.h>
29 #include <errno.h>
30
31 /*
32  * WARNING: Don't even consider trying to compile this on a system where
33  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
34  */
35
36 static int      inet_pton4 (const char *src, u_char *dst);
37 static int      inet_pton6 (const char *src, u_char *dst);
38
39 /* int
40  * inet_pton(af, src, dst)
41  *      convert from presentation format (which usually means ASCII printable)
42  *      to network format (which is usually some kind of binary format).
43  * return:
44  *      1 if the address was valid for the specified address family
45  *      0 if the address wasn't valid (`dst' is untouched in this case)
46  *      -1 if some other error occurred (`dst' is untouched in this case, too)
47  * author:
48  *      Paul Vixie, 1996.
49  */
50 int
51 inet_pton(int af, const char *src, void *dst)
52 {
53         switch (af) {
54         case AF_INET:
55                 return (inet_pton4(src, dst));
56         case AF_INET6:
57                 return (inet_pton6(src, dst));
58         default:
59                 errno = EAFNOSUPPORT;
60                 return (-1);
61         }
62         /* NOTREACHED */
63 }
64
65 /* int
66  * inet_pton4(src, dst)
67  *      like inet_aton() but without all the hexadecimal and shorthand.
68  * return:
69  *      1 if `src' is a valid dotted quad, else 0.
70  * notice:
71  *      does not touch `dst' unless it's returning 1.
72  * author:
73  *      Paul Vixie, 1996.
74  */
75 static int
76 inet_pton4(const char *src, u_char *dst)
77 {
78         static const char digits[] = "0123456789";
79         int saw_digit, octets, ch;
80         u_char tmp[NS_INADDRSZ], *tp;
81
82         saw_digit = 0;
83         octets = 0;
84         *(tp = tmp) = 0;
85         while ((ch = *src++) != '\0') {
86                 const char *pch;
87
88                 if ((pch = strchr(digits, ch)) != NULL) {
89                         u_int new = *tp * 10 + (pch - digits);
90
91                         if (new > 255)
92                                 return (0);
93                         *tp = new;
94                         if (! saw_digit) {
95                                 if (++octets > 4)
96                                         return (0);
97                                 saw_digit = 1;
98                         }
99                 } else if (ch == '.' && saw_digit) {
100                         if (octets == 4)
101                                 return (0);
102                         *++tp = 0;
103                         saw_digit = 0;
104                 } else
105                         return (0);
106         }
107         if (octets < 4)
108                 return (0);
109
110         memcpy(dst, tmp, NS_INADDRSZ);
111         return (1);
112 }
113
114 /* int
115  * inet_pton6(src, dst)
116  *      convert presentation level address to network order binary form.
117  * return:
118  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
119  * notice:
120  *      (1) does not touch `dst' unless it's returning 1.
121  *      (2) :: in a full address is silently ignored.
122  * credit:
123  *      inspired by Mark Andrews.
124  * author:
125  *      Paul Vixie, 1996.
126  */
127 static int
128 inet_pton6(const char *src, u_char *dst)
129 {
130         static const char xdigits_l[] = "0123456789abcdef",
131                           xdigits_u[] = "0123456789ABCDEF";
132         u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
133         const char *xdigits, *curtok;
134         int ch, saw_xdigit;
135         u_int val;
136
137         memset((tp = tmp), '\0', NS_IN6ADDRSZ);
138         endp = tp + NS_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 = xdigits_l), ch)) == NULL)
151                         pch = strchr((xdigits = xdigits_u), 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                         }
168                         if (tp + NS_INT16SZ > endp)
169                                 return (0);
170                         *tp++ = (u_char) (val >> 8) & 0xff;
171                         *tp++ = (u_char) val & 0xff;
172                         saw_xdigit = 0;
173                         val = 0;
174                         continue;
175                 }
176                 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
177                     inet_pton4(curtok, tp) > 0) {
178                         tp += NS_INADDRSZ;
179                         saw_xdigit = 0;
180                         break;  /* '\0' was seen by inet_pton4(). */
181                 }
182                 return (0);
183         }
184         if (saw_xdigit) {
185                 if (tp + NS_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                 for (i = 1; i <= n; i++) {
199                         endp[- i] = colonp[n - i];
200                         colonp[n - i] = 0;
201                 }
202                 tp = endp;
203         }
204         if (tp != endp)
205                 return (0);
206         memcpy(dst, tmp, NS_IN6ADDRSZ);
207         return (1);
208 }
209
210 /*
211  * Weak aliases for applications that use certain private entry points,
212  * and fail to include <arpa/inet.h>.
213  */
214 #undef inet_pton
215 __weak_reference(__inet_pton, inet_pton);