1 |
#include "EXTERN.h" |
2 |
#include "perl.h" |
3 |
#include "XSUB.h" |
4 |
|
5 |
#include <poll.h> |
6 |
#include <adns.h> |
7 |
|
8 |
#include "EVAPI.h" |
9 |
|
10 |
static struct pollfd *fds; |
11 |
static int nfd, mfd; |
12 |
static ev_io *iow; |
13 |
static ev_timer tw; |
14 |
static ev_idle iw; |
15 |
static ev_prepare pw; |
16 |
static struct timeval tv_now; |
17 |
static int outstanding; |
18 |
|
19 |
static void |
20 |
outstanding_inc (adns_state ads) |
21 |
{ |
22 |
if (!outstanding++) |
23 |
ev_prepare_start (EV_DEFAULT_ &pw); |
24 |
} |
25 |
|
26 |
static void |
27 |
outstanding_dec (adns_state ads) |
28 |
{ |
29 |
--outstanding; |
30 |
} |
31 |
|
32 |
struct ctx |
33 |
{ |
34 |
SV *self; |
35 |
adns_state ads; |
36 |
adns_query query; |
37 |
SV *cb; |
38 |
}; |
39 |
|
40 |
static SV * |
41 |
ha2sv (adns_rr_hostaddr *rr) |
42 |
{ |
43 |
AV *av = newAV (); |
44 |
av_push (av, newSVpv (rr->host, 0)); |
45 |
// TODO: add addresses |
46 |
|
47 |
return newRV_noinc ((SV *)av); |
48 |
} |
49 |
|
50 |
static void |
51 |
process (adns_state ads) |
52 |
{ |
53 |
dSP; |
54 |
|
55 |
for (;;) |
56 |
{ |
57 |
int i; |
58 |
adns_query q = 0; |
59 |
adns_answer *a; |
60 |
void *ctx; |
61 |
SV *cb; |
62 |
struct ctx *c; |
63 |
int r = adns_check (ads, &q, &a, &ctx); |
64 |
|
65 |
if (r) |
66 |
break; |
67 |
|
68 |
c = (struct ctx *)ctx; |
69 |
cb = c->cb; |
70 |
c->cb = 0; outstanding_dec (ads); |
71 |
SvREFCNT_dec (c->self); |
72 |
|
73 |
assert (cb); |
74 |
|
75 |
PUSHMARK (SP); |
76 |
|
77 |
EXTEND (SP, a->nrrs + 2); |
78 |
PUSHs (sv_2mortal (newSViv (a->status))); |
79 |
PUSHs (sv_2mortal (newSViv (a->expires))); |
80 |
|
81 |
for (i = 0; i < a->nrrs; ++i) |
82 |
{ |
83 |
SV *sv; |
84 |
|
85 |
switch (a->type & adns_r_unknown ? adns_r_unknown : a->type) |
86 |
{ |
87 |
case adns_r_ns_raw: |
88 |
case adns_r_cname: |
89 |
case adns_r_ptr: |
90 |
case adns_r_ptr_raw: |
91 |
sv = newSVpv (a->rrs.str [i], 0); |
92 |
break; |
93 |
|
94 |
case adns_r_txt: |
95 |
{ |
96 |
AV *av = newAV (); |
97 |
adns_rr_intstr *rr = a->rrs.manyistr [i]; |
98 |
|
99 |
while (rr->str) |
100 |
{ |
101 |
av_push (av, newSVpvn (rr->str, rr->i)); |
102 |
++rr; |
103 |
} |
104 |
|
105 |
sv = newRV_noinc ((SV *)av); |
106 |
} |
107 |
break; |
108 |
|
109 |
case adns_r_a: |
110 |
sv = newSVpv (inet_ntoa (a->rrs.inaddr [i]), 0); |
111 |
break; |
112 |
|
113 |
case adns_r_ns: |
114 |
sv = ha2sv (a->rrs.hostaddr + i); |
115 |
break; |
116 |
|
117 |
case adns_r_hinfo: |
118 |
{ |
119 |
/* untested */ |
120 |
AV *av = newAV (); |
121 |
adns_rr_intstrpair *rr = a->rrs.intstrpair + i; |
122 |
|
123 |
av_push (av, newSVpvn (rr->array [0].str, rr->array [0].i)); |
124 |
av_push (av, newSVpvn (rr->array [1].str, rr->array [1].i)); |
125 |
|
126 |
sv = newRV_noinc ((SV *)av); |
127 |
} |
128 |
break; |
129 |
|
130 |
case adns_r_rp: |
131 |
case adns_r_rp_raw: |
132 |
{ |
133 |
/* untested */ |
134 |
AV *av = newAV (); |
135 |
adns_rr_strpair *rr = a->rrs.strpair + i; |
136 |
|
137 |
av_push (av, newSVpv (rr->array [0], 0)); |
138 |
av_push (av, newSVpv (rr->array [1], 0)); |
139 |
|
140 |
sv = newRV_noinc ((SV *)av); |
141 |
} |
142 |
break; |
143 |
|
144 |
case adns_r_mx: |
145 |
{ |
146 |
AV *av = newAV (); |
147 |
adns_rr_inthostaddr *rr = a->rrs.inthostaddr + i; |
148 |
|
149 |
av_push (av, newSViv (rr->i)); |
150 |
av_push (av, ha2sv (&rr->ha)); |
151 |
|
152 |
sv = newRV_noinc ((SV *)av); |
153 |
} |
154 |
break; |
155 |
|
156 |
case adns_r_mx_raw: |
157 |
{ |
158 |
AV *av = newAV (); |
159 |
adns_rr_intstr *rr = a->rrs.intstr + i; |
160 |
|
161 |
av_push (av, newSViv (rr->i)); |
162 |
av_push (av, newSVpv (rr->str, 0)); |
163 |
|
164 |
sv = newRV_noinc ((SV *)av); |
165 |
} |
166 |
break; |
167 |
|
168 |
case adns_r_soa: |
169 |
case adns_r_soa_raw: |
170 |
{ |
171 |
AV *av = newAV (); |
172 |
adns_rr_soa *rr = a->rrs.soa + i; |
173 |
|
174 |
av_push (av, newSVpv (rr->mname, 0)); |
175 |
av_push (av, newSVpv (rr->rname, 0)); |
176 |
av_push (av, newSVuv (rr->serial)); |
177 |
av_push (av, newSVuv (rr->refresh)); |
178 |
av_push (av, newSVuv (rr->retry)); |
179 |
av_push (av, newSVuv (rr->expire)); |
180 |
av_push (av, newSVuv (rr->minimum)); |
181 |
|
182 |
sv = newRV_noinc ((SV *)av); |
183 |
} |
184 |
break; |
185 |
|
186 |
case adns_r_srv_raw: |
187 |
{ |
188 |
AV *av = newAV (); |
189 |
adns_rr_srvraw *rr = a->rrs.srvraw + i; |
190 |
|
191 |
av_push (av, newSViv (rr->priority)); |
192 |
av_push (av, newSViv (rr->weight)); |
193 |
av_push (av, newSViv (rr->port)); |
194 |
av_push (av, newSVpv (rr->host, 0)); |
195 |
|
196 |
sv = newRV_noinc ((SV *)av); |
197 |
} |
198 |
break; |
199 |
|
200 |
case adns_r_srv: |
201 |
{ |
202 |
AV *av = newAV (); |
203 |
adns_rr_srvha *rr = a->rrs.srvha + i; |
204 |
|
205 |
av_push (av, newSViv (rr->priority)); |
206 |
av_push (av, newSViv (rr->weight)); |
207 |
av_push (av, newSViv (rr->port)); |
208 |
av_push (av, ha2sv (&rr->ha)); |
209 |
|
210 |
sv = newRV_noinc ((SV *)av); |
211 |
} |
212 |
break; |
213 |
|
214 |
case adns_r_unknown: |
215 |
sv = newSVpvn (a->rrs.byteblock [i].data, a->rrs.byteblock [i].len); |
216 |
break; |
217 |
|
218 |
default: |
219 |
case adns_r_addr: |
220 |
sv = &PL_sv_undef; /* not supported */ |
221 |
break; |
222 |
} |
223 |
|
224 |
PUSHs (sv_2mortal (sv)); |
225 |
} |
226 |
|
227 |
free (a); |
228 |
|
229 |
PUTBACK; |
230 |
call_sv (cb, G_VOID | G_DISCARD | G_EVAL); |
231 |
SPAGAIN; |
232 |
|
233 |
if (SvTRUE (ERRSV)) |
234 |
warn ("%s", SvPV_nolen (ERRSV)); |
235 |
|
236 |
SvREFCNT_dec (cb); |
237 |
} |
238 |
} |
239 |
|
240 |
static void |
241 |
update_now (EV_P) |
242 |
{ |
243 |
ev_tstamp t = ev_now (EV_A); |
244 |
|
245 |
tv_now.tv_sec = (long)t; |
246 |
tv_now.tv_usec = (long)((t - (ev_tstamp)tv_now.tv_sec) * 1e6); |
247 |
} |
248 |
|
249 |
static void |
250 |
idle_cb (EV_P_ ev_idle *w, int revents) |
251 |
{ |
252 |
ev_idle_stop (EV_A_ w); |
253 |
} |
254 |
|
255 |
static void |
256 |
timer_cb (EV_P_ ev_timer *w, int revents) |
257 |
{ |
258 |
adns_state ads = (adns_state)w->data; |
259 |
update_now (EV_A); |
260 |
|
261 |
adns_processtimeouts (ads, &tv_now); |
262 |
} |
263 |
|
264 |
static void |
265 |
io_cb (EV_P_ ev_io *w, int revents) |
266 |
{ |
267 |
adns_state ads = (adns_state)w->data; |
268 |
update_now (EV_A); |
269 |
|
270 |
if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); |
271 |
if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); |
272 |
} |
273 |
|
274 |
// create io watchers for each fd and a timer before blocking |
275 |
static void |
276 |
prepare_cb (EV_P_ ev_prepare *w, int revents) |
277 |
{ |
278 |
int i; |
279 |
int timeout = 3600000; |
280 |
adns_state ads = (adns_state)w->data; |
281 |
|
282 |
if (ev_is_active (&tw)) |
283 |
ev_timer_stop (EV_A_ &tw); |
284 |
|
285 |
if (ev_is_active (&iw)) |
286 |
ev_idle_stop (EV_A_ &iw); |
287 |
|
288 |
for (i = 0; i < nfd; ++i) |
289 |
ev_io_stop (EV_A_ iow + i); |
290 |
|
291 |
process (ads); |
292 |
|
293 |
if (!outstanding) |
294 |
{ |
295 |
ev_prepare_stop (w); |
296 |
return; |
297 |
} |
298 |
|
299 |
update_now (EV_A); |
300 |
|
301 |
nfd = mfd; |
302 |
|
303 |
while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
304 |
{ |
305 |
mfd = nfd; |
306 |
|
307 |
free (iow); iow = malloc (mfd * sizeof (ev_io)); |
308 |
free (fds); fds = malloc (mfd * sizeof (struct pollfd)); |
309 |
} |
310 |
|
311 |
ev_timer_set (&tw, timeout * 1e-3, 0.); |
312 |
ev_timer_start (EV_A_ &tw); |
313 |
|
314 |
// create one ev_io per pollfd |
315 |
for (i = 0; i < nfd; ++i) |
316 |
{ |
317 |
ev_io *w = iow + i; |
318 |
|
319 |
ev_io_init (w, io_cb, fds [i].fd, |
320 |
((fds [i].events & POLLIN ? EV_READ : 0) |
321 |
| (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
322 |
|
323 |
w->data = (void *)ads; |
324 |
ev_io_start (EV_A_ w); |
325 |
} |
326 |
} |
327 |
|
328 |
static HV *stash; |
329 |
static adns_state ads; |
330 |
|
331 |
MODULE = EV::ADNS PACKAGE = EV::ADNS |
332 |
|
333 |
PROTOTYPES: ENABLE |
334 |
|
335 |
BOOT: |
336 |
{ |
337 |
stash = gv_stashpv ("EV::ADNS", 1); |
338 |
|
339 |
static const struct { |
340 |
const char *name; |
341 |
IV iv; |
342 |
} *civ, const_iv[] = { |
343 |
# define const_iv(name) { # name, (IV) adns_ ## name }, |
344 |
const_iv (if_none) |
345 |
const_iv (if_noenv) |
346 |
const_iv (if_noerrprint) |
347 |
const_iv (if_noserverwarn) |
348 |
const_iv (if_debug) |
349 |
const_iv (if_logpid) |
350 |
const_iv (if_noautosys) |
351 |
const_iv (if_eintr) |
352 |
const_iv (if_nosigpipe) |
353 |
const_iv (if_checkc_entex) |
354 |
const_iv (if_checkc_freq) |
355 |
|
356 |
const_iv (qf_none) |
357 |
const_iv (qf_search) |
358 |
const_iv (qf_usevc) |
359 |
const_iv (qf_owner) |
360 |
const_iv (qf_quoteok_query) |
361 |
const_iv (qf_quoteok_cname) |
362 |
const_iv (qf_quoteok_anshost) |
363 |
const_iv (qf_quotefail_cname) |
364 |
const_iv (qf_cname_loose) |
365 |
const_iv (qf_cname_forbid) |
366 |
|
367 |
const_iv (rrt_typemask) |
368 |
const_iv (_qtf_deref) |
369 |
const_iv (_qtf_mail822) |
370 |
const_iv (r_unknown) |
371 |
const_iv (r_none) |
372 |
const_iv (r_a) |
373 |
const_iv (r_ns_raw) |
374 |
const_iv (r_ns) |
375 |
const_iv (r_cname) |
376 |
const_iv (r_soa_raw) |
377 |
const_iv (r_soa) |
378 |
const_iv (r_ptr_raw) |
379 |
const_iv (r_ptr) |
380 |
const_iv (r_hinfo) |
381 |
const_iv (r_mx_raw) |
382 |
const_iv (r_mx) |
383 |
const_iv (r_txt) |
384 |
const_iv (r_rp_raw) |
385 |
const_iv (r_rp) |
386 |
const_iv (r_srv_raw) |
387 |
const_iv (r_srv) |
388 |
const_iv (r_addr) |
389 |
|
390 |
const_iv (s_ok) |
391 |
const_iv (s_nomemory) |
392 |
const_iv (s_unknownrrtype) |
393 |
const_iv (s_systemfail) |
394 |
const_iv (s_max_localfail) |
395 |
const_iv (s_timeout) |
396 |
const_iv (s_allservfail) |
397 |
const_iv (s_norecurse) |
398 |
const_iv (s_invalidresponse) |
399 |
const_iv (s_unknownformat) |
400 |
const_iv (s_max_remotefail) |
401 |
const_iv (s_rcodeservfail) |
402 |
const_iv (s_rcodeformaterror) |
403 |
const_iv (s_rcodenotimplemented) |
404 |
const_iv (s_rcoderefused) |
405 |
const_iv (s_rcodeunknown) |
406 |
const_iv (s_max_tempfail) |
407 |
const_iv (s_inconsistent) |
408 |
const_iv (s_prohibitedcname) |
409 |
const_iv (s_answerdomaininvalid) |
410 |
const_iv (s_answerdomaintoolong) |
411 |
const_iv (s_invaliddata) |
412 |
const_iv (s_max_misconfig) |
413 |
const_iv (s_querydomainwrong) |
414 |
const_iv (s_querydomaininvalid) |
415 |
const_iv (s_querydomaintoolong) |
416 |
const_iv (s_max_misquery) |
417 |
const_iv (s_nxdomain) |
418 |
const_iv (s_nodata) |
419 |
const_iv (s_max_permfail) |
420 |
}; |
421 |
|
422 |
for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
423 |
newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
424 |
|
425 |
I_EV_API ("EV::ADNS"); |
426 |
|
427 |
adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
428 |
|
429 |
ev_prepare_init (&pw, prepare_cb); |
430 |
pw.data = (void *)ads; |
431 |
|
432 |
ev_init (&iw, idle_cb); ev_set_priority (&iw, EV_MINPRI); |
433 |
iw.data = (void *)ads; |
434 |
ev_init (&tw, timer_cb); |
435 |
tw.data = (void *)ads; |
436 |
} |
437 |
|
438 |
void submit (char *owner, int type, int flags, SV *cb) |
439 |
PPCODE: |
440 |
{ |
441 |
SV *csv = NEWSV (0, sizeof (struct ctx)); |
442 |
struct ctx *c = (struct ctx *)SvPVX (csv); |
443 |
int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); |
444 |
|
445 |
outstanding_inc (ads); |
446 |
|
447 |
if (r) |
448 |
{ |
449 |
SvREFCNT_dec (csv); |
450 |
errno = r; |
451 |
XSRETURN_EMPTY; |
452 |
} |
453 |
else |
454 |
{ |
455 |
SvPOK_only (csv); |
456 |
SvCUR_set (csv, sizeof (struct ctx)); |
457 |
|
458 |
c->self = csv; |
459 |
c->cb = newSVsv (cb); |
460 |
c->ads = ads; |
461 |
|
462 |
if (!ev_is_active (&iw)) |
463 |
ev_idle_start (EV_A_ &iw); |
464 |
|
465 |
if (GIMME_V != G_VOID) |
466 |
{ |
467 |
csv = sv_2mortal (newRV_inc (csv)); |
468 |
sv_bless (csv, stash); |
469 |
XPUSHs (csv); |
470 |
} |
471 |
} |
472 |
} |
473 |
|
474 |
void DESTROY (SV *req) |
475 |
ALIAS: |
476 |
cancel = 1 |
477 |
CODE: |
478 |
{ |
479 |
struct ctx *c; |
480 |
|
481 |
if (!(SvROK (req) && SvOBJECT (SvRV (req)) |
482 |
&& (SvSTASH (SvRV (req)) == stash))) |
483 |
croak ("object is not of type EV::ADNS"); |
484 |
|
485 |
c = (struct ctx *)SvPVX (SvRV (req)); |
486 |
|
487 |
if (c->cb) |
488 |
{ |
489 |
SvREFCNT_dec (c->cb); |
490 |
c->cb = 0; outstanding_dec (c->ads); |
491 |
adns_cancel (c->query); |
492 |
SvREFCNT_dec (c->self); |
493 |
} |
494 |
} |
495 |
|
496 |
|
497 |
|
498 |
|