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);
55 "resolver: failed to submit lookup for %s: %s",name,
64 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
68 return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
71 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
80 adns_afterpoll(st->ast, fds, nfds, tv_now);
84 rv=adns_check(st->ast, &aq, &ans, &qp);
87 if (ans->status!=adns_s_ok) {
88 q->answer(q->cst,NULL); /* Failure */
92 q->answer(q->cst,ans->rrs.inaddr);
96 } else if (rv==EAGAIN || rv==ESRCH) {
99 fatal("resolver_afterpoll: adns_check() returned %d",rv);
106 /* Initialise adns, using parameters supplied */
107 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
108 dict_t *context, list_t *args)
115 st=safe_malloc(sizeof(*st),"adnsresolver_apply");
116 st->cl.description="adns";
117 st->cl.type=CL_RESOLVER;
119 st->cl.interface=&st->ops;
122 st->ops.request=resolve_request;
125 if (!i || i->type!=t_dict) {
126 cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
129 conf=dict_read_string(d,"config",False,"adns",loc);
132 if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
133 fatal_perror("Failed to initialise ADNS");
136 if (adns_init(&st->ast, 0, 0)) {
137 fatal_perror("Failed to initialise ADNS");
141 register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
142 ADNS_POLLFDS_RECOMMENDED+5,"resolver");
144 return new_closure(&st->cl);
147 void resolver_module(dict_t *dict)
149 add_closure(dict,"adns",adnsresolver_apply);