Fix a typo.
[obnox/wireshark/wip.git] / 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$";
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 #include "inet_v6defs.h"
60
61 #include <glib.h>
62
63 #ifndef NS_INADDRSZ
64 #define NS_INADDRSZ     4
65 #endif
66 #ifndef NS_IN6ADDRSZ
67 #define NS_IN6ADDRSZ    16
68 #endif
69 #ifndef NS_INT16SZ
70 #define NS_INT16SZ      2
71 #endif
72
73 /*
74  * WARNING: Don't even consider trying to compile this on a system where
75  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
76  */
77
78 static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
79 static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
80
81 /* char *
82  * inet_ntop(af, src, dst, size)
83  *      convert a network format address to presentation format.
84  * return:
85  *      pointer to presentation format address (`dst'), or NULL (see errno).
86  * author:
87  *      Paul Vixie, 1996.
88  */
89 const char *
90 inet_ntop(af, src, dst, size)
91         int af;
92         const void *src;
93         char *dst;
94         size_t size;
95 {
96         switch (af) {
97         case AF_INET:
98                 return (inet_ntop4(src, dst, size));
99         case AF_INET6:
100                 return (inet_ntop6(src, dst, size));
101         default:
102                 errno = EAFNOSUPPORT;
103                 return (NULL);
104         }
105         /* NOTREACHED */
106 }
107
108 /* const char *
109  * inet_ntop4(src, dst, size)
110  *      format an IPv4 address
111  * return:
112  *      `dst' (as a const)
113  * notes:
114  *      (1) uses no statics
115  *      (2) takes a u_char* not an in_addr as input
116  * author:
117  *      Paul Vixie, 1996.
118  */
119 static const char *
120 inet_ntop4(src, dst, size)
121         const u_char *src;
122         char *dst;
123         size_t size;
124 {
125         static const char fmt[] = "%u.%u.%u.%u";
126         char tmp[sizeof "255.255.255.255"];
127         int nprinted;
128
129         nprinted = g_snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
130         if (nprinted < 0)
131                 return (NULL);  /* we assume "errno" was set by "g_snprintf()" */
132         if ((size_t)nprinted > size) {
133                 errno = ENOSPC;
134                 return (NULL);
135         }
136         strcpy(dst, tmp);
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, 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         strcpy(dst, tmp);
234         return (dst);
235 }