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.2 2003/06/17 04:26:44 dillon 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 __P((const struct sockaddr *, const char *, char *,
84 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
88 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
89 const struct sockaddr *sa;
97 const struct afd *afd;
111 if (sa->sa_len != salen)
114 family = sa->sa_family;
115 for (i = 0; afdl[i].a_af; i++)
116 if (afdl[i].a_af == family) {
123 if (salen != afd->a_socklen)
126 /* network byte order */
127 port = ((const struct sockinet *)sa)->si_port;
128 addr = (const char *)sa + afd->a_off;
130 if (serv == NULL || servlen == 0) {
132 * do nothing in this case.
133 * in case you are wondering if "&&" is more correct than
134 * "||" here: rfc2553bis-03 says that serv == NULL OR
135 * servlen == 0 means that the caller does not want the result.
138 if (flags & NI_NUMERICSERV)
141 sp = getservbyport(port,
142 (flags & NI_DGRAM) ? "udp" : "tcp");
145 if (strlen(sp->s_name) + 1 > servlen)
147 strlcpy(serv, sp->s_name, servlen);
149 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
150 if (strlen(numserv) + 1 > servlen)
152 strlcpy(serv, numserv, servlen);
156 switch (sa->sa_family) {
159 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
160 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
161 flags |= NI_NUMERICHOST;
162 v4a >>= IN_CLASSA_NSHIFT;
164 flags |= NI_NUMERICHOST;
169 const struct sockaddr_in6 *sin6;
170 sin6 = (const struct sockaddr_in6 *)sa;
171 switch (sin6->sin6_addr.s6_addr[0]) {
173 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
175 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
178 flags |= NI_NUMERICHOST;
181 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
182 flags |= NI_NUMERICHOST;
184 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
185 flags |= NI_NUMERICHOST;
192 if (host == NULL || hostlen == 0) {
194 * do nothing in this case.
195 * in case you are wondering if "&&" is more correct than
196 * "||" here: rfc2553bis-03 says that host == NULL or
197 * hostlen == 0 means that the caller does not want the result.
199 } else if (flags & NI_NUMERICHOST) {
202 /* NUMERICHOST and NAMEREQD conflicts with each other */
203 if (flags & NI_NAMEREQD)
212 if ((error = ip6_parsenumeric(sa, addr, host,
213 hostlen, flags)) != 0)
219 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
222 numaddrlen = strlen(numaddr);
223 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
225 strlcpy(host, numaddr, hostlen);
229 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
234 * commented out, since "for local host" is not
235 * implemented here - see RFC2553 p30
237 if (flags & NI_NOFQDN) {
239 p = strchr(hp->h_name, '.');
244 if (strlen(hp->h_name) + 1 > hostlen) {
248 strlcpy(host, hp->h_name, hostlen);
251 if (flags & NI_NAMEREQD)
259 if ((error = ip6_parsenumeric(sa, addr, host,
267 if (inet_ntop(afd->a_af, addr, host,
279 ip6_parsenumeric(sa, addr, host, hostlen, flags)
280 const struct sockaddr *sa;
289 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
292 numaddrlen = strlen(numaddr);
293 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
295 strlcpy(host, numaddr, hostlen);
297 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
298 char zonebuf[MAXHOSTNAMELEN];
301 zonelen = ip6_sa2str(
302 (const struct sockaddr_in6 *)(const void *)sa,
303 zonebuf, sizeof(zonebuf), flags);
306 if (zonelen + 1 + numaddrlen + 1 > hostlen)
309 /* construct <numeric-addr><delim><zoneid> */
310 memcpy(host + numaddrlen + 1, zonebuf,
312 host[numaddrlen] = SCOPE_DELIMITER;
313 host[numaddrlen + 1 + zonelen] = '\0';
321 ip6_sa2str(sa6, buf, bufsiz, flags)
322 const struct sockaddr_in6 *sa6;
327 unsigned int ifindex;
328 const struct in6_addr *a6;
331 ifindex = (unsigned int)sa6->sin6_scope_id;
332 a6 = &sa6->sin6_addr;
334 #ifdef NI_NUMERICSCOPE
335 if ((flags & NI_NUMERICSCOPE) != 0) {
336 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
337 if (n < 0 || n >= bufsiz)
344 /* if_indextoname() does not take buffer size. not a good api... */
345 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
346 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
347 char *p = if_indextoname(ifindex, buf);
354 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
355 if (n < 0 || n >= bufsiz)