1 #include "fake-getaddrinfo.h"
5 * This file includes getaddrinfo(), freeaddrinfo() and gai_strerror().
6 * These funtions are defined in rfc2133.
8 * But these functions are not implemented correctly. The minimum subset
9 * is implemented for ssh use only. For exapmle, this routine assumes
10 * that ai_family is AF_INET. Don't use it for another purpose.
13 #ifndef HAVE_GAI_STRERROR
14 char *gai_strerror(int ecode)
18 return "no address associated with hostname.";
20 return "memory allocation failure.";
22 return "unknown error.";
25 #endif /* !HAVE_GAI_STRERROR */
27 #ifndef HAVE_FREEADDRINFO
28 void freeaddrinfo(struct addrinfo *ai)
30 struct addrinfo *next;
35 } while (NULL != (ai = next));
37 #endif /* !HAVE_FREEADDRINFO */
39 #ifndef HAVE_GETADDRINFO
40 static struct addrinfo *malloc_ai(int port, u_long addr)
44 ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
48 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
50 ai->ai_addr = (struct sockaddr *)(ai + 1);
51 /* XXX -- ssh doesn't use sa_len */
52 ai->ai_addrlen = sizeof(struct sockaddr_in);
53 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
55 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
56 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
61 int getaddrinfo(const char *hostname, const char *servname,
62 const struct addrinfo *hints, struct addrinfo **res)
64 struct addrinfo *cur, *prev = NULL;
70 port = htons(atoi(servname));
74 if (hints && hints->ai_flags & AI_PASSIVE) {
75 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
82 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
88 if (inet_aton(hostname, &in)) {
89 if (NULL != (*res = malloc_ai(port, in.s_addr)))
95 hp = gethostbyname(hostname);
96 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
97 for (i = 0; hp->h_addr_list[i]; i++) {
98 cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
117 #endif /* !HAVE_GETADDRINFO */