1 /* $FreeBSD: src/lib/libc/net/getnameinfo.c,v 1.4.2.5 2002/07/31 10:11:09 ume Exp $ */
2 /* $DragonFly: src/lib/libc/net/getnameinfo.c,v 1.5 2008/10/04 22:09:17 swildner Exp $ */
3 /* $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Issues to be discussed:
36 * - Thread safe-ness must be checked
37 * - RFC2553 says that we should raise error on short buffer. X/Open says
38 * we need to truncate the result. We obey RFC2553 (and X/Open should be
39 * modified). ipngwg rough consensus seems to follow RFC2553.
40 * - What is "local" in NI_FQDN?
41 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
42 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
43 * sin6_scope_id is filled - standardization status?
44 * XXX breaks backward compat for code that expects no scopeid.
48 #include <sys/types.h>
49 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <arpa/nameser.h>
60 static const struct afd
{
67 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
68 offsetof(struct sockaddr_in6
, sin6_addr
)},
70 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
71 offsetof(struct sockaddr_in
, sin_addr
)},
82 static int ip6_parsenumeric (const struct sockaddr
*, const char *, char *,
84 static int ip6_sa2str (const struct sockaddr_in6
*, char *, size_t, int);
88 getnameinfo(const struct sockaddr
*sa
, socklen_t salen
, char *host
,
89 size_t hostlen
, char *serv
, size_t servlen
, int flags
)
91 const struct afd
*afd
;
105 if (sa
->sa_len
!= salen
)
108 family
= sa
->sa_family
;
109 for (i
= 0; afdl
[i
].a_af
; i
++)
110 if (afdl
[i
].a_af
== family
) {
117 if (salen
!= afd
->a_socklen
)
120 /* network byte order */
121 port
= ((const struct sockinet
*)sa
)->si_port
;
122 addr
= (const char *)sa
+ afd
->a_off
;
124 if (serv
== NULL
|| servlen
== 0) {
126 * do nothing in this case.
127 * in case you are wondering if "&&" is more correct than
128 * "||" here: rfc2553bis-03 says that serv == NULL OR
129 * servlen == 0 means that the caller does not want the result.
132 if (flags
& NI_NUMERICSERV
)
135 sp
= getservbyport(port
,
136 (flags
& NI_DGRAM
) ? "udp" : "tcp");
139 if (strlen(sp
->s_name
) + 1 > servlen
)
141 strlcpy(serv
, sp
->s_name
, servlen
);
143 snprintf(numserv
, sizeof(numserv
), "%u", ntohs(port
));
144 if (strlen(numserv
) + 1 > servlen
)
146 strlcpy(serv
, numserv
, servlen
);
150 switch (sa
->sa_family
) {
153 ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
154 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
155 flags
|= NI_NUMERICHOST
;
156 v4a
>>= IN_CLASSA_NSHIFT
;
158 flags
|= NI_NUMERICHOST
;
163 const struct sockaddr_in6
*sin6
;
164 sin6
= (const struct sockaddr_in6
*)sa
;
165 switch (sin6
->sin6_addr
.s6_addr
[0]) {
167 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
169 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
172 flags
|= NI_NUMERICHOST
;
175 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
176 flags
|= NI_NUMERICHOST
;
178 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
179 flags
|= NI_NUMERICHOST
;
186 if (host
== NULL
|| hostlen
== 0) {
188 * do nothing in this case.
189 * in case you are wondering if "&&" is more correct than
190 * "||" here: rfc2553bis-03 says that host == NULL or
191 * hostlen == 0 means that the caller does not want the result.
193 } else if (flags
& NI_NUMERICHOST
) {
196 /* NUMERICHOST and NAMEREQD conflicts with each other */
197 if (flags
& NI_NAMEREQD
)
206 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
207 hostlen
, flags
)) != 0)
213 if (inet_ntop(afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
216 numaddrlen
= strlen(numaddr
);
217 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
219 strlcpy(host
, numaddr
, hostlen
);
223 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
228 * commented out, since "for local host" is not
229 * implemented here - see RFC2553 p30
231 if (flags
& NI_NOFQDN
) {
233 p
= strchr(hp
->h_name
, '.');
238 if (strlen(hp
->h_name
) + 1 > hostlen
) {
242 strlcpy(host
, hp
->h_name
, hostlen
);
245 if (flags
& NI_NAMEREQD
)
253 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
261 if (inet_ntop(afd
->a_af
, addr
, host
,
273 ip6_parsenumeric(const struct sockaddr
*sa
, const char *addr
, char *host
,
274 size_t hostlen
, int flags
)
279 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
)) == NULL
)
282 numaddrlen
= strlen(numaddr
);
283 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
285 strlcpy(host
, numaddr
, hostlen
);
287 if (((const struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
288 char zonebuf
[MAXHOSTNAMELEN
];
291 zonelen
= ip6_sa2str(
292 (const struct sockaddr_in6
*)(const void *)sa
,
293 zonebuf
, sizeof(zonebuf
), flags
);
296 if (zonelen
+ 1 + numaddrlen
+ 1 > hostlen
)
299 /* construct <numeric-addr><delim><zoneid> */
300 memcpy(host
+ numaddrlen
+ 1, zonebuf
,
302 host
[numaddrlen
] = SCOPE_DELIMITER
;
303 host
[numaddrlen
+ 1 + zonelen
] = '\0';
311 ip6_sa2str(const struct sockaddr_in6
*sa6
, char *buf
, size_t bufsiz
, int flags
)
313 unsigned int ifindex
;
314 const struct in6_addr
*a6
;
317 ifindex
= (unsigned int)sa6
->sin6_scope_id
;
318 a6
= &sa6
->sin6_addr
;
320 #ifdef NI_NUMERICSCOPE
321 if ((flags
& NI_NUMERICSCOPE
) != 0) {
322 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
323 if (n
< 0 || n
>= bufsiz
)
330 /* if_indextoname() does not take buffer size. not a good api... */
331 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
) ||
332 IN6_IS_ADDR_MC_NODELOCAL(a6
)) && bufsiz
>= IF_NAMESIZE
) {
333 char *p
= if_indextoname(ifindex
, buf
);
340 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
341 if (n
< 0 || n
>= bufsiz
)