chiark / gitweb /
autoconf: Update to autoconf 2.69
[secnet.git] / 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.ia.sin.sin_family=AF_INET;
47         ca.ia.sin.sin_port=htons(port);
48         if (inet_aton(trimmed,&ca.ia.sin.sin_addr))
49             cb(cst,&ca,1,1);
50         else
51             cb(cst,0,0,0);
52         return True;
53     }
54
55     q=safe_malloc(sizeof *q,"resolve_request");
56     q->cst=cst;
57     q->comm=comm;
58     q->port=port;
59     q->answer=cb;
60
61     rv=adns_submit(st->ast, name, adns_r_addr, 0, q, &q->query);
62     if (rv) {
63         Message(M_WARNING,
64                 "resolver: failed to submit lookup for %s: %s",name,
65                 adns_strerror(rv));
66         free(q);
67         return False;
68     }
69
70     return True;
71 }
72
73 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
74                                int *timeout_io)
75 {
76     struct adns *st=sst;
77     return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
78 }
79
80 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
81 {
82     struct adns *st=sst;
83     adns_query aq;
84     adns_answer *ans;
85     void *qp;
86     struct query *q;
87     int rv;
88
89     adns_afterpoll(st->ast, fds, nfds, tv_now);
90
91     while (True) {
92         aq=NULL;
93         rv=adns_check(st->ast, &aq, &ans, &qp);
94         if (rv==0) {
95             q=qp;
96             if (ans->status!=adns_s_ok) {
97                 q->answer(q->cst,NULL,0,0); /* Failure */
98                 free(q);
99                 free(ans);
100             } else {
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;
105                      rslot<ans->nrrs;
106                      rslot++) {
107                     total++;
108                     if (!(wslot<ca_len)) continue;
109                     adns_rr_addr *ra=&ans->rrs.addr[rslot];
110                     struct comm_addr *ca=&ca_buf[wslot];
111                     ca->comm=q->comm;
112                     switch (ra->addr.sa.sa_family) {
113                     case AF_INET:
114                         assert(ra->len == sizeof(ca->ia.sin));
115                         break;
116                     default:
117                         /* silently skip unexpected AFs from adns */
118                         continue;
119                     }
120                     memcpy(&ca->ia,&ra->addr,ra->len);
121                     wslot++;
122                 }
123                 q->answer(q->cst,ca_buf,wslot,total);
124                 free(q);
125                 free(ans);
126             }
127         } else if (rv==EAGAIN || rv==ESRCH) {
128             break;
129         } else {
130             fatal("resolver_afterpoll: adns_check() returned %d",rv);
131         }
132     }
133
134     return;
135 }
136
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)
140 {
141     struct adns *st;
142     dict_t *d;
143     item_t *i;
144     string_t conf;
145
146     st=safe_malloc(sizeof(*st),"adnsresolver_apply");
147     st->cl.description="adns";
148     st->cl.type=CL_RESOLVER;
149     st->cl.apply=NULL;
150     st->cl.interface=&st->ops;
151     st->loc=loc;
152     st->ops.st=st;
153     st->ops.request=resolve_request;
154
155     i=list_elem(args,0);
156     if (!i || i->type!=t_dict) {
157         cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
158     }
159     d=i->data.dict;
160     conf=dict_read_string(d,"config",False,"adns",loc);
161
162     if (conf) {
163         if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
164             fatal_perror("Failed to initialise ADNS");
165         }
166     } else {
167         if (adns_init(&st->ast, 0, 0)) {
168             fatal_perror("Failed to initialise ADNS");
169         }
170     }
171
172     register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
173                       ADNS_POLLFDS_RECOMMENDED+5,"resolver");
174
175     return new_closure(&st->cl);
176 }
177
178 void resolver_module(dict_t *dict)
179 {
180     add_closure(dict,"adns",adnsresolver_apply);
181 }