rework langinfo code for ABI compat and for use by time code
[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 *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict 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 line[512];
62         FILE *f, _f;
63         unsigned char _buf[1024];
64         char *z;
65         int result;
66         int cnt;
67
68         if (host && strlen(host)>255) return EAI_NONAME;
69         if (serv && strlen(serv)>32) return EAI_SERVICE;
70
71         if (type && !proto)
72                 proto = type==SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
73         if (!type && proto)
74                 type = proto==IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
75
76         if (serv) {
77                 if (!*serv) return EAI_SERVICE;
78                 port = strtoul(serv, &z, 10);
79                 if (*z) {
80                         size_t servlen = strlen(serv);
81                         char *end = line;
82
83                         if (flags & AI_NUMERICSERV) return EAI_SERVICE;
84
85                         f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
86                         if (!f) return EAI_SERVICE;
87                         while (fgets(line, sizeof line, f)) {
88                                 if (strncmp(line, serv, servlen) || !isspace(line[servlen]))
89                                         continue;
90                                 port = strtoul(line+servlen, &end, 10);
91                                 if (strncmp(end, proto==IPPROTO_UDP ? "/udp" : "/tcp", 4))
92                                         continue;
93                                 break;
94                         }
95                         __fclose_ca(f);
96                         if (feof(f)) return EAI_SERVICE;
97                 }
98                 if (port > 65535) return EAI_SERVICE;
99                 port = htons(port);
100         }
101
102         if (!host) {
103                 if (family == AF_UNSPEC) {
104                         cnt = 2; family = AF_INET;
105                 } else {
106                         cnt = 1;
107                 }
108                 buf = calloc(sizeof *buf, cnt);
109                 if (!buf) return EAI_MEMORY;
110                 for (i=0; i<cnt; i++) {
111                         if (i) family = AF_INET6;
112                         buf[i].ai.ai_protocol = proto;
113                         buf[i].ai.ai_socktype = type;
114                         buf[i].ai.ai_addr = (void *)&buf[i].sa;
115                         buf[i].ai.ai_addrlen = family==AF_INET6
116                                 ? sizeof sa.sin6 : sizeof sa.sin;
117                         buf[i].ai.ai_family = family;
118                         buf[i].sa.sin.sin_family = family;
119                         buf[i].sa.sin.sin_port = port;
120                         if (i+1<cnt) buf[i].ai.ai_next = &buf[i+1].ai;
121                         if (!(flags & AI_PASSIVE)) {
122                                 if (family == AF_INET) {
123                                         0[(uint8_t*)&buf[i].sa.sin.sin_addr.s_addr]=127;
124                                         3[(uint8_t*)&buf[i].sa.sin.sin_addr.s_addr]=1;
125                                 } else buf[i].sa.sin6.sin6_addr.s6_addr[15] = 1;
126                         }
127                 }
128                 *res = &buf->ai;
129                 return 0;
130         }
131
132         if (!*host) return EAI_NONAME;
133
134         /* Try as a numeric address */
135         if (__ipparse(&sa, family, host) >= 0) {
136                 buf = calloc(sizeof *buf, 1+EXTRA);
137                 if (!buf) return EAI_MEMORY;
138                 family = sa.sin.sin_family;
139                 buf->ai.ai_protocol = proto;
140                 buf->ai.ai_socktype = type;
141                 buf->ai.ai_addr = (void *)&buf->sa;
142                 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
143                 buf->ai.ai_family = family;
144                 buf->ai.ai_canonname = (char *)host;
145                 buf->sa = sa;
146                 buf->sa.sin.sin_port = port;
147                 *res = &buf->ai;
148                 return 0;
149         }
150
151         if (flags & AI_NUMERICHOST) return EAI_NONAME;
152
153         f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
154         if (f) while (fgets(line, sizeof line, f)) {
155                 char *p;
156                 size_t l = strlen(host);
157
158                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
159                 for(p=line+1; (p=strstr(p, host)) &&
160                         (!isspace(p[-1]) || !isspace(p[l])); p++);
161                 if (!p) continue;
162                 __fclose_ca(f);
163
164                 /* Isolate IP address to parse */
165                 for (p=line; *p && !isspace(*p); p++);
166                 *p++ = 0;
167                 if (__ipparse(&sa, family, line) < 0) return EAI_NONAME;
168
169                 /* Allocate and fill result buffer */
170                 buf = calloc(sizeof *buf, 1+EXTRA);
171                 if (!buf) return EAI_MEMORY;
172                 family = sa.sin.sin_family;
173                 buf->ai.ai_protocol = proto;
174                 buf->ai.ai_socktype = type;
175                 buf->ai.ai_addr = (void *)&buf->sa;
176                 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
177                 buf->ai.ai_family = family;
178                 buf->sa = sa;
179                 buf->sa.sin.sin_port = port;
180
181                 /* Extract first name as canonical name */
182                 for (; *p && isspace(*p); p++);
183                 buf->ai.ai_canonname = (void *)(buf+1);
184                 snprintf(buf->ai.ai_canonname, 256, "%s", p);
185                 for (p=buf->ai.ai_canonname; *p && !isspace(*p); p++);
186                 *p = 0;
187                 if (!is_valid(buf->ai.ai_canonname))
188                         buf->ai.ai_canonname = 0;
189
190                 *res = &buf->ai;
191                 return 0;
192         }
193         if (f) __fclose_ca(f);
194
195 #if 0
196         f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
197         if (f) while (fgets(line, sizeof line, f)) {
198                 if (!isspace(line[10]) || (strncmp(line, "search", 6)
199                         && strncmp(line, "domain", 6))) continue;
200         }
201         if (f) __fclose_ca(f);
202 #endif
203
204         /* Perform one or more DNS queries for host */
205         memset(reply, 0, sizeof reply);
206         result = __dns_query(reply, host, family, 0);
207         if (result < 0) return result;
208
209         cnt = __dns_count_addrs(reply, result);
210         if (cnt <= 0) return EAI_NONAME;
211
212         buf = calloc(sizeof *buf, cnt+EXTRA);
213         if (!buf) return EAI_MEMORY;
214
215         i = 0;
216         if (family != AF_INET6) {
217                 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply, RR_A, 0);
218                 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
219         }
220         if (family != AF_INET) {
221                 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply, RR_AAAA, 0);
222                 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
223         }
224         if (result>1) {
225                 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply+512, RR_A, 0);
226                 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
227                 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply+512, RR_AAAA, 0);
228                 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
229         }
230
231         if (__dns_get_rr((void *)&buf[cnt], 0, 256, 1, reply, RR_CNAME, 1) <= 0)
232                 strcpy((void *)&buf[cnt], host);
233
234         for (i=0; i<cnt; i++) {
235                 buf[i].ai.ai_protocol = proto;
236                 buf[i].ai.ai_socktype = type;
237                 buf[i].ai.ai_addr = (void *)&buf[i].sa;
238                 buf[i].ai.ai_addrlen = buf[i].sa.sin.sin_family==AF_INET6
239                         ? sizeof sa.sin6 : sizeof sa.sin;
240                 buf[i].ai.ai_family = buf[i].sa.sin.sin_family;
241                 buf[i].sa.sin.sin_port = port;
242                 buf[i].ai.ai_next = &buf[i+1].ai;
243                 buf[i].ai.ai_canonname = (void *)&buf[cnt];
244         }
245         buf[cnt-1].ai.ai_next = 0;
246         *res = &buf->ai;
247
248         return 0;
249 }