xref: /netbsd-src/external/bsd/nsd/dist/compat/inet_ntop.c (revision d83a80ee7fb31190352cf1f781441e06ca6a86db)
1 /* From openssh 4.3p2 compat/inet_ntop.c */
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 /* OPENBSD ORIGINAL: lib/libc/net/inet_ntop.c */
19 
20 #include <config.h>
21 
22 #ifndef HAVE_INET_NTOP
23 
24 #include <sys/param.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdio.h>
31 
32 #ifndef IN6ADDRSZ
33 #define IN6ADDRSZ   16   /* IPv6 T_AAAA */
34 #endif
35 
36 #ifndef INT16SZ
37 #define INT16SZ     2    /* for systems without 16-bit ints */
38 #endif
39 
40 /*
41  * WARNING: Don't even consider trying to compile this on a system where
42  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
43  */
44 
45 static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
46 static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
47 
48 /* char *
49  * inet_ntop(af, src, dst, size)
50  *	convert a network format address to presentation format.
51  * return:
52  *	pointer to presentation format address (`dst'), or NULL (see errno).
53  * author:
54  *	Paul Vixie, 1996.
55  */
56 const char *
inet_ntop(int af,const void * src,char * dst,size_t size)57 inet_ntop(int af, const void *src, char *dst, size_t size)
58 {
59 	switch (af) {
60 	case AF_INET:
61 		return (inet_ntop4(src, dst, size));
62 	case AF_INET6:
63 		return (inet_ntop6(src, dst, size));
64 	default:
65 		errno = EAFNOSUPPORT;
66 		return (NULL);
67 	}
68 	/* NOTREACHED */
69 }
70 
71 /* const char *
72  * inet_ntop4(src, dst, size)
73  *	format an IPv4 address, more or less like inet_ntoa()
74  * return:
75  *	`dst' (as a const)
76  * notes:
77  *	(1) uses no statics
78  *	(2) takes a u_char* not an in_addr as input
79  * author:
80  *	Paul Vixie, 1996.
81  */
82 static const char *
inet_ntop4(const u_char * src,char * dst,size_t size)83 inet_ntop4(const u_char *src, char *dst, size_t size)
84 {
85 	static const char fmt[] = "%u.%u.%u.%u";
86 	char tmp[sizeof "255.255.255.255"];
87 	int l;
88 
89 	l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
90 	if (l <= 0 || l >= (int)size) {
91 		errno = ENOSPC;
92 		return (NULL);
93 	}
94 	strlcpy(dst, tmp, size);
95 	return (dst);
96 }
97 
98 /* const char *
99  * inet_ntop6(src, dst, size)
100  *	convert IPv6 binary address into presentation (printable) format
101  * author:
102  *	Paul Vixie, 1996.
103  */
104 static const char *
inet_ntop6(const u_char * src,char * dst,size_t size)105 inet_ntop6(const u_char *src, char *dst, size_t size)
106 {
107 	/*
108 	 * Note that int32_t and int16_t need only be "at least" large enough
109 	 * to contain a value of the specified size.  On some systems, like
110 	 * Crays, there is no such thing as an integer variable with 16 bits.
111 	 * Keep this in mind if you think this function should have been coded
112 	 * to use pointer overlays.  All the world's not a VAX.
113 	 */
114 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
115 	char *tp, *ep;
116 	struct { int base, len; } best, cur;
117 	u_int words[IN6ADDRSZ / INT16SZ];
118 	int i;
119 	int advance;
120 
121 	/*
122 	 * Preprocess:
123 	 *	Copy the input (bytewise) array into a wordwise array.
124 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
125 	 */
126 	memset(words, '\0', sizeof words);
127 	for (i = 0; i < IN6ADDRSZ; i++)
128 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
129 	best.base = -1;
130 	cur.base = -1;
131 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
132 		if (words[i] == 0) {
133 			if (cur.base == -1)
134 				cur.base = i, cur.len = 1;
135 			else
136 				cur.len++;
137 		} else {
138 			if (cur.base != -1) {
139 				if (best.base == -1 || cur.len > best.len)
140 					best = cur;
141 				cur.base = -1;
142 			}
143 		}
144 	}
145 	if (cur.base != -1) {
146 		if (best.base == -1 || cur.len > best.len)
147 			best = cur;
148 	}
149 	if (best.base != -1 && best.len < 2)
150 		best.base = -1;
151 
152 	/*
153 	 * Format the result.
154 	 */
155 	tp = tmp;
156 	ep = tmp + sizeof(tmp);
157 	for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
158 		/* Are we inside the best run of 0x00's? */
159 		if (best.base != -1 && i >= best.base &&
160 		    i < (best.base + best.len)) {
161 			if (i == best.base) {
162 				if (tp + 1 >= ep)
163 					return (NULL);
164 				*tp++ = ':';
165 			}
166 			continue;
167 		}
168 		/* Are we following an initial run of 0x00s or any real hex? */
169 		if (i != 0) {
170 			if (tp + 1 >= ep)
171 				return (NULL);
172 			*tp++ = ':';
173 		}
174 		/* Is this address an encapsulated IPv4? */
175 		if (i == 6 && best.base == 0 &&
176 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
177 			if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
178 				return (NULL);
179 			tp += strlen(tp);
180 			break;
181 		}
182 		advance = snprintf(tp, ep - tp, "%x", words[i]);
183 		if (advance <= 0 || advance >= ep - tp)
184 			return (NULL);
185 		tp += advance;
186 	}
187 	/* Was it a trailing run of 0x00's? */
188 	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
189 		if (tp + 1 >= ep)
190 			return (NULL);
191 		*tp++ = ':';
192 	}
193 	if (tp + 1 >= ep)
194 		return (NULL);
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 	strlcpy(dst, tmp, size);
205 	return (dst);
206 }
207 
208 #endif /* !HAVE_INET_NTOP */
209