chiark / gitweb /
Python IP addresses: Use modern ipaddr.py - supports IPv6
[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=
38 #ifdef CONFIG_IPV6
39         ADNS_ADDR2TEXT_BUFLEN*2
40 #else
41         50
42 #endif
43         ;
44     ssize_t l=strlen(name);
45     if (name[0]=='[' && l<maxlitlen && l>2 && name[l-1]==']') {
46         char trimmed[maxlitlen+1];
47         memcpy(trimmed,name+1,l-2);
48         trimmed[l-2]=0;
49         struct comm_addr ca;
50         FILLZERO(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,msg);
64         } else {
65             cb(cst,&ca,1,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,0);
72         else
73             cb(cst,0,0,"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->answer=cb;
83
84     rv=adns_submit(st->ast, name, adns_r_addr, 0, q, &q->query);
85     if (rv) {
86         Message(M_WARNING,
87                 "resolver: failed to submit lookup for %s: %s",name,
88                 adns_strerror(rv));
89         free(q);
90         return False;
91     }
92
93     return True;
94 }
95
96 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
97                                int *timeout_io)
98 {
99     struct adns *st=sst;
100     return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
101 }
102
103 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
104 {
105     struct adns *st=sst;
106     adns_query aq;
107     adns_answer *ans;
108     void *qp;
109     struct query *q;
110     int rv;
111
112     adns_afterpoll(st->ast, fds, nfds, tv_now);
113
114     while (True) {
115         aq=NULL;
116         rv=adns_check(st->ast, &aq, &ans, &qp);
117         if (rv==0) {
118             q=qp;
119             if (ans->status!=adns_s_ok) {
120                 q->answer(q->cst,NULL,0,adns_strerror(ans->status));
121                 free(q);
122                 free(ans);
123             } else {
124                 int rslot, wslot;
125                 int ca_len=MIN(ans->nrrs,MAX_PEER_ADDRS);
126                 struct comm_addr ca_buf[ca_len];
127                 FILLZERO(ca_buf);
128                 for (rslot=0, wslot=0;
129                      rslot<ans->nrrs && wslot<ca_len;
130                      rslot++) {
131                     adns_rr_addr *ra=&ans->rrs.addr[rslot];
132                     struct comm_addr *ca=&ca_buf[wslot];
133                     ca->comm=q->comm;
134                     /* copy fields individually so we leave holes zeroed: */
135                     switch (ra->addr.sa.sa_family) {
136                     case AF_INET:
137                         assert(ra->len == sizeof(ca->ia.sin));
138                         ca->ia.sin.sin_family=ra->addr.inet.sin_family;
139                         ca->ia.sin.sin_addr=  ra->addr.inet.sin_addr;
140                         ca->ia.sin.sin_port=  htons(q->port);
141                         wslot++;
142                         break;
143 #ifdef CONFIG_IPV6
144                     case AF_INET6:
145                         assert(ra->len == sizeof(ca->ia.sin6));
146                         ca->ia.sin6.sin6_family=ra->addr.inet6.sin6_family;
147                         ca->ia.sin6.sin6_addr=  ra->addr.inet6.sin6_addr;
148                         ca->ia.sin6.sin6_port=  htons(q->port);
149                         wslot++;
150                         break;
151 #endif /*CONFIG_IPV6*/
152                     default:
153                         break;
154                     }
155                 }
156                 q->answer(q->cst,ca_buf,wslot,0);
157                 free(q);
158                 free(ans);
159             }
160         } else if (rv==EAGAIN || rv==ESRCH) {
161             break;
162         } else {
163             fatal("resolver_afterpoll: adns_check() returned %d",rv);
164         }
165     }
166
167     return;
168 }
169
170 /* Initialise adns, using parameters supplied */
171 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
172                                   dict_t *context, list_t *args)
173 {
174     struct adns *st;
175     dict_t *d;
176     item_t *i;
177     string_t conf;
178
179     st=safe_malloc(sizeof(*st),"adnsresolver_apply");
180     st->cl.description="adns";
181     st->cl.type=CL_RESOLVER;
182     st->cl.apply=NULL;
183     st->cl.interface=&st->ops;
184     st->loc=loc;
185     st->ops.st=st;
186     st->ops.request=resolve_request;
187
188     i=list_elem(args,0);
189     if (!i || i->type!=t_dict) {
190         cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
191     }
192     d=i->data.dict;
193     conf=dict_read_string(d,"config",False,"adns",loc);
194
195     if (conf) {
196         if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
197             fatal_perror("Failed to initialise ADNS");
198         }
199     } else {
200         if (adns_init(&st->ast, 0, 0)) {
201             fatal_perror("Failed to initialise ADNS");
202         }
203     }
204
205     register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
206                       ADNS_POLLFDS_RECOMMENDED+5,"resolver");
207
208     return new_closure(&st->cl);
209 }
210
211 void resolver_module(dict_t *dict)
212 {
213     add_closure(dict,"adns",adnsresolver_apply);
214 }