]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/NetUtil/inet_ntop.c
2 Convert a binary network address into a presentable (printable) format.
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
14 * Copyright (c) 1996-1999 by Internet Software Consortium.
16 * Permission to use, copy, modify, and distribute this software for any
17 * purpose with or without fee is hereby granted, provided that the above
18 * copyright notice and this permission notice appear in all copies.
20 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
21 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
23 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
25 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
26 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 NetBSD: inet_ntop.c,v 1.3.4.2 2007/05/17 21:25:14 jdc Exp
29 inet_ntop.c,v 1.3.18.2 2005/11/03 23:02:22 marka Exp
31 #include <LibConfig.h>
33 //#include "port_before.h"
35 #include "namespace.h"
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <arpa/nameser.h>
50 //#include "port_after.h"
53 __weak_alias(inet_ntop
,_inet_ntop
)
57 * WARNING: Don't even consider trying to compile this on a system where
58 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
61 static const char *inet_ntop4(const u_char
*src
, char *dst
, socklen_t size
);
62 static const char *inet_ntop6(const u_char
*src
, char *dst
, socklen_t size
);
65 * inet_ntop(af, src, dst, size)
66 * convert a network format address to presentation format.
68 * pointer to presentation format address (`dst'), or NULL (see errno).
73 inet_ntop(int af
, const void *src
, char *dst
, socklen_t size
)
76 _DIAGASSERT(src
!= NULL
);
77 _DIAGASSERT(dst
!= NULL
);
81 return (inet_ntop4(src
, dst
, size
));
83 return (inet_ntop6(src
, dst
, size
));
92 * inet_ntop4(src, dst, size)
93 * format an IPv4 address, more or less like inet_ntoa()
98 * (2) takes a u_char* not an in_addr as input
103 inet_ntop4(const u_char
*src
, char *dst
, socklen_t size
)
105 char tmp
[sizeof "255.255.255.255"];
108 _DIAGASSERT(src
!= NULL
);
109 _DIAGASSERT(dst
!= NULL
);
111 l
= snprintf(tmp
, sizeof(tmp
), "%u.%u.%u.%u",
112 src
[0], src
[1], src
[2], src
[3]);
113 if (l
<= 0 || (socklen_t
) l
>= size
) {
117 //strlcpy(dst, tmp, size);
118 strncpyX(dst
, tmp
, (size_t)size
);
123 * inet_ntop6(src, dst, size)
124 * convert IPv6 binary address into presentation (printable) format
129 inet_ntop6(const u_char
*src
, char *dst
, socklen_t size
)
132 * Note that int32_t and int16_t need only be "at least" large enough
133 * to contain a value of the specified size. On some systems, like
134 * Crays, there is no such thing as an integer variable with 16 bits.
135 * Keep this in mind if you think this function should have been coded
136 * to use pointer overlays. All the world's not a VAX.
138 char tmp
[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
140 struct { int base
, len
; } best
, cur
;
141 unsigned int words
[NS_IN6ADDRSZ
/ NS_INT16SZ
];
145 _DIAGASSERT(src
!= NULL
);
146 _DIAGASSERT(dst
!= NULL
);
150 * Copy the input (bytewise) array into a wordwise array.
151 * Find the longest run of 0x00's in src[] for :: shorthanding.
153 memset(words
, '\0', sizeof words
);
154 for (i
= 0; i
< NS_IN6ADDRSZ
; i
++)
155 words
[i
/ 2] |= (src
[i
] << ((1 - (i
% 2)) << 3));
160 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
163 cur
.base
= i
, cur
.len
= 1;
167 if (cur
.base
!= -1) {
168 if (best
.base
== -1 || cur
.len
> best
.len
)
174 if (cur
.base
!= -1) {
175 if (best
.base
== -1 || cur
.len
> best
.len
)
178 if (best
.base
!= -1 && best
.len
< 2)
185 ep
= tmp
+ sizeof(tmp
);
186 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
187 /* Are we inside the best run of 0x00's? */
188 if (best
.base
!= -1 && i
>= best
.base
&&
189 i
< (best
.base
+ best
.len
)) {
194 /* Are we following an initial run of 0x00s or any real hex? */
200 /* Is this address an encapsulated IPv4? */
201 if (i
== 6 && best
.base
== 0 &&
203 (best
.len
== 7 && words
[7] != 0x0001) ||
204 (best
.len
== 5 && words
[5] == 0xffff))) {
205 if (!inet_ntop4(src
+12, tp
, (socklen_t
)(ep
- tp
)))
210 advance
= snprintf(tp
, (size_t)(ep
- tp
), "%x", words
[i
]);
211 if (advance
<= 0 || advance
>= ep
- tp
)
215 /* Was it a trailing run of 0x00's? */
216 if (best
.base
!= -1 && (best
.base
+ best
.len
) ==
217 (NS_IN6ADDRSZ
/ NS_INT16SZ
)) {
227 * Check for overflow, copy, and we're done.
229 if ((size_t)(tp
- tmp
) > size
) {
233 //strlcpy(dst, tmp, size);
234 strncpyX(dst
, tmp
, (size_t)size
);