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