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