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