xref: /haiku/src/add-ons/kernel/network/protocols/ipv6/ipv6_utils.cpp (revision 579f1dbca962a2a03df54f69fdc6e9423f91f20e)
1 /*
2  * Copyright 2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 /*
7  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 1996-1999 by Internet Software Consortium.
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS.	 IN NO EVENT SHALL ISC BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22 
23 
24 #include <net_datalink.h>
25 
26 #include <ByteOrder.h>
27 #include <KernelExport.h>
28 #include <NetUtilities.h>
29 
30 #include <memory.h>
31 #include <netinet6/in6.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "ipv6_utils.h"
37 
38 
39 #define NS_IN6ADDRSZ 16
40 #define NS_INT16SZ 2
41 
42 #define SPRINTF(x) ((size_t)sprintf x)
43 
44 
45 /*!	Convert IPv6 binary address into presentation (printable) format.
46 	Author: Paul Vixie, 1996.
47 	\return pointer to dst string if address as been printed
48 	\return NULL if the buffer is too short
49 */
50 const char *
51 ip6_sprintf(const in6_addr *srcaddr, char *dst, size_t size)
52 {
53 	/*
54 	 * Note that int32_t and int16_t need only be "at least" large enough
55 	 * to contain a value of the specified size.  On some systems, like
56 	 * Crays, there is no such thing as an integer variable with 16 bits.
57 	 * Keep this in mind if you think this function should have been coded
58 	 * to use pointer overlays.	 All the world's not a VAX.
59 	 */
60 	char tmp[INET6_ADDRSTRLEN], *tp;
61 	struct { int base, len; } best, cur;
62 	uint16 words[NS_IN6ADDRSZ / NS_INT16SZ];
63 	int i;
64 	const uint8 *src = srcaddr->s6_addr;
65 
66 	/*
67 	 * Preprocess:
68 	 *	Copy the input (bytewise) array into a wordwise array.
69 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
70 	 */
71 	memset(words, '\0', sizeof words);
72 	for (i = 0; i < NS_IN6ADDRSZ; i++)
73 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
74 	best.base = -1;
75 	best.len = 0;
76 	cur.base = -1;
77 	cur.len = 0;
78 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
79 		if (words[i] == 0) {
80 			if (cur.base == -1)
81 				cur.base = i, cur.len = 1;
82 			else
83 				cur.len++;
84 		} else {
85 			if (cur.base != -1) {
86 				if (best.base == -1 || cur.len > best.len)
87 					best = cur;
88 				cur.base = -1;
89 			}
90 		}
91 	}
92 	if (cur.base != -1) {
93 		if (best.base == -1 || cur.len > best.len)
94 			best = cur;
95 	}
96 	if (best.base != -1 && best.len < 2)
97 		best.base = -1;
98 
99 	/*
100 	 * Format the result.
101 	 */
102 	tp = tmp;
103 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
104 		/* Are we inside the best run of 0x00's? */
105 		if (best.base != -1 && i >= best.base &&
106 			i < (best.base + best.len)) {
107 			if (i == best.base)
108 				*tp++ = ':';
109 			continue;
110 		}
111 		/* Are we following an initial run of 0x00s or any real hex? */
112 		if (i != 0)
113 			*tp++ = ':';
114 		/* Is this address an encapsulated IPv4? */
115 #if 0
116 		if (i == 6 && best.base == 0 && (best.len == 6 ||
117 			(best.len == 7 && words[7] != 0x0001) ||
118 			(best.len == 5 && words[5] == 0xffff))) {
119 			if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
120 				return (NULL);
121 			tp += strlen(tp);
122 			break;
123 		}
124 #endif
125 		tp += SPRINTF((tp, "%x", words[i]));
126 	}
127 	/* Was it a trailing run of 0x00's? */
128 	if (best.base != -1 && (best.base + best.len) ==
129 		(NS_IN6ADDRSZ / NS_INT16SZ))
130 		*tp++ = ':';
131 	*tp++ = '\0';
132 
133 	/*
134 	 * Check for overflow, copy, and we're done.
135 	 */
136 	if ((size_t)(tp - tmp) > size)
137 		return NULL;
138 
139 	strcpy(dst, tmp);
140 	return dst;
141 }
142