chiark / gitweb /
site: Fix bugs when resolver request submission fails
[secnet] / 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     if (rv) {
54         Message(M_WARNING,
55                 "resolver: failed to submit lookup for %s: %s",name,
56                 adns_strerror(rv));
57         free(q);
58         return False;
59     }
60
61     return True;
62 }
63
64 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
65                                int *timeout_io)
66 {
67     struct adns *st=sst;
68     return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
69 }
70
71 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
72 {
73     struct adns *st=sst;
74     adns_query aq;
75     adns_answer *ans;
76     void *qp;
77     struct query *q;
78     int rv;
79
80     adns_afterpoll(st->ast, fds, nfds, tv_now);
81
82     while (True) {
83         aq=NULL;
84         rv=adns_check(st->ast, &aq, &ans, &qp);
85         if (rv==0) {
86             q=qp;
87             if (ans->status!=adns_s_ok) {
88                 q->answer(q->cst,NULL); /* Failure */
89                 free(q);
90                 free(ans);
91             } else {
92                 q->answer(q->cst,ans->rrs.inaddr);
93                 free(q);
94                 free(ans);
95             }
96         } else if (rv==EAGAIN || rv==ESRCH) {
97             break;
98         } else {
99             fatal("resolver_afterpoll: adns_check() returned %d",rv);
100         }
101     }
102
103     return;
104 }
105
106 /* Initialise adns, using parameters supplied */
107 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
108                                   dict_t *context, list_t *args)
109 {
110     struct adns *st;
111     dict_t *d;
112     item_t *i;
113     string_t conf;
114
115     st=safe_malloc(sizeof(*st),"adnsresolver_apply");
116     st->cl.description="adns";
117     st->cl.type=CL_RESOLVER;
118     st->cl.apply=NULL;
119     st->cl.interface=&st->ops;
120     st->loc=loc;
121     st->ops.st=st;
122     st->ops.request=resolve_request;
123
124     i=list_elem(args,0);
125     if (!i || i->type!=t_dict) {
126         cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
127     }
128     d=i->data.dict;
129     conf=dict_read_string(d,"config",False,"adns",loc);
130
131     if (conf) {
132         if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
133             fatal_perror("Failed to initialise ADNS");
134         }
135     } else {
136         if (adns_init(&st->ast, 0, 0)) {
137             fatal_perror("Failed to initialise ADNS");
138         }
139     }
140
141     register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
142                       ADNS_POLLFDS_RECOMMENDED+5,"resolver");
143
144     return new_closure(&st->cl);
145 }
146
147 void resolver_module(dict_t *dict)
148 {
149     add_closure(dict,"adns",adnsresolver_apply);
150 }