]> git.meshlink.io Git - meshlink/blob - src/fake-getaddrinfo.c
Fix a debug message being logged incorrectly.
[meshlink] / src / fake-getaddrinfo.c
1 /*
2  * fake library for ssh
3  *
4  * This file includes getaddrinfo(), freeaddrinfo() and gai_strerror().
5  * These functions are defined in rfc2133.
6  *
7  * But these functions are not implemented correctly. The minimum subset
8  * is implemented for ssh use only. For example, this routine assumes
9  * that ai_family is AF_INET. Don't use it for another purpose.
10  */
11
12 #include "system.h"
13
14 #include "fake-getaddrinfo.h"
15 #include "xalloc.h"
16
17
18 #if !HAVE_DECL_GAI_STRERROR
19 char *gai_strerror(int ecode) {
20         switch(ecode) {
21         case EAI_NODATA:
22                 return "No address associated with hostname";
23
24         case EAI_MEMORY:
25                 return "Memory allocation failure";
26
27         case EAI_FAMILY:
28                 return "Address family not supported";
29
30         default:
31                 return "Unknown error";
32         }
33 }
34 #endif /* !HAVE_GAI_STRERROR */
35
36 #if !HAVE_DECL_FREEADDRINFO
37 void freeaddrinfo(struct addrinfo *ai) {
38         struct addrinfo *next;
39
40         while(ai) {
41                 next = ai->ai_next;
42                 free(ai);
43                 ai = next;
44         }
45 }
46 #endif /* !HAVE_FREEADDRINFO */
47
48 #if !HAVE_DECL_GETADDRINFO
49 static struct addrinfo *malloc_ai(uint16_t port, uint32_t addr) {
50         struct addrinfo *ai;
51
52         ai = xzalloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
53
54         ai->ai_addr = (struct sockaddr *)(ai + 1);
55         ai->ai_addrlen = sizeof(struct sockaddr_in);
56         ai->ai_addr->sa_family = ai->ai_family = AF_INET;
57
58         ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
59         ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
60
61         return ai;
62 }
63
64 int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res) {
65         struct addrinfo *prev = NULL;
66         struct hostent *hp;
67         struct in_addr in = {0};
68         int i;
69         uint16_t port = 0;
70
71         if(hints && hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC) {
72                 return EAI_FAMILY;
73         }
74
75         if(servname) {
76                 port = htons(atoi(servname));
77         }
78
79         if(hints && hints->ai_flags & AI_PASSIVE) {
80                 *res = malloc_ai(port, htonl(0x00000000));
81                 return 0;
82         }
83
84         if(!hostname) {
85                 *res = malloc_ai(port, htonl(0x7f000001));
86                 return 0;
87         }
88
89         hp = gethostbyname(hostname);
90
91         if(!hp || !hp->h_addr_list || !hp->h_addr_list[0]) {
92                 return EAI_NODATA;
93         }
94
95         for(i = 0; hp->h_addr_list[i]; i++) {
96                 *res = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
97
98                 if(prev) {
99                         prev->ai_next = *res;
100                 }
101
102                 prev = *res;
103         }
104
105         return 0;
106 }
107 #endif /* !HAVE_GETADDRINFO */