4 * Issues to be discussed:
5 * - Thread safe-ness must be checked
8 #if ( defined(__linux__) || defined(__linux) || defined(LINUX) )
11 # define IF_NAMESIZE IFNAMSIZ
13 # define IF_NAMESIZE 16
19 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * This product includes software developed by WIDE Project and
34 * 4. Neither the name of the project nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 #include <port_before.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/nameser.h>
58 #include <arpa/inet.h>
66 #include <port_after.h>
69 * Note that a_off will be dynamically adjusted so that to be consistent
70 * with the definition of sockaddr_in{,6}.
71 * The value presented below is just a guess.
79 /* first entry is linked last... */
80 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
81 offsetof(struct sockaddr_in
, sin_addr
)},
82 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
83 offsetof(struct sockaddr_in6
, sin6_addr
)},
95 static int ip6_parsenumeric
__P((const struct sockaddr
*, const char *, char *,
97 #ifdef HAVE_SIN6_SCOPE_ID
98 static int ip6_sa2str
__P((const struct sockaddr_in6
*, char *, size_t, int));
102 getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
103 const struct sockaddr
*sa
;
123 const struct sockaddr_in6
*sin6
;
130 if (len
!= salen
) return EAI_FAIL
;
133 family
= sa
->sa_family
;
134 for (i
= 0; afdl
[i
].a_af
; i
++)
135 if (afdl
[i
].a_af
== family
) {
142 if (salen
!= afd
->a_socklen
) return EAI_FAIL
;
144 port
= ((const struct sockinet
*)sa
)->si_port
; /*%< network byte order */
145 addr
= (const char *)sa
+ afd
->a_off
;
147 if (serv
== NULL
|| servlen
== 0U) {
149 * rfc2553bis says that serv == NULL or servlen == 0 means that
150 * the caller does not want the result.
152 } else if (flags
& NI_NUMERICSERV
) {
153 sprintf(numserv
, "%d", ntohs(port
));
154 if (strlen(numserv
) > servlen
)
156 strcpy(serv
, numserv
);
158 sp
= getservbyport(port
, (flags
& NI_DGRAM
) ? "udp" : "tcp");
160 if (strlen(sp
->s_name
) + 1 > servlen
)
162 strcpy(serv
, sp
->s_name
);
167 switch (sa
->sa_family
) {
169 if (ntohl(*(const u_int32_t
*)addr
) >> IN_CLASSA_NSHIFT
== 0)
170 flags
|= NI_NUMERICHOST
;
173 sin6
= (const struct sockaddr_in6
*)sa
;
174 switch (sin6
->sin6_addr
.s6_addr
[0]) {
176 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
178 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
181 flags
|= NI_NUMERICHOST
;
184 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
185 flags
|= NI_NUMERICHOST
;
186 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
187 flags
|= NI_NUMERICHOST
;
192 if (host
== NULL
|| hostlen
== 0U) {
194 * rfc2553bis says that host == NULL or hostlen == 0 means that
195 * the caller does not want the result.
197 } else if (flags
& NI_NUMERICHOST
) {
200 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
203 if (flags
& NI_NOFQDN
) {
204 p
= strchr(hp
->h_name
, '.');
207 if (strlen(hp
->h_name
) + 1 > hostlen
)
209 strcpy(host
, hp
->h_name
);
211 if (flags
& NI_NAMEREQD
)
219 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
227 if (inet_ntop(afd
->a_af
, addr
, numaddr
,
228 sizeof(numaddr
)) == NULL
)
230 if (strlen(numaddr
) + 1 > hostlen
)
232 strcpy(host
, numaddr
);
240 ip6_parsenumeric(const struct sockaddr
*sa
, const char *addr
, char *host
,
241 size_t hostlen
, int flags
)
246 #ifndef HAVE_SIN6_SCOPE_ID
251 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
))
255 numaddrlen
= strlen(numaddr
);
256 if (numaddrlen
+ 1 > hostlen
) /*%< don't forget terminator */
258 strcpy(host
, numaddr
);
260 #ifdef HAVE_SIN6_SCOPE_ID
261 if (((const struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
262 char scopebuf
[MAXHOSTNAMELEN
]; /*%< XXX */
265 /* ip6_sa2str never fails */
266 scopelen
= ip6_sa2str((const struct sockaddr_in6
*)sa
,
267 scopebuf
, sizeof(scopebuf
), flags
);
269 if (scopelen
+ 1 + numaddrlen
+ 1 > hostlen
)
272 /* construct <numeric-addr><delim><scopeid> */
273 memcpy(host
+ numaddrlen
+ 1, scopebuf
,
275 host
[numaddrlen
] = SCOPE_DELIMITER
;
276 host
[numaddrlen
+ 1 + scopelen
] = '\0';
283 #ifdef HAVE_SIN6_SCOPE_ID
286 ip6_sa2str(const struct sockaddr_in6
*sa6
, char *buf
,
287 size_t bufsiz
, int flags
)
289 #ifdef USE_IFNAMELINKID
290 unsigned int ifindex
= (unsigned int)sa6
->sin6_scope_id
;
291 const struct in6_addr
*a6
= &sa6
->sin6_addr
;
295 #ifdef NI_NUMERICSCOPE
296 if (flags
& NI_NUMERICSCOPE
) {
297 sprintf(tmp
, "%u", sa6
->sin6_scope_id
);
299 strncpy(buf
, tmp
, bufsiz
- 1);
300 buf
[bufsiz
- 1] = '\0';
306 #ifdef USE_IFNAMELINKID
308 * For a link-local address, convert the index to an interface
309 * name, assuming a one-to-one mapping between links and interfaces.
310 * Note, however, that this assumption is stronger than the
311 * specification of the scoped address architecture; the
312 * specficication says that more than one interfaces can belong to
316 /* if_indextoname() does not take buffer size. not a good api... */
317 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
)) &&
318 bufsiz
>= IF_NAMESIZE
) {
319 char *p
= if_indextoname(ifindex
, buf
);
327 sprintf(tmp
, "%u", sa6
->sin6_scope_id
);
329 strncpy(buf
, tmp
, bufsiz
- 1);
330 buf
[bufsiz
- 1] = '\0';