xref: /netbsd-src/external/bsd/tcpdump/dist/addrtostr.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*
2  * Copyright (c) 1999 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
21  *
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.
25  *
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.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: addrtostr.c,v 1.4 2017/09/08 14:01:12 christos Exp $");
42 #endif
43 
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 
48 #include <netdissect-stdinc.h>
49 #include "addrtostr.h"
50 
51 #include <stdio.h>
52 #include <string.h>
53 
54 /*
55  *
56  */
57 
58 #ifndef IN6ADDRSZ
59 #define IN6ADDRSZ   16   /* IPv6 T_AAAA */
60 #endif
61 
62 #ifndef INT16SZ
63 #define INT16SZ     2    /* word size */
64 #endif
65 
66 const char *
67 addrtostr (const void *src, char *dst, size_t size)
68 {
69     const u_char *srcaddr = (const u_char *)src;
70     const char digits[] = "0123456789";
71     int i;
72     const char *orig_dst = dst;
73 
74     if (size < INET_ADDRSTRLEN) {
75 	errno = ENOSPC;
76 	return NULL;
77     }
78     for (i = 0; i < 4; ++i) {
79     	int n = *srcaddr++;
80 	int non_zerop = 0;
81 
82 	if (non_zerop || n / 100 > 0) {
83 	    *dst++ = digits[n / 100];
84 	    n %= 100;
85 	    non_zerop = 1;
86 	}
87 	if (non_zerop || n / 10 > 0) {
88 	    *dst++ = digits[n / 10];
89 	    n %= 10;
90 	    non_zerop = 1;
91 	}
92 	*dst++ = digits[n];
93 	if (i != 3)
94 	    *dst++ = '.';
95     }
96     *dst++ = '\0';
97     return orig_dst;
98 }
99 
100 /*
101  * Convert IPv6 binary address into presentation (printable) format.
102  */
103 const char *
104 addrtostr6 (const void *src, char *dst, size_t size)
105 {
106   /*
107    * Note that int32_t and int16_t need only be "at least" large enough
108    * to contain a value of the specified size.  On some systems, like
109    * Crays, there is no such thing as an integer variable with 16 bits.
110    * Keep this in mind if you think this function should have been coded
111    * to use pointer overlays.  All the world's not a VAX.
112    */
113   const u_char *srcaddr = (const u_char *)src;
114   char *dp;
115   size_t space_left, added_space;
116   int snprintfed;
117   struct {
118     int base;
119     int len;
120   } best, cur;
121   uint16_t words [IN6ADDRSZ / INT16SZ];
122   int  i;
123 
124   /* Preprocess:
125    *  Copy the input (bytewise) array into a wordwise array.
126    *  Find the longest run of 0x00's in src[] for :: shorthanding.
127    */
128   for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
129       words[i] = (srcaddr[2*i] << 8) | srcaddr[2*i + 1];
130 
131   best.len = 0;
132   best.base = -1;
133   cur.len = 0;
134   cur.base  = -1;
135   for (i = 0; i < (int)(IN6ADDRSZ / INT16SZ); i++)
136   {
137     if (words[i] == 0)
138     {
139       if (cur.base == -1)
140            cur.base = i, cur.len = 1;
141       else cur.len++;
142     }
143     else if (cur.base != -1)
144     {
145       if (best.base == -1 || cur.len > best.len)
146          best = cur;
147       cur.base = -1;
148     }
149   }
150   if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
151      best = cur;
152   if (best.base != -1 && best.len < 2)
153      best.base = -1;
154 
155   /* Format the result.
156    */
157   dp = dst;
158   space_left = size;
159 #define APPEND_CHAR(c) \
160     { \
161         if (space_left == 0) { \
162             errno = ENOSPC; \
163             return (NULL); \
164         } \
165         *dp++ = c; \
166         space_left--; \
167     }
168   for (i = 0; i < (int)(IN6ADDRSZ / INT16SZ); i++)
169   {
170     /* Are we inside the best run of 0x00's?
171      */
172     if (best.base != -1 && i >= best.base && i < (best.base + best.len))
173     {
174       if (i == best.base)
175       	 APPEND_CHAR(':');
176       continue;
177     }
178 
179     /* Are we following an initial run of 0x00s or any real hex?
180      */
181     if (i != 0)
182        APPEND_CHAR(':');
183 
184     /* Is this address an encapsulated IPv4?
185      */
186     if (i == 6 && best.base == 0 &&
187         (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
188     {
189       if (!addrtostr(srcaddr+12, dp, space_left))
190       {
191         errno = ENOSPC;
192         return (NULL);
193       }
194       added_space = strlen(dp);
195       dp += added_space;
196       space_left -= added_space;
197       break;
198     }
199     snprintfed = snprintf (dp, space_left, "%x", words[i]);
200     if (snprintfed < 0)
201         return (NULL);
202     if ((size_t) snprintfed >= space_left)
203     {
204         errno = ENOSPC;
205         return (NULL);
206     }
207     dp += snprintfed;
208     space_left -= snprintfed;
209   }
210 
211   /* Was it a trailing run of 0x00's?
212    */
213   if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
214      APPEND_CHAR(':');
215   APPEND_CHAR('\0');
216 
217   return (dst);
218 }
219