K&R style braces
[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 #include "xalloc.h"
18
19 #ifndef HAVE_GAI_STRERROR
20 char *gai_strerror(int ecode) {
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         struct addrinfo *next;
37
38         while(ai) {
39                 next = ai->ai_next;
40                 free(ai);
41                 ai = next;
42         }
43 }
44 #endif /* !HAVE_FREEADDRINFO */
45
46 #ifndef HAVE_GETADDRINFO
47 static struct addrinfo *malloc_ai(uint16_t port, uint32_t addr) {
48         struct addrinfo *ai;
49
50         ai = xmalloc_and_zero(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
51         
52         ai->ai_addr = (struct sockaddr *)(ai + 1);
53         ai->ai_addrlen = sizeof(struct sockaddr_in);
54         ai->ai_addr->sa_family = ai->ai_family = AF_INET;
55
56         ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
57         ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
58         
59         return ai;
60 }
61
62 int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res) {
63         struct addrinfo *prev = NULL;
64         struct hostent *hp;
65         struct in_addr in = {0};
66         int i;
67         uint16_t port = 0;
68
69         if(hints && hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC)
70                 return EAI_FAMILY;
71
72         if (servname)
73                 port = htons(atoi(servname));
74
75         if (hints && hints->ai_flags & AI_PASSIVE) {
76                 *res = malloc_ai(port, htonl(0x00000000));
77                 return 0;
78         }
79                 
80         if (!hostname) {
81                 *res = malloc_ai(port, htonl(0x7f000001));
82                 return 0;
83         }
84         
85         hp = gethostbyname(hostname);
86
87         if(!hp || !hp->h_addr_list || !hp->h_addr_list[0])
88                 return EAI_NODATA;
89
90         for (i = 0; hp->h_addr_list[i]; i++) {
91                 *res = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
92
93                 if(prev)
94                         prev->ai_next = *res;
95
96                 prev = *res;
97         }
98
99         return 0;
100 }
101 #endif /* !HAVE_GETADDRINFO */