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.
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
24 #include "fake-getaddrinfo.h"
26 #ifndef HAVE_GAI_STRERROR
27 char *gai_strerror(int ecode)
31 return "no address associated with hostname.";
33 return "memory allocation failure.";
35 return "unknown error.";
38 #endif /* !HAVE_GAI_STRERROR */
40 #ifndef HAVE_FREEADDRINFO
41 void freeaddrinfo(struct addrinfo *ai)
43 struct addrinfo *next;
48 } while (NULL != (ai = next));
50 #endif /* !HAVE_FREEADDRINFO */
52 #ifndef HAVE_GETADDRINFO
53 static struct addrinfo *malloc_ai(int port, u_long addr)
57 ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
61 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
63 ai->ai_addr = (struct sockaddr *)(ai + 1);
64 /* XXX -- ssh doesn't use sa_len */
65 ai->ai_addrlen = sizeof(struct sockaddr_in);
66 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
68 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
69 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
74 int getaddrinfo(const char *hostname, const char *servname,
75 const struct addrinfo *hints, struct addrinfo **res)
77 struct addrinfo *cur, *prev = NULL;
83 port = htons(atoi(servname));
87 if (hints && hints->ai_flags & AI_PASSIVE) {
88 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
95 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
101 #ifdef HAVE_INET_ATON
102 if (inet_aton(hostname, &in)) {
103 if (NULL != (*res = malloc_ai(port, in.s_addr)))
110 hp = gethostbyname(hostname);
111 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
112 for (i = 0; hp->h_addr_list[i]; i++) {
113 cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
132 #endif /* !HAVE_GETADDRINFO */