Be consistent.
[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                 default:
27                         return "unknown error.";
28         }
29 }    
30 #endif /* !HAVE_GAI_STRERROR */
31
32 #ifndef HAVE_FREEADDRINFO
33 void freeaddrinfo(struct addrinfo *ai)
34 {
35         struct addrinfo *next;
36
37         do {
38                 next = ai->ai_next;
39                 free(ai);
40         } while (NULL != (ai = next));
41 }
42 #endif /* !HAVE_FREEADDRINFO */
43
44 #ifndef HAVE_GETADDRINFO
45 static struct addrinfo *malloc_ai(int port, uint32_t addr)
46 {
47         struct addrinfo *ai;
48
49         ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
50         if (ai == NULL)
51                 return(NULL);
52         
53         memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
54         
55         ai->ai_addr = (struct sockaddr *)(ai + 1);
56         /* XXX -- ssh doesn't use sa_len */
57         ai->ai_addrlen = sizeof(struct sockaddr_in);
58         ai->ai_addr->sa_family = ai->ai_family = AF_INET;
59
60         ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
61         ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
62         
63         return(ai);
64 }
65
66 int getaddrinfo(const char *hostname, const char *servname, 
67                 const struct addrinfo *hints, struct addrinfo **res)
68 {
69         struct addrinfo *cur, *prev = NULL;
70         struct hostent *hp;
71         struct in_addr in;
72         int i, port;
73
74         if (servname)
75                 port = htons(atoi(servname));
76         else
77                 port = 0;
78
79         if (hints && hints->ai_flags & AI_PASSIVE) {
80                 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
81                         return 0;
82                 else
83                         return EAI_MEMORY;
84         }
85                 
86         if (!hostname) {
87                 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
88                         return 0;
89                 else
90                         return EAI_MEMORY;
91         }
92         
93 #ifdef HAVE_INET_ATON
94         if (inet_aton(hostname, &in)) {
95                 if (NULL != (*res = malloc_ai(port, in.s_addr)))
96                         return 0;
97                 else
98                         return EAI_MEMORY;
99         }
100 #endif
101         
102         hp = gethostbyname(hostname);
103         if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
104                 for (i = 0; hp->h_addr_list[i]; i++) {
105                         cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
106                         if (cur == NULL) {
107                                 if (*res)
108                                         freeaddrinfo(*res);
109                                 return EAI_MEMORY;
110                         }
111                         
112                         if (prev)
113                                 prev->ai_next = cur;
114                         else
115                                 *res = cur;
116
117                         prev = cur;
118                 }
119                 return 0;
120         }
121         
122         return EAI_NODATA;
123 }
124 #endif /* !HAVE_GETADDRINFO */