1 #include <sys/socket.h>
2 #include <netinet/in.h>
13 #include "stdio_impl.h"
16 static void cleanup(void *p)
18 __syscall(SYS_close, (intptr_t)p);
21 static unsigned long mtime()
24 clock_gettime(CLOCK_REALTIME, &ts);
25 return (unsigned long)ts.tv_sec * 1000
26 + ts.tv_nsec / 1000000;
29 int __res_msend(int nqueries, const unsigned char *const *queries,
30 const int *qlens, unsigned char *const *answers, int *alens, int asize)
34 unsigned char _buf[256];
35 char line[64], *s, *z;
36 int timeout = 5000, attempts = 2, retry_interval;
38 struct sockaddr_in sin;
39 struct sockaddr_in6 sin6;
40 } sa = {0}, ns[3] = {{0}};
41 socklen_t sl = sizeof sa.sin;
49 unsigned long t0, t1, t2;
51 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
53 /* Get nameservers from resolv.conf, fallback to localhost */
54 f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
55 if (f) for (nns=0; nns<3 && fgets(line, sizeof line, f); ) {
56 if (!strncmp(line, "options", 7) && isspace(line[7])) {
59 p = strstr(line, "timeout:");
60 if (p && isdigit(p[8])) {
62 x = strtoul(p, &z, 10);
63 if (z != p) timeout = x < 30 ? x*1000 : 30000;
65 p = strstr(line, "attempts:");
66 if (p && isdigit(p[9])) {
68 x = strtoul(p, &z, 10);
69 if (z != p) attempts = x < 10 ? x : 10;
70 if (!attempts) attempts = 1;
73 if (strncmp(line, "nameserver", 10) || !isspace(line[10]))
75 for (s=line+11; isspace(*s); s++);
76 for (z=s; *z && !isspace(*z); z++);
79 if (inet_pton(AF_INET, s, &ns[nns].sin.sin_addr)>0) {
80 ns[nns].sin.sin_port = htons(53);
81 ns[nns++].sin.sin_family = AF_INET;
82 } else if (inet_pton(AF_INET6, s, &ns[nns].sin6.sin6_addr)>0) {
84 ns[nns].sin6.sin6_port = htons(53);
85 ns[nns++].sin6.sin6_family = family = AF_INET6;
88 if (f) __fclose_ca(f);
90 ns[0].sin.sin_family = AF_INET;
91 ns[0].sin.sin_port = htons(53);
92 ns[0].sin.sin_addr.s_addr = htonl(0x7f000001);
96 /* Get local address and open/bind a socket */
97 sa.sin.sin_family = family;
98 fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
100 /* Handle case where system lacks IPv6 support */
101 if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
102 fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
105 if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) return -1;
107 /* Past this point, there are no errors. Each individual query will
108 * yield either no reply (indicated by zero length) or an answer
109 * packet which is up to the caller to interpret. */
111 pthread_cleanup_push(cleanup, (void *)(intptr_t)fd);
112 pthread_setcancelstate(cs, 0);
114 /* Convert any IPv4 addresses in a mixed environment to v4-mapped */
115 if (family == AF_INET6) {
116 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0);
117 for (i=0; i<nns; i++) {
118 if (ns[i].sin.sin_family != AF_INET) continue;
119 memcpy(ns[i].sin6.sin6_addr.s6_addr+12,
120 &ns[i].sin.sin_addr, 4);
121 memcpy(ns[i].sin6.sin6_addr.s6_addr,
122 "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
123 ns[i].sin6.sin6_family = AF_INET6;
124 ns[i].sin6.sin6_flowinfo = 0;
125 ns[i].sin6.sin6_scope_id = 0;
129 memset(alens, 0, sizeof *alens * nqueries);
133 retry_interval = timeout / attempts;
135 t1 = t2 - retry_interval;
137 for (; t2-t0 < timeout; t2=mtime()) {
138 if (t2-t1 >= retry_interval) {
139 /* Query all configured namservers in parallel */
140 for (i=0; i<nqueries; i++)
142 for (j=0; j<nns; j++)
143 sendto(fd, queries[i],
144 qlens[i], MSG_NOSIGNAL,
149 /* Wait for a response, or until time to retry */
150 if (poll(&pfd, 1, t1+retry_interval-t2) <= 0) continue;
152 while ((rlen = recvfrom(fd, answers[next], asize, 0,
153 (void *)&sa, (socklen_t[1]){sl})) >= 0) {
155 /* Ignore non-identifiable packets (no query id) */
156 if (rlen < 2) continue;
158 /* Ignore replies from addresses we didn't send to */
159 for (i=0; i<nns && memcmp(ns+i, &sa, sl); i++);
160 if (i==nns) continue;
162 /* Find which query this answer goes with, if any */
163 for (i=next; i<nqueries && (
164 answers[next][0] != queries[i][0] ||
165 answers[next][1] != queries[i][1] ); i++);
166 if (i==nqueries) continue;
167 if (alens[i]) continue;
169 /* Store answer in the right slot, or update next
170 * available temp slot if it's already in place. */
173 for (; next<nqueries && alens[next]; next++);
175 memcpy(answers[i], answers[next], rlen);
177 if (next == nqueries) goto out;
181 pthread_cleanup_pop(1);