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