]>
git.saurik.com Git - apple/libc.git/blob - net/inet_ntop.c
2 * Copyright (c) 2003,2005,2006,2011,2012 Apple, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
25 * Copyright (c) 1996-1999 by Internet Software Consortium.
27 * Permission to use, copy, modify, and distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
31 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
32 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
34 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
37 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 #include <arpa/inet.h>
41 #include <arpa/nameser.h>
43 #include <netinet/in.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
50 #define MAX_V4_ADDR_LEN 16
52 const char * inet_ntop6(const struct in6_addr
*addr
, char *dst
, socklen_t size
);
53 const char * inet_ntop4(const struct in_addr
*addr
, char *dst
, socklen_t size
);
56 inet_ntop(int af
, const void *addr
, char *buf
, socklen_t len
)
58 if (addr
&& af
== AF_INET6
) return inet_ntop6(addr
, buf
, len
);
59 if (addr
&& af
== AF_INET
) return inet_ntop4(addr
, buf
, len
);
66 * inet_ntop6(src, dst, size)
67 * convert IPv6 binary address into presentation (printable) format
72 inet_ntop6(const struct in6_addr
*addr
, char *dst
, socklen_t size
)
74 const u_char
*src
= addr
->__u6_addr
.__u6_addr8
;
76 * Note that int32_t and int16_t need only be "at least" large enough
77 * to contain a value of the specified size. On some systems, like
78 * Crays, there is no such thing as an integer variable with 16 bits.
79 * Keep this in mind if you think this function should have been coded
80 * to use pointer overlays. All the world's not a VAX.
82 char tmp
[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp
;
83 struct { int base
, len
; } best
, cur
;
84 u_int words
[NS_IN6ADDRSZ
/ NS_INT16SZ
];
89 * Copy the input (bytewise) array into a wordwise array.
90 * Find the longest run of 0x00's in src[] for :: shorthanding.
92 memset(words
, '\0', sizeof words
);
93 for (i
= 0; i
< NS_IN6ADDRSZ
; i
++)
94 words
[i
/ 2] |= (src
[i
] << ((1 - (i
% 2)) << 3));
99 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
101 if (cur
.base
== -1) {
108 if (cur
.base
!= -1) {
109 if (best
.base
== -1 || cur
.len
> best
.len
)
115 if (cur
.base
!= -1) {
116 if (best
.base
== -1 || cur
.len
> best
.len
)
119 if (best
.base
!= -1 && best
.len
< 2)
126 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
127 /* Are we inside the best run of 0x00's? */
128 if (best
.base
!= -1 && i
>= best
.base
&&
129 i
< (best
.base
+ best
.len
)) {
134 /* Are we following an initial run of 0x00s or any real hex? */
137 /* Is this address an encapsulated IPv4? */
138 if (i
== 6 && best
.base
== 0 && (best
.len
== 6 ||
139 (best
.len
== 7 && words
[7] != 0x0001) ||
140 (best
.len
== 5 && words
[5] == 0xffff))) {
141 struct in_addr ipv4_addr
;
142 memcpy(&ipv4_addr
, src
+12, sizeof(ipv4_addr
));
143 if (!inet_ntop4(&ipv4_addr
, tp
, (socklen_t
)sizeof(tmp
) - (socklen_t
)(tp
- tmp
))) {
150 tp
+= sprintf(tp
, "%x", words
[i
]);
152 /* Was it a trailing run of 0x00's? */
153 if (best
.base
!= -1 && (best
.base
+ best
.len
) ==
154 (NS_IN6ADDRSZ
/ NS_INT16SZ
))
159 * Check for overflow, copy, and we're done.
161 if ((socklen_t
)(tp
- tmp
) > size
) {
170 inet_ntop4(const struct in_addr
*addr
, char *dst
, socklen_t size
)
172 char tmp
[MAX_V4_ADDR_LEN
], *p
;
173 const u_int8_t
*ap
= (u_int8_t
*)&addr
->s_addr
;
178 errno
= EAFNOSUPPORT
;
187 memset(tmp
, 0, MAX_V4_ADDR_LEN
);
189 /* 3 dots, trailing nul */
194 for (i
= 0; i
< 4; i
++, ap
++) {
195 snprintf(p
, 4, "%d", *ap
);
199 if (i
< 3) *p
++ = '.';
207 memcpy(dst
, tmp
, len
);