Add ws_load_library and ws_module_open, which respectively call
[obnox/wireshark/wip.git] / wsutil / inet_pton.c
1 /*
2  * Copyright (c) 1996,1999 by Internet Software Consortium.
3  *
4  * $Id$
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
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifdef HAVE_SYS_PARAM_H
25 #include <sys/param.h>
26 #endif
27
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
31
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>         /* needed to define AF_ values on UNIX */
34 #endif
35
36 #ifdef HAVE_WINSOCK2_H
37 #include <winsock2.h>   /* needed to define AF_ values on Windows */
38 #if _MSC_VER < 1600     /* errno.h defines EAFNOSUPPORT in Windows VC10 (and presumably eventually in VC11 ...) */
39 #define EAFNOSUPPORT    WSAEAFNOSUPPORT
40 #endif
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
45 #endif
46
47 #ifdef HAVE_ARPA_INET_H
48 #include <arpa/inet.h>
49 #endif
50
51 #ifdef HAVE_ARPA_NAMESER_H
52 #include <arpa/nameser.h>
53 #endif
54
55 #include <string.h>
56 #include <errno.h>
57
58 #include "inet_v6defs.h"
59
60 #ifndef NS_INADDRSZ
61 #define NS_INADDRSZ     4
62 #endif
63 #ifndef NS_IN6ADDRSZ
64 #define NS_IN6ADDRSZ    16
65 #endif
66 #ifndef NS_INT16SZ
67 #define NS_INT16SZ      2
68 #endif
69
70 /*
71  * WARNING: Don't even consider trying to compile this on a system where
72  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
73  */
74
75 #ifdef AF_INET
76 static int      inet_pton4 __P((const char *src, u_char *dst));
77 #endif
78 #ifdef AF_INET6
79 static int      inet_pton6 __P((const char *src, u_char *dst));
80 #endif
81
82 /* int
83  * inet_pton(af, src, dst)
84  *      convert from presentation format (which usually means ASCII printable)
85  *      to network format (which is usually some kind of binary format).
86  * return:
87  *      1 if the address was valid for the specified address family
88  *      0 if the address wasn't valid (`dst' is untouched in this case)
89  *      -1 if some other error occurred (`dst' is untouched in this case, too)
90  * author:
91  *      Paul Vixie, 1996.
92  */
93 int
94 inet_pton(af, src, dst)
95         int af;
96         const char *src;
97         void *dst;
98 {
99         switch (af) {
100 #ifdef AF_INET
101         case AF_INET:
102                 return (inet_pton4(src, dst));
103 #endif
104 #ifdef AF_INET6
105         case AF_INET6:
106                 return (inet_pton6(src, dst));
107 #endif
108         default:
109                 errno = EAFNOSUPPORT;
110                 return (-1);
111         }
112         /* NOTREACHED */
113 }
114
115 #ifdef AF_INET
116 /* int
117  * inet_pton4(src, dst)
118  *      like inet_aton() but without all the hexadecimal and shorthand.
119  * return:
120  *      1 if `src' is a valid dotted quad, else 0.
121  * notice:
122  *      does not touch `dst' unless it's returning 1.
123  * author:
124  *      Paul Vixie, 1996.
125  */
126 static int
127 inet_pton4(src, dst)
128         const char *src;
129         u_char *dst;
130 {
131         static const char digits[] = "0123456789";
132         int saw_digit, octets, ch;
133         u_char tmp[NS_INADDRSZ], *tp;
134
135         saw_digit = 0;
136         octets = 0;
137         *(tp = tmp) = 0;
138         while ((ch = *src++) != '\0') {
139                 const char *pch;
140
141                 if ((pch = strchr(digits, ch)) != NULL) {
142                         size_t new = *tp * 10 + (pch - digits);
143
144                         if (new > 255)
145                                 return (0);
146                         *tp = (u_char) new;
147                         if (! saw_digit) {
148                                 if (++octets > 4)
149                                         return (0);
150                                 saw_digit = 1;
151                         }
152                 } else if (ch == '.' && saw_digit) {
153                         if (octets == 4)
154                                 return (0);
155                         *++tp = 0;
156                         saw_digit = 0;
157                 } else
158                         return (0);
159         }
160         if (octets < 4)
161                 return (0);
162         memcpy(dst, tmp, NS_INADDRSZ);
163         return (1);
164 }
165 #endif
166
167 #ifdef AF_INET6
168 /* int
169  * inet_pton6(src, dst)
170  *      convert presentation level address to network order binary form.
171  * return:
172  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
173  * notice:
174  *      (1) does not touch `dst' unless it's returning 1.
175  *      (2) :: in a full address is silently ignored.
176  * credit:
177  *      inspired by Mark Andrews.
178  * author:
179  *      Paul Vixie, 1996.
180  */
181 static int
182 inet_pton6(src, dst)
183         const char *src;
184         u_char *dst;
185 {
186         static const char xdigits_l[] = "0123456789abcdef",
187                           xdigits_u[] = "0123456789ABCDEF";
188         u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
189         const char *xdigits, *curtok;
190         int ch, saw_xdigit;
191         u_int val;
192
193         memset((tp = tmp), '\0', NS_IN6ADDRSZ);
194         endp = tp + NS_IN6ADDRSZ;
195         colonp = NULL;
196         /* Leading :: requires some special handling. */
197         if (*src == ':')
198                 if (*++src != ':')
199                         return (0);
200         curtok = src;
201         saw_xdigit = 0;
202         val = 0;
203         while ((ch = *src++) != '\0') {
204                 const char *pch;
205
206                 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
207                         pch = strchr((xdigits = xdigits_u), ch);
208                 if (pch != NULL) {
209                         val <<= 4;
210                         val |= (pch - xdigits);
211                         if (val > 0xffff)
212                                 return (0);
213                         saw_xdigit = 1;
214                         continue;
215                 }
216                 if (ch == ':') {
217                         curtok = src;
218                         if (!saw_xdigit) {
219                                 if (colonp)
220                                         return (0);
221                                 colonp = tp;
222                                 continue;
223                         } else if (*src == '\0') {
224                                 return (0);
225                         }
226                         if (tp + NS_INT16SZ > endp)
227                                 return (0);
228                         *tp++ = (u_char) (val >> 8) & 0xff;
229                         *tp++ = (u_char) val & 0xff;
230                         saw_xdigit = 0;
231                         val = 0;
232                         continue;
233                 }
234                 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
235                     inet_pton4(curtok, tp) > 0) {
236                         tp += NS_INADDRSZ;
237                         saw_xdigit = 0;
238                         break;  /* '\0' was seen by inet_pton4(). */
239                 }
240                 return (0);
241         }
242         if (saw_xdigit) {
243                 if (tp + NS_INT16SZ > endp)
244                         return (0);
245                 *tp++ = (u_char) (val >> 8) & 0xff;
246                 *tp++ = (u_char) val & 0xff;
247         }
248         if (colonp != NULL) {
249                 /*
250                  * Since some memmove()'s erroneously fail to handle
251                  * overlapping regions, we'll do the shift by hand.
252                  */
253                 const int n = (int) (tp - colonp);
254                 int i;
255
256                 if (tp == endp)
257                         return (0);
258                 for (i = 1; i <= n; i++) {
259                         endp[- i] = colonp[n - i];
260                         colonp[n - i] = 0;
261                 }
262                 tp = endp;
263         }
264         if (tp != endp)
265                 return (0);
266         memcpy(dst, tmp, NS_IN6ADDRSZ);
267         return (1);
268 }
269 #endif