1 /* Name resolution using adns */
6 #error secnet requires ADNS version 1.0 or above
15 struct resolver_if ops;
22 resolve_answer_fn *answer;
26 static resolve_request_fn resolve_request;
27 static bool_t resolve_request(void *sst, cstring_t name,
28 resolve_answer_fn *cb, void *cst)
33 const int maxlitlen=50;
35 ssize_t l=strlen(name);
36 if (name[0]=='[' && l<maxlitlen && l>2 && name[l-1]==']') {
37 char trimmed[maxlitlen+1];
38 memcpy(trimmed,name+1,l-2);
41 if (inet_aton(trimmed,&ia))
48 q=safe_malloc(sizeof *q,"resolve_request");
52 rv=adns_submit(st->ast, name, adns_r_a, 0, q, &q->query);
57 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
61 return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
64 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
73 adns_afterpoll(st->ast, fds, nfds, tv_now);
77 rv=adns_check(st->ast, &aq, &ans, &qp);
80 if (ans->status!=adns_s_ok) {
81 q->answer(q->cst,NULL); /* Failure */
85 q->answer(q->cst,ans->rrs.inaddr);
89 } else if (rv==EAGAIN || rv==ESRCH) {
92 fatal("resolver_afterpoll: adns_check() returned %d",rv);
99 /* Initialise adns, using parameters supplied */
100 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
101 dict_t *context, list_t *args)
108 st=safe_malloc(sizeof(*st),"adnsresolver_apply");
109 st->cl.description="adns";
110 st->cl.type=CL_RESOLVER;
112 st->cl.interface=&st->ops;
115 st->ops.request=resolve_request;
118 if (!i || i->type!=t_dict) {
119 cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
122 conf=dict_read_string(d,"config",False,"adns",loc);
125 if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
126 fatal_perror("Failed to initialise ADNS");
129 if (adns_init(&st->ast, 0, 0)) {
130 fatal_perror("Failed to initialise ADNS");
134 register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
135 ADNS_POLLFDS_RECOMMENDED+5,"resolver");
137 return new_closure(&st->cl);
140 void resolver_module(dict_t *dict)
142 add_closure(dict,"adns",adnsresolver_apply);