xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/inet_ntop.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1 /*	$NetBSD: inet_ntop.c,v 1.2 2024/08/18 20:47:14 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1996-2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*! \file */
21 
22 #if defined(LIBC_SCCS) && !defined(lint)
23 static char rcsid[] =
24 	"Id: inet_ntop.c,v 1.21 2009/07/17 23:47:41 tbox Exp ";
25 #endif /* LIBC_SCCS and not lint */
26 
27 #include <config.h>
28 
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include <isc/net.h>
34 #include <isc/print.h>
35 
36 #define NS_INT16SZ	 2
37 #define NS_IN6ADDRSZ	16
38 
39 /*
40  * WARNING: Don't even consider trying to compile this on a system where
41  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
42  */
43 
44 static const char *inet_ntop4(const unsigned char *src, char *dst,
45 			      size_t size);
46 
47 #ifdef AF_INET6
48 static const char *inet_ntop6(const unsigned char *src, char *dst,
49 			      size_t size);
50 #endif
51 const char *isc_net_ntop(int af, const void *src, char *dst, size_t size);
52 
53 /*! char *
54  * isc_net_ntop(af, src, dst, size)
55  *	convert a network format address to presentation format.
56  * \return
57  *	pointer to presentation format address (`dst'), or NULL (see errno).
58  * \author
59  *	Paul Vixie, 1996.
60  */
61 const char *
62 isc_net_ntop(int af, const void *src, char *dst, size_t size)
63 {
64 	switch (af) {
65 	case AF_INET:
66 		return (inet_ntop4(src, dst, size));
67 #ifdef AF_INET6
68 	case AF_INET6:
69 		return (inet_ntop6(src, dst, size));
70 #endif
71 	default:
72 		errno = EAFNOSUPPORT;
73 		return (NULL);
74 	}
75 	/* NOTREACHED */
76 }
77 
78 /*! const char *
79  * inet_ntop4(src, dst, size)
80  *	format an IPv4 address
81  * \return
82  *	`dst' (as a const)
83  * \note
84  *	(1) uses no statics
85  * \note
86  *	(2) takes a unsigned char* not an in_addr as input
87  * \author
88  *	Paul Vixie, 1996.
89  */
90 static const char *
91 inet_ntop4(const unsigned char *src, char *dst, size_t size)
92 {
93 #define	fmt "%u.%u.%u.%u"
94 	char tmp[sizeof("255.255.255.255")];
95 	int len;
96 
97 	len = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
98 	if (len < 0 || (size_t)len >= size)
99 	{
100 		errno = ENOSPC;
101 		return (NULL);
102 	}
103 	memcpy(dst, tmp, 1 + len);
104 
105 	return (dst);
106 }
107 
108 /*! const char *
109  * isc_inet_ntop6(src, dst, size)
110  *	convert IPv6 binary address into presentation (printable) format
111  * \author
112  *	Paul Vixie, 1996.
113  */
114 #ifdef AF_INET6
115 static const char *
116 inet_ntop6(const unsigned char *src, char *dst, size_t size)
117 {
118 	/*
119 	 * Note that int32_t and int16_t need only be "at least" large enough
120 	 * to contain a value of the specified size.  On some systems, like
121 	 * Crays, there is no such thing as an integer variable with 16 bits.
122 	 * Keep this in mind if you think this function should have been coded
123 	 * to use pointer overlays.  All the world's not a VAX.
124 	 */
125 	char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
126 	struct { int base, len; } best, cur;
127 	unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
128 	int i;
129 
130 	/*
131 	 * Preprocess:
132 	 *	Copy the input (bytewise) array into a wordwise array.
133 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
134 	 */
135 	memset(words, '\0', sizeof(words));
136 	for (i = 0; i < NS_IN6ADDRSZ; i++)
137 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
138 	best.base = -1;
139 	cur.base = -1;
140 	best.len = cur.len = 0;
141 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
142 		if (words[i] == 0) {
143 			if (cur.base == -1)
144 				cur.base = i, cur.len = 1;
145 			else
146 				cur.len++;
147 		} else {
148 			if (cur.base != -1) {
149 				if (best.base == -1 || cur.len > best.len)
150 					best = cur;
151 				cur.base = -1;
152 			}
153 		}
154 	}
155 	if (cur.base != -1) {
156 		if (best.base == -1 || cur.len > best.len)
157 			best = cur;
158 	}
159 	if (best.base != -1 && best.len < 2)
160 		best.base = -1;
161 
162 	/*
163 	 * Format the result.
164 	 */
165 	tp = tmp;
166 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
167 		/* Are we inside the best run of 0x00's? */
168 		if (best.base != -1 && i >= best.base &&
169 		    i < (best.base + best.len)) {
170 			if (i == best.base)
171 				*tp++ = ':';
172 			continue;
173 		}
174 		/* Are we following an initial run of 0x00s or any real hex? */
175 		if (i != 0)
176 			*tp++ = ':';
177 		/* Is this address an encapsulated IPv4? */
178 		if (i == 6 && best.base == 0 && (best.len == 6 ||
179 		    (best.len == 7 && words[7] != 0x0001) ||
180 		    (best.len == 5 && words[5] == 0xffff))) {
181 			if (!inet_ntop4(src+12, tp,
182 					sizeof(tmp) - (tp - tmp)))
183 				return (NULL);
184 			tp += strlen(tp);
185 			break;
186 		}
187 		tp += snprintf(tp, sizeof(tmp) - (tp - tmp), "%x", words[i]);
188 	}
189 	/* Was it a trailing run of 0x00's? */
190 	if (best.base != -1 && (best.base + best.len) ==
191 	    (NS_IN6ADDRSZ / NS_INT16SZ))
192 		*tp++ = ':';
193 	*tp++ = '\0';
194 
195 	/*
196 	 * Check for overflow, copy, and we're done.
197 	 */
198 	if ((size_t)(tp - tmp) > size) {
199 		errno = ENOSPC;
200 		return (NULL);
201 	}
202 	memcpy(dst, tmp, (size_t)(tp - tmp));
203 	return (dst);
204 }
205 #endif /* AF_INET6 */
206