xref: /netbsd/external/bsd/ntp/dist/lib/isc/inet_ntop.c (revision 6550d01e)
1 /*	$NetBSD: inet_ntop.c,v 1.2 2010/12/04 23:08:33 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007  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.19 2007/06/19 23:47:17 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 #include "ntp_sprintf.h"	/* NTP local change, helps SunOS 4 */
37 
38 #define NS_INT16SZ	 2
39 #define NS_IN6ADDRSZ	16
40 
41 /*
42  * WARNING: Don't even consider trying to compile this on a system where
43  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
44  */
45 
46 #ifdef notdef
47 static const char *inet_ntop4(const unsigned char *src, char *dst,
48 			      size_t size);
49 
50 #ifdef AF_INET6
51 static const char *inet_ntop6(const unsigned char *src, char *dst,
52 			      size_t size);
53 #endif
54 #endif
55 
56 #ifdef notdef
57 /*! char *
58  * isc_net_ntop(af, src, dst, size)
59  *	convert a network format address to presentation format.
60  * \return
61  *	pointer to presentation format address (`dst'), or NULL (see errno).
62  * \author
63  *	Paul Vixie, 1996.
64  */
65 const char *
66 isc_net_ntop(int af, const void *src, char *dst, size_t size)
67 {
68 	switch (af) {
69 	case AF_INET:
70 		return (inet_ntop4(src, dst, size));
71 #ifdef AF_INET6
72 	case AF_INET6:
73 		return (inet_ntop6(src, dst, size));
74 #endif
75 	default:
76 		errno = EAFNOSUPPORT;
77 		return (NULL);
78 	}
79 	/* NOTREACHED */
80 }
81 
82 /*! const char *
83  * inet_ntop4(src, dst, size)
84  *	format an IPv4 address
85  * \return
86  *	`dst' (as a const)
87  * \note
88  *	(1) uses no statics
89  * \note
90  *	(2) takes a unsigned char* not an in_addr as input
91  * \author
92  *	Paul Vixie, 1996.
93  */
94 static const char *
95 inet_ntop4(const unsigned char *src, char *dst, size_t size)
96 {
97 	static const char *fmt = "%u.%u.%u.%u";
98 	char tmp[sizeof("255.255.255.255")];
99 
100 	/* NTP local change to use SNPRINTF() macro for SunOS4 compat */
101 	if (SNPRINTF((tmp, sizeof(tmp), fmt, src[0], src[1], src[2],
102 		      src[3])) >= size)
103 	{
104 		errno = ENOSPC;
105 		return (NULL);
106 	}
107 	strcpy(dst, tmp);
108 
109 	return (dst);
110 }
111 
112 /*! const char *
113  * isc_inet_ntop6(src, dst, size)
114  *	convert IPv6 binary address into presentation (printable) format
115  * \author
116  *	Paul Vixie, 1996.
117  */
118 #ifdef AF_INET6
119 static const char *
120 inet_ntop6(const unsigned char *src, char *dst, size_t size)
121 {
122 	/*
123 	 * Note that int32_t and int16_t need only be "at least" large enough
124 	 * to contain a value of the specified size.  On some systems, like
125 	 * Crays, there is no such thing as an integer variable with 16 bits.
126 	 * Keep this in mind if you think this function should have been coded
127 	 * to use pointer overlays.  All the world's not a VAX.
128 	 */
129 	char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
130 	struct { int base, len; } best, cur;
131 	unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
132 	int i;
133 
134 	/*
135 	 * Preprocess:
136 	 *	Copy the input (bytewise) array into a wordwise array.
137 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
138 	 */
139 	memset(words, '\0', sizeof(words));
140 	for (i = 0; i < NS_IN6ADDRSZ; i++)
141 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
142 	best.base = -1;
143 	cur.base = -1;
144 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
145 		if (words[i] == 0) {
146 			if (cur.base == -1)
147 				cur.base = i, cur.len = 1;
148 			else
149 				cur.len++;
150 		} else {
151 			if (cur.base != -1) {
152 				if (best.base == -1 || cur.len > best.len)
153 					best = cur;
154 				cur.base = -1;
155 			}
156 		}
157 	}
158 	if (cur.base != -1) {
159 		if (best.base == -1 || cur.len > best.len)
160 			best = cur;
161 	}
162 	if (best.base != -1 && best.len < 2)
163 		best.base = -1;
164 
165 	/*
166 	 * Format the result.
167 	 */
168 	tp = tmp;
169 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
170 		/* Are we inside the best run of 0x00's? */
171 		if (best.base != -1 && i >= best.base &&
172 		    i < (best.base + best.len)) {
173 			if (i == best.base)
174 				*tp++ = ':';
175 			continue;
176 		}
177 		/* Are we following an initial run of 0x00s or any real hex? */
178 		if (i != 0)
179 			*tp++ = ':';
180 		/* Is this address an encapsulated IPv4? */
181 		if (i == 6 && best.base == 0 &&
182 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
183 			if (!inet_ntop4(src+12, tp,
184 					sizeof(tmp) - (tp - tmp)))
185 				return (NULL);
186 			tp += strlen(tp);
187 			break;
188 		}
189 		tp += SPRINTF((tp, "%x", words[i]));	/* NTP local change */
190 	}
191 	/* Was it a trailing run of 0x00's? */
192 	if (best.base != -1 && (best.base + best.len) ==
193 	    (NS_IN6ADDRSZ / NS_INT16SZ))
194 		*tp++ = ':';
195 	*tp++ = '\0';
196 
197 	/*
198 	 * Check for overflow, copy, and we're done.
199 	 */
200 	if ((size_t)(tp - tmp) > size) {
201 		errno = ENOSPC;
202 		return (NULL);
203 	}
204 	strcpy(dst, tmp);
205 	return (dst);
206 }
207 #endif /* AF_INET6 */
208 #endif
209