chiark / gitweb /
Mobile sites: Maintain multiple addresses for some peers (new feature)
[secnet.git] / resolver.c
1 /* Name resolution using adns */
2
3 #include <errno.h>
4 #include "secnet.h"
5 #ifndef HAVE_LIBADNS
6 #error secnet requires ADNS version 1.0 or above
7 #endif
8 #include <adns.h>
9 #include <arpa/inet.h>
10 #include <string.h>
11
12
13 struct adns {
14     closure_t cl;
15     struct resolver_if ops;
16     struct cloc loc;
17     adns_state ast;
18 };
19
20 struct query {
21     void *cst;
22     resolve_answer_fn *answer;
23     adns_query query;
24 };
25
26 static resolve_request_fn resolve_request;
27 static bool_t resolve_request(void *sst, cstring_t name,
28                               resolve_answer_fn *cb, void *cst)
29 {
30     struct adns *st=sst;
31     struct query *q;
32     int rv;
33     const int maxlitlen=50;
34
35     ssize_t l=strlen(name);
36     if (name[0]=='[' && l<maxlitlen && l>2 && name[l-1]==']') {
37         char trimmed[maxlitlen+1];
38         memcpy(trimmed,name+1,l-2);
39         trimmed[l-2]=0;
40         struct in_addr ia;
41         if (inet_aton(trimmed,&ia))
42             cb(cst,&ia);
43         else
44             cb(cst,0);
45         return True;
46     }
47
48     q=safe_malloc(sizeof *q,"resolve_request");
49     q->cst=cst;
50     q->answer=cb;
51
52     rv=adns_submit(st->ast, name, adns_r_a, 0, q, &q->query);
53
54     return rv==0;
55 }
56
57 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
58                                int *timeout_io)
59 {
60     struct adns *st=sst;
61     return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
62 }
63
64 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
65 {
66     struct adns *st=sst;
67     adns_query aq;
68     adns_answer *ans;
69     void *qp;
70     struct query *q;
71     int rv;
72
73     adns_afterpoll(st->ast, fds, nfds, tv_now);
74
75     while (True) {
76         aq=NULL;
77         rv=adns_check(st->ast, &aq, &ans, &qp);
78         if (rv==0) {
79             q=qp;
80             if (ans->status!=adns_s_ok) {
81                 q->answer(q->cst,NULL); /* Failure */
82                 free(q);
83                 free(ans);
84             } else {
85                 q->answer(q->cst,ans->rrs.inaddr);
86                 free(q);
87                 free(ans);
88             }
89         } else if (rv==EAGAIN || rv==ESRCH) {
90             break;
91         } else {
92             fatal("resolver_afterpoll: adns_check() returned %d",rv);
93         }
94     }
95
96     return;
97 }
98
99 /* Initialise adns, using parameters supplied */
100 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
101                                   dict_t *context, list_t *args)
102 {
103     struct adns *st;
104     dict_t *d;
105     item_t *i;
106     string_t conf;
107
108     st=safe_malloc(sizeof(*st),"adnsresolver_apply");
109     st->cl.description="adns";
110     st->cl.type=CL_RESOLVER;
111     st->cl.apply=NULL;
112     st->cl.interface=&st->ops;
113     st->loc=loc;
114     st->ops.st=st;
115     st->ops.request=resolve_request;
116
117     i=list_elem(args,0);
118     if (!i || i->type!=t_dict) {
119         cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
120     }
121     d=i->data.dict;
122     conf=dict_read_string(d,"config",False,"adns",loc);
123
124     if (conf) {
125         if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
126             fatal_perror("Failed to initialise ADNS");
127         }
128     } else {
129         if (adns_init(&st->ast, 0, 0)) {
130             fatal_perror("Failed to initialise ADNS");
131         }
132     }
133
134     register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
135                       ADNS_POLLFDS_RECOMMENDED+5,"resolver");
136
137     return new_closure(&st->cl);
138 }
139
140 void resolver_module(dict_t *dict)
141 {
142     add_closure(dict,"adns",adnsresolver_apply);
143 }