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