Be a little more explicit in our description of tvb_get_ptr.
[metze/wireshark/wip.git] / inet_ntop.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 1996-1999 by Internet Software Consortium.
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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #if defined(LIBC_SCCS) && !defined(lint)
26 static char rcsid[] = "$Id$";
27 #endif /* LIBC_SCCS and not lint */
28
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
31 #endif
32
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>         /* needed to define AF_ values on UNIX */
39 #endif
40
41 #ifdef HAVE_WINSOCK2_H
42 #include <winsock2.h>           /* needed to define AF_ values on Windows */
43 #define EAFNOSUPPORT    WSAEAFNOSUPPORT
44 #endif
45
46 #ifdef HAVE_NETINET_IN_H
47 #include <netinet/in.h>
48 #endif
49
50 #ifdef HAVE_ARPA_INET_H
51 #include <arpa/inet.h>
52 #endif
53
54 #ifdef HAVE_ARPA_NAMESER_H
55 #include <arpa/nameser.h>
56 #endif
57
58 #include <errno.h>
59 #include <stdio.h>
60 #include <string.h>
61
62 #include "inet_v6defs.h"
63
64 #include <glib.h>
65
66 #ifndef NS_INADDRSZ
67 #define NS_INADDRSZ     4
68 #endif
69 #ifndef NS_IN6ADDRSZ
70 #define NS_IN6ADDRSZ    16
71 #endif
72 #ifndef NS_INT16SZ
73 #define NS_INT16SZ      2
74 #endif
75
76 /*
77  * WARNING: Don't even consider trying to compile this on a system where
78  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
79  */
80
81 static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
82 static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
83
84 /* char *
85  * inet_ntop(af, src, dst, size)
86  *      convert a network format address to presentation format.
87  * return:
88  *      pointer to presentation format address (`dst'), or NULL (see errno).
89  * author:
90  *      Paul Vixie, 1996.
91  */
92 const char *
93 inet_ntop(af, src, dst, size)
94         int af;
95         const void *src;
96         char *dst;
97         size_t size;
98 {
99         switch (af) {
100         case AF_INET:
101                 return (inet_ntop4(src, dst, size));
102         case AF_INET6:
103                 return (inet_ntop6(src, dst, size));
104         default:
105                 errno = EAFNOSUPPORT;
106                 return (NULL);
107         }
108         /* NOTREACHED */
109 }
110
111 /* const char *
112  * inet_ntop4(src, dst, size)
113  *      format an IPv4 address
114  * return:
115  *      `dst' (as a const)
116  * notes:
117  *      (1) uses no statics
118  *      (2) takes a u_char* not an in_addr as input
119  * author:
120  *      Paul Vixie, 1996.
121  */
122 static const char *
123 inet_ntop4(src, dst, size)
124         const u_char *src;
125         char *dst;
126         size_t size;
127 {
128         static const char fmt[] = "%u.%u.%u.%u";
129         char tmp[sizeof "255.255.255.255"];
130         int nprinted;
131
132         nprinted = g_snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
133         /* Note: nprinted *excludes* the trailing '\0' character */
134         if ((size_t)nprinted >= size) {
135                 errno = ENOSPC;
136                 return (NULL);
137         }
138         g_strlcpy(dst, tmp, size);
139         return (dst);
140 }
141
142 /* const char *
143  * inet_ntop6(src, dst, size)
144  *      convert IPv6 binary address into presentation (printable) format
145  * author:
146  *      Paul Vixie, 1996.
147  */
148 static const char *
149 inet_ntop6(src, dst, size)
150         const u_char *src;
151         char *dst;
152         size_t size;
153 {
154         /*
155          * Note that int32_t and int16_t need only be "at least" large enough
156          * to contain a value of the specified size.  On some systems, like
157          * Crays, there is no such thing as an integer variable with 16 bits.
158          * Keep this in mind if you think this function should have been coded
159          * to use pointer overlays.  All the world's not a VAX.
160          */
161         char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
162         struct { int base, len; } best, cur;
163         u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
164         int i;
165
166         /*
167          * Preprocess:
168          *      Copy the input (bytewise) array into a wordwise array.
169          *      Find the longest run of 0x00's in src[] for :: shorthanding.
170          */
171         memset(words, '\0', sizeof words);
172         for (i = 0; i < NS_IN6ADDRSZ; i++)
173                 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
174         best.base = -1;
175         cur.base = -1;
176         for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
177                 if (words[i] == 0) {
178                         if (cur.base == -1)
179                                 cur.base = i, cur.len = 1;
180                         else
181                                 cur.len++;
182                 } else {
183                         if (cur.base != -1) {
184                                 if (best.base == -1 || cur.len > best.len)
185                                         best = cur;
186                                 cur.base = -1;
187                         }
188                 }
189         }
190         if (cur.base != -1) {
191                 if (best.base == -1 || cur.len > best.len)
192                         best = cur;
193         }
194         if (best.base != -1 && best.len < 2)
195                 best.base = -1;
196
197         /*
198          * Format the result.
199          */
200         tp = tmp;
201         for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
202                 /* Are we inside the best run of 0x00's? */
203                 if (best.base != -1 && i >= best.base &&
204                     i < (best.base + best.len)) {
205                         if (i == best.base)
206                                 *tp++ = ':';
207                         continue;
208                 }
209                 /* Are we following an initial run of 0x00s or any real hex? */
210                 if (i != 0)
211                         *tp++ = ':';
212                 /* Is this address an encapsulated IPv4? */
213                 if (i == 6 && best.base == 0 &&
214                     (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
215                         if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
216                                 return (NULL);
217                         tp += strlen(tp);
218                         break;
219                 }
220                 tp += g_snprintf(tp, (gulong) (sizeof tmp - (tp - tmp)), "%x", words[i]);
221         }
222         /* Was it a trailing run of 0x00's? */
223         if (best.base != -1 && (best.base + best.len) ==
224             (NS_IN6ADDRSZ / NS_INT16SZ))
225                 *tp++ = ':';
226         *tp++ = '\0';
227
228         /*
229          * Check for overflow, copy, and we're done.
230          */
231         if ((size_t)(tp - tmp) > size) {
232                 errno = ENOSPC;
233                 return (NULL);
234         }
235         g_strlcpy(dst, tmp, size);
236         return (dst);
237 }