Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / inet_ntop.c
blobcad1efaf2c19218d2f8972bab0df7979c5d547bd
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
15 * SOFTWARE.
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>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.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);
40 /* char *
41 * inet_ntop(af, src, dst, size)
42 * convert a network format address to presentation format.
43 * return:
44 * pointer to presentation format address (`dst'), or NULL (see errno).
45 * author:
46 * Paul Vixie, 1996.
48 const char *
49 inet_ntop(void)
51 int af = REG_D0;
52 const void * __restrict src = (const void *)REG_A0;
53 char * __restrict dst = (char *)REG_A1;
54 socklen_t size = (socklen_t)REG_D1;
56 switch (af) {
57 case AF_INET:
58 return (inet_ntop4(src, dst, size));
59 case AF_INET6:
60 return (inet_ntop6(src, dst, size));
61 default:
62 SocketBaseTags(SBTM_SETVAL(SBTC_ERRNO),EAFNOSUPPORT,TAG_DONE);
63 return (NULL);
65 /* NOTREACHED */
68 /* const char *
69 * inet_ntop4(src, dst, size)
70 * format an IPv4 address, more or less like inet_ntoa()
71 * return:
72 * `dst' (as a const)
73 * notes:
74 * (1) uses no statics
75 * (2) takes a u_char* not an in_addr as input
76 * author:
77 * Paul Vixie, 1996.
79 static const char *
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])
85 >= size) {
86 SocketBaseTags(SBTM_SETVAL(SBTC_ERRNO),ENOSPC,TAG_DONE);
87 return (NULL);
89 return (dst);
92 /* const char *
93 * inet_ntop6(src, dst, size)
94 * convert IPv6 binary address into presentation (printable) format
95 * author:
96 * Paul Vixie, 1996.
98 static const char *
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];
111 int i;
114 * Preprocess:
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));
121 best.base = -1;
122 cur.base = -1;
123 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
124 if (words[i] == 0) {
125 if (cur.base == -1)
126 cur.base = i, cur.len = 1;
127 else
128 cur.len++;
129 } else {
130 if (cur.base != -1) {
131 if (best.base == -1 || cur.len > best.len)
132 best = cur;
133 cur.base = -1;
137 if (cur.base != -1) {
138 if (best.base == -1 || cur.len > best.len)
139 best = cur;
141 if (best.base != -1 && best.len < 2)
142 best.base = -1;
145 * Format the result.
147 tp = tmp;
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)) {
152 if (i == best.base)
153 *tp++ = ':';
154 continue;
156 /* Are we following an initial run of 0x00s or any real hex? */
157 if (i != 0)
158 *tp++ = ':';
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)))
163 return (NULL);
164 tp += strlen(tp);
165 break;
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))
172 *tp++ = ':';
173 *tp++ = '\0';
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);
180 return (NULL);
182 strcpy(dst, tmp);
183 return (dst);