chiark / gitweb /
Make -d option consistent.
[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
10
11 struct adns {
12     closure_t cl;
13     struct resolver_if ops;
14     struct cloc loc;
15     adns_state ast;
16 };
17
18 struct query {
19     void *cst;
20     resolve_answer_fn *answer;
21     adns_query query;
22 };
23
24 static resolve_request_fn resolve_request;
25 static bool_t resolve_request(void *sst, cstring_t name,
26                               resolve_answer_fn *cb, void *cst)
27 {
28     struct adns *st=sst;
29     struct query *q;
30     int rv;
31
32     q=safe_malloc(sizeof *q,"resolve_request");
33     q->cst=cst;
34     q->answer=cb;
35
36     rv=adns_submit(st->ast, name, adns_r_a, 0, q, &q->query);
37
38     return rv==0;
39 }
40
41 static int resolver_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
42                                int *timeout_io)
43 {
44     struct adns *st=sst;
45     return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now);
46 }
47
48 static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
49 {
50     struct adns *st=sst;
51     adns_query aq;
52     adns_answer *ans;
53     void *qp;
54     struct query *q;
55     int rv;
56
57     adns_afterpoll(st->ast, fds, nfds, tv_now);
58
59     while (True) {
60         aq=NULL;
61         rv=adns_check(st->ast, &aq, &ans, &qp);
62         if (rv==0) {
63             q=qp;
64             if (ans->status!=adns_s_ok) {
65                 q->answer(q->cst,NULL); /* Failure */
66                 free(q);
67                 free(ans);
68             } else {
69                 q->answer(q->cst,ans->rrs.inaddr);
70                 free(q);
71                 free(ans);
72             }
73         } else if (rv==EAGAIN || rv==ESRCH) {
74             break;
75         } else {
76             fatal("resolver_afterpoll: adns_check() returned %d",rv);
77         }
78     }
79
80     return;
81 }
82
83 /* Initialise adns, using parameters supplied */
84 static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
85                                   dict_t *context, list_t *args)
86 {
87     struct adns *st;
88     dict_t *d;
89     item_t *i;
90     string_t conf;
91
92     st=safe_malloc(sizeof(*st),"adnsresolver_apply");
93     st->cl.description="adns";
94     st->cl.type=CL_RESOLVER;
95     st->cl.apply=NULL;
96     st->cl.interface=&st->ops;
97     st->loc=loc;
98     st->ops.st=st;
99     st->ops.request=resolve_request;
100
101     i=list_elem(args,0);
102     if (!i || i->type!=t_dict) {
103         cfgfatal(st->loc,"adns","first argument must be a dictionary\n");
104     }
105     d=i->data.dict;
106     conf=dict_read_string(d,"config",False,"adns",loc);
107
108     if (conf) {
109         if (adns_init_strcfg(&st->ast, 0, 0, conf)) {
110             fatal_perror("Failed to initialise ADNS");
111         }
112     } else {
113         if (adns_init(&st->ast, 0, 0)) {
114             fatal_perror("Failed to initialise ADNS");
115         }
116     }
117
118     register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
119                       ADNS_POLLFDS_RECOMMENDED+5,"resolver");
120
121     return new_closure(&st->cl);
122 }
123
124 void resolver_module(dict_t *dict)
125 {
126     add_closure(dict,"adns",adnsresolver_apply);
127 }