chiark / gitweb /
resolver: Support IPv6 name resolution
[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.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 #ifdef CONFIG_IPV6
119                     case AF_INET6:
120                         assert(ra->len == sizeof(ca->ia.sin6));
121                         break;
122 #endif /*CONFIG_IPV6*/
123                     default:
124                         /* silently skip unexpected AFs from adns */
125                         continue;
126                     }
127                     memcpy(&ca->ia,&ra->addr,ra->len);
128                     wslot++;
129                 }
130                 q->answer(q->cst,ca_buf,wslot,total);
131                 free(q);
132                 free(ans);
133             }
134         } else if (rv==EAGAIN || rv==ESRCH) {
135             break;
136         } else {
137             fatal("resolver_afterpoll: adns_check() returned %d",rv);
138         }
139     }
140
141     return;
142 }
143
144 /* Initialise adns, using parameters supplied */
145 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
146                                   dict_t *context, list_t *args)
147 {
148     struct adns *st;
149     dict_t *d;
150     item_t *i;
151     string_t conf;
152
153     st=safe_malloc(sizeof(*st),"adnsresolver_apply");
154     st->cl.description="adns";
155     st->cl.type=CL_RESOLVER;
156     st->cl.apply=NULL;
157     st->cl.interface=&st->ops;
158     st->loc=loc;
159     st->ops.st=st;
160     st->ops.request=resolve_request;
161
162     i=list_elem(args,0);
163     if (!i || i->type!=t_dict) {
164         cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
165     }
166     d=i->data.dict;
167     conf=dict_read_string(d,"config",False,"adns",loc);
168
169     if (conf) {
170         if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
171             fatal_perror("Failed to initialise ADNS");
172         }
173     } else {
174         if (adns_init(&st->ast, 0, 0)) {
175             fatal_perror("Failed to initialise ADNS");
176         }
177     }
178
179     register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
180                       ADNS_POLLFDS_RECOMMENDED+5,"resolver");
181
182     return new_closure(&st->cl);
183 }
184
185 void resolver_module(dict_t *dict)
186 {
187     add_closure(dict,"adns",adnsresolver_apply);
188 }