4 * This file includes getaddrinfo(), freeaddrinfo() and gai_strerror().
5 * These funtions are defined in rfc2133.
7 * But these functions are not implemented correctly. The minimum subset
8 * is implemented for ssh use only. For exapmle, this routine assumes
9 * that ai_family is AF_INET. Don't use it for another purpose.
16 #include "fake-getaddrinfo.h"
18 #ifndef HAVE_GAI_STRERROR
19 char *gai_strerror(int ecode)
23 return "No address associated with hostname";
25 return "Memory allocation failure";
27 return "Unknown error";
30 #endif /* !HAVE_GAI_STRERROR */
32 #ifndef HAVE_FREEADDRINFO
33 void freeaddrinfo(struct addrinfo *ai)
35 struct addrinfo *next;
43 #endif /* !HAVE_FREEADDRINFO */
45 #ifndef HAVE_GETADDRINFO
46 static struct addrinfo *malloc_ai(uint16_t port, uint32_t addr)
50 ai = xmalloc_and_zero(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
52 ai->ai_addr = (struct sockaddr *)(ai + 1);
53 ai->ai_addrlen = sizeof(struct sockaddr_in);
54 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
56 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
57 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
62 int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
64 struct addrinfo *prev = NULL;
66 struct in_addr in = {0};
71 port = htons(atoi(servname));
73 if (hints && hints->ai_flags & AI_PASSIVE) {
74 *res = malloc_ai(port, htonl(0x00000000));
79 *res = malloc_ai(port, htonl(0x7f000001));
83 hp = gethostbyname(hostname);
85 if(!hp || !hp->h_addr_list[0])
88 for (i = 0; hp->h_addr_list[i]; i++) {
89 *res = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
99 #endif /* !HAVE_GETADDRINFO */