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