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.
11 * In the case not using 'configure --enable-ipv6', this getaddrinfo.c
12 * will be used if you have broken getaddrinfo or no getaddrinfo.
18 #ifndef HAVE_GAI_STRERROR
25 return "no address associated with hostname.";
27 return "memory allocation failure.";
29 return "unknown error.";
32 #endif /* !HAVE_GAI_STRERROR */
34 #ifndef HAVE_FREEADDRINFO
39 struct addrinfo *next;
46 #endif /* !HAVE_FREEADDRINFO */
48 #ifndef HAVE_GETADDRINFO
49 static struct addrinfo *
56 if (ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) +
57 sizeof(struct sockaddr_in))) {
58 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
59 ai->ai_addr = (struct sockaddr *)(ai + 1);
60 /* XXX -- ssh doesn't use sa_len */
61 ai->ai_addrlen = sizeof(struct sockaddr_in);
62 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
63 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
64 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
72 getaddrinfo(hostname, servname, hints, res)
73 const char *hostname, *servname;
74 const struct addrinfo *hints;
75 struct addrinfo **res;
77 struct addrinfo *cur, *prev = NULL;
82 port = htons(atoi(servname));
85 if (hints && hints->ai_flags & AI_PASSIVE)
86 if (*res = malloc_ai(port, htonl(0x00000000)))
91 if (*res = malloc_ai(port, htonl(0x7f000001)))
95 if (inet_addr(hostname) != -1)
96 if (*res = malloc_ai(port, inet_addr(hostname)))
100 if ((hp = gethostbyname(hostname)) &&
101 hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
102 for (i = 0; hp->h_addr_list[i]; i++)
103 if (cur = malloc_ai(port,
104 ((struct in_addr *)hp->h_addr_list[i])->s_addr)) {
119 #endif /* !HAVE_GETADDRINFO */