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"
19 #ifndef HAVE_GAI_STRERROR
20 char *gai_strerror(int ecode)
24 return "No address associated with hostname";
26 return "Memory allocation failure";
28 return "Address family not supported";
30 return "Unknown error";
33 #endif /* !HAVE_GAI_STRERROR */
35 #ifndef HAVE_FREEADDRINFO
36 void freeaddrinfo(struct addrinfo *ai)
38 struct addrinfo *next;
46 #endif /* !HAVE_FREEADDRINFO */
48 #ifndef HAVE_GETADDRINFO
49 static struct addrinfo *malloc_ai(uint16_t port, uint32_t addr)
53 ai = xmalloc_and_zero(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
55 ai->ai_addr = (struct sockaddr *)(ai + 1);
56 ai->ai_addrlen = sizeof(struct sockaddr_in);
57 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
59 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
60 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
65 int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
67 struct addrinfo *prev = NULL;
69 struct in_addr in = {0};
73 if(hints && hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC)
77 port = htons(atoi(servname));
79 if (hints && hints->ai_flags & AI_PASSIVE) {
80 *res = malloc_ai(port, htonl(0x00000000));
85 *res = malloc_ai(port, htonl(0x7f000001));
89 hp = gethostbyname(hostname);
91 if(!hp || !hp->h_addr_list || !hp->h_addr_list[0])
94 for (i = 0; hp->h_addr_list[i]; i++) {
95 *res = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
105 #endif /* !HAVE_GETADDRINFO */