Fix fake getnameinfo() and check more arguments.
[tinc] / lib / fake-getaddrinfo.c
1 /*
2  * fake library for ssh
3  *
4  * This file includes getaddrinfo(), freeaddrinfo() and gai_strerror().
5  * These funtions are defined in rfc2133.
6  *
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.
10  */
11
12 #include "system.h"
13
14 #include "ipv4.h"
15 #include "ipv6.h"
16 #include "fake-getaddrinfo.h"
17
18 #ifndef HAVE_GAI_STRERROR
19 char *gai_strerror(int ecode)
20 {
21         switch (ecode) {
22                 case EAI_NODATA:
23                         return "No address associated with hostname";
24                 case EAI_MEMORY:
25                         return "Memory allocation failure";
26                 case EAI_FAMILY:
27                         return "Address family not supported";
28                 default:
29                         return "Unknown error";
30         }
31 }    
32 #endif /* !HAVE_GAI_STRERROR */
33
34 #ifndef HAVE_FREEADDRINFO
35 void freeaddrinfo(struct addrinfo *ai)
36 {
37         struct addrinfo *next;
38
39         while(ai) {
40                 next = ai->ai_next;
41                 free(ai);
42                 ai = next;
43         }
44 }
45 #endif /* !HAVE_FREEADDRINFO */
46
47 #ifndef HAVE_GETADDRINFO
48 static struct addrinfo *malloc_ai(uint16_t port, uint32_t addr)
49 {
50         struct addrinfo *ai;
51
52         ai = xmalloc_and_zero(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 {
66         struct addrinfo *prev = NULL;
67         struct hostent *hp;
68         struct in_addr in = {0};
69         int i;
70         uint16_t port = 0;
71
72         if(hints && hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC)
73                 return EAI_FAMILY;
74
75         if (servname)
76                 port = htons(atoi(servname));
77
78         if (hints && hints->ai_flags & AI_PASSIVE) {
79                 *res = malloc_ai(port, htonl(0x00000000));
80                 return 0;
81         }
82                 
83         if (!hostname) {
84                 *res = malloc_ai(port, htonl(0x7f000001));
85                 return 0;
86         }
87         
88         hp = gethostbyname(hostname);
89
90         if(!hp || !hp->h_addr_list || !hp->h_addr_list[0])
91                 return EAI_NODATA;
92
93         for (i = 0; hp->h_addr_list[i]; i++) {
94                 *res = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
95
96                 if(prev)
97                         prev->ai_next = *res;
98
99                 prev = *res;
100         }
101
102         return 0;
103 }
104 #endif /* !HAVE_GETADDRINFO */