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