Add tcpdump option, disabled by default
[tomato.git] / release / src / router / tcpdump / missing / inet_ntop.c
blobd17d5925737d7a623d4d909f871fe0630a7da463
1 /*
2 * Copyright (c) 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 /* $Id: inet_ntop.c,v 1.8 2005-02-09 02:25:46 guy Exp $ */
41 #ifndef lint
42 static const char rcsid[] _U_ =
43 "@(#) $Header: /tcpdump/master/tcpdump/missing/inet_ntop.c,v 1.8 2005-02-09 02:25:46 guy Exp $";
44 #endif
46 #include <tcpdump-stdinc.h>
48 #include <stdio.h>
49 #include <errno.h>
55 #ifndef IN6ADDRSZ
56 #define IN6ADDRSZ 16 /* IPv6 T_AAAA */
57 #endif
59 #ifndef INT16SZ
60 #define INT16SZ 2 /* word size */
61 #endif
63 static const char *
64 inet_ntop_v4 (const void *src, char *dst, size_t size)
66 const char digits[] = "0123456789";
67 int i;
68 struct in_addr *addr = (struct in_addr *)src;
69 u_long a = ntohl(addr->s_addr);
70 const char *orig_dst = dst;
72 if (size < INET_ADDRSTRLEN) {
73 errno = ENOSPC;
74 return NULL;
76 for (i = 0; i < 4; ++i) {
77 int n = (a >> (24 - i * 8)) & 0xFF;
78 int non_zerop = 0;
80 if (non_zerop || n / 100 > 0) {
81 *dst++ = digits[n / 100];
82 n %= 100;
83 non_zerop = 1;
85 if (non_zerop || n / 10 > 0) {
86 *dst++ = digits[n / 10];
87 n %= 10;
88 non_zerop = 1;
90 *dst++ = digits[n];
91 if (i != 3)
92 *dst++ = '.';
94 *dst++ = '\0';
95 return orig_dst;
98 #ifdef INET6
100 * Convert IPv6 binary address into presentation (printable) format.
102 static const char *
103 inet_ntop_v6 (const u_char *src, char *dst, size_t size)
106 * Note that int32_t and int16_t need only be "at least" large enough
107 * to contain a value of the specified size. On some systems, like
108 * Crays, there is no such thing as an integer variable with 16 bits.
109 * Keep this in mind if you think this function should have been coded
110 * to use pointer overlays. All the world's not a VAX.
112 char tmp [INET6_ADDRSTRLEN+1];
113 char *tp;
114 struct {
115 long base;
116 long len;
117 } best, cur;
118 u_long words [IN6ADDRSZ / INT16SZ];
119 int i;
121 /* Preprocess:
122 * Copy the input (bytewise) array into a wordwise array.
123 * Find the longest run of 0x00's in src[] for :: shorthanding.
125 memset (words, 0, sizeof(words));
126 for (i = 0; i < IN6ADDRSZ; i++)
127 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
129 best.base = -1;
130 cur.base = -1;
131 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
133 if (words[i] == 0)
135 if (cur.base == -1)
136 cur.base = i, cur.len = 1;
137 else cur.len++;
139 else if (cur.base != -1)
141 if (best.base == -1 || cur.len > best.len)
142 best = cur;
143 cur.base = -1;
146 if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
147 best = cur;
148 if (best.base != -1 && best.len < 2)
149 best.base = -1;
151 /* Format the result.
153 tp = tmp;
154 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
156 /* Are we inside the best run of 0x00's?
158 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
160 if (i == best.base)
161 *tp++ = ':';
162 continue;
165 /* Are we following an initial run of 0x00s or any real hex?
167 if (i != 0)
168 *tp++ = ':';
170 /* Is this address an encapsulated IPv4?
172 if (i == 6 && best.base == 0 &&
173 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
175 if (!inet_ntop_v4(src+12, tp, sizeof(tmp) - (tp - tmp)))
177 errno = ENOSPC;
178 return (NULL);
180 tp += strlen(tp);
181 break;
183 tp += sprintf (tp, "%lX", words[i]);
186 /* Was it a trailing run of 0x00's?
188 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
189 *tp++ = ':';
190 *tp++ = '\0';
192 /* Check for overflow, copy, and we're done.
194 if ((size_t)(tp - tmp) > size)
196 errno = ENOSPC;
197 return (NULL);
199 return strcpy (dst, tmp);
200 return (NULL);
202 #endif /* INET6 */
205 const char *
206 inet_ntop(int af, const void *src, char *dst, size_t size)
208 switch (af) {
209 case AF_INET :
210 return inet_ntop_v4 (src, dst, size);
211 #ifdef INET6
212 case AF_INET6:
213 return inet_ntop_v6 ((const u_char*)src, dst, size);
214 #endif
215 default :
216 errno = EAFNOSUPPORT;
217 return NULL;