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 adns_state ads; |
11 |
|
12 |
struct ctx |
13 |
{ |
14 |
SV *req; |
15 |
SV *cb; |
16 |
}; |
17 |
|
18 |
static void |
19 |
process () |
20 |
{ |
21 |
for (;;) |
22 |
{ |
23 |
adns_query q; |
24 |
adns_answer *a; |
25 |
void *c; |
26 |
int r = adns_check (ads, &q, &a, &c); |
27 |
|
28 |
if (r) |
29 |
break; |
30 |
} |
31 |
} |
32 |
|
33 |
static struct pollfd *fds; |
34 |
static int nfd; |
35 |
static ev_io *iow; |
36 |
static ev_timer tw; |
37 |
static ev_prepare prepare_ev; |
38 |
static struct timeval *tv_now; |
39 |
|
40 |
static void |
41 |
update_now (EV_P) |
42 |
{ |
43 |
ev_tstamp t = ev_now (EV_P); |
44 |
|
45 |
tv.tv_sec = (long)t; |
46 |
tv.tv_usec = (long)((t - (ev_tstamp)tv.tv_sec) * 1e-6); |
47 |
} |
48 |
|
49 |
static void |
50 |
timer_cb (EV_P_ ev_timer *w, int revents) |
51 |
{ |
52 |
} |
53 |
|
54 |
static void |
55 |
io_cb (EV_P_ ev_io *w, int revents) |
56 |
{ |
57 |
update_now (EV_A); |
58 |
|
59 |
if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); |
60 |
if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); |
61 |
} |
62 |
|
63 |
// create io watchers for each fd and a timer before blocking |
64 |
static void |
65 |
prepare_cb (EV_P_ ev_prepare *w, int revents) |
66 |
{ |
67 |
int i; |
68 |
int timeout = 3600000; |
69 |
|
70 |
ev_timer_stop (EV_A_ &tw); |
71 |
|
72 |
for (i = 0; i < nfd; ++i) |
73 |
ev_io_stop (EV_A_ iow + i); |
74 |
|
75 |
process (); |
76 |
|
77 |
update_now (EV_A); |
78 |
|
79 |
while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
80 |
{ |
81 |
free (iow); iow = malloc (nfd * sizeof (struct pollfd)); |
82 |
free (fds); fds = malloc (nfd * sizeof (ev_io)); |
83 |
} |
84 |
|
85 |
ev_timer_set (&tw, timeout * 1e-3, 0.); |
86 |
ev_timer_start (EV_A_ &tw); |
87 |
|
88 |
// create on ev_io per pollfd |
89 |
for (i = 0; i < nfd; ++i) |
90 |
{ |
91 |
ev_io_init (iow + i, io_cb, fds [i].fd, |
92 |
((fds [i].events & POLLIN ? EV_READ : 0) |
93 |
| (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
94 |
|
95 |
ev_io_start (EV_A_ iow + i); |
96 |
} |
97 |
} |
98 |
|
99 |
MODULE = EV::ADNS PACKAGE = EV::ADNS |
100 |
|
101 |
PROTOTYPES: ENABLE |
102 |
|
103 |
BOOT: |
104 |
{ |
105 |
#if 0 |
106 |
HV *stash = gv_stashpv ("EV::ADNS", 1); |
107 |
|
108 |
static const struct { |
109 |
const char *name; |
110 |
IV iv; |
111 |
} *civ, const_iv[] = { |
112 |
# define const_iv(pfx, name) { # name, (IV) adns_ ## name }, |
113 |
const_iv (if_none) |
114 |
const_iv (if_noenv) |
115 |
const_iv (if_noerrprint) |
116 |
const_iv (if_noserverwarn) |
117 |
const_iv (if_debug) |
118 |
const_iv (if_logpid) |
119 |
const_iv (if_noautosys) |
120 |
const_iv (if_eintr) |
121 |
const_iv (if_nosigpipe) |
122 |
const_iv (if_checkc_entex) |
123 |
const_iv (if_checkc_freq) |
124 |
|
125 |
const_iv (qf_none) |
126 |
const_iv (qf_search) |
127 |
const_iv (qf_usevc) |
128 |
const_iv (qf_owner) |
129 |
const_iv (qf_quoteok_query) |
130 |
const_iv (qf_quoteok_cname) |
131 |
const_iv (qf_quoteok_anshost) |
132 |
const_iv (qf_quotefail_cname) |
133 |
const_iv (qf_cname_loose) |
134 |
const_iv (qf_cname_forbid) |
135 |
|
136 |
const_iv (rrt_typemask) |
137 |
const_iv (_qtf_deref) |
138 |
const_iv (_qtf_mail822) |
139 |
const_iv (r_unknown) |
140 |
const_iv (r_none) |
141 |
const_iv (r_a) |
142 |
const_iv (r_ns_raw) |
143 |
const_iv (r_ns) |
144 |
const_iv (r_cname) |
145 |
const_iv (r_soa_raw) |
146 |
const_iv (r_soa) |
147 |
const_iv (r_ptr_raw) |
148 |
const_iv (r_ptr) |
149 |
const_iv (r_hinfo) |
150 |
const_iv (r_mx_raw) |
151 |
const_iv (r_mx) |
152 |
const_iv (r_txt) |
153 |
const_iv (r_rp_raw) |
154 |
const_iv (r_rp) |
155 |
const_iv (r_srv_raw) |
156 |
const_iv (r_srv) |
157 |
const_iv (r_addr) |
158 |
|
159 |
const_iv (s_ok) |
160 |
const_iv (s_nomemory) |
161 |
const_iv (s_unknownrrtype) |
162 |
const_iv (s_systemfail) |
163 |
const_iv (s_max_localfail) |
164 |
const_iv (s_timeout) |
165 |
const_iv (s_allservfail) |
166 |
const_iv (s_norecurse) |
167 |
const_iv (s_invalidresponse) |
168 |
const_iv (s_unknownformat) |
169 |
const_iv (s_max_remotefail) |
170 |
const_iv (s_rcodeservfail) |
171 |
const_iv (s_rcodeformaterror) |
172 |
const_iv (s_rcodenotimplemented) |
173 |
const_iv (s_rcoderefused) |
174 |
const_iv (s_rcodeunknown) |
175 |
const_iv (s_max_tempfail) |
176 |
const_iv (s_inconsistent) |
177 |
const_iv (s_prohibitedcname) |
178 |
const_iv (s_answerdomaininvalid) |
179 |
const_iv (s_answerdomaintoolong) |
180 |
const_iv (s_invaliddata) |
181 |
const_iv (s_max_misconfig) |
182 |
const_iv (s_querydomainwrong) |
183 |
const_iv (s_querydomaininvalid) |
184 |
const_iv (s_querydomaintoolong) |
185 |
const_iv (s_max_misquery) |
186 |
const_iv (s_nxdomain) |
187 |
const_iv (s_nodata) |
188 |
const_iv (s_max_permfail) |
189 |
}; |
190 |
|
191 |
for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
192 |
newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
193 |
#endif |
194 |
|
195 |
I_EV_API ("EV::ADNS"); |
196 |
|
197 |
ev_prepare_init (&prepare_ev, prepare_cb); ev_prepare_start (EV_DEFAULT_ &prepare_ev); |
198 |
ev_unref (); |
199 |
|
200 |
ev_init (&tw, timer_cb); |
201 |
|
202 |
adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
203 |
} |
204 |
|
205 |
int adns_submit (char *owner, int type, int flags, SV *cb) |
206 |
CODE: |
207 |
{ |
208 |
struct ctx *c = (struct ctx *)malloc (sizeof (ctx)); |
209 |
adns_query q; |
210 |
int r = adns_submit (owner, type, flags, (void *)c, &q); |
211 |
|
212 |
if (r) |
213 |
{ |
214 |
free (c); |
215 |
XSRETURN_EMPTY; |
216 |
} |
217 |
} |
218 |
|
219 |
|
220 |
|