addrtostr.c revision 1.4 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1999 Kungliga Tekniska Hgskolan
3 1.1 christos * (Royal Institute of Technology, Stockholm, Sweden).
4 1.1 christos * All rights reserved.
5 1.1 christos *
6 1.1 christos * Redistribution and use in source and binary forms, with or without
7 1.1 christos * modification, are permitted provided that the following conditions
8 1.1 christos * are met:
9 1.1 christos *
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos *
13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer in the
15 1.1 christos * documentation and/or other materials provided with the distribution.
16 1.1 christos *
17 1.1 christos * 3. All advertising materials mentioning features or use of this software
18 1.1 christos * must display the following acknowledgement:
19 1.1 christos * This product includes software developed by the Kungliga Tekniska
20 1.1 christos * Hgskolan and its contributors.
21 1.1 christos *
22 1.1 christos * 4. Neither the name of the Institute nor the names of its contributors
23 1.1 christos * may be used to endorse or promote products derived from this software
24 1.1 christos * without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 christos * SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.2 christos #include <sys/cdefs.h>
40 1.2 christos #ifndef lint
41 1.4 christos __RCSID("$NetBSD: addrtostr.c,v 1.4 2017/09/08 14:01:12 christos Exp $");
42 1.2 christos #endif
43 1.2 christos
44 1.1 christos #ifdef HAVE_CONFIG_H
45 1.1 christos #include "config.h"
46 1.1 christos #endif
47 1.1 christos
48 1.1 christos #include <netdissect-stdinc.h>
49 1.1 christos #include "addrtostr.h"
50 1.1 christos
51 1.1 christos #include <stdio.h>
52 1.1 christos #include <string.h>
53 1.1 christos
54 1.1 christos /*
55 1.1 christos *
56 1.1 christos */
57 1.1 christos
58 1.1 christos #ifndef IN6ADDRSZ
59 1.1 christos #define IN6ADDRSZ 16 /* IPv6 T_AAAA */
60 1.1 christos #endif
61 1.1 christos
62 1.1 christos #ifndef INT16SZ
63 1.1 christos #define INT16SZ 2 /* word size */
64 1.1 christos #endif
65 1.1 christos
66 1.1 christos const char *
67 1.1 christos addrtostr (const void *src, char *dst, size_t size)
68 1.1 christos {
69 1.1 christos const u_char *srcaddr = (const u_char *)src;
70 1.1 christos const char digits[] = "0123456789";
71 1.1 christos int i;
72 1.1 christos const char *orig_dst = dst;
73 1.1 christos
74 1.1 christos if (size < INET_ADDRSTRLEN) {
75 1.1 christos errno = ENOSPC;
76 1.1 christos return NULL;
77 1.1 christos }
78 1.1 christos for (i = 0; i < 4; ++i) {
79 1.1 christos int n = *srcaddr++;
80 1.1 christos int non_zerop = 0;
81 1.1 christos
82 1.1 christos if (non_zerop || n / 100 > 0) {
83 1.1 christos *dst++ = digits[n / 100];
84 1.1 christos n %= 100;
85 1.1 christos non_zerop = 1;
86 1.1 christos }
87 1.1 christos if (non_zerop || n / 10 > 0) {
88 1.1 christos *dst++ = digits[n / 10];
89 1.1 christos n %= 10;
90 1.1 christos non_zerop = 1;
91 1.1 christos }
92 1.1 christos *dst++ = digits[n];
93 1.1 christos if (i != 3)
94 1.1 christos *dst++ = '.';
95 1.1 christos }
96 1.1 christos *dst++ = '\0';
97 1.1 christos return orig_dst;
98 1.1 christos }
99 1.1 christos
100 1.1 christos /*
101 1.1 christos * Convert IPv6 binary address into presentation (printable) format.
102 1.1 christos */
103 1.1 christos const char *
104 1.1 christos addrtostr6 (const void *src, char *dst, size_t size)
105 1.1 christos {
106 1.1 christos /*
107 1.1 christos * Note that int32_t and int16_t need only be "at least" large enough
108 1.1 christos * to contain a value of the specified size. On some systems, like
109 1.1 christos * Crays, there is no such thing as an integer variable with 16 bits.
110 1.1 christos * Keep this in mind if you think this function should have been coded
111 1.1 christos * to use pointer overlays. All the world's not a VAX.
112 1.1 christos */
113 1.1 christos const u_char *srcaddr = (const u_char *)src;
114 1.3 spz char *dp;
115 1.3 spz size_t space_left, added_space;
116 1.3 spz int snprintfed;
117 1.1 christos struct {
118 1.4 christos int base;
119 1.4 christos int len;
120 1.1 christos } best, cur;
121 1.4 christos uint16_t words [IN6ADDRSZ / INT16SZ];
122 1.4 christos int i;
123 1.1 christos
124 1.1 christos /* Preprocess:
125 1.1 christos * Copy the input (bytewise) array into a wordwise array.
126 1.1 christos * Find the longest run of 0x00's in src[] for :: shorthanding.
127 1.1 christos */
128 1.4 christos for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
129 1.4 christos words[i] = (srcaddr[2*i] << 8) | srcaddr[2*i + 1];
130 1.1 christos
131 1.1 christos best.len = 0;
132 1.1 christos best.base = -1;
133 1.1 christos cur.len = 0;
134 1.1 christos cur.base = -1;
135 1.4 christos for (i = 0; i < (int)(IN6ADDRSZ / INT16SZ); i++)
136 1.1 christos {
137 1.1 christos if (words[i] == 0)
138 1.1 christos {
139 1.1 christos if (cur.base == -1)
140 1.1 christos cur.base = i, cur.len = 1;
141 1.1 christos else cur.len++;
142 1.1 christos }
143 1.1 christos else if (cur.base != -1)
144 1.1 christos {
145 1.1 christos if (best.base == -1 || cur.len > best.len)
146 1.1 christos best = cur;
147 1.1 christos cur.base = -1;
148 1.1 christos }
149 1.1 christos }
150 1.1 christos if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
151 1.1 christos best = cur;
152 1.1 christos if (best.base != -1 && best.len < 2)
153 1.1 christos best.base = -1;
154 1.1 christos
155 1.1 christos /* Format the result.
156 1.1 christos */
157 1.3 spz dp = dst;
158 1.3 spz space_left = size;
159 1.3 spz #define APPEND_CHAR(c) \
160 1.3 spz { \
161 1.3 spz if (space_left == 0) { \
162 1.3 spz errno = ENOSPC; \
163 1.3 spz return (NULL); \
164 1.3 spz } \
165 1.3 spz *dp++ = c; \
166 1.3 spz space_left--; \
167 1.3 spz }
168 1.4 christos for (i = 0; i < (int)(IN6ADDRSZ / INT16SZ); i++)
169 1.1 christos {
170 1.1 christos /* Are we inside the best run of 0x00's?
171 1.1 christos */
172 1.1 christos if (best.base != -1 && i >= best.base && i < (best.base + best.len))
173 1.1 christos {
174 1.1 christos if (i == best.base)
175 1.3 spz APPEND_CHAR(':');
176 1.1 christos continue;
177 1.1 christos }
178 1.1 christos
179 1.1 christos /* Are we following an initial run of 0x00s or any real hex?
180 1.1 christos */
181 1.1 christos if (i != 0)
182 1.3 spz APPEND_CHAR(':');
183 1.1 christos
184 1.1 christos /* Is this address an encapsulated IPv4?
185 1.1 christos */
186 1.1 christos if (i == 6 && best.base == 0 &&
187 1.1 christos (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
188 1.1 christos {
189 1.3 spz if (!addrtostr(srcaddr+12, dp, space_left))
190 1.1 christos {
191 1.1 christos errno = ENOSPC;
192 1.1 christos return (NULL);
193 1.1 christos }
194 1.3 spz added_space = strlen(dp);
195 1.3 spz dp += added_space;
196 1.3 spz space_left -= added_space;
197 1.1 christos break;
198 1.1 christos }
199 1.4 christos snprintfed = snprintf (dp, space_left, "%x", words[i]);
200 1.3 spz if (snprintfed < 0)
201 1.3 spz return (NULL);
202 1.3 spz if ((size_t) snprintfed >= space_left)
203 1.3 spz {
204 1.3 spz errno = ENOSPC;
205 1.3 spz return (NULL);
206 1.3 spz }
207 1.3 spz dp += snprintfed;
208 1.3 spz space_left -= snprintfed;
209 1.1 christos }
210 1.1 christos
211 1.1 christos /* Was it a trailing run of 0x00's?
212 1.1 christos */
213 1.1 christos if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
214 1.3 spz APPEND_CHAR(':');
215 1.3 spz APPEND_CHAR('\0');
216 1.1 christos
217 1.3 spz return (dst);
218 1.1 christos }
219