2 * Issues to be discussed:
3 * - Thread safe-ness must be checked
6 #if ( defined(__linux__) || defined(__linux) || defined(LINUX) )
9 # define IF_NAMESIZE IFNAMSIZ
11 # define IF_NAMESIZE 16
17 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by WIDE Project and
32 * 4. Neither the name of the project nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 #include <port_before.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/nameser.h>
56 #include <arpa/inet.h>
64 #include <port_after.h>
67 * Note that a_off will be dynamically adjusted so that to be consistent
68 * with the definition of sockaddr_in{,6}.
69 * The value presented below is just a guess.
77 /* first entry is linked last... */
78 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
79 offsetof(struct sockaddr_in
, sin_addr
)},
80 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
81 offsetof(struct sockaddr_in6
, sin6_addr
)},
93 static int ip6_parsenumeric
__P((const struct sockaddr
*, const char *, char *,
95 #ifdef HAVE_SIN6_SCOPE_ID
96 static int ip6_sa2str
__P((const struct sockaddr_in6
*, char *, size_t, int));
100 getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
101 const struct sockaddr
*sa
;
121 const struct sockaddr_in6
*sin6
;
128 if (len
!= salen
) return EAI_FAIL
;
131 family
= sa
->sa_family
;
132 for (i
= 0; afdl
[i
].a_af
; i
++)
133 if (afdl
[i
].a_af
== family
) {
140 if (salen
!= afd
->a_socklen
) return EAI_FAIL
;
142 port
= ((const struct sockinet
*)sa
)->si_port
; /*%< network byte order */
143 addr
= (const char *)sa
+ afd
->a_off
;
145 if (serv
== NULL
|| servlen
== 0U) {
147 * rfc2553bis says that serv == NULL or servlen == 0 means that
148 * the caller does not want the result.
150 } else if (flags
& NI_NUMERICSERV
) {
151 sprintf(numserv
, "%d", ntohs(port
));
152 if (strlen(numserv
) > servlen
)
154 strcpy(serv
, numserv
);
156 sp
= getservbyport(port
, (flags
& NI_DGRAM
) ? "udp" : "tcp");
158 if (strlen(sp
->s_name
) + 1 > servlen
)
160 strcpy(serv
, sp
->s_name
);
165 switch (sa
->sa_family
) {
167 if (ntohl(*(const u_int32_t
*)addr
) >> IN_CLASSA_NSHIFT
== 0)
168 flags
|= NI_NUMERICHOST
;
171 sin6
= (const struct sockaddr_in6
*)sa
;
172 switch (sin6
->sin6_addr
.s6_addr
[0]) {
174 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
176 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
179 flags
|= NI_NUMERICHOST
;
182 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
183 flags
|= NI_NUMERICHOST
;
184 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
185 flags
|= NI_NUMERICHOST
;
190 if (host
== NULL
|| hostlen
== 0U) {
192 * rfc2553bis says that host == NULL or hostlen == 0 means that
193 * the caller does not want the result.
195 } else if (flags
& NI_NUMERICHOST
) {
198 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
201 if (flags
& NI_NOFQDN
) {
202 p
= strchr(hp
->h_name
, '.');
205 if (strlen(hp
->h_name
) + 1 > hostlen
)
207 strcpy(host
, hp
->h_name
);
209 if (flags
& NI_NAMEREQD
)
217 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
225 if (inet_ntop(afd
->a_af
, addr
, numaddr
,
226 sizeof(numaddr
)) == NULL
)
228 if (strlen(numaddr
) + 1 > hostlen
)
230 strcpy(host
, numaddr
);
238 ip6_parsenumeric(const struct sockaddr
*sa
, const char *addr
, char *host
,
239 size_t hostlen
, int flags
)
244 #ifndef HAVE_SIN6_SCOPE_ID
249 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
))
253 numaddrlen
= strlen(numaddr
);
254 if (numaddrlen
+ 1 > hostlen
) /*%< don't forget terminator */
256 strcpy(host
, numaddr
);
258 #ifdef HAVE_SIN6_SCOPE_ID
259 if (((const struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
260 char scopebuf
[MAXHOSTNAMELEN
]; /*%< XXX */
263 /* ip6_sa2str never fails */
264 scopelen
= ip6_sa2str((const struct sockaddr_in6
*)sa
,
265 scopebuf
, sizeof(scopebuf
), flags
);
267 if (scopelen
+ 1 + numaddrlen
+ 1 > hostlen
)
270 /* construct <numeric-addr><delim><scopeid> */
271 memcpy(host
+ numaddrlen
+ 1, scopebuf
,
273 host
[numaddrlen
] = SCOPE_DELIMITER
;
274 host
[numaddrlen
+ 1 + scopelen
] = '\0';
281 #ifdef HAVE_SIN6_SCOPE_ID
284 ip6_sa2str(const struct sockaddr_in6
*sa6
, char *buf
,
285 size_t bufsiz
, int flags
)
287 #ifdef USE_IFNAMELINKID
288 unsigned int ifindex
= (unsigned int)sa6
->sin6_scope_id
;
289 const struct in6_addr
*a6
= &sa6
->sin6_addr
;
293 #ifdef NI_NUMERICSCOPE
294 if (flags
& NI_NUMERICSCOPE
) {
295 sprintf(tmp
, "%u", sa6
->sin6_scope_id
);
297 strncpy(buf
, tmp
, bufsiz
- 1);
298 buf
[bufsiz
- 1] = '\0';
304 #ifdef USE_IFNAMELINKID
306 * For a link-local address, convert the index to an interface
307 * name, assuming a one-to-one mapping between links and interfaces.
308 * Note, however, that this assumption is stronger than the
309 * specification of the scoped address architecture; the
310 * specficication says that more than one interfaces can belong to
314 /* if_indextoname() does not take buffer size. not a good api... */
315 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
)) &&
316 bufsiz
>= IF_NAMESIZE
) {
317 char *p
= if_indextoname(ifindex
, buf
);
325 sprintf(tmp
, "%u", sa6
->sin6_scope_id
);
327 strncpy(buf
, tmp
, bufsiz
- 1);
328 buf
[bufsiz
- 1] = '\0';