1 /*	$OpenBSD: inet_ntop.c,v 1.9 2014/02/05 14:20:43 millert Exp $	*/
2 
3 /*
4  * SPDX-License-Identifier: ISC
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 
22 #include <config.h>
23 
24 #if !defined(HAVE_INET_NTOP)
25 
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <arpa/nameser.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <stdio.h>
34 
35 #include "sudo_compat.h"
36 
37 #ifndef EAFNOSUPPORT
38 # define EAFNOSUPPORT EINVAL
39 #endif
40 
41 #ifndef NS_IN6ADDRSZ
42 # ifdef IN6ADDRSZ
43 #  define NS_IN6ADDRSZ IN6ADDRSZ
44 # else
45 #  define NS_IN6ADDRSZ 16
46 # endif
47 #endif
48 #ifndef NS_INT16SZ
49 # ifdef INT16SZ
50 #  define NS_INT16SZ INT16SZ
51 # else
52 #  define NS_INT16SZ 2
53 # endif
54 #endif
55 #ifndef INET6_ADDRSTRLEN
56 # define INET6_ADDRSTRLEN 46
57 #endif
58 
59 /*
60  * WARNING: Don't even consider trying to compile this on a system where
61  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
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 unsigned char* not an in_addr as input
72  * author:
73  *	Paul Vixie, 1996.
74  */
75 static const char *
inet_ntop4(const unsigned char * src,char * dst,socklen_t size)76 inet_ntop4(const unsigned char *src, char *dst, socklen_t size)
77 {
78 	const char fmt[] = "%u.%u.%u.%u";
79 	int len;
80 
81 	len = snprintf(dst, size, fmt, src[0], src[1], src[2], src[3]);
82 	if (len < 0 || len >= size) {
83 		errno = ENOSPC;
84 		return (NULL);
85 	}
86 	return (dst);
87 }
88 
89 #ifdef HAVE_STRUCT_IN6_ADDR
90 /* const char *
91  * inet_ntop6(src, dst, size)
92  *	convert IPv6 binary address into presentation (printable) format
93  * author:
94  *	Paul Vixie, 1996.
95  */
96 static const char *
inet_ntop6(const unsigned char * src,char * dst,socklen_t size)97 inet_ntop6(const unsigned char *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 *cp, *ep;
107 	struct { int base, len; } best, cur;
108 	unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
109 	int i;
110 	int advance;
111 
112 	/*
113 	 * Preprocess:
114 	 *	Copy the input (bytewise) array into a wordwise array.
115 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
116 	 */
117 	memset(words, 0, sizeof(words));
118 	for (i = 0; i < NS_IN6ADDRSZ; i++)
119 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
120 	best.base = -1;
121 	best.len = 0;
122 	cur.base = -1;
123 	cur.len = 0;
124 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
125 		if (words[i] == 0) {
126 			if (cur.base == -1)
127 				cur.base = i, cur.len = 1;
128 			else
129 				cur.len++;
130 		} else {
131 			if (cur.base != -1) {
132 				if (best.base == -1 || cur.len > best.len)
133 					best = cur;
134 				cur.base = -1;
135 			}
136 		}
137 	}
138 	if (cur.base != -1) {
139 		if (best.base == -1 || cur.len > best.len)
140 			best = cur;
141 	}
142 	if (best.base != -1 && best.len < 2)
143 		best.base = -1;
144 
145 	/*
146 	 * Format the result.
147 	 */
148 	cp = dst;
149 	ep = dst + size;
150 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ) && cp < ep; i++) {
151 		/* Are we inside the best run of 0x00's? */
152 		if (best.base != -1 && i >= best.base &&
153 		    i < (best.base + best.len)) {
154 			if (i == best.base) {
155 				if (cp + 1 >= ep) {
156 					errno = ENOSPC;
157 					return (NULL);
158 				}
159 				*cp++ = ':';
160 			}
161 			continue;
162 		}
163 		/* Are we following an initial run of 0x00s or any real hex? */
164 		if (i != 0) {
165 			if (cp + 1 >= ep) {
166 				errno = ENOSPC;
167 				return (NULL);
168 			}
169 			*cp++ = ':';
170 		}
171 		/* Is this address an encapsulated IPv4? */
172 		if (i == 6 && best.base == 0 &&
173 		    (best.len == 6 ||
174 		    (best.len == 7 && words[7] != 0x0001) ||
175 		    (best.len == 5 && words[5] == 0xffff))) {
176 			if (!inet_ntop4(src + 12, cp, (socklen_t)(ep - cp)))
177 				return (NULL);
178 			cp += strlen(cp);
179 			break;
180 		}
181 		advance = snprintf(cp, (size_t)(ep - cp), "%x", words[i]);
182 		if (advance <= 0 || advance >= ep - cp) {
183 			errno = ENOSPC;
184 			return (NULL);
185 		}
186 		cp += advance;
187 	}
188 	/* Was it a trailing run of 0x00's? */
189 	if (best.base != -1 &&
190 	    (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ)) {
191 		if (cp + 1 >= ep) {
192 			errno = ENOSPC;
193 			return (NULL);
194 		}
195 		*cp++ = ':';
196 	}
197 	if (cp + 1 >= ep) {
198 		errno = ENOSPC;
199 		return (NULL);
200 	}
201 	*cp++ = '\0';
202 
203 	return (dst);
204 }
205 #endif /* HAVE_STRUCT_IN6_ADDR */
206 
207 /* const char *
208  * inet_ntop(af, src, dst, size)
209  *	convert a network format address to presentation format.
210  * return:
211  *	pointer to presentation format address (`dst'), or NULL (see errno).
212  * author:
213  *	Paul Vixie, 1996.
214  */
215 const char *
sudo_inet_ntop(int af,const void * src,char * dst,socklen_t size)216 sudo_inet_ntop(int af, const void *src, char *dst, socklen_t size)
217 {
218 	switch (af) {
219 	case AF_INET:
220 		return (inet_ntop4(src, dst, size));
221 #ifdef HAVE_STRUCT_IN6_ADDR
222 	case AF_INET6:
223 		return (inet_ntop6(src, dst, size));
224 #endif
225 	default:
226 		errno = EAFNOSUPPORT;
227 		return (NULL);
228 	}
229 	/* NOTREACHED */
230 }
231 
232 #endif /* !HAVE_INET_NTOP */
233