1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file lwinetntop.c */
13 
14 #include <config.h>
15 
16 #include <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19 
20 #include <lwres/net.h>
21 #include "print_p.h"
22 
23 #define NS_INT16SZ	 2
24 #define NS_IN6ADDRSZ	16
25 
26 /*
27  * WARNING: Don't even consider trying to compile this on a system where
28  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
29  */
30 
31 static const char *inet_ntop4(const unsigned char *src, char *dst,
32 			      size_t size);
33 
34 #ifdef AF_INET6
35 static const char *inet_ntop6(const unsigned char *src, char *dst,
36 			      size_t size);
37 #endif
38 
39 /*! char *
40  * lwres_net_ntop(af, src, dst, size)
41  *	convert a network format address to presentation format.
42  * return:
43  *	pointer to presentation format address (`dst'), or NULL (see errno).
44  * author:
45  *	Paul Vixie, 1996.
46  */
47 const char *
lwres_net_ntop(int af,const void * src,char * dst,size_t size)48 lwres_net_ntop(int af, const void *src, char *dst, size_t size) {
49 	switch (af) {
50 	case AF_INET:
51 		return (inet_ntop4(src, dst, size));
52 #ifdef AF_INET6
53 	case AF_INET6:
54 		return (inet_ntop6(src, dst, size));
55 #endif
56 	default:
57 		errno = EAFNOSUPPORT;
58 		return (NULL);
59 	}
60 	/* NOTREACHED */
61 }
62 
63 /*! const char *
64  * inet_ntop4(src, dst, size)
65  *	format an IPv4 address
66  * return:
67  *	`dst' (as a const)
68  * notes:
69  *	(1) uses no statics
70  *	(2) takes a unsigned char* not an in_addr as input
71  * author:
72  *	Paul Vixie, 1996.
73  */
74 static const char *
inet_ntop4(const unsigned char * src,char * dst,size_t size)75 inet_ntop4(const unsigned char *src, char *dst, size_t size) {
76 	static const char fmt[] = "%u.%u.%u.%u";
77 	char tmp[sizeof("255.255.255.255")];
78 	size_t len;
79 
80 	len = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
81 	if (len >= size) {
82 		errno = ENOSPC;
83 		return (NULL);
84 	}
85 	strcpy(dst, tmp);
86 
87 	return (dst);
88 }
89 
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 #ifdef AF_INET6
97 static const char *
inet_ntop6(const unsigned char * src,char * dst,size_t size)98 inet_ntop6(const unsigned char *src, char *dst, size_t size) {
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 	unsigned int words[NS_IN6ADDRSZ / NS_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 	memset(words, '\0', sizeof(words));
117 	for (i = 0; i < NS_IN6ADDRSZ; i++)
118 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
119 	best.base = -1;
120 	best.len = 0;
121 	cur.base = -1;
122 	cur.len = 0;
123 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
124 		if (words[i] == 0) {
125 			if (cur.base == -1)
126 				cur.base = i, cur.len = 1;
127 			else
128 				cur.len++;
129 		} else {
130 			if (cur.base != -1) {
131 				if (best.base == -1 || cur.len > best.len)
132 					best = cur;
133 				cur.base = -1;
134 			}
135 		}
136 	}
137 	if (cur.base != -1) {
138 		if (best.base == -1 || cur.len > best.len)
139 			best = cur;
140 	}
141 	if (best.base != -1 && best.len < 2)
142 		best.base = -1;
143 
144 	/*
145 	 * Format the result.
146 	 */
147 	tp = tmp;
148 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
149 		/* Are we inside the best run of 0x00's? */
150 		if (best.base != -1 && i >= best.base &&
151 		    i < (best.base + best.len)) {
152 			if (i == best.base)
153 				*tp++ = ':';
154 			continue;
155 		}
156 		/* Are we following an initial run of 0x00s or any real hex? */
157 		if (i != 0)
158 			*tp++ = ':';
159 		/* Is this address an encapsulated IPv4? */
160 		if (i == 6 && best.base == 0 &&
161 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
162 			if (!inet_ntop4(src+12, tp,
163 					sizeof(tmp) - (tp - tmp)))
164 				return (NULL);
165 			tp += strlen(tp);
166 			break;
167 		}
168 		tp += sprintf(tp, "%x", words[i]); /* XXX */
169 	}
170 	/* Was it a trailing run of 0x00's? */
171 	if (best.base != -1 && (best.base + best.len) ==
172 	    (NS_IN6ADDRSZ / NS_INT16SZ))
173 		*tp++ = ':';
174 	*tp++ = '\0';
175 
176 	/*
177 	 * Check for overflow, copy, and we're done.
178 	 */
179 	if ((size_t)(tp - tmp) > size) {
180 		errno = ENOSPC;
181 		return (NULL);
182 	}
183 	strcpy(dst, tmp);
184 	return (dst);
185 }
186 #endif /* AF_INET6 */
187