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.
17 #ifndef HAVE_GAI_STRERROR
18 char *gai_strerror(int ecode)
22 return "no address associated with hostname.";
24 return "memory allocation failure.";
26 return "unknown error.";
29 #endif /* !HAVE_GAI_STRERROR */
31 #ifndef HAVE_FREEADDRINFO
32 void freeaddrinfo(struct addrinfo *ai)
34 struct addrinfo *next;
39 } while (NULL != (ai = next));
41 #endif /* !HAVE_FREEADDRINFO */
43 #ifndef HAVE_GETADDRINFO
44 static struct addrinfo *malloc_ai(int port, u_long addr)
48 ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
52 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
54 ai->ai_addr = (struct sockaddr *)(ai + 1);
55 /* XXX -- ssh doesn't use sa_len */
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,
66 const struct addrinfo *hints, struct addrinfo **res)
68 struct addrinfo *cur, *prev = NULL;
74 port = htons(atoi(servname));
78 if (hints && hints->ai_flags & AI_PASSIVE) {
79 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
86 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
92 if (inet_aton(hostname, &in)) {
93 if (NULL != (*res = malloc_ai(port, in.s_addr)))
99 hp = gethostbyname(hostname);
100 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
101 for (i = 0; hp->h_addr_list[i]; i++) {
102 cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
121 #endif /* !HAVE_GETADDRINFO */