1 /* Name resolution using adns */
6 #error secnet requires ADNS version 1.0 or above
13 struct resolver_if ops
;
20 resolve_answer_fn
*answer
;
24 static resolve_request_fn resolve_request
;
25 static bool_t
resolve_request(void *sst
, cstring_t name
,
26 resolve_answer_fn
*cb
, void *cst
)
32 q
=safe_malloc(sizeof *q
,"resolve_request");
36 rv
=adns_submit(st
->ast
, name
, adns_r_a
, 0, q
, &q
->query
);
41 static int resolver_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
45 return adns_beforepoll(st
->ast
, fds
, nfds_io
, timeout_io
, tv_now
);
48 static void resolver_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
)
57 adns_afterpoll(st
->ast
, fds
, nfds
, tv_now
);
61 rv
=adns_check(st
->ast
, &aq
, &ans
, &qp
);
64 if (ans
->status
!=adns_s_ok
) {
65 q
->answer(q
->cst
,NULL
); /* Failure */
69 q
->answer(q
->cst
,ans
->rrs
.inaddr
);
73 } else if (rv
==EAGAIN
|| rv
==ESRCH
) {
76 fatal("resolver_afterpoll: adns_check() returned %d",rv
);
83 /* Initialise adns, using parameters supplied */
84 static list_t
*adnsresolver_apply(closure_t
*self
, struct cloc loc
,
85 dict_t
*context
, list_t
*args
)
92 st
=safe_malloc(sizeof(*st
),"adnsresolver_apply");
93 st
->cl
.description
="adns";
94 st
->cl
.type
=CL_RESOLVER
;
96 st
->cl
.interface
=&st
->ops
;
99 st
->ops
.request
=resolve_request
;
102 if (!i
|| i
->type
!=t_dict
) {
103 cfgfatal(st
->loc
,"adns","first argument must be a dictionary\n");
106 conf
=dict_read_string(d
,"config",False
,"adns",loc
);
109 if (adns_init_strcfg(&st
->ast
, 0, 0, conf
)) {
110 fatal_perror("Failed to initialise ADNS");
113 if (adns_init(&st
->ast
, 0, 0)) {
114 fatal_perror("Failed to initialise ADNS");
118 register_for_poll(st
, resolver_beforepoll
, resolver_afterpoll
,
119 ADNS_POLLFDS_RECOMMENDED
+5,"resolver");
121 return new_closure(&st
->cl
);
124 void resolver_module(dict_t
*dict
)
126 add_closure(dict
,"adns",adnsresolver_apply
);