sensitivity of packet range options fine tuning:
[obnox/wireshark/wip.git] / epan / 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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #if defined(LIBC_SCCS) && !defined(lint)
23 static char rcsid[] = "$Id: inet_ntop.c,v 1.7 2002/10/29 00:28:10 guy Exp $";
24 #endif /* LIBC_SCCS and not lint */
25
26 #ifdef HAVE_SYS_PARAM_H
27 #include <sys/param.h>
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>         /* needed to define AF_ values on UNIX */
36 #endif
37
38 #ifdef HAVE_WINSOCK2_H
39 #include <winsock2.h>           /* needed to define AF_ values on Windows */
40 #define EAFNOSUPPORT    WSAEAFNOSUPPORT
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 <errno.h>
56 #include <stdio.h>
57 #include <string.h>
58
59 #ifdef NEED_SNPRINTF_H
60 # include "snprintf.h"
61 #endif
62
63 #include "inet_v6defs.h"
64
65 #ifndef NS_INADDRSZ
66 #define NS_INADDRSZ     4
67 #endif
68 #ifndef NS_IN6ADDRSZ
69 #define NS_IN6ADDRSZ    16
70 #endif
71 #ifndef NS_INT16SZ
72 #define NS_INT16SZ      2
73 #endif
74
75 /*
76  * WARNING: Don't even consider trying to compile this on a system where
77  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
78  */
79
80 static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
81 static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
82
83 /* char *
84  * inet_ntop(af, src, dst, size)
85  *      convert a network format address to presentation format.
86  * return:
87  *      pointer to presentation format address (`dst'), or NULL (see errno).
88  * author:
89  *      Paul Vixie, 1996.
90  */
91 const char *
92 inet_ntop(af, src, dst, size)
93         int af;
94         const void *src;
95         char *dst;
96         size_t size;
97 {
98         switch (af) {
99         case AF_INET:
100                 return (inet_ntop4(src, dst, size));
101         case AF_INET6:
102                 return (inet_ntop6(src, dst, size));
103         default:
104                 errno = EAFNOSUPPORT;
105                 return (NULL);
106         }
107         /* NOTREACHED */
108 }
109
110 /* const char *
111  * inet_ntop4(src, dst, size)
112  *      format an IPv4 address
113  * return:
114  *      `dst' (as a const)
115  * notes:
116  *      (1) uses no statics
117  *      (2) takes a u_char* not an in_addr as input
118  * author:
119  *      Paul Vixie, 1996.
120  */
121 static const char *
122 inet_ntop4(src, dst, size)
123         const u_char *src;
124         char *dst;
125         size_t size;
126 {
127         static const char fmt[] = "%u.%u.%u.%u";
128         char tmp[sizeof "255.255.255.255"];
129         int nprinted;
130
131         nprinted = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
132         if (nprinted < 0)
133                 return (NULL);  /* we assume "errno" was set by "snprintf()" */
134         if ((size_t)nprinted > size) {
135                 errno = ENOSPC;
136                 return (NULL);
137         }
138         strcpy(dst, tmp);
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 += sprintf(tp, "%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         strcpy(dst, tmp);
236         return (dst);
237 }