libc/net: Add NI_NUMERICSCOPE flag for getnameinfo().
[dragonfly.git] / lib / libc / net / getnameinfo.c
bloba4ae67708a52048454b90419e45b226cb758ef3b
1 /* $FreeBSD: src/lib/libc/net/getnameinfo.c,v 1.20 2007/02/28 21:18:38 bms Exp $ */
2 /* $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * Copyright (c) 2000 Ben Harris.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
31 * SUCH DAMAGE.
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.
45 * beware on merge.
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <arpa/nameser.h>
56 #include <netdb.h>
57 #include <resolv.h>
58 #include <string.h>
59 #include <stddef.h>
60 #include <errno.h>
62 static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
63 size_t, char *, size_t, int);
64 #ifdef INET6
65 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
66 size_t, int);
67 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
68 #endif
69 static int getnameinfo_link(const struct sockaddr *, socklen_t, char *,
70 size_t, char *, size_t, int);
71 static int hexname(const u_int8_t *, size_t, char *, size_t);
73 int
74 getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host,
75 size_t hostlen, char *serv, size_t servlen, int flags)
78 switch (sa->sa_family) {
79 case AF_INET:
80 #ifdef INET6
81 case AF_INET6:
82 #endif
83 return getnameinfo_inet(sa, salen, host, hostlen, serv,
84 servlen, flags);
85 case AF_LINK:
86 return getnameinfo_link(sa, salen, host, hostlen, serv,
87 servlen, flags);
88 default:
89 return EAI_FAMILY;
93 static const struct afd {
94 int a_af;
95 size_t a_addrlen;
96 socklen_t a_socklen;
97 int a_off;
98 } afdl [] = {
99 #ifdef INET6
100 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
101 offsetof(struct sockaddr_in6, sin6_addr)},
102 #endif
103 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
104 offsetof(struct sockaddr_in, sin_addr)},
105 { .a_af = 0 },
108 struct sockinet {
109 u_char si_len;
110 u_char si_family;
111 u_short si_port;
114 static int
115 getnameinfo_inet(const struct sockaddr *sa, socklen_t salen, char *host,
116 size_t hostlen, char *serv, size_t servlen, int flags)
118 const struct afd *afd;
119 struct servent *sp;
120 struct hostent *hp;
121 u_short port;
122 int family, i;
123 const char *addr;
124 u_int32_t v4a;
125 int h_error;
126 char numserv[512];
127 char numaddr[512];
129 if (sa == NULL)
130 return EAI_FAIL;
132 family = sa->sa_family;
133 for (i = 0; afdl[i].a_af; i++)
134 if (afdl[i].a_af == family) {
135 afd = &afdl[i];
136 goto found;
138 return EAI_FAMILY;
140 found:
141 if (salen != afd->a_socklen)
142 return EAI_FAIL;
144 /* network byte order */
145 port = ((const struct sockinet *)sa)->si_port;
146 addr = (const char *)sa + afd->a_off;
148 if (serv == NULL || servlen == 0) {
150 * do nothing in this case.
151 * in case you are wondering if "&&" is more correct than
152 * "||" here: rfc2553bis-03 says that serv == NULL OR
153 * servlen == 0 means that the caller does not want the result.
155 } else {
156 if (flags & NI_NUMERICSERV)
157 sp = NULL;
158 else {
159 sp = getservbyport(port,
160 (flags & NI_DGRAM) ? "udp" : "tcp");
162 if (sp) {
163 if (strlen(sp->s_name) + 1 > servlen)
164 return EAI_MEMORY;
165 strlcpy(serv, sp->s_name, servlen);
166 } else {
167 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
168 if (strlen(numserv) + 1 > servlen)
169 return EAI_MEMORY;
170 strlcpy(serv, numserv, servlen);
174 switch (sa->sa_family) {
175 case AF_INET:
176 v4a = (u_int32_t)
177 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
178 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
179 flags |= NI_NUMERICHOST;
180 v4a >>= IN_CLASSA_NSHIFT;
181 if (v4a == 0)
182 flags |= NI_NUMERICHOST;
183 break;
184 #ifdef INET6
185 case AF_INET6:
187 const struct sockaddr_in6 *sin6;
188 sin6 = (const struct sockaddr_in6 *)sa;
189 switch (sin6->sin6_addr.s6_addr[0]) {
190 case 0x00:
191 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
193 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
195 else
196 flags |= NI_NUMERICHOST;
197 break;
198 default:
199 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
200 flags |= NI_NUMERICHOST;
202 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
203 flags |= NI_NUMERICHOST;
204 break;
207 break;
208 #endif
210 if (host == NULL || hostlen == 0) {
212 * do nothing in this case.
213 * in case you are wondering if "&&" is more correct than
214 * "||" here: rfc2553bis-03 says that host == NULL or
215 * hostlen == 0 means that the caller does not want the result.
217 } else if (flags & NI_NUMERICHOST) {
218 size_t numaddrlen;
220 /* NUMERICHOST and NAMEREQD conflicts with each other */
221 if (flags & NI_NAMEREQD)
222 return EAI_NONAME;
224 switch(afd->a_af) {
225 #ifdef INET6
226 case AF_INET6:
228 int error;
230 if ((error = ip6_parsenumeric(sa, addr, host,
231 hostlen, flags)) != 0)
232 return(error);
233 break;
235 #endif
236 default:
237 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
238 == NULL)
239 return EAI_SYSTEM;
240 numaddrlen = strlen(numaddr);
241 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
242 return EAI_MEMORY;
243 strlcpy(host, numaddr, hostlen);
244 break;
246 } else {
247 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
249 if (hp) {
250 #if 0
252 * commented out, since "for local host" is not
253 * implemented here - see RFC2553 p30
255 if (flags & NI_NOFQDN) {
256 char *p;
257 p = strchr(hp->h_name, '.');
258 if (p)
259 *p = '\0';
261 #endif
262 if (strlen(hp->h_name) + 1 > hostlen) {
263 freehostent(hp);
264 return EAI_MEMORY;
266 strlcpy(host, hp->h_name, hostlen);
267 freehostent(hp);
268 } else {
269 if (flags & NI_NAMEREQD)
270 return EAI_NONAME;
271 switch(afd->a_af) {
272 #ifdef INET6
273 case AF_INET6:
275 int error;
277 if ((error = ip6_parsenumeric(sa, addr, host,
278 hostlen,
279 flags)) != 0)
280 return(error);
281 break;
283 #endif
284 default:
285 if (inet_ntop(afd->a_af, addr, host,
286 hostlen) == NULL)
287 return EAI_SYSTEM;
288 break;
292 return(0);
295 #ifdef INET6
296 static int
297 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
298 size_t hostlen, int flags)
300 size_t numaddrlen;
301 char numaddr[512];
303 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
304 return EAI_SYSTEM;
306 numaddrlen = strlen(numaddr);
307 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
308 return EAI_OVERFLOW;
309 strlcpy(host, numaddr, hostlen);
311 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
312 char zonebuf[MAXHOSTNAMELEN];
313 int zonelen;
315 zonelen = ip6_sa2str(
316 (const struct sockaddr_in6 *)(const void *)sa,
317 zonebuf, sizeof(zonebuf), flags);
318 if (zonelen < 0)
319 return EAI_OVERFLOW;
320 if (zonelen + 1 + numaddrlen + 1 > hostlen)
321 return EAI_OVERFLOW;
323 /* construct <numeric-addr><delim><zoneid> */
324 memcpy(host + numaddrlen + 1, zonebuf,
325 (size_t)zonelen);
326 host[numaddrlen] = SCOPE_DELIMITER;
327 host[numaddrlen + 1 + zonelen] = '\0';
330 return 0;
333 /* ARGSUSED */
334 static int
335 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz,
336 int flags __unused)
338 unsigned int ifindex;
339 const struct in6_addr *a6;
340 int n;
342 ifindex = (unsigned int)sa6->sin6_scope_id;
343 a6 = &sa6->sin6_addr;
345 if ((flags & NI_NUMERICSCOPE) != 0) {
346 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
347 if (n < 0 || n >= bufsiz)
348 return -1;
349 else
350 return n;
353 /* if_indextoname() does not take buffer size. not a good api... */
354 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
355 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
356 char *p = if_indextoname(ifindex, buf);
357 if (p) {
358 return(strlen(p));
362 /* last resort */
363 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
364 if (n < 0 || (size_t)n >= bufsiz)
365 return -1;
366 else
367 return n;
369 #endif /* INET6 */
372 * getnameinfo_link():
373 * Format a link-layer address into a printable format, paying attention to
374 * the interface type.
376 /* ARGSUSED */
377 static int
378 getnameinfo_link(const struct sockaddr *sa, socklen_t salen __unused,
379 char *host, size_t hostlen, char *serv, size_t servlen,
380 int flags __unused)
382 const struct sockaddr_dl *sdl =
383 (const struct sockaddr_dl *)(const void *)sa;
384 int n;
386 if (serv != NULL && servlen > 0)
387 *serv = '\0';
389 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
390 n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
391 if (n > hostlen) {
392 *host = '\0';
393 return EAI_MEMORY;
395 return 0;
398 switch (sdl->sdl_type) {
400 * The following have zero-length addresses.
401 * IFT_FAITH (net/faith/if_faith.c)
402 * IFT_GIF (net/gif/if_gif.c)
403 * IFT_LOOP (net/if_loop.c, net/disc/if_disc.c)
404 * IFT_PPP (net/ppp/if_ppp.c, net/sppp/if_spppsubr.c)
405 * IFT_SLIP (net/sl/if_sl.c)
406 * IFT_STF (net/stf/if_stf.c)
407 * IFT_L2VLAN (net/vlan/if_vlan.c)
408 * IFT_BRIDGE (net/bridge/if_bridge.c)
411 * The following use IPv4 addresses as link-layer addresses:
412 * IFT_OTHER (net/gre/if_gre.c)
413 * IFT_OTHER (netproto/ipsec/xform_ipip.c)
415 /* default below is believed correct for all these. */
416 case IFT_ARCNET:
417 case IFT_ETHER:
418 case IFT_FDDI:
419 case IFT_HIPPI:
420 case IFT_ISO88025:
421 default:
422 return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
423 host, hostlen);
427 static int
428 hexname(const u_int8_t *cp, size_t len, char *host, size_t hostlen)
430 int i, n;
431 char *outp = host;
433 *outp = '\0';
434 for (i = 0; i < len; i++) {
435 n = snprintf(outp, hostlen, "%s%02x",
436 i ? ":" : "", cp[i]);
437 if (n < 0 || n >= hostlen) {
438 *host = '\0';
439 return EAI_MEMORY;
441 outp += n;
442 hostlen -= n;
444 return 0;