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