cares.xs

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