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.
14 #include <sys/types.h>
15 #include <netinet/in.h>
16 #include <sys/socket.h>
22 #include "fake-getaddrinfo.h"
24 #ifndef HAVE_GAI_STRERROR
25 char *gai_strerror(int ecode)
29 return "no address associated with hostname.";
31 return "memory allocation failure.";
33 return "unknown error.";
36 #endif /* !HAVE_GAI_STRERROR */
38 #ifndef HAVE_FREEADDRINFO
39 void freeaddrinfo(struct addrinfo *ai)
41 struct addrinfo *next;
46 } while (NULL != (ai = next));
48 #endif /* !HAVE_FREEADDRINFO */
50 #ifndef HAVE_GETADDRINFO
51 static struct addrinfo *malloc_ai(int port, u_long addr)
55 ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
59 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
61 ai->ai_addr = (struct sockaddr *)(ai + 1);
62 /* XXX -- ssh doesn't use sa_len */
63 ai->ai_addrlen = sizeof(struct sockaddr_in);
64 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
66 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
67 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
72 int getaddrinfo(const char *hostname, const char *servname,
73 const struct addrinfo *hints, struct addrinfo **res)
75 struct addrinfo *cur, *prev = NULL;
81 port = htons(atoi(servname));
85 if (hints && hints->ai_flags & AI_PASSIVE) {
86 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
93 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
99 if (inet_aton(hostname, &in)) {
100 if (NULL != (*res = malloc_ai(port, in.s_addr)))
106 hp = gethostbyname(hostname);
107 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
108 for (i = 0; hp->h_addr_list[i]; i++) {
109 cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
128 #endif /* !HAVE_GETADDRINFO */