Commit | Line | Data |
---|---|---|
2fe58dfd SE |
1 | /* Name resolution using adns */ |
2 | ||
3 | #include <errno.h> | |
4 | #include "secnet.h" | |
d3fe100d SE |
5 | #ifndef HAVE_LIBADNS |
6 | #error secnet requires ADNS version 1.0 or above | |
7 | #endif | |
2fe58dfd SE |
8 | #include <adns.h> |
9 | ||
d3fe100d | 10 | |
2fe58dfd SE |
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 | ||
fe5e9cc4 SE |
24 | static resolve_request_fn resolve_request; |
25 | static bool_t resolve_request(void *sst, cstring_t name, | |
2fe58dfd SE |
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, const struct timeval *tv_now, | |
43 | uint64_t *now) | |
44 | { | |
45 | struct adns *st=sst; | |
46 | return adns_beforepoll(st->ast, fds, nfds_io, timeout_io, tv_now); | |
47 | } | |
48 | ||
49 | static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds, | |
50 | const struct timeval *tv_now, uint64_t *now) | |
51 | { | |
52 | struct adns *st=sst; | |
53 | adns_query aq; | |
54 | adns_answer *ans; | |
55 | void *qp; | |
56 | struct query *q; | |
57 | int rv; | |
58 | ||
59 | adns_afterpoll(st->ast, fds, nfds, tv_now); | |
60 | ||
61 | while (True) { | |
62 | aq=NULL; | |
63 | rv=adns_check(st->ast, &aq, &ans, &qp); | |
64 | if (rv==0) { | |
65 | q=qp; | |
66 | if (ans->status!=adns_s_ok) { | |
67 | q->answer(q->cst,NULL); /* Failure */ | |
68 | free(q); | |
69 | free(ans); | |
70 | } else { | |
71 | q->answer(q->cst,ans->rrs.inaddr); | |
72 | free(q); | |
73 | free(ans); | |
74 | } | |
75 | } else if (rv==EAGAIN || rv==ESRCH) { | |
76 | break; | |
77 | } else { | |
4f5e39ec | 78 | fatal("resolver_afterpoll: adns_check() returned %d",rv); |
2fe58dfd SE |
79 | } |
80 | } | |
81 | ||
82 | return; | |
83 | } | |
84 | ||
85 | /* Initialise adns, using parameters supplied */ | |
86 | static list_t *adnsresolver_apply(closure_t *self, struct cloc loc, | |
87 | dict_t *context, list_t *args) | |
88 | { | |
89 | struct adns *st; | |
90 | dict_t *d; | |
91 | item_t *i; | |
92 | string_t conf; | |
93 | ||
94 | st=safe_malloc(sizeof(*st),"adnsresolver_apply"); | |
95 | st->cl.description="adns"; | |
96 | st->cl.type=CL_RESOLVER; | |
97 | st->cl.apply=NULL; | |
98 | st->cl.interface=&st->ops; | |
99 | st->loc=loc; | |
100 | st->ops.st=st; | |
101 | st->ops.request=resolve_request; | |
102 | ||
103 | i=list_elem(args,0); | |
104 | if (!i || i->type!=t_dict) { | |
105 | cfgfatal(st->loc,"adns","first argument must be a dictionary\n"); | |
106 | } | |
107 | d=i->data.dict; | |
108 | conf=dict_read_string(d,"config",False,"adns",loc); | |
109 | ||
110 | if (conf) { | |
111 | if (adns_init_strcfg(&st->ast, 0, 0, conf)) { | |
112 | fatal_perror("Failed to initialise ADNS"); | |
113 | } | |
114 | } else { | |
115 | if (adns_init(&st->ast, 0, 0)) { | |
116 | fatal_perror("Failed to initialise ADNS"); | |
117 | } | |
118 | } | |
119 | ||
120 | register_for_poll(st, resolver_beforepoll, resolver_afterpoll, | |
121 | ADNS_POLLFDS_RECOMMENDED+5,"resolver"); | |
122 | ||
123 | return new_closure(&st->cl); | |
124 | } | |
125 | ||
2fe58dfd SE |
126 | void resolver_module(dict_t *dict) |
127 | { | |
128 | add_closure(dict,"adns",adnsresolver_apply); | |
129 | } |