1 /* Name resolution using adns */
7 #error secnet requires ADNS version 1.0 or above
10 #include <arpa/inet.h>
16 struct resolver_if ops;
25 resolve_answer_fn *answer;
29 static resolve_request_fn resolve_request;
30 static bool_t resolve_request(void *sst, cstring_t name,
31 int port, struct comm_if *comm,
32 resolve_answer_fn *cb, void *cst)
37 const int maxlitlen=50;
39 ssize_t l=strlen(name);
40 if (name[0]=='[' && l<maxlitlen && l>2 && name[l-1]==']') {
41 char trimmed[maxlitlen+1];
42 memcpy(trimmed,name+1,l-2);
46 ca.ia.sin.sin_family=AF_INET;
47 ca.ia.sin.sin_port=htons(port);
48 if (inet_aton(trimmed,&ca.ia.sin.sin_addr))
55 q=safe_malloc(sizeof *q,"resolve_request");
61 rv=adns_submit(st->ast, name, adns_r_addr, 0, q, &q->query);
64 "resolver: failed to submit lookup for %s: %s",name,
73 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
77 return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
80 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
89 adns_afterpoll(st->ast, fds, nfds, tv_now);
93 rv=adns_check(st->ast, &aq, &ans, &qp);
96 if (ans->status!=adns_s_ok) {
97 q->answer(q->cst,NULL,0,0); /* Failure */
101 int rslot, wslot, total;
102 int ca_len=MIN(ans->nrrs,MAX_PEER_ADDRS);
103 struct comm_addr ca_buf[ca_len];
104 for (rslot=0, wslot=0, total=0;
108 if (!(wslot<ca_len)) continue;
109 adns_rr_addr *ra=&ans->rrs.addr[rslot];
110 struct comm_addr *ca=&ca_buf[wslot];
112 switch (ra->addr.sa.sa_family) {
114 assert(ra->len == sizeof(ca->ia.sin));
117 /* silently skip unexpected AFs from adns */
120 memcpy(&ca->ia,&ra->addr,ra->len);
123 q->answer(q->cst,ca_buf,wslot,total);
127 } else if (rv==EAGAIN || rv==ESRCH) {
130 fatal("resolver_afterpoll: adns_check() returned %d",rv);
137 /* Initialise adns, using parameters supplied */
138 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
139 dict_t *context, list_t *args)
146 st=safe_malloc(sizeof(*st),"adnsresolver_apply");
147 st->cl.description="adns";
148 st->cl.type=CL_RESOLVER;
150 st->cl.interface=&st->ops;
153 st->ops.request=resolve_request;
156 if (!i || i->type!=t_dict) {
157 cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
160 conf=dict_read_string(d,"config",False,"adns",loc);
163 if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
164 fatal_perror("Failed to initialise ADNS");
167 if (adns_init(&st->ast, 0, 0)) {
168 fatal_perror("Failed to initialise ADNS");
172 register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
173 ADNS_POLLFDS_RECOMMENDED+5,"resolver");
175 return new_closure(&st->cl);
178 void resolver_module(dict_t *dict)
180 add_closure(dict,"adns",adnsresolver_apply);