1 /* $FreeBSD: src/lib/libc/net/getnameinfo.c,v 1.5 2000/07/05 05:09:17 itojun Exp $ */
2 /* $KAME: getnameinfo.c,v 1.43 2000/06/12 04:27:03 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Issues to be discussed:
35 * - Thread safe-ness must be checked
36 * - Return values. There seems to be no standard for return value (RFC2553)
37 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
38 * - RFC2553 says that we should raise error on short buffer. X/Open says
39 * we need to truncate the result. We obey RFC2553 (and X/Open should be
41 * - What is "local" in NI_FQDN?
42 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
43 * - (KAME extension) NI_WITHSCOPEID when called with global address,
44 * and sin6_scope_id filled
47 #include <sys/types.h>
48 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <arpa/nameser.h>
71 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
72 offsetof(struct sockaddr_in6
, sin6_addr
)},
74 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
75 offsetof(struct sockaddr_in
, sin_addr
)},
87 static int ip6_parsenumeric
__P((const struct sockaddr
*, const char *, char *,
89 static int ip6_sa2str
__P((const struct sockaddr_in6
*, char *, size_t, int));
92 #define ENI_NOSOCKET EAI_FAIL /*XXX*/
93 #define ENI_NOSERVNAME EAI_NONAME
94 #define ENI_NOHOSTNAME EAI_NONAME
95 #define ENI_MEMORY EAI_MEMORY
96 #define ENI_SYSTEM EAI_SYSTEM
97 #define ENI_FAMILY EAI_FAMILY
98 #define ENI_SALEN EAI_FAMILY
101 getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
102 const struct sockaddr
*sa
;
124 if (sa
->sa_len
!= salen
)
128 family
= sa
->sa_family
;
129 for (i
= 0; afdl
[i
].a_af
; i
++)
130 if (afdl
[i
].a_af
== family
) {
137 if (salen
!= afd
->a_socklen
)
140 /* network byte order */
141 port
= ((const struct sockinet
*)sa
)->si_port
;
142 addr
= (const char *)sa
+ afd
->a_off
;
144 if (serv
== NULL
|| servlen
== 0) {
146 * do nothing in this case.
147 * in case you are wondering if "&&" is more correct than
148 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
149 * means that the caller does not want the result.
152 if (flags
& NI_NUMERICSERV
)
155 sp
= getservbyport(port
,
156 (flags
& NI_DGRAM
) ?
"udp" : "tcp");
159 if (strlen(sp
->s_name
) > servlen
)
161 strcpy(serv
, sp
->s_name
);
163 snprintf(numserv
, sizeof(numserv
), "%d", ntohs(port
));
164 if (strlen(numserv
) > servlen
)
166 strcpy(serv
, numserv
);
170 switch (sa
->sa_family
) {
173 ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
174 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
175 flags
|= NI_NUMERICHOST
;
176 v4a
>>= IN_CLASSA_NSHIFT
;
178 flags
|= NI_NUMERICHOST
;
183 const struct sockaddr_in6
*sin6
;
184 sin6
= (const struct sockaddr_in6
*)sa
;
185 switch (sin6
->sin6_addr
.s6_addr
[0]) {
187 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
189 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
192 flags
|= NI_NUMERICHOST
;
195 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
196 flags
|= NI_NUMERICHOST
;
198 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
199 flags
|= NI_NUMERICHOST
;
206 if (host
== NULL
|| hostlen
== 0) {
208 * do nothing in this case.
209 * in case you are wondering if "&&" is more correct than
210 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
211 * means that the caller does not want the result.
213 } else if (flags
& NI_NUMERICHOST
) {
216 /* NUMERICHOST and NAMEREQD conflicts with each other */
217 if (flags
& NI_NAMEREQD
)
218 return ENI_NOHOSTNAME
;
226 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
227 hostlen
, flags
)) != 0)
233 if (inet_ntop(afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
236 numaddrlen
= strlen(numaddr
);
237 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
239 strcpy(host
, numaddr
);
245 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
247 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
253 * commented out, since "for local host" is not
254 * implemented here - see RFC2553 p30
256 if (flags
& NI_NOFQDN
) {
258 p
= strchr(hp
->h_name
, '.');
263 if (strlen(hp
->h_name
) > hostlen
) {
269 strcpy(host
, hp
->h_name
);
274 if (flags
& NI_NAMEREQD
)
275 return ENI_NOHOSTNAME
;
282 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
290 if (inet_ntop(afd
->a_af
, addr
, host
,
302 ip6_parsenumeric(sa
, addr
, host
, hostlen
, flags
)
303 const struct sockaddr
*sa
;
312 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
))
316 numaddrlen
= strlen(numaddr
);
317 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
319 strcpy(host
, numaddr
);
321 #ifdef NI_WITHSCOPEID
323 #ifdef DONT_OPAQUE_SCOPEID
324 (IN6_IS_ADDR_LINKLOCAL((struct in6_addr
*)addr
) ||
325 IN6_IS_ADDR_MULTICAST((struct in6_addr
*)addr
)) &&
327 ((const struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
328 #ifndef ALWAYS_WITHSCOPE
329 if (flags
& NI_WITHSCOPEID
)
330 #endif /* !ALWAYS_WITHSCOPE */
332 char scopebuf
[MAXHOSTNAMELEN
];
335 /* ip6_sa2str never fails */
336 scopelen
= ip6_sa2str((const struct sockaddr_in6
*)sa
,
337 scopebuf
, sizeof(scopebuf
),
339 if (scopelen
+ 1 + numaddrlen
+ 1 > hostlen
)
342 * construct <numeric-addr><delim><scopeid>
344 memcpy(host
+ numaddrlen
+ 1, scopebuf
,
346 host
[numaddrlen
] = SCOPE_DELIMITER
;
347 host
[numaddrlen
+ 1 + scopelen
] = '\0';
350 #endif /* NI_WITHSCOPEID */
357 ip6_sa2str(sa6
, buf
, bufsiz
, flags
)
358 const struct sockaddr_in6
*sa6
;
363 unsigned int ifindex
= (unsigned int)sa6
->sin6_scope_id
;
364 const struct in6_addr
*a6
= &sa6
->sin6_addr
;
366 #ifdef NI_NUMERICSCOPE
367 if (flags
& NI_NUMERICSCOPE
) {
368 return(snprintf(buf
, bufsiz
, "%d", sa6
->sin6_scope_id
));
372 /* if_indextoname() does not take buffer size. not a good api... */
373 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
)) &&
374 bufsiz
>= IF_NAMESIZE
) {
375 char *p
= if_indextoname(ifindex
, buf
);
382 return(snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
));