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