Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / getnameinfo.c
blob876d810b45f5563ea5b764b783ca1e095f0b90bc
1 /* $NetBSD$ */
3 /*
4 * Issues to be discussed:
5 * - Thread safe-ness must be checked
6 */
8 #if ( defined(__linux__) || defined(__linux) || defined(LINUX) )
9 #ifndef IF_NAMESIZE
10 # ifdef IFNAMSIZ
11 # define IF_NAMESIZE IFNAMSIZ
12 # else
13 # define IF_NAMESIZE 16
14 # endif
15 #endif
16 #endif
19 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * This product includes software developed by WIDE Project and
33 * its contributors.
34 * 4. Neither the name of the project nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
51 #include <port_before.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/nameser.h>
58 #include <arpa/inet.h>
59 #include <net/if.h>
61 #include <netdb.h>
62 #include <resolv.h>
63 #include <string.h>
64 #include <stddef.h>
66 #include <port_after.h>
68 /*%
69 * Note that a_off will be dynamically adjusted so that to be consistent
70 * with the definition of sockaddr_in{,6}.
71 * The value presented below is just a guess.
73 static struct afd {
74 int a_af;
75 int a_addrlen;
76 size_t a_socklen;
77 int a_off;
78 } afdl [] = {
79 /* first entry is linked last... */
80 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
81 offsetof(struct sockaddr_in, sin_addr)},
82 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
83 offsetof(struct sockaddr_in6, sin6_addr)},
84 {0, 0, 0, 0},
87 struct sockinet {
88 #ifdef HAVE_SA_LEN
89 u_char si_len;
90 #endif
91 u_char si_family;
92 u_short si_port;
95 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
96 size_t, int));
97 #ifdef HAVE_SIN6_SCOPE_ID
98 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
99 #endif
102 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
103 const struct sockaddr *sa;
104 size_t salen;
105 char *host;
106 size_t hostlen;
107 char *serv;
108 size_t servlen;
109 int flags;
111 struct afd *afd;
112 struct servent *sp;
113 struct hostent *hp;
114 u_short port;
115 #ifdef HAVE_SA_LEN
116 size_t len;
117 #endif
118 int family, i;
119 const char *addr;
120 char *p;
121 char numserv[512];
122 char numaddr[512];
123 const struct sockaddr_in6 *sin6;
125 if (sa == NULL)
126 return EAI_FAIL;
128 #ifdef HAVE_SA_LEN
129 len = sa->sa_len;
130 if (len != salen) return EAI_FAIL;
131 #endif
133 family = sa->sa_family;
134 for (i = 0; afdl[i].a_af; i++)
135 if (afdl[i].a_af == family) {
136 afd = &afdl[i];
137 goto found;
139 return EAI_FAMILY;
141 found:
142 if (salen != afd->a_socklen) return EAI_FAIL;
144 port = ((const struct sockinet *)sa)->si_port; /*%< network byte order */
145 addr = (const char *)sa + afd->a_off;
147 if (serv == NULL || servlen == 0U) {
149 * rfc2553bis says that serv == NULL or servlen == 0 means that
150 * the caller does not want the result.
152 } else if (flags & NI_NUMERICSERV) {
153 sprintf(numserv, "%d", ntohs(port));
154 if (strlen(numserv) > servlen)
155 return EAI_MEMORY;
156 strcpy(serv, numserv);
157 } else {
158 sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
159 if (sp) {
160 if (strlen(sp->s_name) + 1 > servlen)
161 return EAI_MEMORY;
162 strcpy(serv, sp->s_name);
163 } else
164 return EAI_NONAME;
167 switch (sa->sa_family) {
168 case AF_INET:
169 if (ntohl(*(const u_int32_t *)addr) >> IN_CLASSA_NSHIFT == 0)
170 flags |= NI_NUMERICHOST;
171 break;
172 case AF_INET6:
173 sin6 = (const struct sockaddr_in6 *)sa;
174 switch (sin6->sin6_addr.s6_addr[0]) {
175 case 0x00:
176 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
178 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
180 else
181 flags |= NI_NUMERICHOST;
182 break;
183 default:
184 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
185 flags |= NI_NUMERICHOST;
186 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
187 flags |= NI_NUMERICHOST;
188 break;
190 break;
192 if (host == NULL || hostlen == 0U) {
194 * rfc2553bis says that host == NULL or hostlen == 0 means that
195 * the caller does not want the result.
197 } else if (flags & NI_NUMERICHOST) {
198 goto numeric;
199 } else {
200 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
202 if (hp) {
203 if (flags & NI_NOFQDN) {
204 p = strchr(hp->h_name, '.');
205 if (p) *p = '\0';
207 if (strlen(hp->h_name) + 1 > hostlen)
208 return EAI_MEMORY;
209 strcpy(host, hp->h_name);
210 } else {
211 if (flags & NI_NAMEREQD)
212 return EAI_NONAME;
213 numeric:
214 switch(afd->a_af) {
215 case AF_INET6:
217 int error;
219 if ((error = ip6_parsenumeric(sa, addr, host,
220 hostlen,
221 flags)) != 0)
222 return(error);
223 break;
226 default:
227 if (inet_ntop(afd->a_af, addr, numaddr,
228 sizeof(numaddr)) == NULL)
229 return EAI_NONAME;
230 if (strlen(numaddr) + 1 > hostlen)
231 return EAI_MEMORY;
232 strcpy(host, numaddr);
236 return(0);
239 static int
240 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
241 size_t hostlen, int flags)
243 size_t numaddrlen;
244 char numaddr[512];
246 #ifndef HAVE_SIN6_SCOPE_ID
247 UNUSED(sa);
248 UNUSED(flags);
249 #endif
251 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
252 == NULL)
253 return EAI_SYSTEM;
255 numaddrlen = strlen(numaddr);
256 if (numaddrlen + 1 > hostlen) /*%< don't forget terminator */
257 return EAI_MEMORY;
258 strcpy(host, numaddr);
260 #ifdef HAVE_SIN6_SCOPE_ID
261 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
262 char scopebuf[MAXHOSTNAMELEN]; /*%< XXX */
263 int scopelen;
265 /* ip6_sa2str never fails */
266 scopelen = ip6_sa2str((const struct sockaddr_in6 *)sa,
267 scopebuf, sizeof(scopebuf), flags);
269 if (scopelen + 1 + numaddrlen + 1 > hostlen)
270 return EAI_MEMORY;
272 /* construct <numeric-addr><delim><scopeid> */
273 memcpy(host + numaddrlen + 1, scopebuf,
274 scopelen);
275 host[numaddrlen] = SCOPE_DELIMITER;
276 host[numaddrlen + 1 + scopelen] = '\0';
278 #endif
280 return 0;
283 #ifdef HAVE_SIN6_SCOPE_ID
284 /* ARGSUSED */
285 static int
286 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf,
287 size_t bufsiz, int flags)
289 #ifdef USE_IFNAMELINKID
290 unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
291 const struct in6_addr *a6 = &sa6->sin6_addr;
292 #endif
293 char tmp[64];
295 #ifdef NI_NUMERICSCOPE
296 if (flags & NI_NUMERICSCOPE) {
297 sprintf(tmp, "%u", sa6->sin6_scope_id);
298 if (bufsiz != 0U) {
299 strncpy(buf, tmp, bufsiz - 1);
300 buf[bufsiz - 1] = '\0';
302 return(strlen(tmp));
304 #endif
306 #ifdef USE_IFNAMELINKID
308 * For a link-local address, convert the index to an interface
309 * name, assuming a one-to-one mapping between links and interfaces.
310 * Note, however, that this assumption is stronger than the
311 * specification of the scoped address architecture; the
312 * specficication says that more than one interfaces can belong to
313 * a single link.
316 /* if_indextoname() does not take buffer size. not a good api... */
317 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
318 bufsiz >= IF_NAMESIZE) {
319 char *p = if_indextoname(ifindex, buf);
320 if (p) {
321 return(strlen(p));
324 #endif
326 /* last resort */
327 sprintf(tmp, "%u", sa6->sin6_scope_id);
328 if (bufsiz != 0U) {
329 strncpy(buf, tmp, bufsiz - 1);
330 buf[bufsiz - 1] = '\0';
332 return(strlen(tmp));
334 #endif
336 /*! \file */