1 /**
2  * @file ntop.c  Network address structure functions
3  *
4  * Copyright (C) 2010 Creytiv.com
5  */
6 
7 #define _BSD_SOURCE 1
8 #define _DEFAULT_SOURCE 1
9 
10 #ifdef HAVE_INET_NTOP
11 #ifdef WIN32
12 #ifdef _MSC_VER
13 #pragma warning (disable: 4090)
14 #endif
15 #include <windows.h>
16 #else
17 #define __USE_BSD 1    /**< Use BSD code */
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <arpa/inet.h>
21 #define __USE_POSIX 1  /**< Use POSIX code */
22 #include <netdb.h>
23 #endif /* WIN32 */
24 #endif
25 #include <string.h>
26 #include <re_types.h>
27 #include <re_fmt.h>
28 #include <re_mbuf.h>
29 #include <re_sa.h>
30 #include "sa.h"
31 
32 
33 #define DEBUG_MODULE "net_ntop"
34 #define DEBUG_LEVEL 5
35 #include <re_dbg.h>
36 
37 
38 #ifndef HAVE_INET_NTOP
39 
40 
41 #define NS_IN6ADDRSZ     16      /**< IPv6 T_AAAA */
42 #define NS_INT16SZ       2       /**< #/bytes of data in a u_int16_t */
43 
44 
45 static const char*
inet_ntop4(const u_char * src,char * dst,size_t size)46 inet_ntop4(const u_char *src, char *dst, size_t size)
47 {
48 	if (re_snprintf(dst, size, "%u.%u.%u.%u",
49 			src[0], src[1], src[2], src[3]) < 0) {
50 		errno = ENOSPC;
51 		dst[size-1] = 0;
52 		return NULL;
53 	}
54 
55 	return dst;
56 }
57 
58 
59 #ifdef HAVE_INET6
60 /* const char *
61  * inet_ntop6(src, dst, size)
62  *	convert IPv6 binary address into presentation (printable) format
63  * author:
64  *	Paul Vixie, 1996.
65  */
66 
67 static const char *
inet_ntop6(const u_char * src,char * dst,size_t size)68 inet_ntop6(const u_char *src, char *dst, size_t size)
69 {
70 	/*
71 	 * Note that int32_t and int16_t need only be "at least" large enough
72 	 * to contain a value of the specified size.  On some systems, like
73 	 * Crays, there is no such thing as an integer variable with 16 bits.
74 	 * Keep this in mind if you think this function should have been coded
75 	 * to use pointer overlays.  All the world's not a VAX.
76 	 */
77 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
78 	struct { int base, len; } best, cur;
79 	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
80 	int i;
81 
82 	/*
83 	 * Preprocess:
84 	 *	Copy the input (bytewise) array into a wordwise array.
85 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
86 	 */
87 	memset(words, '\0', sizeof words);
88 	for (i = 0; i < NS_IN6ADDRSZ; i++)
89 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
90 	best.base = -1;
91 	best.len = 0;
92 	cur.base = -1;
93 	cur.len = 0;
94 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
95 		if (words[i] == 0) {
96 			if (cur.base == -1)
97 				cur.base = i, cur.len = 1;
98 			else
99 				cur.len++;
100 		}
101 		else {
102 			if (cur.base != -1) {
103 				if (best.base == -1 || cur.len > best.len)
104 					best = cur;
105 				cur.base = -1;
106 			}
107 		}
108 	}
109 	if (cur.base != -1) {
110 		if (best.base == -1 || cur.len > best.len)
111 			best = cur;
112 	}
113 	if (best.base != -1 && best.len < 2)
114 		best.base = -1;
115 
116 	/*
117 	 * Format the result.
118 	 */
119 	tp = tmp;
120 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
121 		/* Are we inside the best run of 0x00's? */
122 		if (best.base != -1 && i >= best.base &&
123 		    i < (best.base + best.len)) {
124 			if (i == best.base)
125 				*tp++ = ':';
126 			continue;
127 		}
128 		/* Are we following an initial run of 0x00s or any real hex?*/
129 		if (i != 0)
130 			*tp++ = ':';
131 		/* Is this address an encapsulated IPv4? */
132 		if (i == 6 && best.base == 0 &&
133 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
134 			if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
135 				return NULL;
136 			tp += strlen(tp);
137 			break;
138 		}
139 		tp += sprintf(tp, "%x", words[i]);
140 	}
141 	/* Was it a trailing run of 0x00's? */
142 	if (best.base != -1 && (best.base + best.len) ==
143 	    (NS_IN6ADDRSZ / NS_INT16SZ))
144 		*tp++ = ':';
145 	*tp++ = '\0';
146 
147 	/*
148 	 * Check for overflow, copy, and we're done.
149 	 */
150 	if ((size_t)(tp - tmp) > size) {
151 		errno = ENOSPC;
152 		return NULL;
153 	}
154 	strcpy(dst, tmp);
155 
156 	return dst;
157 }
158 #endif
159 
160 
161 /*
162  * Implementation of inet_ntop()
163  */
164 const char* inet_ntop(int af, const void *src, char *dst, size_t size);
inet_ntop(int af,const void * src,char * dst,size_t size)165 const char* inet_ntop(int af, const void *src, char *dst, size_t size)
166 {
167 	switch (af) {
168 
169 	case AF_INET:
170 		return inet_ntop4(src, dst, size);
171 
172 #ifdef HAVE_INET6
173 	case AF_INET6:
174 		return inet_ntop6(src, dst, size);
175 #endif
176 
177 	default:
178 		DEBUG_WARNING("inet_ntop: unknown address family %d\n", af);
179 		return NULL;
180 	}
181 }
182 #endif
183 
184 
185 /*
186  *  POSIX  1003.1-2001 marks gethostbyaddr() and gethostbyname() obsolescent.
187  *  See getaddrinfo(3), getnameinfo(3), gai_strerror(3).
188  */
189 
190 
191 /**
192  * Convert network address structure to a character string
193  *
194  * @param sa   Socket address
195  * @param buf  Buffer to return IP address
196  * @param size Size of buffer
197  *
198  * @return 0 if success, otherwise errorcode
199  */
net_inet_ntop(const struct sa * sa,char * buf,int size)200 int net_inet_ntop(const struct sa *sa, char *buf, int size)
201 {
202 	if (!sa || !buf || !size)
203 		return EINVAL;
204 
205 	switch (sa->u.sa.sa_family) {
206 
207 	case AF_INET:
208 		inet_ntop(AF_INET, &sa->u.in.sin_addr, buf, size);
209 		break;
210 
211 #ifdef HAVE_INET6
212 	case AF_INET6:
213 		inet_ntop(AF_INET6, &sa->u.in6.sin6_addr, buf, size);
214 		break;
215 #endif
216 
217 	default:
218 		return EAFNOSUPPORT;
219 	}
220 
221 	return 0;
222 }
223