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
,
42 int *timeout_io
, const struct timeval
*tv_now
,
46 return adns_beforepoll(st
->ast
, fds
, nfds_io
, timeout_io
, tv_now
);
49 static void resolver_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
,
50 const struct timeval
*tv_now
, uint64_t *now
)
59 adns_afterpoll(st
->ast
, fds
, nfds
, tv_now
);
63 rv
=adns_check(st
->ast
, &aq
, &ans
, &qp
);
66 if (ans
->status
!=adns_s_ok
) {
67 q
->answer(q
->cst
,NULL
); /* Failure */
71 q
->answer(q
->cst
,ans
->rrs
.inaddr
);
75 } else if (rv
==EAGAIN
|| rv
==ESRCH
) {
78 fatal("resolver_afterpoll: adns_check() returned %d",rv
);
85 /* Initialise adns, using parameters supplied */
86 static list_t
*adnsresolver_apply(closure_t
*self
, struct cloc loc
,
87 dict_t
*context
, list_t
*args
)
94 st
=safe_malloc(sizeof(*st
),"adnsresolver_apply");
95 st
->cl
.description
="adns";
96 st
->cl
.type
=CL_RESOLVER
;
98 st
->cl
.interface
=&st
->ops
;
101 st
->ops
.request
=resolve_request
;
104 if (!i
|| i
->type
!=t_dict
) {
105 cfgfatal(st
->loc
,"adns","first argument must be a dictionary\n");
108 conf
=dict_read_string(d
,"config",False
,"adns",loc
);
111 if (adns_init_strcfg(&st
->ast
, 0, 0, conf
)) {
112 fatal_perror("Failed to initialise ADNS");
115 if (adns_init(&st
->ast
, 0, 0)) {
116 fatal_perror("Failed to initialise ADNS");
120 register_for_poll(st
, resolver_beforepoll
, resolver_afterpoll
,
121 ADNS_POLLFDS_RECOMMENDED
+5,"resolver");
123 return new_closure(&st
->cl
);
126 void resolver_module(dict_t
*dict
)
128 add_closure(dict
,"adns",adnsresolver_apply
);