1 /* Copyright (c) 1996 by Internet Software Consortium.
2 * Copyright (c) 2005 by Pavel Fedin
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 #include <amitcp/socketbasetags.h>
19 #include <emul/emulregs.h>
20 #include <proto/socket.h>
21 #include <sys/cdefs.h>
22 #include <sys/param.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <arpa/nameser.h>
33 * WARNING: Don't even consider trying to compile this on a system where
34 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
37 static const char *inet_ntop4(const u_char
*src
, char *dst
, socklen_t size
);
38 static const char *inet_ntop6(const u_char
*src
, char *dst
, socklen_t size
);
41 * inet_ntop(af, src, dst, size)
42 * convert a network format address to presentation format.
44 * pointer to presentation format address (`dst'), or NULL (see errno).
52 const void * __restrict src
= (const void *)REG_A0
;
53 char * __restrict dst
= (char *)REG_A1
;
54 socklen_t size
= (socklen_t
)REG_D1
;
58 return (inet_ntop4(src
, dst
, size
));
60 return (inet_ntop6(src
, dst
, size
));
62 SocketBaseTags(SBTM_SETVAL(SBTC_ERRNO
),EAFNOSUPPORT
,TAG_DONE
);
69 * inet_ntop4(src, dst, size)
70 * format an IPv4 address, more or less like inet_ntoa()
75 * (2) takes a u_char* not an in_addr as input
80 inet_ntop4(const u_char
*src
, char *dst
, socklen_t size
)
82 static const char fmt
[] = "%u.%u.%u.%u";
84 if ((socklen_t
)snprintf(dst
, size
, fmt
, src
[0], src
[1], src
[2], src
[3])
86 SocketBaseTags(SBTM_SETVAL(SBTC_ERRNO
),ENOSPC
,TAG_DONE
);
93 * inet_ntop6(src, dst, size)
94 * convert IPv6 binary address into presentation (printable) format
99 inet_ntop6(const u_char
*src
, char *dst
, socklen_t size
)
102 * Note that int32_t and int16_t need only be "at least" large enough
103 * to contain a value of the specified size. On some systems, like
104 * Crays, there is no such thing as an integer variable with 16 bits.
105 * Keep this in mind if you think this function should have been coded
106 * to use pointer overlays. All the world's not a VAX.
108 char tmp
[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp
;
109 struct { int base
, len
; } best
, cur
;
110 u_int words
[NS_IN6ADDRSZ
/ NS_INT16SZ
];
115 * Copy the input (bytewise) array into a wordwise array.
116 * Find the longest run of 0x00's in src[] for :: shorthanding.
118 memset(words
, '\0', sizeof words
);
119 for (i
= 0; i
< NS_IN6ADDRSZ
; i
++)
120 words
[i
/ 2] |= (src
[i
] << ((1 - (i
% 2)) << 3));
123 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
126 cur
.base
= i
, cur
.len
= 1;
130 if (cur
.base
!= -1) {
131 if (best
.base
== -1 || cur
.len
> best
.len
)
137 if (cur
.base
!= -1) {
138 if (best
.base
== -1 || cur
.len
> best
.len
)
141 if (best
.base
!= -1 && best
.len
< 2)
148 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
149 /* Are we inside the best run of 0x00's? */
150 if (best
.base
!= -1 && i
>= best
.base
&&
151 i
< (best
.base
+ best
.len
)) {
156 /* Are we following an initial run of 0x00s or any real hex? */
159 /* Is this address an encapsulated IPv4? */
160 if (i
== 6 && best
.base
== 0 &&
161 (best
.len
== 6 || (best
.len
== 5 && words
[5] == 0xffff))) {
162 if (!inet_ntop4(src
+12, tp
, sizeof tmp
- (tp
- tmp
)))
167 tp
+= sprintf(tp
, "%x", words
[i
]);
169 /* Was it a trailing run of 0x00's? */
170 if (best
.base
!= -1 && (best
.base
+ best
.len
) ==
171 (NS_IN6ADDRSZ
/ NS_INT16SZ
))
176 * Check for overflow, copy, and we're done.
178 if ((socklen_t
)(tp
- tmp
) > size
) {
179 SocketBaseTags(SBTM_SETVAL(SBTC_ERRNO
),ENOSPC
,TAG_DONE
);