cares.xs

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