port numbers should always be interpreted as decimal
[musl] / src / network / getaddrinfo.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <netdb.h>
4 #include <netinet/in.h>
5 #include <sys/socket.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include "__dns.h"
10 #include "stdio_impl.h"
11
12 static int is_valid(const char *host)
13 {
14         const unsigned char *s;
15         if (strlen(host)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0;
16         for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
17         return !*s;
18 }
19
20 #if 0
21 static int have_af(int family)
22 {
23         struct sockaddr_in6 sin6 = { .sin6_family = family };
24         socklen_t sl = family == AF_INET
25                 ? sizeof(struct sockaddr_in)
26                 : sizeof(struct sockaddr_in6);
27         int sock = socket(family, SOCK_STREAM, 0);
28         int have = !bind(sock, (void *)&sin6, sl);
29         close(sock);
30         return have;
31 }
32 #endif
33
34 #include <stdlib.h>
35 #include <netdb.h>
36
37 union sa {
38         struct sockaddr_in sin;
39         struct sockaddr_in6 sin6;
40 };
41
42 struct aibuf {
43         struct addrinfo ai;
44         union sa sa;
45 };
46
47 /* Extra slots needed for storing canonical name */
48 #define EXTRA ((256+sizeof(struct aibuf)-1)/sizeof(struct aibuf))
49
50 int getaddrinfo(const char *host, const char *serv, const struct addrinfo *hint, struct addrinfo **res)
51 {
52         int flags = hint ? hint->ai_flags : 0;
53         int family = hint ? hint->ai_family : AF_UNSPEC;
54         int type = hint ? hint->ai_socktype : 0;
55         int proto = hint ? hint->ai_protocol : 0;
56         unsigned long port = 0;
57         struct aibuf *buf;
58         union sa sa = {{0}};
59         unsigned char reply[1024];
60         int i, j;
61         //char hostbuf[256];
62         char line[512];
63         FILE *f, _f;
64         unsigned char _buf[64];
65         char *z;
66         int result;
67         int cnt;
68
69         if (host && strlen(host)>255) return EAI_NONAME;
70         if (serv && strlen(serv)>32) return EAI_SERVICE;
71
72         if (type && !proto)
73                 proto = type==SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
74         if (!type && proto)
75                 type = proto==IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
76
77         if (serv) {
78                 if (!*serv) return EAI_SERVICE;
79                 port = strtoul(serv, &z, 10);
80                 if (!*z && port > 65535) return EAI_SERVICE;
81                 if (!port) {
82                         if (flags & AI_NUMERICSERV) return EAI_SERVICE;
83
84                         //f = fopen("/etc/services", "rb");
85                         return EAI_SERVICE;
86                 }
87                 port = htons(port);
88         }
89
90         if (!host) {
91                 if (family == AF_UNSPEC) family = AF_INET;
92                 buf = calloc(sizeof *buf, 1+EXTRA);
93                 if (!buf) return EAI_MEMORY;
94                 buf->ai.ai_protocol = proto;
95                 buf->ai.ai_socktype = type;
96                 buf->ai.ai_addr = (void *)&buf->sa;
97                 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
98                 buf->ai.ai_family = family;
99                 buf->sa.sin.sin_family = family;
100                 buf->sa.sin.sin_port = port;
101                 if (!(flags & AI_PASSIVE)) {
102                         if (family == AF_INET) {
103                                 0[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=127;
104                                 3[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=1;
105                         } else buf[0].sa.sin6.sin6_addr.s6_addr[15] = 1;
106                 }
107                 *res = &buf->ai;
108                 return 0;
109         }
110
111         if (!*host) return EAI_NONAME;
112
113         /* Try as a numeric address */
114         if (__ipparse(&sa, family, host) >= 0) {
115                 buf = calloc(sizeof *buf, 1+EXTRA);
116                 if (!buf) return EAI_MEMORY;
117                 family = sa.sin.sin_family;
118                 buf->ai.ai_protocol = proto;
119                 buf->ai.ai_socktype = type;
120                 buf->ai.ai_addr = (void *)&buf->sa;
121                 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
122                 buf->ai.ai_family = family;
123                 buf->ai.ai_canonname = (char *)host;
124                 buf->sa = sa;
125                 buf->sa.sin.sin_port = port;
126                 *res = &buf->ai;
127                 return 0;
128         }
129
130         if (flags & AI_NUMERICHOST) return EAI_NONAME;
131
132         f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
133         if (f) while (fgets(line, sizeof line, f)) {
134                 char *p;
135                 size_t l = strlen(host);
136
137                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
138                 for(p=line+1; (p=strstr(p, host)) &&
139                         (!isspace(p[-1]) || !isspace(p[l])); p++);
140                 if (!p) continue;
141                 __fclose_ca(f);
142
143                 /* Isolate IP address to parse */
144                 for (p=line; *p && !isspace(*p); p++);
145                 *p++ = 0;
146                 if (__ipparse(&sa, family, line) < 0) return EAI_NONAME;
147
148                 /* Allocate and fill result buffer */
149                 buf = calloc(sizeof *buf, 1+EXTRA);
150                 if (!buf) return EAI_MEMORY;
151                 family = sa.sin.sin_family;
152                 buf->ai.ai_protocol = proto;
153                 buf->ai.ai_socktype = type;
154                 buf->ai.ai_addr = (void *)&buf->sa;
155                 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
156                 buf->ai.ai_family = family;
157                 buf->sa = sa;
158                 buf->sa.sin.sin_port = port;
159
160                 /* Extract first name as canonical name */
161                 for (; *p && isspace(*p); p++);
162                 buf->ai.ai_canonname = (void *)(buf+1);
163                 snprintf(buf->ai.ai_canonname, 256, "%s", p);
164                 for (p=buf->ai.ai_canonname; *p && !isspace(*p); p++);
165                 *p = 0;
166                 if (!is_valid(buf->ai.ai_canonname))
167                         buf->ai.ai_canonname = 0;
168
169                 *res = &buf->ai;
170                 return 0;
171         }
172         if (f) __fclose_ca(f);
173
174 #if 0
175         f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
176         if (f) while (fgets(line, sizeof line, f)) {
177                 if (!isspace(line[10]) || (strncmp(line, "search", 6)
178                         && strncmp(line, "domain", 6))) continue;
179         }
180         if (f) __fclose_ca(f);
181 #endif
182
183         /* Perform one or more DNS queries for host */
184         memset(reply, 0, sizeof reply);
185         result = __dns_query(reply, host, family, 0);
186         if (result < 0) return result;
187
188         cnt = __dns_count_addrs(reply, result);
189         if (cnt <= 0) return EAI_NONAME;
190
191         buf = calloc(sizeof *buf, cnt+EXTRA);
192         if (!buf) return EAI_MEMORY;
193
194         i = 0;
195         if (family != AF_INET6) {
196                 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply, RR_A, 0);
197                 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
198         }
199         if (family != AF_INET) {
200                 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply, RR_AAAA, 0);
201                 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
202         }
203         if (result>1) {
204                 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply+512, RR_A, 0);
205                 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
206                 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply+512, RR_AAAA, 0);
207                 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
208         }
209
210         if (__dns_get_rr((void *)&buf[cnt], 0, 256, 1, reply, RR_CNAME, 1) < 0)
211                 strcpy((void *)&buf[cnt], host);
212
213         for (i=0; i<cnt; i++) {
214                 buf[i].ai.ai_protocol = proto;
215                 buf[i].ai.ai_socktype = type;
216                 buf[i].ai.ai_addr = (void *)&buf[i].sa;
217                 buf[i].ai.ai_addrlen = buf[i].sa.sin.sin_family==AF_INET6
218                         ? sizeof sa.sin6 : sizeof sa.sin;
219                 buf[i].ai.ai_family = buf[i].sa.sin.sin_family;
220                 buf[i].sa.sin.sin_port = port;
221                 buf[i].ai.ai_next = &buf[i+1].ai;
222                 buf[i].ai.ai_canonname = (void *)&buf[cnt];
223         }
224         buf[cnt-1].ai.ai_next = 0;
225         *res = &buf->ai;
226
227         return 0;
228 }