cares.xs

filter: Statementstmt 90.8% 737 / 811 Branchbran 62.0% 505 / 814 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.4% 1242 / 1625 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/cares.pm ↑ Summary
1 /*
2 * EV::cares - high-performance async DNS resolver using c-ares and EV
3 */
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 #include "EVAPI.h"
10  
11 #include <ares.h>
12  
13 #if ARES_VERSION < 0x012208
14 #error EV::cares requires c-ares >= 1.34.8
15 #endif
16  
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <stdlib.h>
24  
25 /* suppress c-ares deprecation warnings - these functions still work */
26 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
27  
28 /* DNS type constants (removed from ares_dns.h in newer c-ares) */
29 #ifndef T_A
30 #define T_A 1
31 #endif
32 #ifndef T_NS
33 #define T_NS 2
34 #endif
35 #ifndef T_CNAME
36 #define T_CNAME 5
37 #endif
38 #ifndef T_SOA
39 #define T_SOA 6
40 #endif
41 #ifndef T_PTR
42 #define T_PTR 12
43 #endif
44 #ifndef T_MX
45 #define T_MX 15
46 #endif
47 #ifndef T_TXT
48 #define T_TXT 16
49 #endif
50 #ifndef T_AAAA
51 #define T_AAAA 28
52 #endif
53 #ifndef T_SRV
54 #define T_SRV 33
55 #endif
56 #ifndef T_NAPTR
57 #define T_NAPTR 35
58 #endif
59 #ifndef T_DS
60 #define T_DS 43
61 #endif
62 #ifndef T_RRSIG
63 #define T_RRSIG 46
64 #endif
65 #ifndef T_DNSKEY
66 #define T_DNSKEY 48
67 #endif
68 #ifndef T_TLSA
69 #define T_TLSA 52
70 #endif
71 #ifndef T_SVCB
72 #define T_SVCB 64
73 #endif
74 #ifndef T_HTTPS
75 #define T_HTTPS 65
76 #endif
77 #ifndef T_CAA
78 #define T_CAA 257
79 #endif
80 #ifndef T_ANY
81 #define T_ANY 255
82 #endif
83  
84 /* Modern ares_dns_record_t API (HTTPS/SVCB parsing) — c-ares >= 1.28 */
85 #ifndef HAVE_ARES_DNS_PARSE
86 # if defined(ARES_VERSION) && ARES_VERSION >= 0x011C00
87 # define HAVE_ARES_DNS_PARSE 1
88 # endif
89 #endif
90  
91 /* DNS class constants */
92 #ifndef C_IN
93 #define C_IN 1
94 #endif
95 #ifndef C_CHAOS
96 #define C_CHAOS 3
97 #endif
98 #ifndef C_HS
99 #define C_HS 4
100 #endif
101 #ifndef C_ANY
102 #define C_ANY 255
103 #endif
104  
105 /* helper macro for BOOT constant registration */
106 #define CONST_IV(stash, name) newCONSTSUB(stash, #name, newSViv(name))
107  
108 /* flags that may not exist in older c-ares */
109 #ifndef ARES_FLAG_NO_DFLT_SVR
110 #define ARES_FLAG_NO_DFLT_SVR 0
111 #endif
112 #ifndef ARES_FLAG_DNS0x20
113 #define ARES_FLAG_DNS0x20 0
114 #endif
115 #ifndef ARES_ESERVICE
116 #define ARES_ESERVICE 25
117 #endif
118 #ifndef ARES_ENOSERVER
119 #define ARES_ENOSERVER 26
120 #endif
121  
122 #define EV_CARES_MAGIC 0xCA7E5001
123 #define INLINE_IO 16
124  
125 #define REQUIRE_LIVE(self) \
126 STMT_START { \
127 if ((self)->magic != EV_CARES_MAGIC) croak("EV::cares: invalid object"); \
128 if ((self)->destroyed) croak("EV::cares: resolver is destroyed"); \
129 } STMT_END
130  
131 #define REQUIRE_CB(cb) \
132 STMT_START { \
133 if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) \
134 croak("EV::cares: callback must be a CODE reference"); \
135 } STMT_END
136  
137 typedef struct ev_cares_s ev_cares_t;
138 typedef ev_cares_t *EV__cares;
139  
140 typedef struct {
141 ev_io watcher;
142 ares_socket_t fd;
143 } ev_cares_io_t;
144  
145 typedef struct ev_cares_extra_io_s {
146 ev_cares_io_t io;
147 struct ev_cares_extra_io_s *next;
148 } ev_cares_extra_io_t;
149  
150 struct ev_cares_s {
151 U32 magic;
152 ares_channel channel;
153 struct ev_loop *loop;
154 SV *loop_sv; /* keeps a custom EV::Loop alive; NULL for default loop */
155 ev_timer timer;
156 ev_cares_io_t ios[INLINE_IO];
157 ev_cares_extra_io_t *extra_ios;
158 int active_queries;
159 int destroyed;
160 int in_callback; /* ares call depth; keeps callbacks from freeing self */
161 int free_pending; /* deferred Safefree after last callback */
162 int cleanup_pending; /* deferred channel teardown after last callback */
163 int last_timeouts; /* timeouts count from the most recent callback */
164 };
165  
166 typedef struct {
167 ev_cares_t *resolver;
168 SV *cb;
169 int qtype;
170 int with_ttl; /* addrinfo_cb returns hashrefs when set */
171 int by_addr; /* host_cb returns h_name+h_aliases instead of h_addr_list */
172 } ev_cares_req_t;
173  
174 /* forward declarations */
175 static void update_timer(ev_cares_t *self);
176 static void cleanup_now(ev_cares_t *self);
177  
178 /* Bracket an ares_* call that may fire callbacks synchronously. Guards
179 * against the user destroying the resolver from inside such a callback: both
180 * the Safefree of self AND the ares_destroy of the channel are deferred until
181 * the outermost ares_* call has fully unwound, because the channel is still
182 * referenced further up the C stack until then (destroying it inline is a
183 * use-after-free that musl turns into a hard crash; glibc only tolerates it). */
184 #define ARES_CALL_BEGIN(self) (self)->in_callback++
185 /* Deferred teardown at the outermost unwind: cleanup_now may free self
186 (it consumes free_pending), so nothing may touch self after it runs. */
187 #define ARES_CALL_END(self) \
188 STMT_START { \
189 if (--(self)->in_callback == 0) { \
190 if ((self)->cleanup_pending) cleanup_now(self); \
191 else if ((self)->free_pending) { \
192 (self)->free_pending = 0; \
193 Safefree(self); \
194 } else update_timer(self); \
195 } else { \
196 update_timer(self); \
197 } \
198 } STMT_END
199 static void io_cb(EV_P_ ev_io *w, int revents);
200 static void timer_cb(EV_P_ ev_timer *w, int revents);
201 static void sock_state_cb(void *data, ares_socket_t fd, int readable, int writable);
202 static void cleanup(ev_cares_t *self);
203  
204 /* qtype is set by callers that need it (only search()); Newxz zeroes it. */
205 static ev_cares_req_t *
206 452 new_req(pTHX_ ev_cares_t *self, SV *cb) {
207 ev_cares_req_t *req;
208 452 Newxz(req, 1, ev_cares_req_t);
209 452 req->resolver = self;
210 452 req->cb = SvREFCNT_inc_simple_NN(SvRV(cb));
211 452 self->active_queries++;
212 452 return req;
213 }
214  
215 /* Build a comma-separated server list. Each AV element may be either
216 a plain string (e.g. "8.8.8.8" or "8.8.8.8:53") or a hashref
217 { host => ..., port => ... }. Sparse-array holes are skipped
218 without emitting a stray comma. */
219 static SV *
220 10 av_to_csv(pTHX_ AV *av) {
221 10 SSize_t i, len = av_len(av) + 1;
222 10 SV *csv = sv_2mortal(newSVpvs(""));
223 10 int written = 0;
224 43 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 33 10 43
225 33 SV **elem = av_fetch(av, i, 0);
226 33 ▶ if (!elem) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!elem) continue; 0 33 33
227 /* Validate (and croak) before bumping `written` so an aborted
228 iteration can't leave a dangling separator. */
229 39 ▶ if (SvROK(*elem) && SvTYPE(SvRV(*elem)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*elem) && SvTYPE(SvRV(*elem)) == SVt_PVHV) { 6 27 33
if (SvROK(*elem) && SvTYPE(SvRV(*elem)) == SVt_PVHV) { 6 0 6
230 6 HV *hv = (HV *)SvRV(*elem);
231 6 SV **host = hv_fetchs(hv, "host", 0);
232 6 SV **port = hv_fetchs(hv, "port", 0);
233 6 ▶ if (!host)
Branch: 1 branch
BranchTrueFalseTotal
if (!host) 0 6 6
234 0 croak("EV::cares: server hashref missing 'host' key (index %d)",
235 (int)i);
236 6 ▶ if (written++) sv_catpvs(csv, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (written++) sv_catpvs(csv, ","); 1 5 6
237 6 ▶ if (port) {
Branch: 1 branch
BranchTrueFalseTotal
if (port) { 5 1 6
238 5 const char *h = SvPV_nolen(*host);
239 5 const char *scope = strchr(h, '%');
240 5 ▶ int bracket = h[0] != '[' && strchr(h, ':') != NULL;
Branch: 2 branches
BranchTrueFalseTotal
int bracket = h[0] != '[' && strchr(h, ':') != NULL; 3 2 5
int bracket = h[0] != '[' && strchr(h, ':') != NULL; 2 1 3
241 5 ▶ if (bracket) sv_catpvs(csv, "[");
Branch: 1 branch
BranchTrueFalseTotal
if (bracket) sv_catpvs(csv, "["); 2 3 5
242 5 ▶ sv_catpvn(csv, h, scope ? (STRLEN)(scope - h) : strlen(h));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(csv, h, scope ? (STRLEN)(scope - h) : strlen(h)); 0 5 5
243 5 ▶ if (bracket) sv_catpvs(csv, "]");
Branch: 1 branch
BranchTrueFalseTotal
if (bracket) sv_catpvs(csv, "]"); 2 3 5
244 5 sv_catpvf(csv, ":%d", (int)SvIV(*port));
245 5 ▶ if (scope) sv_catpv(csv, scope);
Branch: 1 branch
BranchTrueFalseTotal
if (scope) sv_catpv(csv, scope); 0 5 5
246 } else {
247 1 sv_catsv(csv, *host);
248 }
249 } else {
250 27 ▶ if (written++) sv_catpvs(csv, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (written++) sv_catpvs(csv, ","); 22 5 27
251 27 sv_catsv(csv, *elem);
252 }
253 }
254 10 return csv;
255 }
256  
257 static void
258 452 free_req(pTHX_ ev_cares_req_t *req) {
259 452 req->resolver->active_queries--;
260 452 SvREFCNT_dec(req->cb);
261 452 Safefree(req);
262 452 }
263  
264 /* ---- EV integration ---- */
265  
266 static void
267 770 sock_state_cb(void *data, ares_socket_t fd, int readable, int writable) {
268 dTHX;
269 770 ev_cares_t *self = (ev_cares_t *)data;
270 770 ev_cares_io_t *io = NULL;
271 770 ev_cares_extra_io_t **extra = NULL;
272 770 int i, events = 0;
273  
274 770 ▶ if (self->destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->destroyed) return; 98 672 770
275  
276 672 ▶ if (readable) events |= EV_READ;
Branch: 1 branch
BranchTrueFalseTotal
if (readable) events |= EV_READ; 385 287 672
277 672 ▶ if (writable) events |= EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (writable) events |= EV_WRITE; 0 672 672
278  
279 /* find existing watcher for this fd */
280 9738 ▶ for (i = 0; i < INLINE_IO; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < INLINE_IO; i++) 9224 514 9738
281 9224 ▶ if (self->ios[i].fd == fd) { io = &self->ios[i]; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->ios[i].fd == fd) { io = &self->ios[i]; break; } 158 9066 9224
282 672 ▶ if (!io) {
Branch: 1 branch
BranchTrueFalseTotal
if (!io) { 514 158 672
283 4377 ▶ for (extra = &self->extra_ios; *extra; extra = &(*extra)->next)
Branch: 1 branch
BranchTrueFalseTotal
for (extra = &self->extra_ios; *extra; extra = &(*extra)->next) 3992 385 4377
284 3992 ▶ if ((*extra)->io.fd == fd) { io = &(*extra)->io; break; }
Branch: 1 branch
BranchTrueFalseTotal
if ((*extra)->io.fd == fd) { io = &(*extra)->io; break; } 129 3863 3992
285 }
286  
287 672 ▶ if (events) {
Branch: 1 branch
BranchTrueFalseTotal
if (events) { 385 287 672
288 385 ▶ if (!io) {
Branch: 1 branch
BranchTrueFalseTotal
if (!io) { 385 0 385
289 4691 ▶ for (i = 0; i < INLINE_IO; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < INLINE_IO; i++) 4482 209 4691
290 4482 ▶ if (self->ios[i].fd == ARES_SOCKET_BAD) { io = &self->ios[i]; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->ios[i].fd == ARES_SOCKET_BAD) { io = &self->ios[i]; break; } 176 4306 4482
291 385 ▶ if (!io) {
Branch: 1 branch
BranchTrueFalseTotal
if (!io) { 209 176 385
292 209 Newxz(*extra, 1, ev_cares_extra_io_t);
293 209 io = &(*extra)->io;
294 }
295 385 io->fd = fd;
296 385 ev_io_init(&io->watcher, io_cb, (int)fd, events);
297 385 io->watcher.data = (void *)self;
298 } else {
299 0 ev_io_stop(self->loop, &io->watcher);
300 0 ev_io_set(&io->watcher, (int)fd, events);
301 }
302 385 ev_io_start(self->loop, &io->watcher);
303 287 ▶ } else if (io) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (io) { 287 0 287
304 287 ev_io_stop(self->loop, &io->watcher);
305 287 io->fd = ARES_SOCKET_BAD;
306 287 ▶ if (extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra) { 129 158 287
307 129 ev_cares_extra_io_t *old = *extra;
308 129 *extra = old->next;
309 129 Safefree(old);
310 }
311 }
312 }
313  
314 /* io_cb / timer_cb: cleanup() sets destroyed=1 before nulling channel,
315 so the destroyed flag implies !channel; one check covers both. */
316 static void
317 225 io_cb(EV_P_ ev_io *w, int revents) {
318 225 ev_cares_t *self = (ev_cares_t *)w->data;
319 225 ev_cares_io_t *io = (ev_cares_io_t *)w;
320 225 ares_socket_t rfd = ARES_SOCKET_BAD, wfd = ARES_SOCKET_BAD;
321  
322 225 ▶ if (self->destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->destroyed) return; 0 225 225
323  
324 225 ▶ if (revents & EV_READ) rfd = io->fd;
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_READ) rfd = io->fd; 225 0 225
325 225 ▶ if (revents & EV_WRITE) wfd = io->fd;
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) wfd = io->fd; 0 225 225
326 225 ▶ if ((revents & EV_ERROR) && rfd == ARES_SOCKET_BAD && wfd == ARES_SOCKET_BAD)
Branch: 3 branches
BranchTrueFalseTotal
if ((revents & EV_ERROR) && rfd == ARES_SOCKET_BAD && wfd == ARES_SOCKET_BAD) 0 225 225
if ((revents & EV_ERROR) && rfd == ARES_SOCKET_BAD && wfd == ARES_SOCKET_BAD) 0 0 0
if ((revents & EV_ERROR) && rfd == ARES_SOCKET_BAD && wfd == ARES_SOCKET_BAD) 0 0 0
327 0 rfd = io->fd;
328  
329 225 ARES_CALL_BEGIN(self);
330 225 ares_process_fd(self->channel, rfd, wfd);
331 225 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 225 0 225
ARES_CALL_END(self); 2 223 225
ARES_CALL_END(self); 0 223 223
332 }
333  
334 static void
335 1 timer_cb(EV_P_ ev_timer *w, int revents) {
336 1 ev_cares_t *self = (ev_cares_t *)w->data;
337  
338 1 ▶ if (self->destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->destroyed) return; 0 1 1
339  
340 1 ARES_CALL_BEGIN(self);
341 1 ares_process_fd(self->channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
342 1 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 1 0 1
ARES_CALL_END(self); 0 1 1
ARES_CALL_END(self); 0 1 1
343 }
344  
345 static void
346 702 update_timer(ev_cares_t *self) {
347 struct timeval tv, *tvp;
348  
349 819 ▶ if (self->destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->destroyed) return; 0 702 702
350  
351 702 tvp = ares_timeout(self->channel, NULL, &tv);
352 702 ▶ if (!tvp && !ev_is_active(&self->timer) && !ev_is_pending(&self->timer))
Branch: 3 branches
BranchTrueFalseTotal
if (!tvp && !ev_is_active(&self->timer) && !ev_is_pending(&self->timer)) 157 545 702
if (!tvp && !ev_is_active(&self->timer) && !ev_is_pending(&self->timer)) 117 40 157
if (!tvp && !ev_is_active(&self->timer) && !ev_is_pending(&self->timer)) 117 0 117
353 117 return;
354 585 ev_timer_stop(self->loop, &self->timer);
355 585 ▶ if (tvp) {
Branch: 1 branch
BranchTrueFalseTotal
if (tvp) { 545 40 585
356 545 double after = (double)tvp->tv_sec + (double)tvp->tv_usec / 1e6;
357 545 ▶ if (after < 0.001) after = 0.001;
Branch: 1 branch
BranchTrueFalseTotal
if (after < 0.001) after = 0.001; 0 545 545
358 545 ev_timer_set(&self->timer, after, 0.);
359 545 ev_timer_start(self->loop, &self->timer);
360 }
361 }
362  
363 /* Actual teardown: stop watchers, destroy the channel, release the loop.
364 Only safe when not inside an ares_* call (see cleanup()). ares_destroy
365 fires each still-pending callback inline (ARES_EDESTRUCTION); bracketing
366 it with in_callback++/-- keeps those nested epilogues off the teardown
367 branches. This is also the single site that consumes free_pending, so
368 cleanup_now may free self -- callers must not touch self afterwards. */
369 static void
370 117 cleanup_now(ev_cares_t *self) {
371 int i;
372  
373 117 self->cleanup_pending = 0;
374  
375 117 ev_timer_stop(self->loop, &self->timer);
376 1989 ▶ for (i = 0; i < INLINE_IO; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < INLINE_IO; i++) { 1872 117 1989
377 1872 ▶ if (self->ios[i].fd != ARES_SOCKET_BAD) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ios[i].fd != ARES_SOCKET_BAD) { 18 1854 1872
378 18 ev_io_stop(self->loop, &self->ios[i].watcher);
379 18 self->ios[i].fd = ARES_SOCKET_BAD;
380 }
381 }
382 197 ▶ while (self->extra_ios) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->extra_ios) { 80 117 197
383 80 ev_cares_extra_io_t *extra = self->extra_ios;
384 80 self->extra_ios = extra->next;
385 80 ev_io_stop(self->loop, &extra->io.watcher);
386 80 Safefree(extra);
387 }
388  
389 /* Nested epilogues fired by ares_destroy must see in_callback > 0. */
390 117 self->in_callback++;
391 117 ares_destroy(self->channel);
392 117 self->in_callback--;
393 117 self->channel = NULL;
394  
395 /* Release our hold on a user-supplied EV::Loop. Doing this only after
396 the watchers are stopped means the loop is still valid above. */
397 117 ▶ if (self->loop_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->loop_sv) { 3 114 117
398 dTHX;
399 3 SvREFCNT_dec(self->loop_sv);
400 3 self->loop_sv = NULL;
401 }
402  
403 /* single consumer of free_pending; nothing may touch self after */
404 117 ▶ if (self->free_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->free_pending) { 2 115 117
405 2 self->free_pending = 0;
406 2 Safefree(self);
407 }
408 117 }
409  
410 static void
411 134 cleanup(ev_cares_t *self) {
412 134 ▶ if (self->destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->destroyed) return; 17 117 134
413 117 self->destroyed = 1;
414  
415 /* If a callback dispatched synchronously from inside an ares_* call asked
416 us to destroy (e.g. $resolver->destroy from a resolve callback that
417 ares_getaddrinfo fired inline), the channel is still referenced further
418 up the C stack. Tearing it down now is a use-after-free; defer it to
419 the ARES_CALL_END / CB_EPILOGUE that unwinds in_callback to zero. */
420 117 ▶ if (self->in_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_callback) { 5 112 117
421 5 self->cleanup_pending = 1;
422 5 return;
423 }
424  
425 112 cleanup_now(self);
426 }
427  
428 /* ---- Perl callbacks, guarded by the enclosing ares call ---- */
429  
430 /* All c-ares callbacks share this prologue. The macro relies on each
431 caller having an `int timeouts` parameter (per the c-ares callback
432 ABI) so it can record the latest retry count on the resolver. */
433 #define CB_PROLOGUE(arg, status_val) \
434 ev_cares_req_t *req = (ev_cares_req_t *)(arg); \
435 ev_cares_t *self = req->resolver; \
436 dTHX; dSP; \
437 self->last_timeouts = timeouts; \
438 ENTER; SAVETMPS; \
439 PUSHMARK(SP); \
440 mXPUSHi(status_val)
441  
442 #define CB_EPILOGUE \
443 PUTBACK; \
444 { \
445 call_sv(req->cb, G_DISCARD | G_EVAL); \
446 if (SvTRUE(ERRSV)) { \
447 SPAGAIN; \
448 PUSHMARK(SP); \
449 mXPUSHs(newSVsv(ERRSV)); \
450 PUTBACK; \
451 call_pv("EV::cares::_callback_error", G_DISCARD | G_EVAL); \
452 sv_setsv(ERRSV, &PL_sv_undef); \
453 } \
454 } \
455 FREETMPS; LEAVE; \
456 free_req(aTHX_ req)
457  
458 /* ---- query callbacks ---- */
459  
460 static void
461 73 addrinfo_cb(void *arg, int status, int timeouts, struct ares_addrinfo *result) {
462 73 ▶ CB_PROLOGUE(arg, status);
Branch: 2 branches
BranchTrueFalseTotal
CB_PROLOGUE(arg, status); 0 73 73
CB_PROLOGUE(arg, status); 0 73 73
463  
464 73 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 52 21 73
465 52 ▶ if (status == ARES_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == ARES_SUCCESS) { 52 0 52
466 struct ares_addrinfo_node *node;
467 struct ares_addrinfo_cname *cn;
468 52 const char *cname = NULL;
469 52 ▶ if (req->with_ttl && result->cnames) {
Branch: 2 branches
BranchTrueFalseTotal
if (req->with_ttl && result->cnames) { 7 45 52
if (req->with_ttl && result->cnames) { 2 5 7
470 2 cname = result->name;
471 2 ▶ if (cname) {
Branch: 1 branch
BranchTrueFalseTotal
if (cname) { 2 0 2
472 4 ▶ for (cn = result->cnames; cn; cn = cn->next)
Branch: 1 branch
BranchTrueFalseTotal
for (cn = result->cnames; cn; cn = cn->next) 3 1 4
473 3 ▶ if (cn->alias && strcasecmp(cname, cn->alias) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (cn->alias && strcasecmp(cname, cn->alias) == 0) { 3 0 3
if (cn->alias && strcasecmp(cname, cn->alias) == 0) { 1 2 3
474 1 cname = NULL;
475 1 break;
476 }
477 }
478 2 ▶ for (cn = result->cnames; !cname && cn; cn = cn->next) {
Branch: 2 branches
BranchTrueFalseTotal
for (cn = result->cnames; !cname && cn; cn = cn->next) { 1 1 2
for (cn = result->cnames; !cname && cn; cn = cn->next) { 1 0 1
479 struct ares_addrinfo_cname *alias;
480 3 ▶ for (alias = result->cnames; alias; alias = alias->next)
Branch: 1 branch
BranchTrueFalseTotal
for (alias = result->cnames; alias; alias = alias->next) 2 1 3
481 2 ▶ if (alias->alias && strcasecmp(cn->name, alias->alias) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (alias->alias && strcasecmp(cn->name, alias->alias) == 0) 2 0 2
if (alias->alias && strcasecmp(cn->name, alias->alias) == 0) 0 2 2
482 0 break;
483 1 ▶ if (!alias) { cname = cn->name; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!alias) { cname = cn->name; break; } 1 0 1
484 }
485 }
486 156 ▶ for (node = result->nodes; node; node = node->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (node = result->nodes; node; node = node->ai_next) { 104 52 156
487 char ip[INET6_ADDRSTRLEN];
488 104 ▶ if (node->ai_family == AF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->ai_family == AF_INET) { 63 41 104
489 63 struct sockaddr_in *sin = (struct sockaddr_in *)node->ai_addr;
490 63 inet_ntop(AF_INET, &sin->sin_addr, ip, sizeof(ip));
491 41 ▶ } else if (node->ai_family == AF_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (node->ai_family == AF_INET6) { 41 0 41
492 41 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)node->ai_addr;
493 41 inet_ntop(AF_INET6, &sin6->sin6_addr, ip, sizeof(ip));
494 } else {
495 0 continue;
496 }
497 104 ▶ if (req->with_ttl) {
Branch: 1 branch
BranchTrueFalseTotal
if (req->with_ttl) { 9 95 104
498 9 HV *hv = newHV();
499 9 hv_stores(hv, "addr", newSVpv(ip, 0));
500 9 hv_stores(hv, "family", newSViv(node->ai_family));
501 9 hv_stores(hv, "ttl", newSViv(node->ai_ttl));
502 9 hv_stores(hv, "timeouts", newSViv(timeouts));
503 9 ▶ if (cname)
Branch: 1 branch
BranchTrueFalseTotal
if (cname) 2 7 9
504 2 hv_stores(hv, "canonname", newSVpv(cname, 0));
505 9 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 9 9
506 } else {
507 95 ▶ mXPUSHp(ip, strlen(ip));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(ip, strlen(ip)); 0 95 95
508 }
509 }
510 }
511 52 ares_freeaddrinfo(result);
512 }
513  
514 73 ▶ CB_EPILOGUE;
Branch: 7 branches
BranchTrueFalseTotal
CB_EPILOGUE; 73 0 73
CB_EPILOGUE; 2 71 73
CB_EPILOGUE; 0 2 2
CB_EPILOGUE; 0 2 2
CB_EPILOGUE; 2 0 2
CB_EPILOGUE; 2 0 2
CB_EPILOGUE; 73 0 73
515 73 }
516  
517 static void
518 4 host_cb(void *arg, int status, int timeouts, struct hostent *hostent) {
519 4 ▶ CB_PROLOGUE(arg, status);
Branch: 2 branches
BranchTrueFalseTotal
CB_PROLOGUE(arg, status); 0 4 4
CB_PROLOGUE(arg, status); 0 4 4
520  
521 4 ▶ if (status == ARES_SUCCESS && hostent) {
Branch: 2 branches
BranchTrueFalseTotal
if (status == ARES_SUCCESS && hostent) { 4 0 4
if (status == ARES_SUCCESS && hostent) { 4 0 4
522 4 ▶ if (req->by_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if (req->by_addr) { 3 1 4
523 /* reverse lookup: return canonical name + any aliases */
524 3 ▶ if (hostent->h_name)
Branch: 1 branch
BranchTrueFalseTotal
if (hostent->h_name) 3 0 3
525 3 ▶ mXPUSHp(hostent->h_name, strlen(hostent->h_name));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(hostent->h_name, strlen(hostent->h_name)); 0 3 3
526 3 ▶ if (hostent->h_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (hostent->h_aliases) { 3 0 3
527 char **p;
528 4 ▶ for (p = hostent->h_aliases; *p; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = hostent->h_aliases; *p; p++) 1 3 4
529 1 ▶ mXPUSHp(*p, strlen(*p));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(*p, strlen(*p)); 0 1 1
530 }
531 } else {
532 /* forward lookup: return the address list */
533 char **p;
534 2 ▶ for (p = hostent->h_addr_list; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = hostent->h_addr_list; *p; p++) { 1 1 2
535 char ip[INET6_ADDRSTRLEN];
536 1 ▶ if (hostent->h_addrtype == AF_INET)
Branch: 1 branch
BranchTrueFalseTotal
if (hostent->h_addrtype == AF_INET) 1 0 1
537 1 inet_ntop(AF_INET, *p, ip, sizeof(ip));
538 else
539 0 inet_ntop(AF_INET6, *p, ip, sizeof(ip));
540 1 ▶ mXPUSHp(ip, strlen(ip));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(ip, strlen(ip)); 0 1 1
541 }
542 }
543 }
544  
545 4 ▶ CB_EPILOGUE;
Branch: 7 branches
BranchTrueFalseTotal
CB_EPILOGUE; 4 0 4
CB_EPILOGUE; 0 4 4
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 4 0 4
546 4 }
547  
548 static void
549 342 raw_cb(void *arg, int status, int timeouts, unsigned char *abuf, int alen) {
550 342 ▶ CB_PROLOGUE(arg, status);
Branch: 2 branches
BranchTrueFalseTotal
CB_PROLOGUE(arg, status); 0 342 342
CB_PROLOGUE(arg, status); 0 342 342
551  
552 342 ▶ if (status == ARES_SUCCESS && abuf && alen > 0)
Branch: 3 branches
BranchTrueFalseTotal
if (status == ARES_SUCCESS && abuf && alen > 0) 182 160 342
if (status == ARES_SUCCESS && abuf && alen > 0) 182 0 182
if (status == ARES_SUCCESS && abuf && alen > 0) 182 0 182
553 182 ▶ mXPUSHp((char *)abuf, alen);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp((char *)abuf, alen); 0 182 182
554  
555 342 ▶ CB_EPILOGUE;
Branch: 7 branches
BranchTrueFalseTotal
CB_EPILOGUE; 342 0 342
CB_EPILOGUE; 0 342 342
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 342 0 342
556 342 }
557  
558 static void
559 32 search_cb(void *arg, int status, int timeouts, unsigned char *abuf, int alen) {
560 32 ▶ CB_PROLOGUE(arg, status);
Branch: 2 branches
BranchTrueFalseTotal
CB_PROLOGUE(arg, status); 0 32 32
CB_PROLOGUE(arg, status); 0 32 32
561  
562 32 ▶ if (status == ARES_SUCCESS && abuf && alen > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (status == ARES_SUCCESS && abuf && alen > 0) { 31 1 32
if (status == ARES_SUCCESS && abuf && alen > 0) { 31 0 31
if (status == ARES_SUCCESS && abuf && alen > 0) { 31 0 31
563 31 switch (req->qtype) {
564  
565 7 case T_A: {
566 7 struct hostent *host = NULL;
567 7 ▶ if (ares_parse_a_reply(abuf, alen, &host, NULL, NULL) == ARES_SUCCESS && host) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_a_reply(abuf, alen, &host, NULL, NULL) == ARES_SUCCESS && host) { 7 0 7
if (ares_parse_a_reply(abuf, alen, &host, NULL, NULL) == ARES_SUCCESS && host) { 7 0 7
568 char **p;
569 25 ▶ for (p = host->h_addr_list; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = host->h_addr_list; *p; p++) { 18 7 25
570 char ip[INET_ADDRSTRLEN];
571 18 inet_ntop(AF_INET, *p, ip, sizeof(ip));
572 18 ▶ mXPUSHp(ip, strlen(ip));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(ip, strlen(ip)); 0 18 18
573 }
574 7 ares_free_hostent(host);
575 }
576 7 break;
577 }
578  
579 1 case T_AAAA: {
580 1 struct hostent *host = NULL;
581 1 ▶ if (ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL) == ARES_SUCCESS && host) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL) == ARES_SUCCESS && host) { 1 0 1
if (ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL) == ARES_SUCCESS && host) { 1 0 1
582 char **p;
583 5 ▶ for (p = host->h_addr_list; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = host->h_addr_list; *p; p++) { 4 1 5
584 char ip[INET6_ADDRSTRLEN];
585 4 inet_ntop(AF_INET6, *p, ip, sizeof(ip));
586 4 ▶ mXPUSHp(ip, strlen(ip));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(ip, strlen(ip)); 0 4 4
587 }
588 1 ares_free_hostent(host);
589 }
590 1 break;
591 }
592  
593 2 case T_MX: {
594 2 struct ares_mx_reply *mx_out = NULL;
595 2 ▶ if (ares_parse_mx_reply(abuf, alen, &mx_out) == ARES_SUCCESS && mx_out) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_mx_reply(abuf, alen, &mx_out) == ARES_SUCCESS && mx_out) { 2 0 2
if (ares_parse_mx_reply(abuf, alen, &mx_out) == ARES_SUCCESS && mx_out) { 2 0 2
596 struct ares_mx_reply *mx;
597 4 ▶ for (mx = mx_out; mx; mx = mx->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (mx = mx_out; mx; mx = mx->next) { 2 2 4
598 2 HV *hv = newHV();
599 2 hv_stores(hv, "priority", newSViv(mx->priority));
600 2 hv_stores(hv, "host", newSVpv(mx->host, 0));
601 2 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 2 2
602 }
603 2 ares_free_data(mx_out);
604 }
605 2 break;
606 }
607  
608 1 case T_SRV: {
609 1 struct ares_srv_reply *srv_out = NULL;
610 1 ▶ if (ares_parse_srv_reply(abuf, alen, &srv_out) == ARES_SUCCESS && srv_out) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_srv_reply(abuf, alen, &srv_out) == ARES_SUCCESS && srv_out) { 1 0 1
if (ares_parse_srv_reply(abuf, alen, &srv_out) == ARES_SUCCESS && srv_out) { 1 0 1
611 struct ares_srv_reply *srv;
612 2 ▶ for (srv = srv_out; srv; srv = srv->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (srv = srv_out; srv; srv = srv->next) { 1 1 2
613 1 HV *hv = newHV();
614 1 hv_stores(hv, "priority", newSViv(srv->priority));
615 1 hv_stores(hv, "weight", newSViv(srv->weight));
616 1 hv_stores(hv, "port", newSViv(srv->port));
617 1 hv_stores(hv, "target", newSVpv(srv->host, 0));
618 1 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 1 1
619 }
620 1 ares_free_data(srv_out);
621 }
622 1 break;
623 }
624  
625 2 case T_TXT: {
626 2 struct ares_txt_ext *txt_out = NULL;
627 2 ▶ if (ares_parse_txt_reply_ext(abuf, alen, &txt_out) == ARES_SUCCESS && txt_out) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_txt_reply_ext(abuf, alen, &txt_out) == ARES_SUCCESS && txt_out) { 2 0 2
if (ares_parse_txt_reply_ext(abuf, alen, &txt_out) == ARES_SUCCESS && txt_out) { 2 0 2
628 struct ares_txt_ext *txt;
629 2 SV *current = NULL;
630 22 ▶ for (txt = txt_out; txt; txt = txt->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (txt = txt_out; txt; txt = txt->next) { 20 2 22
631 20 ▶ if (txt->record_start || !current) {
Branch: 2 branches
BranchTrueFalseTotal
if (txt->record_start || !current) { 1 19 20
if (txt->record_start || !current) { 0 1 1
632 19 ▶ if (current) mXPUSHs(current);
Branch: 2 branches
BranchTrueFalseTotal
if (current) mXPUSHs(current); 17 2 19
if (current) mXPUSHs(current); 0 17 17
633 19 current = newSVpvn((char *)txt->txt, txt->length);
634 } else {
635 1 sv_catpvn(current, (char *)txt->txt, txt->length);
636 }
637 }
638 2 ▶ if (current) mXPUSHs(current);
Branch: 2 branches
BranchTrueFalseTotal
if (current) mXPUSHs(current); 2 0 2
if (current) mXPUSHs(current); 0 2 2
639 2 ares_free_data(txt_out);
640 }
641 2 break;
642 }
643  
644 1 case T_NS: {
645 1 struct hostent *host = NULL;
646 1 ▶ if (ares_parse_ns_reply(abuf, alen, &host) == ARES_SUCCESS && host) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_ns_reply(abuf, alen, &host) == ARES_SUCCESS && host) { 1 0 1
if (ares_parse_ns_reply(abuf, alen, &host) == ARES_SUCCESS && host) { 1 0 1
647 1 ▶ if (host->h_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (host->h_aliases) { 1 0 1
648 char **p;
649 5 ▶ for (p = host->h_aliases; *p; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = host->h_aliases; *p; p++) 4 1 5
650 4 ▶ mXPUSHp(*p, strlen(*p));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(*p, strlen(*p)); 0 4 4
651 }
652 1 ares_free_hostent(host);
653 }
654 1 break;
655 }
656  
657 1 case T_SOA: {
658 1 struct ares_soa_reply *soa = NULL;
659 1 ▶ if (ares_parse_soa_reply(abuf, alen, &soa) == ARES_SUCCESS && soa) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_soa_reply(abuf, alen, &soa) == ARES_SUCCESS && soa) { 1 0 1
if (ares_parse_soa_reply(abuf, alen, &soa) == ARES_SUCCESS && soa) { 1 0 1
660 1 HV *hv = newHV();
661 1 hv_stores(hv, "mname", newSVpv(soa->nsname, 0));
662 1 hv_stores(hv, "rname", newSVpv(soa->hostmaster, 0));
663 1 hv_stores(hv, "serial", newSVuv(soa->serial));
664 1 hv_stores(hv, "refresh", newSVuv(soa->refresh));
665 1 hv_stores(hv, "retry", newSVuv(soa->retry));
666 1 hv_stores(hv, "expire", newSVuv(soa->expire));
667 1 hv_stores(hv, "minttl", newSVuv(soa->minttl));
668 1 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 1 1
669 1 ares_free_data(soa);
670 }
671 1 break;
672 }
673  
674 0 case T_PTR: {
675 0 struct hostent *host = NULL;
676 /* family only affects h_addrtype/h_length, not PTR name parsing;
677 we pass NULL addr so these fields are unused */
678 0 ▶ if (ares_parse_ptr_reply(abuf, alen, NULL, 0, AF_UNSPEC, &host) == ARES_SUCCESS && host) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_ptr_reply(abuf, alen, NULL, 0, AF_UNSPEC, &host) == ARES_SUCCESS && host) { 0 0 0
if (ares_parse_ptr_reply(abuf, alen, NULL, 0, AF_UNSPEC, &host) == ARES_SUCCESS && host) { 0 0 0
679 0 ▶ if (host->h_name)
Branch: 1 branch
BranchTrueFalseTotal
if (host->h_name) 0 0 0
680 0 ▶ mXPUSHp(host->h_name, strlen(host->h_name));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(host->h_name, strlen(host->h_name)); 0 0 0
681 0 ▶ if (host->h_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (host->h_aliases) { 0 0 0
682 char **p;
683 0 ▶ for (p = host->h_aliases; *p; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = host->h_aliases; *p; p++) 0 0 0
684 0 ▶ mXPUSHp(*p, strlen(*p));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(*p, strlen(*p)); 0 0 0
685 }
686 0 ares_free_hostent(host);
687 }
688 0 break;
689 }
690  
691 1 case T_NAPTR: {
692 1 struct ares_naptr_reply *naptr_out = NULL;
693 1 ▶ if (ares_parse_naptr_reply(abuf, alen, &naptr_out) == ARES_SUCCESS && naptr_out) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_naptr_reply(abuf, alen, &naptr_out) == ARES_SUCCESS && naptr_out) { 1 0 1
if (ares_parse_naptr_reply(abuf, alen, &naptr_out) == ARES_SUCCESS && naptr_out) { 1 0 1
694 struct ares_naptr_reply *n;
695 4 ▶ for (n = naptr_out; n; n = n->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = naptr_out; n; n = n->next) { 3 1 4
696 3 HV *hv = newHV();
697 3 hv_stores(hv, "order", newSViv(n->order));
698 3 hv_stores(hv, "preference", newSViv(n->preference));
699 3 hv_stores(hv, "flags", newSVpv((char *)n->flags, 0));
700 3 hv_stores(hv, "service", newSVpv((char *)n->service, 0));
701 3 hv_stores(hv, "regexp", newSVpv((char *)n->regexp, 0));
702 3 hv_stores(hv, "replacement", newSVpv(n->replacement, 0));
703 3 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 3 3
704 }
705 1 ares_free_data(naptr_out);
706 }
707 1 break;
708 }
709  
710 1 case T_CAA: {
711 1 struct ares_caa_reply *caa_out = NULL;
712 1 ▶ if (ares_parse_caa_reply(abuf, alen, &caa_out) == ARES_SUCCESS && caa_out) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_parse_caa_reply(abuf, alen, &caa_out) == ARES_SUCCESS && caa_out) { 1 0 1
if (ares_parse_caa_reply(abuf, alen, &caa_out) == ARES_SUCCESS && caa_out) { 1 0 1
713 struct ares_caa_reply *c;
714 12 ▶ for (c = caa_out; c; c = c->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = caa_out; c; c = c->next) { 11 1 12
715 11 HV *hv = newHV();
716 11 hv_stores(hv, "critical", newSViv(c->critical));
717 11 hv_stores(hv, "property", newSVpvn((char *)c->property, c->plength));
718 11 hv_stores(hv, "value", newSVpvn((char *)c->value, c->length));
719 11 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 11 11
720 }
721 1 ares_free_data(caa_out);
722 }
723 1 break;
724 }
725  
726 #ifdef HAVE_ARES_DNS_PARSE
727 12 case T_DS:
728 case T_RRSIG:
729 case T_DNSKEY: {
730 /* DS/DNSKEY/RRSIG aren't in c-ares' parsed type set, so they
731 arrive as ARES_REC_TYPE_RAW_RR. Pull out the rdata and
732 parse the wire format ourselves (RFC 4034). */
733 12 ares_dns_record_t *dnsrec = NULL;
734 12 ▶ if (ares_dns_parse(abuf, alen, ARES_DNS_PARSE_AN_EXT_RAW, &dnsrec) == ARES_SUCCESS && dnsrec) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_dns_parse(abuf, alen, ARES_DNS_PARSE_AN_EXT_RAW, &dnsrec) == ARES_SUCCESS && dnsrec) { 12 0 12
if (ares_dns_parse(abuf, alen, ARES_DNS_PARSE_AN_EXT_RAW, &dnsrec) == ARES_SUCCESS && dnsrec) { 12 0 12
735 12 size_t cnt = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
736 size_t i;
737 25 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 13 12 25
738 13 const ares_dns_rr_t *rr = ares_dns_record_rr_get_const(
739 dnsrec, ARES_SECTION_ANSWER, i);
740 unsigned short raw_type;
741 const unsigned char *rd;
742 size_t rdlen;
743 HV *hv;
744  
745 13 ▶ if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_RAW_RR)
Branch: 1 branch
BranchTrueFalseTotal
if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_RAW_RR) 0 13 13
746 0 continue;
747 13 raw_type = ares_dns_rr_get_u16(rr, ARES_RR_RAW_RR_TYPE);
748 13 ▶ if (raw_type != req->qtype) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (raw_type != req->qtype) continue; 0 13 13
749  
750 13 rd = ares_dns_rr_get_bin(rr, ARES_RR_RAW_RR_DATA, &rdlen);
751 13 ▶ if (!rd) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!rd) continue; 0 13 13
752  
753 13 hv = newHV();
754 13 ▶ if (raw_type == T_DS && rdlen >= 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (raw_type == T_DS && rdlen >= 4) { 4 9 13
if (raw_type == T_DS && rdlen >= 4) { 4 0 4
755 4 hv_stores(hv, "key_tag", newSViv((rd[0] << 8) | rd[1]));
756 4 hv_stores(hv, "algorithm", newSViv(rd[2]));
757 4 hv_stores(hv, "digest_type", newSViv(rd[3]));
758 4 hv_stores(hv, "digest",
759 newSVpvn((const char *)rd + 4, rdlen - 4));
760 9 ▶ } else if (raw_type == T_DNSKEY && rdlen >= 4) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (raw_type == T_DNSKEY && rdlen >= 4) { 5 4 9
} else if (raw_type == T_DNSKEY && rdlen >= 4) { 5 0 5
761 5 hv_stores(hv, "flags", newSViv((rd[0] << 8) | rd[1]));
762 5 hv_stores(hv, "protocol", newSViv(rd[2]));
763 5 hv_stores(hv, "algorithm", newSViv(rd[3]));
764 5 hv_stores(hv, "public_key",
765 newSVpvn((const char *)rd + 4, rdlen - 4));
766 8 ▶ } else if (raw_type == T_RRSIG && rdlen >= 18) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (raw_type == T_RRSIG && rdlen >= 18) { 4 0 4
} else if (raw_type == T_RRSIG && rdlen >= 18) { 4 0 4
767 4 size_t pos = 18;
768 char signer[256];
769 4 size_t snlen = 0;
770 4 int signer_overflow = 0;
771 4 int saw_null_label = 0;
772 /* uncompressed DNS labels (RFC 4034 sec 3.1.7) */
773 10 ▶ while (pos < rdlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < rdlen) { 10 0 10
774 10 unsigned char l = rd[pos++];
775 size_t need;
776 10 ▶ if (l == 0) { saw_null_label = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (l == 0) { saw_null_label = 1; break; } 4 6 10
777 6 ▶ if ((l & 0xc0) || pos + l > rdlen) {
Branch: 2 branches
BranchTrueFalseTotal
if ((l & 0xc0) || pos + l > rdlen) { 6 0 6
if ((l & 0xc0) || pos + l > rdlen) { 0 6 6
778 0 pos = rdlen + 1; /* malformed */
779 0 break;
780 }
781 6 need = (snlen ? 1 : 0) + l;
782 6 ▶ if (snlen + need >= sizeof signer) {
Branch: 1 branch
BranchTrueFalseTotal
if (snlen + need >= sizeof signer) { 0 6 6
783 0 signer_overflow = 1;
784 0 break;
785 }
786 6 ▶ if (snlen) signer[snlen++] = '.';
Branch: 1 branch
BranchTrueFalseTotal
if (snlen) signer[snlen++] = '.'; 3 3 6
787 6 memcpy(signer + snlen, rd + pos, l);
788 6 snlen += l;
789 6 pos += l;
790 }
791 /* RFC 4034 §3.1.7: signer's name is uncompressed wire
792 format with explicit root label. Reject pointer/
793 overrun, presentation-buffer overflow, or labels
794 that were consumed without a closing null. */
795 4 ▶ if (pos > rdlen || signer_overflow || !saw_null_label) {
Branch: 3 branches
BranchTrueFalseTotal
if (pos > rdlen || signer_overflow || !saw_null_label) { 4 0 4
if (pos > rdlen || signer_overflow || !saw_null_label) { 4 0 4
if (pos > rdlen || signer_overflow || !saw_null_label) { 0 4 4
796 0 SvREFCNT_dec((SV *)hv);
797 0 continue;
798 }
799  
800 4 hv_stores(hv, "type_covered",
801 newSViv((rd[0] << 8) | rd[1]));
802 4 hv_stores(hv, "algorithm", newSViv(rd[2]));
803 4 hv_stores(hv, "labels", newSViv(rd[3]));
804 4 hv_stores(hv, "original_ttl",
805 newSVuv(((U32)rd[4] << 24) | ((U32)rd[5] << 16) |
806 ((U32)rd[6] << 8) | (U32)rd[7]));
807 4 hv_stores(hv, "sig_expiration",
808 newSVuv(((U32)rd[ 8] << 24) | ((U32)rd[ 9] << 16) |
809 ((U32)rd[10] << 8) | (U32)rd[11]));
810 4 hv_stores(hv, "sig_inception",
811 newSVuv(((U32)rd[12] << 24) | ((U32)rd[13] << 16) |
812 ((U32)rd[14] << 8) | (U32)rd[15]));
813 4 hv_stores(hv, "key_tag",
814 newSViv((rd[16] << 8) | rd[17]));
815 4 hv_stores(hv, "signer_name", newSVpvn(signer, snlen));
816 4 hv_stores(hv, "signature",
817 newSVpvn((const char *)rd + pos, rdlen - pos));
818 } else {
819 0 SvREFCNT_dec((SV *)hv);
820 0 continue;
821 }
822 13 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 13 13
823 }
824 12 ares_dns_record_destroy(dnsrec);
825 }
826 12 break;
827 }
828  
829 1 case T_TLSA: {
830 1 ares_dns_record_t *dnsrec = NULL;
831 1 ▶ if (ares_dns_parse(abuf, alen, 0, &dnsrec) == ARES_SUCCESS && dnsrec) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_dns_parse(abuf, alen, 0, &dnsrec) == ARES_SUCCESS && dnsrec) { 1 0 1
if (ares_dns_parse(abuf, alen, 0, &dnsrec) == ARES_SUCCESS && dnsrec) { 1 0 1
832 1 size_t cnt = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
833 size_t i;
834 3 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 2 1 3
835 2 const ares_dns_rr_t *rr = ares_dns_record_rr_get_const(
836 dnsrec, ARES_SECTION_ANSWER, i);
837 HV *hv;
838 const unsigned char *data;
839 size_t data_len;
840  
841 2 ▶ if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_TLSA) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_TLSA) continue; 0 2 2
842  
843 2 hv = newHV();
844 2 hv_stores(hv, "cert_usage",
845 newSViv(ares_dns_rr_get_u8(rr, ARES_RR_TLSA_CERT_USAGE)));
846 2 hv_stores(hv, "selector",
847 newSViv(ares_dns_rr_get_u8(rr, ARES_RR_TLSA_SELECTOR)));
848 2 hv_stores(hv, "matching_type",
849 newSViv(ares_dns_rr_get_u8(rr, ARES_RR_TLSA_MATCH)));
850 2 data = ares_dns_rr_get_bin(rr, ARES_RR_TLSA_DATA, &data_len);
851 2 ▶ hv_stores(hv, "data",
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(hv, "data", 2 0 2
hv_stores(hv, "data", 2 0 2
852 newSVpvn(data ? (const char *)data : "", data ? data_len : 0));
853 2 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 2 2
854 }
855 1 ares_dns_record_destroy(dnsrec);
856 }
857 1 break;
858 }
859  
860 1 case T_HTTPS:
861 case T_SVCB: {
862 1 ares_dns_record_t *dnsrec = NULL;
863 1 ▶ if (ares_dns_parse(abuf, alen, 0, &dnsrec) == ARES_SUCCESS && dnsrec) {
Branch: 2 branches
BranchTrueFalseTotal
if (ares_dns_parse(abuf, alen, 0, &dnsrec) == ARES_SUCCESS && dnsrec) { 1 0 1
if (ares_dns_parse(abuf, alen, 0, &dnsrec) == ARES_SUCCESS && dnsrec) { 1 0 1
864 1 size_t cnt = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
865 size_t i, j;
866 2 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 1 1 2
867 1 const ares_dns_rr_t *rr = ares_dns_record_rr_get_const(
868 dnsrec, ARES_SECTION_ANSWER, i);
869 1 ares_dns_rec_type_t rtype = ares_dns_rr_get_type(rr);
870 ares_dns_rr_key_t prio_key, target_key, params_key;
871 unsigned short prio;
872 const char *target;
873 HV *hv, *params;
874 size_t opt_cnt;
875  
876 1 ▶ if (rtype == ARES_REC_TYPE_HTTPS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rtype == ARES_REC_TYPE_HTTPS) { 1 0 1
877 1 prio_key = ARES_RR_HTTPS_PRIORITY;
878 1 target_key = ARES_RR_HTTPS_TARGET;
879 1 params_key = ARES_RR_HTTPS_PARAMS;
880 0 ▶ } else if (rtype == ARES_REC_TYPE_SVCB) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (rtype == ARES_REC_TYPE_SVCB) { 0 0 0
881 0 prio_key = ARES_RR_SVCB_PRIORITY;
882 0 target_key = ARES_RR_SVCB_TARGET;
883 0 params_key = ARES_RR_SVCB_PARAMS;
884 } else {
885 0 continue;
886 }
887  
888 1 prio = ares_dns_rr_get_u16(rr, prio_key);
889 1 target = ares_dns_rr_get_str(rr, target_key);
890  
891 1 hv = newHV();
892 1 hv_stores(hv, "priority", newSViv(prio));
893 1 ▶ hv_stores(hv, "target", newSVpv(target ? target : "", 0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(hv, "target", newSVpv(target ? target : "", 0)); 1 0 1
894  
895 1 params = newHV();
896 1 opt_cnt = ares_dns_rr_get_opt_cnt(rr, params_key);
897 4 ▶ for (j = 0; j < opt_cnt; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < opt_cnt; j++) { 3 1 4
898 const unsigned char *val;
899 size_t vlen;
900 3 unsigned short opt_id = ares_dns_rr_get_opt(
901 rr, params_key, j, &val, &vlen);
902  
903 3 switch (opt_id) {
904 1 case 1: { /* alpn: list of length-prefixed strings */
905 1 AV *av = newAV();
906 1 size_t pos = 0;
907 3 ▶ while (pos < vlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < vlen) { 2 1 3
908 2 size_t plen = val[pos++];
909 /* zero-length entry would loop forever; bail */
910 2 ▶ if (plen == 0 || pos + plen > vlen) break;
Branch: 2 branches
BranchTrueFalseTotal
if (plen == 0 || pos + plen > vlen) break; 2 0 2
if (plen == 0 || pos + plen > vlen) break; 2 0 2
911 2 av_push(av, newSVpvn((const char *)&val[pos], plen));
912 2 pos += plen;
913 }
914 1 hv_stores(params, "alpn", newRV_noinc((SV *)av));
915 1 break;
916 }
917 0 case 2: /* no-default-alpn */
918 0 hv_stores(params, "no_default_alpn", newSViv(1));
919 0 break;
920 0 case 3: /* port: u16 */
921 0 ▶ if (vlen >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (vlen >= 2) 0 0 0
922 0 hv_stores(params, "port",
923 newSViv((val[0] << 8) | val[1]));
924 0 break;
925 1 case 4: { /* ipv4hint: list of in_addr */
926 1 AV *av = newAV();
927 size_t pos;
928 3 ▶ for (pos = 0; pos + 4 <= vlen; pos += 4) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos + 4 <= vlen; pos += 4) { 2 1 3
929 char ipstr[INET_ADDRSTRLEN];
930 struct in_addr a; /* aligned local — val[pos]
931 is byte-aligned only */
932 2 memcpy(&a, &val[pos], sizeof a);
933 2 inet_ntop(AF_INET, &a, ipstr, sizeof ipstr);
934 2 av_push(av, newSVpv(ipstr, 0));
935 }
936 1 hv_stores(params, "ipv4hint", newRV_noinc((SV *)av));
937 1 break;
938 }
939 0 case 5: /* ech: opaque base64-able blob */
940 0 hv_stores(params, "ech",
941 newSVpvn((const char *)val, vlen));
942 0 break;
943 1 case 6: { /* ipv6hint: list of in6_addr */
944 1 AV *av = newAV();
945 size_t pos;
946 3 ▶ for (pos = 0; pos + 16 <= vlen; pos += 16) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos + 16 <= vlen; pos += 16) { 2 1 3
947 char ipstr[INET6_ADDRSTRLEN];
948 struct in6_addr a; /* aligned local */
949 2 memcpy(&a, &val[pos], sizeof a);
950 2 inet_ntop(AF_INET6, &a, ipstr, sizeof ipstr);
951 2 av_push(av, newSVpv(ipstr, 0));
952 }
953 1 hv_stores(params, "ipv6hint", newRV_noinc((SV *)av));
954 1 break;
955 }
956 0 case 7: /* dohpath */
957 0 hv_stores(params, "dohpath",
958 newSVpvn((const char *)val, vlen));
959 0 break;
960 0 default: {
961 char k[16];
962 0 int klen = snprintf(k, sizeof k, "key%u", opt_id);
963 0 hv_store(params, k, klen,
964 newSVpvn((const char *)val, vlen), 0);
965 0 break;
966 }
967 }
968 }
969 1 hv_stores(hv, "params", newRV_noinc((SV *)params));
970 1 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 1 1
971 }
972 1 ares_dns_record_destroy(dnsrec);
973 }
974 1 break;
975 }
976 #endif
977  
978 0 default:
979 0 ▶ mXPUSHp((char *)abuf, alen);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp((char *)abuf, alen); 0 0 0
980 0 break;
981 }
982 }
983  
984 32 ▶ CB_EPILOGUE;
Branch: 7 branches
BranchTrueFalseTotal
CB_EPILOGUE; 32 0 32
CB_EPILOGUE; 0 32 32
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 32 0 32
985 32 }
986  
987 static void
988 1 nameinfo_cb(void *arg, int status, int timeouts, char *node, char *service) {
989 1 ▶ CB_PROLOGUE(arg, status);
Branch: 2 branches
BranchTrueFalseTotal
CB_PROLOGUE(arg, status); 0 1 1
CB_PROLOGUE(arg, status); 0 1 1
990  
991 1 ▶ if (status == ARES_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == ARES_SUCCESS) { 1 0 1
992 1 ▶ if (node)
Branch: 1 branch
BranchTrueFalseTotal
if (node) 1 0 1
993 1 ▶ mXPUSHp(node, strlen(node));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(node, strlen(node)); 0 1 1
994 else
995 0 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 0 0
996  
997 1 ▶ if (service)
Branch: 1 branch
BranchTrueFalseTotal
if (service) 0 1 1
998 0 ▶ mXPUSHp(service, strlen(service));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(service, strlen(service)); 0 0 0
999 else
1000 1 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 1 1
1001 }
1002  
1003 1 ▶ CB_EPILOGUE;
Branch: 7 branches
BranchTrueFalseTotal
CB_EPILOGUE; 1 0 1
CB_EPILOGUE; 0 1 1
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 0 0 0
CB_EPILOGUE; 1 0 1
1004 1 }
1005  
1006  
1007 MODULE = EV::cares PACKAGE = EV::cares PREFIX = ev_cares_
1008  
1009 BOOT:
1010 {
1011 20 ▶ I_EV_API("EV::cares");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::cares"); 0 20 20
I_EV_API("EV::cares"); 20 0 20
I_EV_API("EV::cares"); 0 20 20
1012 {
1013 int version, rc;
1014 20 const char *version_string = ares_version(&version);
1015 20 ▶ if (version < 0x012208)
Branch: 1 branch
BranchTrueFalseTotal
if (version < 0x012208) 0 20 20
1016 0 croak("EV::cares requires c-ares >= 1.34.8; linked version is %s", version_string);
1017 20 rc = ares_library_init(ARES_LIB_INIT_ALL);
1018 20 ▶ if (rc != ARES_SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != ARES_SUCCESS) 0 20 20
1019 0 croak("EV::cares: ares_library_init: %s", ares_strerror(rc));
1020 }
1021 {
1022 20 HV *stash = gv_stashpvn("EV::cares", 9, GV_ADD);
1023  
1024 /* status codes */
1025 20 CONST_IV(stash, ARES_SUCCESS);
1026 20 CONST_IV(stash, ARES_ENODATA);
1027 20 CONST_IV(stash, ARES_EFORMERR);
1028 20 CONST_IV(stash, ARES_ESERVFAIL);
1029 20 CONST_IV(stash, ARES_ENOTFOUND);
1030 20 CONST_IV(stash, ARES_ENOTIMP);
1031 20 CONST_IV(stash, ARES_EREFUSED);
1032 20 CONST_IV(stash, ARES_EBADQUERY);
1033 20 CONST_IV(stash, ARES_EBADNAME);
1034 20 CONST_IV(stash, ARES_EBADFAMILY);
1035 20 CONST_IV(stash, ARES_EBADRESP);
1036 20 CONST_IV(stash, ARES_ECONNREFUSED);
1037 20 CONST_IV(stash, ARES_ETIMEOUT);
1038 20 CONST_IV(stash, ARES_EOF);
1039 20 CONST_IV(stash, ARES_EFILE);
1040 20 CONST_IV(stash, ARES_ENOMEM);
1041 20 CONST_IV(stash, ARES_EDESTRUCTION);
1042 20 CONST_IV(stash, ARES_EBADSTR);
1043 20 CONST_IV(stash, ARES_EBADFLAGS);
1044 20 CONST_IV(stash, ARES_ENONAME);
1045 20 CONST_IV(stash, ARES_EBADHINTS);
1046 20 CONST_IV(stash, ARES_ENOTINITIALIZED);
1047 20 CONST_IV(stash, ARES_ECANCELLED);
1048 20 CONST_IV(stash, ARES_ESERVICE);
1049 20 CONST_IV(stash, ARES_ENOSERVER);
1050  
1051 /* DNS types */
1052 20 CONST_IV(stash, T_A);
1053 20 CONST_IV(stash, T_NS);
1054 20 CONST_IV(stash, T_CNAME);
1055 20 CONST_IV(stash, T_SOA);
1056 20 CONST_IV(stash, T_PTR);
1057 20 CONST_IV(stash, T_MX);
1058 20 CONST_IV(stash, T_TXT);
1059 20 CONST_IV(stash, T_AAAA);
1060 20 CONST_IV(stash, T_SRV);
1061 20 CONST_IV(stash, T_NAPTR);
1062 20 CONST_IV(stash, T_DS);
1063 20 CONST_IV(stash, T_RRSIG);
1064 20 CONST_IV(stash, T_DNSKEY);
1065 20 CONST_IV(stash, T_TLSA);
1066 20 CONST_IV(stash, T_SVCB);
1067 20 CONST_IV(stash, T_HTTPS);
1068 20 CONST_IV(stash, T_CAA);
1069 20 CONST_IV(stash, T_ANY);
1070  
1071 /* DNS classes */
1072 20 CONST_IV(stash, C_IN);
1073 20 CONST_IV(stash, C_CHAOS);
1074 20 CONST_IV(stash, C_HS);
1075 20 CONST_IV(stash, C_ANY);
1076  
1077 /* channel flags */
1078 20 CONST_IV(stash, ARES_FLAG_USEVC);
1079 20 CONST_IV(stash, ARES_FLAG_PRIMARY);
1080 20 CONST_IV(stash, ARES_FLAG_IGNTC);
1081 20 CONST_IV(stash, ARES_FLAG_NORECURSE);
1082 20 CONST_IV(stash, ARES_FLAG_STAYOPEN);
1083 20 CONST_IV(stash, ARES_FLAG_NOSEARCH);
1084 20 CONST_IV(stash, ARES_FLAG_NOALIASES);
1085 20 CONST_IV(stash, ARES_FLAG_NOCHECKRESP);
1086 20 CONST_IV(stash, ARES_FLAG_EDNS);
1087 20 CONST_IV(stash, ARES_FLAG_NO_DFLT_SVR);
1088 20 CONST_IV(stash, ARES_FLAG_DNS0x20);
1089  
1090 /* addrinfo hint flags */
1091 20 CONST_IV(stash, ARES_AI_CANONNAME);
1092 20 CONST_IV(stash, ARES_AI_NUMERICHOST);
1093 20 CONST_IV(stash, ARES_AI_PASSIVE);
1094 20 CONST_IV(stash, ARES_AI_NUMERICSERV);
1095 20 CONST_IV(stash, ARES_AI_V4MAPPED);
1096 20 CONST_IV(stash, ARES_AI_ALL);
1097 20 CONST_IV(stash, ARES_AI_ADDRCONFIG);
1098 20 CONST_IV(stash, ARES_AI_NOSORT);
1099  
1100 /* nameinfo flags */
1101 20 CONST_IV(stash, ARES_NI_NOFQDN);
1102 20 CONST_IV(stash, ARES_NI_NUMERICHOST);
1103 20 CONST_IV(stash, ARES_NI_NAMEREQD);
1104 20 CONST_IV(stash, ARES_NI_NUMERICSERV);
1105 20 CONST_IV(stash, ARES_NI_DGRAM);
1106 20 CONST_IV(stash, ARES_NI_TCP);
1107 20 CONST_IV(stash, ARES_NI_UDP);
1108  
1109 /* address families */
1110 20 CONST_IV(stash, AF_INET);
1111 20 CONST_IV(stash, AF_INET6);
1112 20 CONST_IV(stash, AF_UNSPEC);
1113 }
1114 }
1115  
1116 void
1117 new(class, ...)
1118 PPCODE:
1119 {
1120 120 const char *class_name = SvPV_nolen(ST(0));
1121 ev_cares_t *self;
1122 struct ares_options opts;
1123 120 int optmask = 0;
1124 int status, i;
1125 120 const char *servers = NULL;
1126 120 struct ev_loop *loop_ptr = EV_DEFAULT;
1127 120 SV *loop_sv = NULL;
1128  
1129 120 ▶ if ((items - 1) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) 0 120 120
1130 0 croak("EV::cares::new: odd number of arguments");
1131  
1132 120 memset(&opts, 0, sizeof(opts));
1133  
1134 311 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 194 117 311
1135 194 const char *key = SvPV_nolen(ST(i));
1136 194 SV *val = ST(i + 1);
1137  
1138 194 ▶ if (strEQ(key, "loop")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(key, "loop")) { 6 188 194
1139 6 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 5 1 6
1140 5 ▶ if (!SvROK(val) || !sv_derived_from(val, "EV::Loop"))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(val) || !sv_derived_from(val, "EV::Loop")) 4 1 5
if (!SvROK(val) || !sv_derived_from(val, "EV::Loop")) 1 3 4
1141 2 croak("EV::cares::new: 'loop' must be an EV::Loop instance");
1142 /* SvIV (not SvIVX) so any get-magic on the inner SV is
1143 honoured before extracting the loop pointer. */
1144 3 loop_ptr = INT2PTR(struct ev_loop *, SvIV(SvRV(val)));
1145 3 loop_sv = val; /* refcount bumped after struct alloc */
1146 }
1147 }
1148 188 ▶ else if (strEQ(key, "timeout")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "timeout")) { 33 155 188
1149 33 opts.timeout = (int)(SvNV(val) * 1000);
1150 33 optmask |= ARES_OPT_TIMEOUTMS;
1151 }
1152 155 ▶ else if (strEQ(key, "tries")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "tries")) { 33 122 155
1153 33 opts.tries = SvIV(val);
1154 33 optmask |= ARES_OPT_TRIES;
1155 }
1156 122 ▶ else if (strEQ(key, "ndots")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "ndots")) { 1 121 122
1157 1 opts.ndots = SvIV(val);
1158 1 optmask |= ARES_OPT_NDOTS;
1159 }
1160 121 ▶ else if (strEQ(key, "flags")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "flags")) { 7 114 121
1161 7 opts.flags = SvIV(val);
1162 7 optmask |= ARES_OPT_FLAGS;
1163 }
1164 114 ▶ else if (strEQ(key, "lookups")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "lookups")) { 49 65 114
1165 49 opts.lookups = SvPV_nolen(val); /* c-ares copies internally */
1166 49 optmask |= ARES_OPT_LOOKUPS;
1167 }
1168 65 ▶ else if (strEQ(key, "tcp_port")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "tcp_port")) { 0 65 65
1169 0 opts.tcp_port = (unsigned short)SvUV(val);
1170 0 optmask |= ARES_OPT_TCP_PORT;
1171 }
1172 65 ▶ else if (strEQ(key, "udp_port")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "udp_port")) { 0 65 65
1173 0 opts.udp_port = (unsigned short)SvUV(val);
1174 0 optmask |= ARES_OPT_UDP_PORT;
1175 }
1176 65 ▶ else if (strEQ(key, "servers")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "servers")) { 25 40 65
1177 25 ▶ if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { 7 18 25
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { 7 0 7
1178 7 AV *av = (AV *)SvRV(val);
1179 7 ▶ if (av_len(av) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(av) < 0) 1 6 7
1180 1 croak("EV::cares::new: 'servers' arrayref is empty");
1181 12 servers = SvPV_nolen(av_to_csv(aTHX_ av));
1182 } else {
1183 18 servers = SvPV_nolen(val);
1184 }
1185 }
1186 40 ▶ else if (strEQ(key, "rotate")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "rotate")) { 2 38 40
1187 2 ▶ if (SvTRUE(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) { 2 0 2
1188 2 #ifdef ARES_OPT_ROTATE
1189 optmask |= ARES_OPT_ROTATE;
1190 #endif
1191 38 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 1 37 38
1192 1 }
1193 1 #ifdef ARES_OPT_EDNSPSZ
1194 else if (strEQ(key, "ednspsz")) {
1195 37 ▶ opts.ednspsz = SvIV(val);
Branch: 1 branch
BranchTrueFalseTotal
opts.ednspsz = SvIV(val); 2 35 37
1196 2 optmask |= ARES_OPT_EDNSPSZ;
1197 2 }
1198 #endif
1199 35 ▶ #ifdef ARES_OPT_RESOLVCONF
Branch: 1 branch
BranchTrueFalseTotal
#ifdef ARES_OPT_RESOLVCONF 9 26 35
1200 9 else if (strEQ(key, "resolvconf")) {
1201 9 opts.resolvconf_path = SvPV_nolen(val); /* c-ares copies internally */
1202 optmask |= ARES_OPT_RESOLVCONF;
1203 26 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 7 19 26
1204 7 #endif
1205 7 #ifdef ARES_OPT_HOSTS_FILE
1206 else if (strEQ(key, "hosts_file")) {
1207 19 ▶ opts.hosts_path = SvPV_nolen(val); /* c-ares copies internally */
Branch: 1 branch
BranchTrueFalseTotal
opts.hosts_path = SvPV_nolen(val); /* c-ares copies internally */ 1 18 19
1208 1 optmask |= ARES_OPT_HOSTS_FILE;
1209 1 }
1210 #endif
1211 18 ▶ #ifdef ARES_OPT_UDP_MAX_QUERIES
Branch: 1 branch
BranchTrueFalseTotal
#ifdef ARES_OPT_UDP_MAX_QUERIES 18 0 18
1212 18 else if (strEQ(key, "udp_max_queries")) {
1213 18 opts.udp_max_queries = SvIV(val);
1214 optmask |= ARES_OPT_UDP_MAX_QUERIES;
1215 }
1216 0 #endif
1217 #ifdef ARES_OPT_MAXTIMEOUTMS
1218 else if (strEQ(key, "maxtimeout")) {
1219 opts.maxtimeout = (int)(SvNV(val) * 1000);
1220 117 optmask |= ARES_OPT_MAXTIMEOUTMS;
1221 117 }
1222 117 #endif
1223 #ifdef ARES_OPT_QUERY_CACHE
1224 else if (strEQ(key, "qcache")) {
1225 opts.qcache_max_ttl = (unsigned int)SvUV(val);
1226 117 ▶ optmask |= ARES_OPT_QUERY_CACHE;
Branch: 1 branch
BranchTrueFalseTotal
optmask |= ARES_OPT_QUERY_CACHE; 3 114 117
1227 1989 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 1872 117 1989
1228 1872 #endif
1229 else {
1230 117 warn("EV::cares::new: unknown option '%s'", key);
1231 117 }
1232 117 }
1233  
1234 117 Newxz(self, 1, ev_cares_t);
1235 117 ▶ self->magic = EV_CARES_MAGIC;
Branch: 1 branch
BranchTrueFalseTotal
self->magic = EV_CARES_MAGIC; 0 117 117
1236 0 ▶ self->loop = loop_ptr;
Branch: 1 branch
BranchTrueFalseTotal
self->loop = loop_ptr; 0 0 0
1237 0 /* Keep the EV::Loop's blessed object alive — its DESTROY calls
1238 0 ev_loop_destroy(). Holding the outer RV is not enough; inc the
1239 underlying SV (the IV that stores the loop pointer). */
1240 if (loop_sv) self->loop_sv = SvREFCNT_inc_simple_NN(SvRV(loop_sv));
1241 117 ▶ for (i = 0; i < INLINE_IO; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < INLINE_IO; i++) 22 95 117
1242 22 self->ios[i].fd = ARES_SOCKET_BAD;
1243 22 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
0 22 22
1244 0 opts.sock_state_cb = sock_state_cb;
1245 0 ▶ opts.sock_state_cb_data = self;
Branch: 1 branch
BranchTrueFalseTotal
opts.sock_state_cb_data = self; 0 0 0
1246 0 optmask |= ARES_OPT_SOCK_STATE_CB;
1247 0  
1248 status = ares_init_options(&self->channel, &opts, optmask);
1249 if (status != ARES_SUCCESS) {
1250 if (self->loop_sv) SvREFCNT_dec(self->loop_sv);
1251 117 Safefree(self);
1252 117 croak("EV::cares::new: ares_init_options: %s", ares_strerror(status));
1253 }
1254  
1255 117 if (servers) {
1256 117 status = ares_set_servers_csv(self->channel, servers);
1257 117 ▶ if (status != ARES_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != ARES_SUCCESS) { 0 117 117
1258 ares_destroy(self->channel);
1259 if (self->loop_sv) SvREFCNT_dec(self->loop_sv);
1260 Safefree(self);
1261 croak("EV::cares::new: set_servers: %s", ares_strerror(status));
1262 }
1263 }
1264  
1265 ev_timer_init(&self->timer, timer_cb, 0., 0.);
1266 self->timer.data = (void *)self;
1267  
1268 {
1269 SV *sv = newSV(0);
1270 sv_setref_pv(sv, class_name, (void *)self);
1271 XPUSHs(sv_2mortal(sv));
1272 }
1273 }
1274  
1275 void
1276 resolve(self, name, cb)
1277 EV::cares self
1278 const char *name
1279 SV *cb
1280 CODE:
1281 {
1282 struct ares_addrinfo_hints hints;
1283 ev_cares_req_t *req;
1284  
1285 64 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 64 64
REQUIRE_LIVE(self); 2 62 64
1286 62 ▶ REQUIRE_CB(cb);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_CB(cb); 61 1 62
REQUIRE_CB(cb); 0 61 61
1287  
1288 61 memset(&hints, 0, sizeof(hints));
1289 61 hints.ai_family = AF_UNSPEC;
1290 61 hints.ai_socktype = SOCK_STREAM;
1291  
1292 61 req = new_req(aTHX_ self, cb);
1293 61 ARES_CALL_BEGIN(self);
1294 61 ares_getaddrinfo(self->channel, name, NULL, &hints, addrinfo_cb, req);
1295 61 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 60 1 61
ARES_CALL_END(self); 2 58 60
ARES_CALL_END(self); 0 58 58
1296 }
1297  
1298 void
1299 resolve_ttl(self, name, cb)
1300 EV::cares self
1301 const char *name
1302 SV *cb
1303 CODE:
1304 {
1305 struct ares_addrinfo_hints hints;
1306 ev_cares_req_t *req;
1307  
1308 4 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 4 4
REQUIRE_LIVE(self); 0 4 4
1309 4 ▶ REQUIRE_CB(cb);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_CB(cb); 4 0 4
REQUIRE_CB(cb); 0 4 4
1310  
1311 4 memset(&hints, 0, sizeof(hints));
1312 4 hints.ai_family = AF_UNSPEC;
1313 4 hints.ai_socktype = SOCK_STREAM;
1314  
1315 4 req = new_req(aTHX_ self, cb);
1316 4 req->with_ttl = 1;
1317 4 ARES_CALL_BEGIN(self);
1318 4 ares_getaddrinfo(self->channel, name, NULL, &hints, addrinfo_cb, req);
1319 4 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 4 0 4
ARES_CALL_END(self); 0 4 4
ARES_CALL_END(self); 0 4 4
1320 }
1321  
1322 void
1323 getaddrinfo(self, node, service, hints_hv, cb)
1324 EV::cares self
1325 SV *node
1326 SV *service
1327 SV *hints_hv
1328 SV *cb
1329 CODE:
1330 {
1331 16 ▶ const char *c_node = SvOK(node) ? SvPV_nolen(node) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *c_node = SvOK(node) ? SvPV_nolen(node) : NULL; 8 0 8
1332 9 ▶ const char *c_service = SvOK(service) ? SvPV_nolen(service) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *c_service = SvOK(service) ? SvPV_nolen(service) : NULL; 1 7 8
1333 struct ares_addrinfo_hints hints;
1334 ev_cares_req_t *req;
1335  
1336 8 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 8 8
REQUIRE_LIVE(self); 0 8 8
1337 8 ▶ REQUIRE_CB(cb);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_CB(cb); 8 0 8
REQUIRE_CB(cb); 0 8 8
1338  
1339 8 memset(&hints, 0, sizeof(hints));
1340 8 hints.ai_family = AF_UNSPEC;
1341 8 hints.ai_socktype = SOCK_STREAM; /* avoid duplicate entries per socktype */
1342  
1343 8 req = new_req(aTHX_ self, cb);
1344  
1345 8 ▶ if (SvROK(hints_hv) && SvTYPE(SvRV(hints_hv)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(hints_hv) && SvTYPE(SvRV(hints_hv)) == SVt_PVHV) { 8 0 8
if (SvROK(hints_hv) && SvTYPE(SvRV(hints_hv)) == SVt_PVHV) { 8 0 8
1346 8 HV *hv = (HV *)SvRV(hints_hv);
1347 SV **sv;
1348 8 ▶ if ((sv = hv_fetchs(hv, "family", 0))) hints.ai_family = SvIV(*sv);
Branch: 1 branch
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "family", 0))) hints.ai_family = SvIV(*sv); 8 0 8
1349 8 ▶ if ((sv = hv_fetchs(hv, "socktype", 0))) hints.ai_socktype = SvIV(*sv);
Branch: 1 branch
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "socktype", 0))) hints.ai_socktype = SvIV(*sv); 0 8 8
1350 8 ▶ if ((sv = hv_fetchs(hv, "protocol", 0))) hints.ai_protocol = SvIV(*sv);
Branch: 1 branch
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "protocol", 0))) hints.ai_protocol = SvIV(*sv); 0 8 8
1351 8 ▶ if ((sv = hv_fetchs(hv, "flags", 0))) hints.ai_flags = SvIV(*sv);
Branch: 1 branch
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "flags", 0))) hints.ai_flags = SvIV(*sv); 2 6 8
1352 8 ▶ if ((sv = hv_fetchs(hv, "ttl", 0)) && SvTRUE(*sv))
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "ttl", 0)) && SvTRUE(*sv)) 3 5 8
if ((sv = hv_fetchs(hv, "ttl", 0)) && SvTRUE(*sv)) 3 0 3
1353 3 req->with_ttl = 1;
1354 }
1355  
1356 8 ARES_CALL_BEGIN(self);
1357 8 ares_getaddrinfo(self->channel, c_node, c_service, &hints, addrinfo_cb, req);
1358 8 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 8 0 8
ARES_CALL_END(self); 0 8 8
ARES_CALL_END(self); 0 8 8
1359 }
1360  
1361 void
1362 gethostbyname(self, name, family, cb)
1363 EV::cares self
1364 const char *name
1365 int family
1366 SV *cb
1367 CODE:
1368 {
1369 ev_cares_req_t *req;
1370 1 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 1 1
REQUIRE_LIVE(self); 0 1 1
1371 1 ▶ REQUIRE_CB(cb);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_CB(cb); 1 0 1
REQUIRE_CB(cb); 0 1 1
1372 1 req = new_req(aTHX_ self, cb);
1373 1 ARES_CALL_BEGIN(self);
1374 1 ares_gethostbyname(self->channel, name, family, host_cb, req);
1375 1 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 1 0 1
ARES_CALL_END(self); 0 1 1
ARES_CALL_END(self); 0 1 1
1376 }
1377  
1378 void
1379 search(self, name, type, ...)
1380 EV::cares self
1381 const char *name
1382 int type
1383 PREINIT:
1384 SV *cb;
1385 33 ▶ int dnsclass = C_IN;
Branch: 1 branch
BranchTrueFalseTotal
int dnsclass = C_IN; 33 0 33
1386 ev_cares_req_t *req;
1387 CODE:
1388 {
1389 33 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 33 33
REQUIRE_LIVE(self); 0 33 33
1390 /* search($name, $type, $cb) -> items == 4
1391 search($name, $type, $class, $cb) -> items == 5 */
1392 33 ▶ if (items == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 4) { 30 3 33
1393 30 cb = ST(3);
1394 3 ▶ } else if (items == 5) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (items == 5) { 2 1 3
1395 2 dnsclass = SvIV(ST(3));
1396 2 cb = ST(4);
1397 } else {
1398 1 croak("Usage: $r->search($name, $type, [$class,] $cb)");
1399 }
1400 32 ▶ REQUIRE_CB(cb);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_CB(cb); 32 0 32
REQUIRE_CB(cb); 0 32 32
1401  
1402 32 req = new_req(aTHX_ self, cb);
1403 32 req->qtype = type;
1404 32 ARES_CALL_BEGIN(self);
1405 32 ares_search(self->channel, name, dnsclass, type, search_cb, req);
1406 32 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 32 0 32
ARES_CALL_END(self); 0 32 32
ARES_CALL_END(self); 0 32 32
1407 }
1408  
1409 void
1410 query(self, name, dnsclass, type, cb)
1411 EV::cares self
1412 const char *name
1413 int dnsclass
1414 int type
1415 SV *cb
1416 CODE:
1417 {
1418 ev_cares_req_t *req;
1419 342 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 342 342
REQUIRE_LIVE(self); 0 342 342
1420 342 ▶ REQUIRE_CB(cb);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_CB(cb); 342 0 342
REQUIRE_CB(cb); 0 342 342
1421 342 req = new_req(aTHX_ self, cb);
1422 342 ARES_CALL_BEGIN(self);
1423 342 ares_query(self->channel, name, dnsclass, type, raw_cb, req);
1424 342 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 342 0 342
ARES_CALL_END(self); 0 342 342
ARES_CALL_END(self); 0 342 342
1425 }
1426  
1427 void
1428 reverse(self, ip, cb)
1429 EV::cares self
1430 const char *ip
1431 SV *cb
1432 CODE:
1433 {
1434 struct in_addr addr4;
1435 struct in6_addr addr6;
1436 ev_cares_req_t *req;
1437 const void *ap;
1438 size_t alen;
1439 int family;
1440  
1441 4 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 4 4
REQUIRE_LIVE(self); 0 4 4
1442 4 ▶ REQUIRE_CB(cb);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_CB(cb); 4 0 4
REQUIRE_CB(cb); 0 4 4
1443  
1444 /* validate before entering the ARES_CALL bracket so croak doesn't
1445 leave in_callback unbalanced */
1446 4 ▶ if (inet_pton(AF_INET, ip, &addr4) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, ip, &addr4) == 1) { 3 1 4
1447 3 family = AF_INET; ap = &addr4; alen = sizeof addr4;
1448 1 ▶ } else if (inet_pton(AF_INET6, ip, &addr6) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (inet_pton(AF_INET6, ip, &addr6) == 1) { 0 1 1
1449 0 family = AF_INET6; ap = &addr6; alen = sizeof addr6;
1450 } else {
1451 1 croak("EV::cares::reverse: invalid IP address: %s", ip);
1452 }
1453  
1454 3 req = new_req(aTHX_ self, cb);
1455 3 req->by_addr = 1;
1456 3 ARES_CALL_BEGIN(self);
1457 3 ares_gethostbyaddr(self->channel, ap, alen, family, host_cb, req);
1458 3 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 3 0 3
ARES_CALL_END(self); 0 3 3
ARES_CALL_END(self); 0 3 3
1459 }
1460  
1461 void
1462 getnameinfo(self, sa, flags, cb)
1463 EV::cares self
1464 SV *sa
1465 int flags
1466 SV *cb
1467 CODE:
1468 {
1469 STRLEN len;
1470 4 const char *addr = SvPV(sa, len);
1471 ev_cares_req_t *req;
1472 struct sockaddr_storage ss;
1473 sa_family_t family;
1474 STRLEN min_len;
1475  
1476 4 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 4 4
REQUIRE_LIVE(self); 0 4 4
1477 4 ▶ REQUIRE_CB(cb);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_CB(cb); 4 0 4
REQUIRE_CB(cb); 0 4 4
1478  
1479 /* Require enough bytes to safely read sa_family (sa_family_t lives at
1480 different offsets across platforms — Linux puts it at byte 0, BSD
1481 has sa_len at byte 0 and sa_family at byte 1). sizeof(struct
1482 sockaddr) is the universal lower bound for either layout. */
1483 4 ▶ if (len < sizeof(struct sockaddr))
Branch: 1 branch
BranchTrueFalseTotal
if (len < sizeof(struct sockaddr)) 1 3 4
1484 1 croak("EV::cares::getnameinfo: sockaddr too short (%d bytes)", (int)len);
1485  
1486 /* Copy into a local sockaddr_storage so both the family read and
1487 the c-ares call see properly-aligned bytes (matters on strict-
1488 alignment architectures like SPARC / classic MIPS). */
1489 3 ▶ if (len > sizeof ss) len = sizeof ss;
Branch: 1 branch
BranchTrueFalseTotal
if (len > sizeof ss) len = sizeof ss; 0 3 3
1490 3 memcpy(&ss, addr, len);
1491 3 family = ((struct sockaddr *)&ss)->sa_family;
1492 3 ▶ if (family == AF_INET) min_len = sizeof(struct sockaddr_in);
Branch: 1 branch
BranchTrueFalseTotal
if (family == AF_INET) min_len = sizeof(struct sockaddr_in); 1 2 3
1493 2 ▶ else if (family == AF_INET6) min_len = sizeof(struct sockaddr_in6);
Branch: 1 branch
BranchTrueFalseTotal
else if (family == AF_INET6) min_len = sizeof(struct sockaddr_in6); 1 1 2
1494 1 else croak("EV::cares::getnameinfo: unsupported sockaddr family %d "
1495 "(need AF_INET or AF_INET6)", (int)family);
1496 2 ▶ if (len < min_len)
Branch: 1 branch
BranchTrueFalseTotal
if (len < min_len) 1 1 2
1497 1 ▶ croak("EV::cares::getnameinfo: sockaddr too short for %s (%d bytes)",
Branch: 1 branch
BranchTrueFalseTotal
croak("EV::cares::getnameinfo: sockaddr too short for %s (%d bytes)", 1 0 1
1498 family == AF_INET6 ? "AF_INET6" : "AF_INET", (int)len);
1499  
1500 1 req = new_req(aTHX_ self, cb);
1501 1 ARES_CALL_BEGIN(self);
1502 1 ares_getnameinfo(self->channel, (const struct sockaddr *)&ss,
1503 (ares_socklen_t)len, flags, nameinfo_cb, req);
1504 1 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 1 0 1
ARES_CALL_END(self); 0 1 1
ARES_CALL_END(self); 0 1 1
1505 }
1506  
1507 void
1508 cancel(self)
1509 EV::cares self
1510 CODE:
1511 10 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 10 10
REQUIRE_LIVE(self); 1 9 10
1512 9 ARES_CALL_BEGIN(self);
1513 9 ares_cancel(self->channel);
1514 9 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 8 1 9
ARES_CALL_END(self); 0 8 8
ARES_CALL_END(self); 0 8 8
1515  
1516 void
1517 set_servers(self, ...)
1518 EV::cares self
1519 CODE:
1520 {
1521 SV *csv;
1522 int rc, i;
1523  
1524 15 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 15 15
REQUIRE_LIVE(self); 0 15 15
1525 15 ▶ if (items <= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items <= 1) 1 14 15
1526 1 croak("EV::cares::set_servers: requires at least one server address");
1527  
1528 /* accept either a single arrayref or a flat list, mirroring `new(servers => ...)` */
1529 18 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) { 12 2 14
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) { 6 6 12
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) { 5 1 6
1530 5 AV *av = (AV *)SvRV(ST(1));
1531 5 ▶ if (av_len(av) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(av) < 0) 1 4 5
1532 1 croak("EV::cares::set_servers: empty arrayref");
1533 4 csv = av_to_csv(aTHX_ av);
1534 } else {
1535 9 csv = sv_2mortal(newSVpvs(""));
1536 19 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 11 8 19
1537 11 ▶ if (SvROK(ST(i)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(ST(i))) 1 10 11
1538 1 croak("EV::cares::set_servers: arg %d is a reference; "
1539 "use the arrayref form for {host,port} hashrefs", i);
1540 10 ▶ if (i > 1) sv_catpvs(csv, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (i > 1) sv_catpvs(csv, ","); 2 8 10
1541 10 sv_catsv(csv, ST(i));
1542 }
1543 }
1544  
1545 12 ARES_CALL_BEGIN(self);
1546 12 rc = ares_set_servers_csv(self->channel, SvPV_nolen(csv));
1547 12 ▶ ARES_CALL_END(self);
Branch: 3 branches
BranchTrueFalseTotal
ARES_CALL_END(self); 12 0 12
ARES_CALL_END(self); 1 11 12
ARES_CALL_END(self); 0 11 11
1548 12 ▶ if (rc != ARES_SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != ARES_SUCCESS) 0 12 12
1549 0 croak("EV::cares::set_servers: %s", ares_strerror(rc));
1550 }
1551  
1552 void
1553 servers(self)
1554 EV::cares self
1555 PPCODE:
1556 {
1557 char *csv;
1558 17 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 17 17
REQUIRE_LIVE(self); 0 17 17
1559 17 csv = ares_get_servers_csv(self->channel);
1560 17 ▶ if (!csv) croak("EV::cares::servers: ares_get_servers_csv failed");
Branch: 1 branch
BranchTrueFalseTotal
if (!csv) croak("EV::cares::servers: ares_get_servers_csv failed"); 0 17 17
1561 17 ▶ mXPUSHp(csv, strlen(csv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(csv, strlen(csv)); 0 17 17
1562 17 ares_free_string(csv);
1563 }
1564  
1565 void
1566 set_local_dev(self, dev)
1567 EV::cares self
1568 const char *dev
1569 CODE:
1570 1 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 1 1
REQUIRE_LIVE(self); 0 1 1
1571 1 ares_set_local_dev(self->channel, dev);
1572  
1573 void
1574 set_local_ip4(self, ip)
1575 EV::cares self
1576 const char *ip
1577 CODE:
1578 {
1579 struct in_addr addr;
1580 2 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 2 2
REQUIRE_LIVE(self); 0 2 2
1581 2 ▶ if (inet_pton(AF_INET, ip, &addr) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, ip, &addr) != 1) 1 1 2
1582 1 croak("EV::cares::set_local_ip4: invalid IPv4 address: %s", ip);
1583 1 ares_set_local_ip4(self->channel, ntohl(addr.s_addr));
1584 }
1585  
1586 void
1587 set_local_ip6(self, ip)
1588 EV::cares self
1589 const char *ip
1590 CODE:
1591 {
1592 struct in6_addr addr;
1593 2 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 2 2
REQUIRE_LIVE(self); 0 2 2
1594 2 ▶ if (inet_pton(AF_INET6, ip, &addr) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET6, ip, &addr) != 1) 1 1 2
1595 1 croak("EV::cares::set_local_ip6: invalid IPv6 address: %s", ip);
1596 1 ares_set_local_ip6(self->channel, (const unsigned char *)&addr);
1597 }
1598  
1599 int
1600 active_queries(self)
1601 EV::cares self
1602 CODE:
1603 264 ▶ RETVAL = self->active_queries;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->active_queries; 241 23 264
1604 OUTPUT:
1605 RETVAL
1606  
1607 int
1608 last_query_timeouts(self)
1609 EV::cares self
1610 CODE:
1611 3 ▶ RETVAL = self->last_timeouts;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->last_timeouts; 0 3 3
1612 OUTPUT:
1613 RETVAL
1614  
1615 int
1616 is_destroyed(self)
1617 EV::cares self
1618 CODE:
1619 51 ▶ RETVAL = self->destroyed;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->destroyed; 33 18 51
1620 OUTPUT:
1621 RETVAL
1622  
1623 SV *
1624 loop(self)
1625 EV::cares self
1626 CODE:
1627 /* Return a fresh RV to the user-supplied EV::Loop blessed object, or
1628 undef when this resolver runs on EV's default loop. Used by
1629 wait_idle() so it pumps the same loop the watchers are armed on. */
1630 17 ▶ RETVAL = self->loop_sv ? newRV_inc(self->loop_sv) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->loop_sv ? newRV_inc(self->loop_sv) : &PL_sv_undef; 1 16 17
1631 OUTPUT:
1632 RETVAL
1633  
1634 double
1635 next_timeout(self)
1636 EV::cares self
1637 CODE:
1638 {
1639 struct timeval tv, *tvp;
1640 2 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 2 2
REQUIRE_LIVE(self); 1 1 2
1641 1 tvp = ares_timeout(self->channel, NULL, &tv);
1642 1 ▶ RETVAL = tvp ? (double)tvp->tv_sec + (double)tvp->tv_usec / 1e6 : -1.0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = tvp ? (double)tvp->tv_sec + (double)tvp->tv_usec / 1e6 : -1.0; 0 1 1
RETVAL = tvp ? (double)tvp->tv_sec + (double)tvp->tv_usec / 1e6 : -1.0; 0 1 1
1643 }
1644 OUTPUT:
1645 RETVAL
1646  
1647 void
1648 set_sortlist(self, sortlist)
1649 EV::cares self
1650 const char *sortlist
1651 CODE:
1652 {
1653 int rc;
1654 2 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 2 2
REQUIRE_LIVE(self); 0 2 2
1655 2 rc = ares_set_sortlist(self->channel, sortlist);
1656 2 ▶ if (rc != ARES_SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != ARES_SUCCESS) 1 1 2
1657 1 croak("EV::cares::set_sortlist: %s", ares_strerror(rc));
1658 }
1659  
1660 void
1661 destroy(self)
1662 EV::cares self
1663 CODE:
1664 /* Deliberately weaker than REQUIRE_LIVE — only checks magic, not
1665 destroyed. This makes double-destroy a silent no-op (cleanup()
1666 early-returns when destroyed=1), per the documented contract. */
1667 17 ▶ if (self->magic != EV_CARES_MAGIC) croak("EV::cares: invalid object");
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_CARES_MAGIC) croak("EV::cares: invalid object"); 0 17 17
1668 17 cleanup(self);
1669  
1670 void
1671 reinit(self)
1672 EV::cares self
1673 CODE:
1674 {
1675 int rc;
1676 ares_channel channel;
1677 10 ▶ REQUIRE_LIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
REQUIRE_LIVE(self); 0 10 10
REQUIRE_LIVE(self); 1 9 10
1678 9 ▶ if (self->active_queries || self->in_callback)
Branch: 2 branches
BranchTrueFalseTotal
if (self->active_queries || self->in_callback) 8 1 9
if (self->active_queries || self->in_callback) 0 8 8
1679 1 croak("EV::cares::reinit: resolver must be idle");
1680 /* ares_reinit may change socket state from a background thread. */
1681 8 rc = ares_dup(&channel, self->channel);
1682 8 ▶ if (rc != ARES_SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != ARES_SUCCESS) 0 8 8
1683 0 croak("EV::cares::reinit: %s", ares_strerror(rc));
1684 8 ares_destroy(self->channel);
1685 8 self->channel = channel;
1686 8 update_timer(self);
1687 }
1688  
1689 void
1690 DESTROY(self)
1691 EV::cares self
1692 CODE:
1693 117 ▶ if (self->magic != EV_CARES_MAGIC) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_CARES_MAGIC) return; 0 117 117
1694  
1695 117 ▶ if (PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) { 0 117 117
1696 /* Global destruction: skip everything that would touch other
1697 SVs or libev (their teardown order is unspecified) and leak
1698 self. Stopping watchers during PL_dirty is unsafe, and
1699 freeing self while watchers still reference it has caused
1700 SEGVs on musl during process exit. OS reclaims at exit. */
1701 0 self->destroyed = 1;
1702 0 self->channel = NULL;
1703 0 return;
1704 }
1705  
1706 117 cleanup(self);
1707 117 ▶ if (self->in_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_callback) { 2 115 117
1708 /* defer Safefree until the outer ARES_CALL_END unwinds. Every
1709 callback path enters through an ARES_CALL_BEGIN bracket (io_cb,
1710 timer_cb, or an XS query method), so the matching ARES_CALL_END
1711 drops in_callback to 0 and frees self there. */
1712 2 self->free_pending = 1;
1713 } else {
1714 115 Safefree(self);
1715 }
1716  
1717 const char *
1718 strerror(...)
1719 CODE:
1720 {
1721 SV *arg;
1722 /* accept both EV::cares::strerror($n) and EV::cares->strerror($n) */
1723 35 ▶ if (items == 1) arg = ST(0);
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) arg = ST(0); 32 3 35
1724 3 ▶ else if (items >= 2) arg = ST(1); /* skip class/instance arg */
Branch: 1 branch
BranchTrueFalseTotal
else if (items >= 2) arg = ST(1); /* skip class/instance arg */ 2 1 3
1725 1 else croak("Usage: EV::cares::strerror(status)");
1726  
1727 /* looks_like_number returns false for refs, so SvROK precheck not needed */
1728 34 ▶ if (!looks_like_number(arg))
Branch: 1 branch
BranchTrueFalseTotal
if (!looks_like_number(arg)) 3 31 34
1729 3 croak("Usage: EV::cares::strerror(status)");
1730  
1731 31 RETVAL = ares_strerror(SvIV(arg));
1732 }
1733 OUTPUT:
1734 RETVAL
1735  
1736 const char *
1737 lib_version(...)
1738 CODE:
1739 5 RETVAL = ares_version(NULL);
1740 OUTPUT:
1741 RETVAL
« blib/lib/EV/cares.pm ↑ Summary