udp: Support IPv6 (mostly)
[secnet] / resolver.c
1 /* Name resolution using adns */
2
3 #include <errno.h>
4 #include "secnet.h"
5 #include "util.h"
6 #ifndef HAVE_LIBADNS
7 #error secnet requires ADNS version 1.0 or above
8 #endif
9 #include <adns.h>
10 #include <arpa/inet.h>
11 #include <string.h>
12
13
14 struct adns {
15 closure_t cl;
16 struct resolver_if ops;
17 struct cloc loc;
18 adns_state ast;
19 };
20
21 struct query {
22 void *cst;
23 int port;
24 struct comm_if *comm;
25 resolve_answer_fn *answer;
26 adns_query query;
27 };
28
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)
33 {
34 struct adns *st=sst;
35 struct query *q;
36 int rv;
37 const int maxlitlen=50;
38
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);
43 trimmed[l-2]=0;
44 struct comm_addr ca;
45 ca.comm=comm;
46 ca.ix=-1;
47 ca.ia.sin.sin_family=AF_INET;
48 ca.ia.sin.sin_port=htons(port);
49 if (inet_aton(trimmed,&ca.ia.sin.sin_addr))
50 cb(cst,&ca,1,1);
51 else
52 cb(cst,0,0,0);
53 return True;
54 }
55
56 q=safe_malloc(sizeof *q,"resolve_request");
57 q->cst=cst;
58 q->comm=comm;
59 q->port=port;
60 q->answer=cb;
61
62 rv=adns_submit(st->ast, name, adns_r_addr, 0, q, &q->query);
63 if (rv) {
64 Message(M_WARNING,
65 "resolver: failed to submit lookup for %s: %s",name,
66 adns_strerror(rv));
67 free(q);
68 return False;
69 }
70
71 return True;
72 }
73
74 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
75 int *timeout_io)
76 {
77 struct adns *st=sst;
78 return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
79 }
80
81 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
82 {
83 struct adns *st=sst;
84 adns_query aq;
85 adns_answer *ans;
86 void *qp;
87 struct query *q;
88 int rv;
89
90 adns_afterpoll(st->ast, fds, nfds, tv_now);
91
92 while (True) {
93 aq=NULL;
94 rv=adns_check(st->ast, &aq, &ans, &qp);
95 if (rv==0) {
96 q=qp;
97 if (ans->status!=adns_s_ok) {
98 q->answer(q->cst,NULL,0,0); /* Failure */
99 free(q);
100 free(ans);
101 } else {
102 int rslot, wslot, total;
103 int ca_len=MIN(ans->nrrs,MAX_PEER_ADDRS);
104 struct comm_addr ca_buf[ca_len];
105 for (rslot=0, wslot=0, total=0;
106 rslot<ans->nrrs;
107 rslot++) {
108 total++;
109 if (!(wslot<ca_len)) continue;
110 adns_rr_addr *ra=&ans->rrs.addr[rslot];
111 struct comm_addr *ca=&ca_buf[wslot];
112 ca->comm=q->comm;
113 ca->ix=-1;
114 switch (ra->addr.sa.sa_family) {
115 case AF_INET:
116 assert(ra->len == sizeof(ca->ia.sin));
117 break;
118 default:
119 /* silently skip unexpected AFs from adns */
120 continue;
121 }
122 memcpy(&ca->ia,&ra->addr,ra->len);
123 wslot++;
124 }
125 q->answer(q->cst,ca_buf,wslot,total);
126 free(q);
127 free(ans);
128 }
129 } else if (rv==EAGAIN || rv==ESRCH) {
130 break;
131 } else {
132 fatal("resolver_afterpoll: adns_check() returned %d",rv);
133 }
134 }
135
136 return;
137 }
138
139 /* Initialise adns, using parameters supplied */
140 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
141 dict_t *context, list_t *args)
142 {
143 struct adns *st;
144 dict_t *d;
145 item_t *i;
146 string_t conf;
147
148 st=safe_malloc(sizeof(*st),"adnsresolver_apply");
149 st->cl.description="adns";
150 st->cl.type=CL_RESOLVER;
151 st->cl.apply=NULL;
152 st->cl.interface=&st->ops;
153 st->loc=loc;
154 st->ops.st=st;
155 st->ops.request=resolve_request;
156
157 i=list_elem(args,0);
158 if (!i || i->type!=t_dict) {
159 cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
160 }
161 d=i->data.dict;
162 conf=dict_read_string(d,"config",False,"adns",loc);
163
164 if (conf) {
165 if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
166 fatal_perror("Failed to initialise ADNS");
167 }
168 } else {
169 if (adns_init(&st->ast, 0, 0)) {
170 fatal_perror("Failed to initialise ADNS");
171 }
172 }
173
174 register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
175 ADNS_POLLFDS_RECOMMENDED+5,"resolver");
176
177 return new_closure(&st->cl);
178 }
179
180 void resolver_module(dict_t *dict)
181 {
182 add_closure(dict,"adns",adnsresolver_apply);
183 }