Rework the trip history window layout.
[openttd-joker.git] / src / 3rdparty / os2 / getnameinfo.c
blob14fd89ef1557e7e6ee0c56e994ea0068ffb83d7f
1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
31 * Issues to be discussed:
32 * - RFC2553 says that we should raise error on short buffer. X/Open says
33 * we need to truncate the result. We obey RFC2553 (and X/Open should be
34 * modified). ipngwg rough consensus seems to follow RFC2553. RFC3493 says
35 * nothing about it, but defines a new error code EAI_OVERFLOW which seems
36 * to be intended the code for this case.
37 * - What is "local" in NI_NOFQDN? (see comments in the code)
38 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
39 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
40 * sin6_scope_id is filled - standardization status?
41 * - what should we do if we should do getservbyport("sctp")?
45 * Considerations about thread-safeness
46 * The code in this file is thread-safe, and so the thread-safeness of
47 * getnameinfo() depends on the property of backend functions.
48 * - getservbyport() is not thread safe for most systems we are targeting.
49 * - getipnodebyaddr() is thread safe. However, many resolver libraries
50 * used in the function are not thread safe.
51 * - gethostbyaddr() is usually not thread safe.
54 #if !HAVE_GETNAMEINFO
56 #include <sys/socket.h>
57 #include <net/if.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 #include <arpa/nameser.h>
61 #include <netdb.h>
62 #include <resolv.h>
63 #include <string.h>
64 #include <stddef.h>
65 #include <errno.h>
66 #include <inttypes.h>
67 #include "getaddrinfo.h"
68 #include "getnameinfo.h"
70 static const struct afd {
71 int a_af;
72 int a_addrlen;
73 int a_socklen;
74 int a_off;
75 int a_portoff;
76 } afdl [] = {
77 #if INET6
78 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
79 offsetof(struct sockaddr_in6, sin6_addr),
80 offsetof(struct sockaddr_in6, sin6_port)},
81 #endif
82 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
83 offsetof(struct sockaddr_in, sin_addr),
84 offsetof(struct sockaddr_in, sin_port)},
85 {0, 0, 0, 0, 0},
88 #if INET6
89 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
90 size_t, int));
91 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
92 #endif
94 int
95 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
96 const struct sockaddr *sa;
97 socklen_t salen;
98 char *host;
99 size_t hostlen;
100 char *serv;
101 size_t servlen;
102 int flags;
104 const struct afd *afd;
105 struct servent *sp;
106 struct hostent *hp;
107 unsigned short port;
108 int family, i;
109 const char *addr;
110 uint32_t v4a;
111 char numserv[512];
113 if (sa == NULL)
114 return EAI_FAIL;
116 #if HAVE_SA_LEN /*XXX*/
117 if (sa->sa_len != salen)
118 return EAI_FAIL;
119 #endif
121 family = sa->sa_family;
122 for (i = 0; afdl[i].a_af; i++)
123 if (afdl[i].a_af == family) {
124 afd = &afdl[i];
125 goto found;
127 return EAI_FAMILY;
129 found:
130 if (salen != afd->a_socklen)
131 return EAI_FAIL;
133 /* network byte order */
134 memcpy(&port, (const char *)sa + afd->a_portoff, sizeof(port));
135 addr = (const char *)sa + afd->a_off;
137 if (serv == NULL || servlen == 0) {
139 * do nothing in this case.
140 * in case you are wondering if "&&" is more correct than
141 * "||" here: RFC3493 says that serv == NULL OR servlen == 0
142 * means that the caller does not want the result.
144 } else {
145 if (flags & NI_NUMERICSERV)
146 sp = NULL;
147 else {
148 sp = getservbyport(port,
149 (flags & NI_DGRAM) ? "udp" : "tcp");
151 if (sp) {
152 if (strlen(sp->s_name) + 1 > servlen)
153 return EAI_OVERFLOW;
154 strncpy(serv, sp->s_name, servlen);
155 } else {
156 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
157 if (strlen(numserv) + 1 > servlen)
158 return EAI_OVERFLOW;
159 strncpy(serv, numserv, servlen);
163 switch (sa->sa_family) {
164 case AF_INET:
165 v4a = (uint32_t)
166 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
167 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
168 flags |= NI_NUMERICHOST;
169 v4a >>= IN_CLASSA_NSHIFT;
170 if (v4a == 0)
171 flags |= NI_NUMERICHOST;
172 break;
173 #if INET6
174 case AF_INET6: {
175 const struct sockaddr_in6 *sin6;
176 sin6 = (const struct sockaddr_in6 *)sa;
177 switch (sin6->sin6_addr.s6_addr[0]) {
178 case 0x00:
179 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
181 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
183 else
184 flags |= NI_NUMERICHOST;
185 break;
186 default:
187 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
188 flags |= NI_NUMERICHOST;
189 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
190 flags |= NI_NUMERICHOST;
191 break;
194 break;
195 #endif
197 if (host == NULL || hostlen == 0) {
199 * do nothing in this case.
200 * in case you are wondering if "&&" is more correct than
201 * "||" here: RFC3493 says that host == NULL or hostlen == 0
202 * means that the caller does not want the result.
204 } else if (flags & NI_NUMERICHOST) {
205 /* NUMERICHOST and NAMEREQD conflicts with each other */
206 if (flags & NI_NAMEREQD)
207 return EAI_NONAME;
209 goto numeric;
210 } else {
211 #if USE_GETIPNODEBY
212 int h_error = 0;
213 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
214 #else
215 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
216 #if 0 // getnameinfo.c:161:9: error: variable 'h_error' set but not used
217 #if HAVE_H_ERRNO
218 h_error = h_errno;
219 #else
220 h_error = EINVAL;
221 #endif
222 #endif /* 0 */
223 #endif
225 if (hp) {
226 #if 0
227 if (flags & NI_NOFQDN) {
229 * According to RFC3493 section 6.2, NI_NOFQDN
230 * means "node name portion of the FQDN shall
231 * be returned for local hosts." The following
232 * code tries to implement it by returning the
233 * first label (the part before the first
234 * period) of the FQDN. However, it is not
235 * clear if this always makes sense, since the
236 * given address may be outside of "local
237 * hosts." Due to the unclear description, we
238 * disable the code in this implementation.
240 char *p;
241 p = strchr(hp->h_name, '.');
242 if (p)
243 *p = '\0';
245 #endif
246 if (strlen(hp->h_name) + 1 > hostlen) {
247 #if USE_GETIPNODEBY
248 freehostent(hp);
249 #endif
250 return EAI_OVERFLOW;
252 strncpy(host, hp->h_name, hostlen);
253 #if USE_GETIPNODEBY
254 freehostent(hp);
255 #endif
256 } else {
257 if (flags & NI_NAMEREQD)
258 return EAI_NONAME;
260 numeric:
261 switch (afd->a_af) {
262 #if INET6
263 case AF_INET6: {
264 int error;
266 if ((error = ip6_parsenumeric(sa, addr, host,
267 hostlen,
268 flags)) != 0)
269 return(error);
270 break;
272 #endif
273 default:
274 if (inet_ntop(afd->a_af, addr, host,
275 hostlen) == NULL)
276 return EAI_SYSTEM;
277 break;
281 return(0);
284 #if INET6
285 static int
286 ip6_parsenumeric(sa, addr, host, hostlen, flags)
287 const struct sockaddr *sa;
288 const char *addr;
289 char *host;
290 size_t hostlen;
291 int flags;
293 int numaddrlen;
294 char numaddr[512];
296 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
297 return EAI_SYSTEM;
299 numaddrlen = strlen(numaddr);
300 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
301 return EAI_OVERFLOW;
302 strncpy(host, numaddr, hostlen);
304 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
305 char zonebuf[SQUIDHOSTNAMELEN];
306 int zonelen;
308 zonelen = ip6_sa2str(
309 (const struct sockaddr_in6 *)(const void *)sa,
310 zonebuf, sizeof(zonebuf), flags);
311 if (zonelen < 0)
312 return EAI_OVERFLOW;
313 if (zonelen + 1 + numaddrlen + 1 > hostlen)
314 return EAI_OVERFLOW;
316 /* construct <numeric-addr><delim><zoneid> */
317 memcpy(host + numaddrlen + 1, zonebuf,
318 (size_t)zonelen);
319 host[numaddrlen] = SCOPE_DELIMITER;
320 host[numaddrlen + 1 + zonelen] = '\0';
323 return 0;
326 /* ARGSUSED */
327 static int
328 ip6_sa2str(sa6, buf, bufsiz, flags)
329 const struct sockaddr_in6 *sa6;
330 char *buf;
331 size_t bufsiz;
332 int flags;
334 unsigned int ifindex;
335 const struct in6_addr *a6;
336 int n;
338 ifindex = (unsigned int)sa6->sin6_scope_id;
339 a6 = &sa6->sin6_addr;
341 #if NI_NUMERICSCOPE
342 if ((flags & NI_NUMERICSCOPE) != 0) {
343 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
344 if (n < 0 || n >= bufsiz)
345 return -1;
346 else
347 return n;
349 #endif
351 /* if_indextoname() does not take buffer size. not a good api... */
352 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
353 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
354 char *p = if_indextoname(ifindex, buf);
355 if (p)
356 return (strlen(p));
359 /* last resort */
360 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
361 if (n < 0 || n >= bufsiz)
362 return -1;
363 else
364 return n;
366 #endif /* INET6 */
367 #endif