strptime.c needs ctype.h.
[metze/wireshark/wip.git] / wsutil / inet_ntop.c
1 /*
2  * Copyright (c) 1996-1999 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18
19 #include "config.h"
20
21 #ifdef HAVE_SYS_PARAM_H
22 #include <sys/param.h>
23 #endif
24
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>         /* needed to define AF_ values on UNIX */
31 #endif
32
33 #ifdef HAVE_WINSOCK2_H
34 #include <winsock2.h>   /* needed to define AF_ values on Windows */
35 #if _MSC_VER < 1600     /* errno.h defines EAFNOSUPPORT in Windows VC10 (and presumably eventually in VC11 ...) */
36 #define EAFNOSUPPORT    WSAEAFNOSUPPORT
37 #endif
38 #endif
39
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
42 #endif
43
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
46 #endif
47
48 #ifdef HAVE_ARPA_NAMESER_H
49 #include <arpa/nameser.h>
50 #endif
51
52 #include <errno.h>
53 #include <stdio.h>
54 #include <string.h>
55
56 #include "inet_v6defs.h"
57
58 #include <glib.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 static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
76 static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
77
78 /* char *
79  * inet_ntop(af, src, dst, size)
80  *      convert a network format address to presentation format.
81  * return:
82  *      pointer to presentation format address (`dst'), or NULL (see errno).
83  * author:
84  *      Paul Vixie, 1996.
85  */
86 const char *
87 inet_ntop(af, src, dst, size)
88         int af;
89         const void *src;
90         char *dst;
91         size_t size;
92 {
93         switch (af) {
94         case AF_INET:
95                 return (inet_ntop4(src, dst, size));
96         case AF_INET6:
97                 return (inet_ntop6(src, dst, size));
98         default:
99                 errno = EAFNOSUPPORT;
100                 return (NULL);
101         }
102         /* NOTREACHED */
103 }
104
105 /* const char *
106  * inet_ntop4(src, dst, size)
107  *      format an IPv4 address
108  * return:
109  *      `dst' (as a const)
110  * notes:
111  *      (1) uses no statics
112  *      (2) takes a u_char* not an in_addr as input
113  * author:
114  *      Paul Vixie, 1996.
115  */
116 static const char *
117 inet_ntop4(src, dst, size)
118         const u_char *src;
119         char *dst;
120         size_t size;
121 {
122         static const char fmt[] = "%u.%u.%u.%u";
123         char tmp[sizeof "255.255.255.255"];
124         int nprinted;
125
126         nprinted = g_snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
127         /* Note: nprinted *excludes* the trailing '\0' character */
128         if ((size_t)nprinted >= size) {
129                 errno = ENOSPC;
130                 return (NULL);
131         }
132         g_strlcpy(dst, tmp, size);
133         return (dst);
134 }
135
136 /* const char *
137  * inet_ntop6(src, dst, size)
138  *      convert IPv6 binary address into presentation (printable) format
139  * author:
140  *      Paul Vixie, 1996.
141  */
142 static const char *
143 inet_ntop6(src, dst, size)
144         const u_char *src;
145         char *dst;
146         size_t size;
147 {
148         /*
149          * Note that int32_t and int16_t need only be "at least" large enough
150          * to contain a value of the specified size.  On some systems, like
151          * Crays, there is no such thing as an integer variable with 16 bits.
152          * Keep this in mind if you think this function should have been coded
153          * to use pointer overlays.  All the world's not a VAX.
154          */
155         char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
156         struct { int base, len; } best, cur;
157         u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
158         int i;
159
160         /*
161          * Preprocess:
162          *      Copy the input (bytewise) array into a wordwise array.
163          *      Find the longest run of 0x00's in src[] for :: shorthanding.
164          */
165         memset(words, '\0', sizeof words);
166         for (i = 0; i < NS_IN6ADDRSZ; i++)
167                 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
168         best.base = -1;
169         cur.base = -1;
170         for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
171                 if (words[i] == 0) {
172                         if (cur.base == -1)
173                                 cur.base = i, cur.len = 1;
174                         else
175                                 cur.len++;
176                 } else {
177                         if (cur.base != -1) {
178                                 if (best.base == -1 || cur.len > best.len)
179                                         best = cur;
180                                 cur.base = -1;
181                         }
182                 }
183         }
184         if (cur.base != -1) {
185                 if (best.base == -1 || cur.len > best.len)
186                         best = cur;
187         }
188         if (best.base != -1 && best.len < 2)
189                 best.base = -1;
190
191         /*
192          * Format the result.
193          */
194         tp = tmp;
195         for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
196                 /* Are we inside the best run of 0x00's? */
197                 if (best.base != -1 && i >= best.base &&
198                     i < (best.base + best.len)) {
199                         if (i == best.base)
200                                 *tp++ = ':';
201                         continue;
202                 }
203                 /* Are we following an initial run of 0x00s or any real hex? */
204                 if (i != 0)
205                         *tp++ = ':';
206                 /* Is this address an encapsulated IPv4? */
207                 if (i == 6 && best.base == 0 &&
208                     (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
209                         if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
210                                 return (NULL);
211                         tp += strlen(tp);
212                         break;
213                 }
214                 tp += g_snprintf(tp, (gulong) (sizeof tmp - (tp - tmp)), "%x", words[i]);
215         }
216         /* Was it a trailing run of 0x00's? */
217         if (best.base != -1 && (best.base + best.len) ==
218             (NS_IN6ADDRSZ / NS_INT16SZ))
219                 *tp++ = ':';
220         *tp++ = '\0';
221
222         /*
223          * Check for overflow, copy, and we're done.
224          */
225         if ((size_t)(tp - tmp) > size) {
226                 errno = ENOSPC;
227                 return (NULL);
228         }
229         g_strlcpy(dst, tmp, size);
230         return (dst);
231 }
232
233 /*
234  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
235  *
236  * Local variables:
237  * c-basic-offset: 8
238  * tab-width: 8
239  * indent-tabs-mode: t
240  * End:
241  *
242  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
243  * :indentSize=8:tabSize=8:noTabs=false:
244  */