chiark / gitweb /
udp: Provide st->addr_configured
[secnet] / resolver.c
CommitLineData
2fe58dfd
SE
1/* Name resolution using adns */
2
3#include <errno.h>
4#include "secnet.h"
cc420616 5#include "util.h"
d3fe100d
SE
6#ifndef HAVE_LIBADNS
7#error secnet requires ADNS version 1.0 or above
8#endif
2fe58dfd 9#include <adns.h>
d1e8fd82
IJ
10#include <arpa/inet.h>
11#include <string.h>
2fe58dfd 12
d3fe100d 13
2fe58dfd
SE
14struct adns {
15 closure_t cl;
16 struct resolver_if ops;
17 struct cloc loc;
18 adns_state ast;
19};
20
21struct query {
22 void *cst;
bc07424d 23 const char *name;
cc420616
IJ
24 int port;
25 struct comm_if *comm;
2fe58dfd
SE
26 resolve_answer_fn *answer;
27 adns_query query;
28};
29
fe5e9cc4
SE
30static resolve_request_fn resolve_request;
31static bool_t resolve_request(void *sst, cstring_t name,
cc420616 32 int port, struct comm_if *comm,
2fe58dfd
SE
33 resolve_answer_fn *cb, void *cst)
34{
35 struct adns *st=sst;
36 struct query *q;
37 int rv;
3f2c70f6
IJ
38 const int maxlitlen=
39#ifdef CONFIG_IPV6
40 ADNS_ADDR2TEXT_BUFLEN*2
41#else
42 50
43#endif
44 ;
d1e8fd82
IJ
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;
cc420616 50 struct comm_addr ca;
cc420616 51 ca.comm=comm;
08b62a6c 52 ca.ix=-1;
3f2c70f6
IJ
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;
bc07424d 63 cb(cst,0,0,0,name,msg);
3f2c70f6 64 } else {
bc07424d 65 cb(cst,&ca,1,1,name,0);
3f2c70f6
IJ
66 }
67#else
a32d56fb
IJ
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))
bc07424d 71 cb(cst,&ca,1,1,name,0);
d1e8fd82 72 else
bc07424d 73 cb(cst,0,0,0,name,"invalid IP address");
3f2c70f6 74#endif
d1e8fd82
IJ
75 return True;
76 }
2fe58dfd 77
952f601f 78 NEW(q);
2fe58dfd 79 q->cst=cst;
cc420616
IJ
80 q->comm=comm;
81 q->port=port;
bc07424d 82 q->name=name;
2fe58dfd
SE
83 q->answer=cb;
84
cc420616 85 rv=adns_submit(st->ast, name, adns_r_addr, 0, q, &q->query);
75829536
IJ
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 }
2fe58dfd 93
75829536 94 return True;
2fe58dfd
SE
95}
96
97static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
90a39563 98 int *timeout_io)
2fe58dfd
SE
99{
100 struct adns *st=sst;
101 return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
102}
103
90a39563 104static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
2fe58dfd
SE
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) {
bc07424d 121 q->answer(q->cst,NULL,0,0,q->name,adns_strerror(ans->status));
2fe58dfd
SE
122 free(q);
123 free(ans);
124 } else {
cc420616
IJ
125 int rslot, wslot, total;
126 int ca_len=MIN(ans->nrrs,MAX_PEER_ADDRS);
127 struct comm_addr ca_buf[ca_len];
cc420616
IJ
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;
08b62a6c 136 ca->ix=-1;
f3d19c02
IJ
137 assert(ra->len <= (int)sizeof(ca->ia));
138 memcpy(&ca->ia,&ra->addr,ra->len);
cc420616
IJ
139 switch (ra->addr.sa.sa_family) {
140 case AF_INET:
a32d56fb 141 assert(ra->len == sizeof(ca->ia.sin));
f3d19c02 142 ca->ia.sin.sin_port=htons(q->port);
cc420616 143 break;
0e646750
IJ
144#ifdef CONFIG_IPV6
145 case AF_INET6:
146 assert(ra->len == sizeof(ca->ia.sin6));
f3d19c02 147 ca->ia.sin6.sin6_port=htons(q->port);
0e646750
IJ
148 break;
149#endif /*CONFIG_IPV6*/
cc420616 150 default:
2093fb5c
IJ
151 /* silently skip unexpected AFs from adns */
152 continue;
cc420616 153 }
2093fb5c 154 wslot++;
cc420616 155 }
bc07424d 156 q->answer(q->cst,ca_buf,wslot,total,q->name,0);
2fe58dfd
SE
157 free(q);
158 free(ans);
159 }
160 } else if (rv==EAGAIN || rv==ESRCH) {
161 break;
162 } else {
4f5e39ec 163 fatal("resolver_afterpoll: adns_check() returned %d",rv);
2fe58dfd
SE
164 }
165 }
166
167 return;
168}
169
170/* Initialise adns, using parameters supplied */
171static 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
b7886fd4 179 NEW(st);
2fe58dfd
SE
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,
32fc582f 206 "resolver");
2fe58dfd
SE
207
208 return new_closure(&st->cl);
209}
210
2fe58dfd
SE
211void resolver_module(dict_t *dict)
212{
213 add_closure(dict,"adns",adnsresolver_apply);
214}