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