chiark / gitweb /
comm: Provide udp_socks_deregister
[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     const char *name;
24     int port;
25     struct comm_if *comm;
26     resolve_answer_fn *answer;
27     adns_query query;
28 };
29
30 static resolve_request_fn resolve_request;
31 static bool_t resolve_request(void *sst, cstring_t name,
32                               int port, struct comm_if *comm,
33                               resolve_answer_fn *cb, void *cst)
34 {
35     struct adns *st=sst;
36     struct query *q;
37     int rv;
38     const int maxlitlen=
39 #ifdef CONFIG_IPV6
40         ADNS_ADDR2TEXT_BUFLEN*2
41 #else
42         50
43 #endif
44         ;
45     ssize_t l=strlen(name);
46     if (name[0]=='[' && l<maxlitlen && l>2 && name[l-1]==']') {
47         char trimmed[maxlitlen+1];
48         memcpy(trimmed,name+1,l-2);
49         trimmed[l-2]=0;
50         struct comm_addr ca;
51         ca.comm=comm;
52         ca.ix=-1;
53 #ifdef CONFIG_IPV6
54         socklen_t salen=sizeof(ca.ia);
55         rv=adns_text2addr(trimmed, port, adns_qf_addrlit_ipv4_quadonly,
56                           &ca.ia.sa, &salen);
57         assert(rv!=ENOSPC);
58         if (rv) {
59             char msg[250];
60             snprintf(msg,sizeof(msg),"invalid address literal: %s",
61                      strerror(rv));
62             msg[sizeof(msg)-1]=0;
63             cb(cst,0,0,0,name,msg);
64         } else {
65             cb(cst,&ca,1,1,name,0);
66         }
67 #else
68         ca.ia.sin.sin_family=AF_INET;
69         ca.ia.sin.sin_port=htons(port);
70         if (inet_aton(trimmed,&ca.ia.sin.sin_addr))
71             cb(cst,&ca,1,1,name,0);
72         else
73             cb(cst,0,0,0,name,"invalid IP address");
74 #endif
75         return True;
76     }
77
78     q=safe_malloc(sizeof *q,"resolve_request");
79     q->cst=cst;
80     q->comm=comm;
81     q->port=port;
82     q->name=name;
83     q->answer=cb;
84
85     rv=adns_submit(st->ast, name, adns_r_addr, 0, q, &q->query);
86     if (rv) {
87         Message(M_WARNING,
88                 "resolver: failed to submit lookup for %s: %s",name,
89                 adns_strerror(rv));
90         free(q);
91         return False;
92     }
93
94     return True;
95 }
96
97 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
98                                int *timeout_io)
99 {
100     struct adns *st=sst;
101     return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
102 }
103
104 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
105 {
106     struct adns *st=sst;
107     adns_query aq;
108     adns_answer *ans;
109     void *qp;
110     struct query *q;
111     int rv;
112
113     adns_afterpoll(st->ast, fds, nfds, tv_now);
114
115     while (True) {
116         aq=NULL;
117         rv=adns_check(st->ast, &aq, &ans, &qp);
118         if (rv==0) {
119             q=qp;
120             if (ans->status!=adns_s_ok) {
121                 q->answer(q->cst,NULL,0,0,q->name,adns_strerror(ans->status));
122                 free(q);
123                 free(ans);
124             } else {
125                 int rslot, wslot, total;
126                 int ca_len=MIN(ans->nrrs,MAX_PEER_ADDRS);
127                 struct comm_addr ca_buf[ca_len];
128                 for (rslot=0, wslot=0, total=0;
129                      rslot<ans->nrrs;
130                      rslot++) {
131                     total++;
132                     if (!(wslot<ca_len)) continue;
133                     adns_rr_addr *ra=&ans->rrs.addr[rslot];
134                     struct comm_addr *ca=&ca_buf[wslot];
135                     ca->comm=q->comm;
136                     ca->ix=-1;
137                     switch (ra->addr.sa.sa_family) {
138                     case AF_INET:
139                         assert(ra->len == sizeof(ca->ia.sin));
140                         break;
141 #ifdef CONFIG_IPV6
142                     case AF_INET6:
143                         assert(ra->len == sizeof(ca->ia.sin6));
144                         break;
145 #endif /*CONFIG_IPV6*/
146                     default:
147                         /* silently skip unexpected AFs from adns */
148                         continue;
149                     }
150                     memcpy(&ca->ia,&ra->addr,ra->len);
151                     wslot++;
152                 }
153                 q->answer(q->cst,ca_buf,wslot,total,q->name,0);
154                 free(q);
155                 free(ans);
156             }
157         } else if (rv==EAGAIN || rv==ESRCH) {
158             break;
159         } else {
160             fatal("resolver_afterpoll: adns_check() returned %d",rv);
161         }
162     }
163
164     return;
165 }
166
167 /* Initialise adns, using parameters supplied */
168 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
169                                   dict_t *context, list_t *args)
170 {
171     struct adns *st;
172     dict_t *d;
173     item_t *i;
174     string_t conf;
175
176     st=safe_malloc(sizeof(*st),"adnsresolver_apply");
177     st->cl.description="adns";
178     st->cl.type=CL_RESOLVER;
179     st->cl.apply=NULL;
180     st->cl.interface=&st->ops;
181     st->loc=loc;
182     st->ops.st=st;
183     st->ops.request=resolve_request;
184
185     i=list_elem(args,0);
186     if (!i || i->type!=t_dict) {
187         cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
188     }
189     d=i->data.dict;
190     conf=dict_read_string(d,"config",False,"adns",loc);
191
192     if (conf) {
193         if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
194             fatal_perror("Failed to initialise ADNS");
195         }
196     } else {
197         if (adns_init(&st->ast, 0, 0)) {
198             fatal_perror("Failed to initialise ADNS");
199         }
200     }
201
202     register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
203                       "resolver");
204
205     return new_closure(&st->cl);
206 }
207
208 void resolver_module(dict_t *dict)
209 {
210     add_closure(dict,"adns",adnsresolver_apply);
211 }