include/fetch/ft_dns.h

filter: Statementstmt 87.3% 55 / 63 Branchbran 64.2% 18 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.2% 73 / 91 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_obs_perl.h ↑ Summary xs/loop.xs »
1 #ifndef FT_DNS_H
2 #define FT_DNS_H
3  
4 /* ft_dns.h - resolving a name without stopping the loop.
5 *
6 * ft_h1_start used to call getaddrinfo() inline. Everything else in Fetch is
7 * loop-driven, so that one call was the only thing in a request that could
8 * stall every OTHER connection on the loop: a nameserver taking three seconds
9 * froze a loop serving hundreds of sockets, and no amount of concurrency
10 * above helped.
11 *
12 * ---- why a thread, and not a DNS client -----------------------------------
13 *
14 * Speaking DNS over UDP on the existing loop would need no thread and no new
15 * link flag. It is rejected because getaddrinfo is not a DNS client, it is
16 * the system's whole resolution policy: /etc/hosts, nsswitch (LDAP,
17 * systemd-resolved, mDNS for .local), search domains, and RFC 6724 ordering.
18 * A hand-written resolver that spoke only DNS would be asynchronous and would
19 * also quietly stop resolving names that resolve today, which is a worse bug
20 * than the one being fixed. Running the real call somewhere else keeps every
21 * one of those semantics. It is what curl does by default too.
22 *
23 * ---- the shape ------------------------------------------------------------
24 *
25 * One detached thread per lookup, and a pipe. The thread calls getaddrinfo,
26 * stores the answer in the shared struct, writes one byte and exits; the loop
27 * watches the read end and picks the answer up. The byte is only the wakeup -
28 * and the write is also what PUBLISHES the struct's fields to the loop
29 * thread, since the read cannot complete before the write.
30 *
31 * THE THREAD TOUCHES NO PERL. No interpreter, no SVs, no Newx/Safefree (which
32 * want an implicit context) - plain malloc and free, which is why the
33 * allocations here look unlike the rest of the dist. The only calls it makes
34 * are getaddrinfo, write, close and free.
35 *
36 * The read end belongs to the connection: it goes in c->fd while the state is
37 * FT_RESOLVING, so the arming, the deadline timer and the teardown that
38 * already exist all work on it unchanged, and a resolve that never answers
39 * fails the request with the ordinary request timeout.
40 *
41 * Lifetime is a refcount of two, one per thread, taken under a mutex; whoever
42 * drops the last one frees. That is what makes cancelling safe: a request torn
43 * down mid-resolve drops its reference and returns immediately, and the thread
44 * frees the struct when it eventually finishes and finds itself alone. Each
45 * side closes only its own end of the pipe, so neither can close a descriptor
46 * the other still holds - or, worse, one the kernel has since given to
47 * something else.
48 *
49 * Without pthreads (FT_HAVE_PTHREAD unset) and on native Windows, ft_dns_start
50 * returns 0 and the caller resolves inline, exactly as before. That is a
51 * degradation in concurrency, never in correctness.
52 */
53  
54 #if defined(FT_HAVE_PTHREAD) && !defined(_WIN32)
55 # define FT_DNS_ASYNC 1
56 # include <pthread.h>
57 # include <unistd.h>
58 #else
59 # define FT_DNS_ASYNC 0
60 #endif
61  
62 typedef struct ft_dns {
63 char *host;
64 char *port;
65 struct addrinfo *ai; /* the answer, freed with the struct */
66 int gai; /* getaddrinfo's return code */
67 int fd_w; /* the THREAD's end of the pipe, only */
68 #if FT_DNS_ASYNC
69 pthread_mutex_t lock;
70 #endif
71 int refs; /* guarded by lock */
72 } ft_dns;
73  
74 /* strdup with plain malloc. Not Newx: the resolver thread frees these and has
75 * no interpreter to hand a Safefree. Not strdup(3) either, which is POSIX
76 * rather than C89 and is not declared everywhere this builds. */
77 12 static char *ft_strdup_plain(const char *s) {
78 size_t n;
79 char *p;
80 12 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 0 12 12
81 12 n = strlen(s) + 1;
82 12 p = (char *)malloc(n);
83 12 ▶ if (p) memcpy(p, s, n);
Branch: 1 branch
BranchTrueFalseTotal
if (p) memcpy(p, s, n); 12 0 12
84 12 return p;
85 }
86  
87 /* Drop one reference; the last one frees. Called from both threads, which is
88 * the whole reason the count is guarded. */
89 12 static void ft_dns_unref(ft_dns *d) {
90 int last;
91 12 ▶ if (!d) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return; 0 12 12
92 #if FT_DNS_ASYNC
93 12 pthread_mutex_lock(&d->lock);
94 12 last = (--d->refs == 0);
95 12 pthread_mutex_unlock(&d->lock);
96 #else
97 last = (--d->refs == 0);
98 #endif
99 12 ▶ if (!last) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!last) return; 6 6 12
100 #if FT_DNS_ASYNC
101 6 pthread_mutex_destroy(&d->lock);
102 #endif
103 /* Reaching zero means the other side has already finished with the
104 * struct, so nothing here can be racing a writer. */
105 6 ▶ if (d->ai) freeaddrinfo(d->ai);
Branch: 1 branch
BranchTrueFalseTotal
if (d->ai) freeaddrinfo(d->ai); 5 1 6
106 6 free(d->host);
107 6 free(d->port);
108 6 free(d);
109 }
110  
111 #if FT_DNS_ASYNC
112  
113 6 static void *ft_dns_thread(void *arg) {
114 6 ft_dns *d = (ft_dns *)arg;
115 6 struct addrinfo hints, *ai = NULL;
116 int gai;
117 6 char b = 1;
118  
119 6 memset(&hints, 0, sizeof hints);
120 6 hints.ai_family = AF_UNSPEC;
121 6 hints.ai_socktype = SOCK_STREAM;
122 6 gai = getaddrinfo(d->host, d->port, &hints, &ai);
123 6 d->gai = gai;
124 6 ▶ d->ai = (gai == 0) ? ai : NULL;
Branch: 1 branch
BranchTrueFalseTotal
d->ai = (gai == 0) ? ai : NULL; 5 1 6
125 6 ▶ if (gai != 0 && ai) freeaddrinfo(ai); /* a failure can still allocate */
Branch: 2 branches
BranchTrueFalseTotal
if (gai != 0 && ai) freeaddrinfo(ai); /* a failure can still allocate */ 1 5 6
if (gai != 0 && ai) freeaddrinfo(ai); /* a failure can still allocate */ 0 1 1
126  
127 /* The wakeup. A short write or an EPIPE - the request was cancelled and
128 * the loop closed its end - is not an error worth reporting to anyone:
129 * the close below wakes a watcher just as well as the byte does. */
130 6 if (write(d->fd_w, &b, 1) < 0) { /* the close is the backstop */ }
131 6 close(d->fd_w);
132 6 ft_dns_unref(d);
133 6 return NULL;
134 }
135  
136 /* Start resolving host:port. Returns the read end of the pipe (>= 0) with
137 * *out set, or -1 if a thread could not be started - in which case the caller
138 * falls back to resolving inline and nothing is left allocated. */
139 6 static int ft_dns_start(const char *host, const char *port, ft_dns **out) {
140 ft_dns *d;
141 int fds[2];
142 pthread_t tid;
143 pthread_attr_t attr;
144 int rc;
145  
146 6 *out = NULL;
147 6 ▶ if (pipe(fds) != 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (pipe(fds) != 0) return -1; 0 6 6
148  
149 6 d = (ft_dns *)calloc(1, sizeof *d);
150 6 ▶ if (!d) { close(fds[0]); close(fds[1]); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!d) { close(fds[0]); close(fds[1]); return -1; } 0 6 6
151 6 d->host = ft_strdup_plain(host);
152 6 d->port = ft_strdup_plain(port);
153 6 d->fd_w = fds[1];
154 6 d->refs = 2; /* this side, and the thread */
155 6 ▶ if (!d->host || !d->port || pthread_mutex_init(&d->lock, NULL) != 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (!d->host || !d->port || pthread_mutex_init(&d->lock, NULL) != 0) { 6 0 6
if (!d->host || !d->port || pthread_mutex_init(&d->lock, NULL) != 0) { 6 0 6
if (!d->host || !d->port || pthread_mutex_init(&d->lock, NULL) != 0) { 0 6 6
156 0 free(d->host); free(d->port); free(d);
157 0 close(fds[0]); close(fds[1]);
158 0 return -1;
159 }
160  
161 /* Detached: nothing ever joins it, and an unjoined joinable thread leaks
162 * its stack for the life of the process. */
163 6 pthread_attr_init(&attr);
164 6 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
165 6 rc = pthread_create(&tid, &attr, ft_dns_thread, d);
166 6 pthread_attr_destroy(&attr);
167 6 ▶ if (rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) { 0 6 6
168 0 d->refs = 1; /* no thread took the other one */
169 0 close(fds[1]);
170 0 ft_dns_unref(d);
171 0 close(fds[0]);
172 0 return -1;
173 }
174 /* fds[1] belongs to the thread from here; this side must not touch it. */
175 6 *out = d;
176 6 return fds[0];
177 }
178  
179 #else /* no threads: the caller resolves inline, as it always did */
180  
181 static int ft_dns_start(const char *host, const char *port, ft_dns **out) {
182 (void)host; (void)port;
183 *out = NULL;
184 return -1;
185 }
186  
187 #endif /* FT_DNS_ASYNC */
188  
189 /* Is this a literal address rather than a name? An IPv4 or IPv6 literal, or a
190 * bracketed IPv6 the URL parser has already unwrapped, needs no resolution at
191 * all - AI_NUMERICHOST makes getaddrinfo a parse with no network in it, so
192 * those requests keep their old zero-latency path and start no thread. Every
193 * test in this dist connects to 127.0.0.1, which is exactly this case. */
194 119 static int ft_dns_numeric(const char *host, const char *port,
195 struct addrinfo **ai) {
196 struct addrinfo hints;
197 119 memset(&hints, 0, sizeof hints);
198 119 hints.ai_family = AF_UNSPEC;
199 119 hints.ai_socktype = SOCK_STREAM;
200 119 hints.ai_flags = AI_NUMERICHOST;
201 119 return getaddrinfo(host, port, &hints, ai) == 0;
202 }
203  
204 #endif /* FT_DNS_H */
« include/fetch/ft_obs_perl.h ↑ Summary xs/loop.xs »