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