* Constify return type of gai_strerror() as per bwg2001-009
[dragonfly.git] / lib / libc / net / getnameinfo.c
blobf4b2c47d3d620a0095faf85f3d8a987429a430e7
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 $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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 <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <arpa/nameser.h>
54 #include <netdb.h>
55 #include <resolv.h>
56 #include <string.h>
57 #include <stddef.h>
58 #include <errno.h>
60 static const struct afd {
61 int a_af;
62 int a_addrlen;
63 int a_socklen;
64 int a_off;
65 } afdl [] = {
66 #ifdef INET6
67 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
68 offsetof(struct sockaddr_in6, sin6_addr)},
69 #endif
70 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
71 offsetof(struct sockaddr_in, sin_addr)},
72 {0, 0, 0},
75 struct sockinet {
76 u_char si_len;
77 u_char si_family;
78 u_short si_port;
81 #ifdef INET6
82 static int ip6_parsenumeric (const struct sockaddr *, const char *, char *,
83 size_t, int);
84 static int ip6_sa2str (const struct sockaddr_in6 *, char *, size_t, int);
85 #endif
87 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;
92 struct servent *sp;
93 struct hostent *hp;
94 u_short port;
95 int family, i;
96 const char *addr;
97 u_int32_t v4a;
98 int h_error;
99 char numserv[512];
100 char numaddr[512];
102 if (sa == NULL)
103 return EAI_FAIL;
105 if (sa->sa_len != salen)
106 return EAI_FAIL;
108 family = sa->sa_family;
109 for (i = 0; afdl[i].a_af; i++)
110 if (afdl[i].a_af == family) {
111 afd = &afdl[i];
112 goto found;
114 return EAI_FAMILY;
116 found:
117 if (salen != afd->a_socklen)
118 return EAI_FAIL;
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.
131 } else {
132 if (flags & NI_NUMERICSERV)
133 sp = NULL;
134 else {
135 sp = getservbyport(port,
136 (flags & NI_DGRAM) ? "udp" : "tcp");
138 if (sp) {
139 if (strlen(sp->s_name) + 1 > servlen)
140 return EAI_MEMORY;
141 strlcpy(serv, sp->s_name, servlen);
142 } else {
143 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
144 if (strlen(numserv) + 1 > servlen)
145 return EAI_MEMORY;
146 strlcpy(serv, numserv, servlen);
150 switch (sa->sa_family) {
151 case AF_INET:
152 v4a = (u_int32_t)
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;
157 if (v4a == 0)
158 flags |= NI_NUMERICHOST;
159 break;
160 #ifdef INET6
161 case AF_INET6:
163 const struct sockaddr_in6 *sin6;
164 sin6 = (const struct sockaddr_in6 *)sa;
165 switch (sin6->sin6_addr.s6_addr[0]) {
166 case 0x00:
167 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
169 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
171 else
172 flags |= NI_NUMERICHOST;
173 break;
174 default:
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;
180 break;
183 break;
184 #endif
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) {
194 int numaddrlen;
196 /* NUMERICHOST and NAMEREQD conflicts with each other */
197 if (flags & NI_NAMEREQD)
198 return EAI_NONAME;
200 switch(afd->a_af) {
201 #ifdef INET6
202 case AF_INET6:
204 int error;
206 if ((error = ip6_parsenumeric(sa, addr, host,
207 hostlen, flags)) != 0)
208 return(error);
209 break;
211 #endif
212 default:
213 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
214 == NULL)
215 return EAI_SYSTEM;
216 numaddrlen = strlen(numaddr);
217 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
218 return EAI_MEMORY;
219 strlcpy(host, numaddr, hostlen);
220 break;
222 } else {
223 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
225 if (hp) {
226 #if 0
228 * commented out, since "for local host" is not
229 * implemented here - see RFC2553 p30
231 if (flags & NI_NOFQDN) {
232 char *p;
233 p = strchr(hp->h_name, '.');
234 if (p)
235 *p = '\0';
237 #endif
238 if (strlen(hp->h_name) + 1 > hostlen) {
239 freehostent(hp);
240 return EAI_MEMORY;
242 strlcpy(host, hp->h_name, hostlen);
243 freehostent(hp);
244 } else {
245 if (flags & NI_NAMEREQD)
246 return EAI_NONAME;
247 switch(afd->a_af) {
248 #ifdef INET6
249 case AF_INET6:
251 int error;
253 if ((error = ip6_parsenumeric(sa, addr, host,
254 hostlen,
255 flags)) != 0)
256 return(error);
257 break;
259 #endif
260 default:
261 if (inet_ntop(afd->a_af, addr, host,
262 hostlen) == NULL)
263 return EAI_SYSTEM;
264 break;
268 return(0);
271 #ifdef INET6
272 static int
273 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
274 size_t hostlen, int flags)
276 int numaddrlen;
277 char numaddr[512];
279 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
280 return EAI_SYSTEM;
282 numaddrlen = strlen(numaddr);
283 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
284 return EAI_OVERFLOW;
285 strlcpy(host, numaddr, hostlen);
287 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
288 char zonebuf[MAXHOSTNAMELEN];
289 int zonelen;
291 zonelen = ip6_sa2str(
292 (const struct sockaddr_in6 *)(const void *)sa,
293 zonebuf, sizeof(zonebuf), flags);
294 if (zonelen < 0)
295 return EAI_OVERFLOW;
296 if (zonelen + 1 + numaddrlen + 1 > hostlen)
297 return EAI_OVERFLOW;
299 /* construct <numeric-addr><delim><zoneid> */
300 memcpy(host + numaddrlen + 1, zonebuf,
301 (size_t)zonelen);
302 host[numaddrlen] = SCOPE_DELIMITER;
303 host[numaddrlen + 1 + zonelen] = '\0';
306 return 0;
309 /* ARGSUSED */
310 static int
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;
315 int n;
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)
324 return -1;
325 else
326 return n;
328 #endif
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);
334 if (p) {
335 return(strlen(p));
339 /* last resort */
340 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
341 if (n < 0 || n >= bufsiz)
342 return -1;
343 else
344 return n;
346 #endif /* INET6 */