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