xref: /netbsd-src/external/bsd/tcpdump/dist/addrtostr.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
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.3 2017/02/05 04:05:05 spz 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     long base;
119     long len;
120   } best, cur;
121   u_long words [IN6ADDRSZ / INT16SZ];
122   u_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   memset (words, 0, sizeof(words));
129   for (i = 0; i < IN6ADDRSZ; i++)
130       words[i/2] |= (srcaddr[i] << ((1 - (i % 2)) << 3));
131 
132   best.len = 0;
133   best.base = -1;
134   cur.len = 0;
135   cur.base  = -1;
136   for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
137   {
138     if (words[i] == 0)
139     {
140       if (cur.base == -1)
141            cur.base = i, cur.len = 1;
142       else cur.len++;
143     }
144     else if (cur.base != -1)
145     {
146       if (best.base == -1 || cur.len > best.len)
147          best = cur;
148       cur.base = -1;
149     }
150   }
151   if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
152      best = cur;
153   if (best.base != -1 && best.len < 2)
154      best.base = -1;
155 
156   /* Format the result.
157    */
158   dp = dst;
159   space_left = size;
160 #define APPEND_CHAR(c) \
161     { \
162         if (space_left == 0) { \
163             errno = ENOSPC; \
164             return (NULL); \
165         } \
166         *dp++ = c; \
167         space_left--; \
168     }
169   for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
170   {
171     /* Are we inside the best run of 0x00's?
172      */
173     if (best.base != -1 && i >= best.base && i < (best.base + best.len))
174     {
175       if (i == best.base)
176       	 APPEND_CHAR(':');
177       continue;
178     }
179 
180     /* Are we following an initial run of 0x00s or any real hex?
181      */
182     if (i != 0)
183        APPEND_CHAR(':');
184 
185     /* Is this address an encapsulated IPv4?
186      */
187     if (i == 6 && best.base == 0 &&
188         (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
189     {
190       if (!addrtostr(srcaddr+12, dp, space_left))
191       {
192         errno = ENOSPC;
193         return (NULL);
194       }
195       added_space = strlen(dp);
196       dp += added_space;
197       space_left -= added_space;
198       break;
199     }
200     snprintfed = snprintf (dp, space_left, "%lx", words[i]);
201     if (snprintfed < 0)
202         return (NULL);
203     if ((size_t) snprintfed >= space_left)
204     {
205         errno = ENOSPC;
206         return (NULL);
207     }
208     dp += snprintfed;
209     space_left -= snprintfed;
210   }
211 
212   /* Was it a trailing run of 0x00's?
213    */
214   if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
215      APPEND_CHAR(':');
216   APPEND_CHAR('\0');
217 
218   return (dst);
219 }
220