1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * fake library for ssh
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * This file includes getaddrinfo(), freeaddrinfo() and gai_strerror().
5*0Sstevel@tonic-gate  * These funtions are defined in rfc2133.
6*0Sstevel@tonic-gate  *
7*0Sstevel@tonic-gate  * But these functions are not implemented correctly. The minimum subset
8*0Sstevel@tonic-gate  * is implemented for ssh use only. For exapmle, this routine assumes
9*0Sstevel@tonic-gate  * that ai_family is AF_INET. Don't use it for another purpose.
10*0Sstevel@tonic-gate  */
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include "includes.h"
13*0Sstevel@tonic-gate #include "ssh.h"
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate RCSID("$Id: fake-getaddrinfo.c,v 1.2 2001/02/09 01:55:36 djm Exp $");
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate #ifndef HAVE_GAI_STRERROR
gai_strerror(int ecode)20*0Sstevel@tonic-gate char *gai_strerror(int ecode)
21*0Sstevel@tonic-gate {
22*0Sstevel@tonic-gate 	switch (ecode) {
23*0Sstevel@tonic-gate 		case EAI_NODATA:
24*0Sstevel@tonic-gate 			return "no address associated with hostname.";
25*0Sstevel@tonic-gate 		case EAI_MEMORY:
26*0Sstevel@tonic-gate 			return "memory allocation failure.";
27*0Sstevel@tonic-gate 		default:
28*0Sstevel@tonic-gate 			return "unknown error.";
29*0Sstevel@tonic-gate 	}
30*0Sstevel@tonic-gate }
31*0Sstevel@tonic-gate #endif /* !HAVE_GAI_STRERROR */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #ifndef HAVE_FREEADDRINFO
freeaddrinfo(struct addrinfo * ai)34*0Sstevel@tonic-gate void freeaddrinfo(struct addrinfo *ai)
35*0Sstevel@tonic-gate {
36*0Sstevel@tonic-gate 	struct addrinfo *next;
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate 	do {
39*0Sstevel@tonic-gate 		next = ai->ai_next;
40*0Sstevel@tonic-gate 		free(ai);
41*0Sstevel@tonic-gate 	} while (NULL != (ai = next));
42*0Sstevel@tonic-gate }
43*0Sstevel@tonic-gate #endif /* !HAVE_FREEADDRINFO */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #ifndef HAVE_GETADDRINFO
malloc_ai(int port,u_long addr)46*0Sstevel@tonic-gate static struct addrinfo *malloc_ai(int port, u_long addr)
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate 	struct addrinfo *ai;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
51*0Sstevel@tonic-gate 	if (ai == NULL)
52*0Sstevel@tonic-gate 		return(NULL);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	ai->ai_addr = (struct sockaddr *)(ai + 1);
57*0Sstevel@tonic-gate 	/* XXX -- ssh doesn't use sa_len */
58*0Sstevel@tonic-gate 	ai->ai_addrlen = sizeof(struct sockaddr_in);
59*0Sstevel@tonic-gate 	ai->ai_addr->sa_family = ai->ai_family = AF_INET;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
62*0Sstevel@tonic-gate 	((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	return(ai);
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
getaddrinfo(const char * hostname,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)67*0Sstevel@tonic-gate int getaddrinfo(const char *hostname, const char *servname,
68*0Sstevel@tonic-gate                 const struct addrinfo *hints, struct addrinfo **res)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	struct addrinfo *cur, *prev = NULL;
71*0Sstevel@tonic-gate 	struct hostent *hp;
72*0Sstevel@tonic-gate 	struct in_addr in;
73*0Sstevel@tonic-gate 	int i, port;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (servname)
76*0Sstevel@tonic-gate 		port = htons(atoi(servname));
77*0Sstevel@tonic-gate 	else
78*0Sstevel@tonic-gate 		port = 0;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	if (hints && hints->ai_flags & AI_PASSIVE) {
81*0Sstevel@tonic-gate 		if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
82*0Sstevel@tonic-gate 			return 0;
83*0Sstevel@tonic-gate 		else
84*0Sstevel@tonic-gate 			return EAI_MEMORY;
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	if (!hostname) {
88*0Sstevel@tonic-gate 		if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
89*0Sstevel@tonic-gate 			return 0;
90*0Sstevel@tonic-gate 		else
91*0Sstevel@tonic-gate 			return EAI_MEMORY;
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (inet_aton(hostname, &in)) {
95*0Sstevel@tonic-gate 		if (NULL != (*res = malloc_ai(port, in.s_addr)))
96*0Sstevel@tonic-gate 			return 0;
97*0Sstevel@tonic-gate 		else
98*0Sstevel@tonic-gate 			return EAI_MEMORY;
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	hp = gethostbyname(hostname);
102*0Sstevel@tonic-gate 	if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
103*0Sstevel@tonic-gate 		for (i = 0; hp->h_addr_list[i]; i++) {
104*0Sstevel@tonic-gate 			cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
105*0Sstevel@tonic-gate 			if (cur == NULL) {
106*0Sstevel@tonic-gate 				if (*res)
107*0Sstevel@tonic-gate 					freeaddrinfo(*res);
108*0Sstevel@tonic-gate 				return EAI_MEMORY;
109*0Sstevel@tonic-gate 			}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 			if (prev)
112*0Sstevel@tonic-gate 				prev->ai_next = cur;
113*0Sstevel@tonic-gate 			else
114*0Sstevel@tonic-gate 				*res = cur;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 			prev = cur;
117*0Sstevel@tonic-gate 		}
118*0Sstevel@tonic-gate 		return 0;
119*0Sstevel@tonic-gate 	}
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	return EAI_NODATA;
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate #endif /* !HAVE_GETADDRINFO */
124