Fix all -Wall -W compiler warnings.
[tinc] / src / 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 #if !HAVE_DECL_GAI_STRERROR
20 char *gai_strerror(int ecode) {
21         switch(ecode) {
22         case EAI_NODATA:
23                 return "No address associated with hostname";
24
25         case EAI_MEMORY:
26                 return "Memory allocation failure";
27
28         case EAI_FAMILY:
29                 return "Address family not supported";
30
31         default:
32                 return "Unknown error";
33         }
34 }
35 #endif /* !HAVE_GAI_STRERROR */
36
37 #if !HAVE_DECL_FREEADDRINFO
38 void freeaddrinfo(struct addrinfo *ai) {
39         struct addrinfo *next;
40
41         while(ai) {
42                 next = ai->ai_next;
43                 free(ai);
44                 ai = next;
45         }
46 }
47 #endif /* !HAVE_FREEADDRINFO */
48
49 #if !HAVE_DECL_GETADDRINFO
50 static struct addrinfo *malloc_ai(uint16_t port, uint32_t addr) {
51         struct addrinfo *ai;
52
53         ai = xmalloc_and_zero(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
54
55         ai->ai_addr = (struct sockaddr *)(ai + 1);
56         ai->ai_addrlen = sizeof(struct sockaddr_in);
57         ai->ai_addr->sa_family = ai->ai_family = AF_INET;
58
59         ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
60         ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
61
62         return ai;
63 }
64
65 int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res) {
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
76         if(servname) {
77                 port = htons(atoi(servname));
78         }
79
80         if(hints && hints->ai_flags & AI_PASSIVE) {
81                 *res = malloc_ai(port, htonl(0x00000000));
82                 return 0;
83         }
84
85         if(!hostname) {
86                 *res = malloc_ai(port, htonl(0x7f000001));
87                 return 0;
88         }
89
90         hp = gethostbyname(hostname);
91
92         if(!hp || !hp->h_addr_list || !hp->h_addr_list[0]) {
93                 return EAI_NODATA;
94         }
95
96         for(i = 0; hp->h_addr_list[i]; i++) {
97                 *res = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
98
99                 if(prev) {
100                         prev->ai_next = *res;
101                 }
102
103                 prev = *res;
104         }
105
106         return 0;
107 }
108 #endif /* !HAVE_GETADDRINFO */