2 * Copyright 2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
7 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
24 #include <net_datalink.h>
26 #include <ByteOrder.h>
27 #include <KernelExport.h>
28 #include <NetUtilities.h>
31 #include <netinet6/in6.h>
36 #include "ipv6_utils.h"
39 #define NS_IN6ADDRSZ 16
42 #define SPRINTF(x) ((size_t)sprintf x)
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
51 ip6_sprintf(const in6_addr
*srcaddr
, char *dst
, size_t size
)
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.
60 char tmp
[INET6_ADDRSTRLEN
], *tp
;
61 struct { int base
, len
; } best
, cur
;
62 uint16 words
[NS_IN6ADDRSZ
/ NS_INT16SZ
];
64 const uint8
*src
= srcaddr
->s6_addr
;
68 * Copy the input (bytewise) array into a wordwise array.
69 * Find the longest run of 0x00's in src[] for :: shorthanding.
71 memset(words
, '\0', sizeof words
);
72 for (i
= 0; i
< NS_IN6ADDRSZ
; i
++)
73 words
[i
/ 2] |= (src
[i
] << ((1 - (i
% 2)) << 3));
78 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
81 cur
.base
= i
, cur
.len
= 1;
86 if (best
.base
== -1 || cur
.len
> best
.len
)
93 if (best
.base
== -1 || cur
.len
> best
.len
)
96 if (best
.base
!= -1 && best
.len
< 2)
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
)) {
111 /* Are we following an initial run of 0x00s or any real hex? */
114 /* Is this address an encapsulated IPv4? */
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
)))
125 tp
+= SPRINTF((tp
, "%x", words
[i
]));
127 /* Was it a trailing run of 0x00's? */
128 if (best
.base
!= -1 && (best
.base
+ best
.len
) ==
129 (NS_IN6ADDRSZ
/ NS_INT16SZ
))
134 * Check for overflow, copy, and we're done.
136 if ((size_t)(tp
- tmp
) > size
)