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