]> git.meshlink.io Git - meshlink/blob - src/fake-getaddrinfo.c
Remove everything GPL that is not copyright Guus Sliepen, update copyright statements.
[meshlink] / 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 "fake-getaddrinfo.h"
15 #include "xalloc.h"
16
17
18 #if !HAVE_DECL_GAI_STRERROR
19 char *gai_strerror(int ecode) {
20         switch (ecode) {
21                 case EAI_NODATA:
22                         return "No address associated with hostname";
23                 case EAI_MEMORY:
24                         return "Memory allocation failure";
25                 case EAI_FAMILY:
26                         return "Address family not supported";
27                 default:
28                         return "Unknown error";
29         }
30 }
31 #endif /* !HAVE_GAI_STRERROR */
32
33 #if !HAVE_DECL_FREEADDRINFO
34 void freeaddrinfo(struct addrinfo *ai) {
35         struct addrinfo *next;
36
37         while(ai) {
38                 next = ai->ai_next;
39                 free(ai);
40                 ai = next;
41         }
42 }
43 #endif /* !HAVE_FREEADDRINFO */
44
45 #if !HAVE_DECL_GETADDRINFO
46 static struct addrinfo *malloc_ai(uint16_t port, uint32_t addr) {
47         struct addrinfo *ai;
48
49         ai = xzalloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
50
51         ai->ai_addr = (struct sockaddr *)(ai + 1);
52         ai->ai_addrlen = sizeof(struct sockaddr_in);
53         ai->ai_addr->sa_family = ai->ai_family = AF_INET;
54
55         ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
56         ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
57
58         return ai;
59 }
60
61 int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res) {
62         struct addrinfo *prev = NULL;
63         struct hostent *hp;
64         struct in_addr in = {0};
65         int i;
66         uint16_t port = 0;
67
68         if(hints && hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC)
69                 return EAI_FAMILY;
70
71         if (servname)
72                 port = htons(atoi(servname));
73
74         if (hints && hints->ai_flags & AI_PASSIVE) {
75                 *res = malloc_ai(port, htonl(0x00000000));
76                 return 0;
77         }
78
79         if (!hostname) {
80                 *res = malloc_ai(port, htonl(0x7f000001));
81                 return 0;
82         }
83
84         hp = gethostbyname(hostname);
85
86         if(!hp || !hp->h_addr_list || !hp->h_addr_list[0])
87                 return EAI_NODATA;
88
89         for (i = 0; hp->h_addr_list[i]; i++) {
90                 *res = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
91
92                 if(prev)
93                         prev->ai_next = *res;
94
95                 prev = *res;
96         }
97
98         return 0;
99 }
100 #endif /* !HAVE_GETADDRINFO */