resolver: construct comm_addr; honour multiple addresses from the resolver
[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 FILLZERO(ca);
46 ca.comm=comm;
47 ca.sin.sin_family=AF_INET;
48 ca.sin.sin_port=htons(port);
49 if (inet_aton(trimmed,&ca.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 FILLZERO(ca_buf);
106 for (rslot=0, wslot=0, total=0;
107 rslot<ans->nrrs;
108 rslot++) {
109 total++;
110 if (!(wslot<ca_len)) continue;
111 adns_rr_addr *ra=&ans->rrs.addr[rslot];
112 struct comm_addr *ca=&ca_buf[wslot];
113 ca->comm=q->comm;
114 /* copy fields individually so we leave holes zeroed: */
115 switch (ra->addr.sa.sa_family) {
116 case AF_INET:
117 assert(ra->len == sizeof(ca->sin));
118 ca->sin.sin_family=ra->addr.inet.sin_family;
119 ca->sin.sin_addr= ra->addr.inet.sin_addr;
120 ca->sin.sin_port= htons(q->port);
121 wslot++;
122 break;
123 default:
124 break;
125 }
126 }
127 q->answer(q->cst,ca_buf,wslot,total);
128 free(q);
129 free(ans);
130 }
131 } else if (rv==EAGAIN || rv==ESRCH) {
132 break;
133 } else {
134 fatal("resolver_afterpoll: adns_check() returned %d",rv);
135 }
136 }
137
138 return;
139 }
140
141 /* Initialise adns, using parameters supplied */
142 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
143 dict_t *context, list_t *args)
144 {
145 struct adns *st;
146 dict_t *d;
147 item_t *i;
148 string_t conf;
149
150 st=safe_malloc(sizeof(*st),"adnsresolver_apply");
151 st->cl.description="adns";
152 st->cl.type=CL_RESOLVER;
153 st->cl.apply=NULL;
154 st->cl.interface=&st->ops;
155 st->loc=loc;
156 st->ops.st=st;
157 st->ops.request=resolve_request;
158
159 i=list_elem(args,0);
160 if (!i || i->type!=t_dict) {
161 cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
162 }
163 d=i->data.dict;
164 conf=dict_read_string(d,"config",False,"adns",loc);
165
166 if (conf) {
167 if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
168 fatal_perror("Failed to initialise ADNS");
169 }
170 } else {
171 if (adns_init(&st->ast, 0, 0)) {
172 fatal_perror("Failed to initialise ADNS");
173 }
174 }
175
176 register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
177 ADNS_POLLFDS_RECOMMENDED+5,"resolver");
178
179 return new_closure(&st->cl);
180 }
181
182 void resolver_module(dict_t *dict)
183 {
184 add_closure(dict,"adns",adnsresolver_apply);
185 }