addrtostr.c revision 1.2 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.2 christos __RCSID("$NetBSD: addrtostr.c,v 1.2 2017/01/24 23:29:13 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.1 christos char tmp [INET6_ADDRSTRLEN+1];
115 1.1 christos char *tp;
116 1.1 christos struct {
117 1.1 christos long base;
118 1.1 christos long len;
119 1.1 christos } best, cur;
120 1.1 christos u_long words [IN6ADDRSZ / INT16SZ];
121 1.1 christos u_int i;
122 1.1 christos
123 1.1 christos /* Preprocess:
124 1.1 christos * Copy the input (bytewise) array into a wordwise array.
125 1.1 christos * Find the longest run of 0x00's in src[] for :: shorthanding.
126 1.1 christos */
127 1.1 christos memset (words, 0, sizeof(words));
128 1.1 christos for (i = 0; i < IN6ADDRSZ; i++)
129 1.1 christos words[i/2] |= (srcaddr[i] << ((1 - (i % 2)) << 3));
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.1 christos for (i = 0; i < (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.1 christos tp = tmp;
158 1.1 christos for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
159 1.1 christos {
160 1.1 christos /* Are we inside the best run of 0x00's?
161 1.1 christos */
162 1.1 christos if (best.base != -1 && i >= best.base && i < (best.base + best.len))
163 1.1 christos {
164 1.1 christos if (i == best.base)
165 1.1 christos *tp++ = ':';
166 1.1 christos continue;
167 1.1 christos }
168 1.1 christos
169 1.1 christos /* Are we following an initial run of 0x00s or any real hex?
170 1.1 christos */
171 1.1 christos if (i != 0)
172 1.1 christos *tp++ = ':';
173 1.1 christos
174 1.1 christos /* Is this address an encapsulated IPv4?
175 1.1 christos */
176 1.1 christos if (i == 6 && best.base == 0 &&
177 1.1 christos (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
178 1.1 christos {
179 1.1 christos if (!addrtostr(srcaddr+12, tp, sizeof(tmp) - (tp - tmp)))
180 1.1 christos {
181 1.1 christos errno = ENOSPC;
182 1.1 christos return (NULL);
183 1.1 christos }
184 1.1 christos tp += strlen(tp);
185 1.1 christos break;
186 1.1 christos }
187 1.1 christos tp += sprintf (tp, "%lx", words[i]);
188 1.1 christos }
189 1.1 christos
190 1.1 christos /* Was it a trailing run of 0x00's?
191 1.1 christos */
192 1.1 christos if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
193 1.1 christos *tp++ = ':';
194 1.1 christos *tp++ = '\0';
195 1.1 christos
196 1.1 christos /* Check for overflow, copy, and we're done.
197 1.1 christos */
198 1.1 christos if ((size_t)(tp - tmp) > size)
199 1.1 christos {
200 1.1 christos errno = ENOSPC;
201 1.1 christos return (NULL);
202 1.1 christos }
203 1.1 christos return strcpy (dst, tmp);
204 1.1 christos }
205