[cd7b81d] | 1 | /*
|
---|
[626f420] | 2 | * Copyright (c) 2014 Mike Belopuhov
|
---|
[e87ab6d] | 3 | *
|
---|
| 4 | * ASR implementation and OpenSMTPD integration are copyright
|
---|
| 5 | * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
|
---|
| 6 | * Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
|
---|
| 7 | * Copyright (c) 2011-2012 Eric Faurot <eric@faurot.net>
|
---|
[cd7b81d] | 8 | *
|
---|
| 9 | * Permission to use, copy, modify, and distribute this software for any
|
---|
| 10 | * purpose with or without fee is hereby granted, provided that the above
|
---|
| 11 | * copyright notice and this permission notice appear in all copies.
|
---|
| 12 | *
|
---|
| 13 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
---|
| 14 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
---|
| 15 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
---|
| 16 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
---|
| 17 | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
|
---|
| 18 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
---|
| 19 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
[e87ab6d] | 22 | #include <sys/types.h>
|
---|
[cd7b81d] | 23 | #include <sys/socket.h>
|
---|
| 24 | #include <sys/time.h>
|
---|
| 25 | #include <netinet/in.h>
|
---|
| 26 | #include <errno.h>
|
---|
[e87ab6d] | 27 | #include <event.h>
|
---|
| 28 | #include <resolv.h>
|
---|
[cd7b81d] | 29 | #include <stdlib.h>
|
---|
[b4049f9] | 30 | #include <string.h>
|
---|
[cd7b81d] | 31 | #include <syslog.h>
|
---|
[e87ab6d] | 32 |
|
---|
| 33 | #include "asr.h"
|
---|
[cd7b81d] | 34 |
|
---|
| 35 | #include "icb.h"
|
---|
| 36 | #include "icbd.h"
|
---|
| 37 |
|
---|
[e87ab6d] | 38 | struct async_event;
|
---|
[e80f9fc] | 39 | struct async_event *async_run_event(struct async *,
|
---|
| 40 | void (*)(int, struct async_res *, void *), void *);
|
---|
| 41 | void dns_done(int, struct async_res *, void *);
|
---|
[460786f] | 42 |
|
---|
| 43 | extern int dodns;
|
---|
| 44 |
|
---|
[e87ab6d] | 45 | void
|
---|
| 46 | dns_done(int ev __attribute__((__unused__)), struct async_res *ar, void *arg)
|
---|
[cd7b81d] | 47 | {
|
---|
[e87ab6d] | 48 | struct icb_session *is = arg;
|
---|
| 49 |
|
---|
| 50 | if (ar->ar_gai_errno == 0) {
|
---|
[e80f9fc] | 51 | icbd_log(is, LOG_DEBUG, "dns resolved %s to %s", is->host,
|
---|
[e87ab6d] | 52 | is->hostname);
|
---|
| 53 | if (strncmp(is->hostname, "localhost",
|
---|
| 54 | sizeof "localhost" - 1) == 0)
|
---|
| 55 | strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
|
---|
| 56 | else if (strlen(is->hostname) < ICB_MAXHOSTLEN)
|
---|
| 57 | strlcpy(is->host, is->hostname, ICB_MAXHOSTLEN);
|
---|
| 58 | } else
|
---|
[e80f9fc] | 59 | icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
|
---|
[e87ab6d] | 60 | gai_strerror(ar->ar_gai_errno));
|
---|
[cd7b81d] | 61 | }
|
---|
| 62 |
|
---|
| 63 | void
|
---|
[e87ab6d] | 64 | dns_rresolv(struct icb_session *is, struct sockaddr *sa)
|
---|
[cd7b81d] | 65 | {
|
---|
[e87ab6d] | 66 | struct async *as;
|
---|
[cd7b81d] | 67 |
|
---|
[e87ab6d] | 68 | if (!dodns)
|
---|
[cd7b81d] | 69 | return;
|
---|
| 70 |
|
---|
| 71 | if (verbose)
|
---|
[e80f9fc] | 72 | icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
|
---|
[cd7b81d] | 73 |
|
---|
[e87ab6d] | 74 | as = getnameinfo_async(sa, sa->sa_len, is->hostname,
|
---|
| 75 | sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
|
---|
| 76 | async_run_event(as, dns_done, is);
|
---|
[cd7b81d] | 77 | }
|
---|
| 78 |
|
---|
[e87ab6d] | 79 | /* Generic libevent glue for asr */
|
---|
[b4049f9] | 80 |
|
---|
[e87ab6d] | 81 | struct async_event {
|
---|
| 82 | struct async *async;
|
---|
| 83 | struct event ev;
|
---|
| 84 | void (*callback)(int, struct async_res *, void *);
|
---|
| 85 | void *arg;
|
---|
| 86 | };
|
---|
[b4049f9] | 87 |
|
---|
[e87ab6d] | 88 | void async_event_dispatch(int, short, void *);
|
---|
[cd7b81d] | 89 |
|
---|
[e87ab6d] | 90 | struct async_event *
|
---|
| 91 | async_run_event(struct async * async,
|
---|
| 92 | void (*cb)(int, struct async_res *, void *), void *arg)
|
---|
| 93 | {
|
---|
| 94 | struct async_event *aev;
|
---|
| 95 | struct timeval tv;
|
---|
| 96 |
|
---|
| 97 | aev = calloc(1, sizeof *aev);
|
---|
| 98 | if (aev == NULL)
|
---|
| 99 | return (NULL);
|
---|
| 100 | aev->async = async;
|
---|
| 101 | aev->callback = cb;
|
---|
| 102 | aev->arg = arg;
|
---|
| 103 | tv.tv_sec = 0;
|
---|
| 104 | tv.tv_usec = 0;
|
---|
| 105 | evtimer_set(&aev->ev, async_event_dispatch, aev);
|
---|
| 106 | evtimer_add(&aev->ev, &tv);
|
---|
| 107 | return (aev);
|
---|
[b4049f9] | 108 | }
|
---|
| 109 |
|
---|
[460786f] | 110 | void
|
---|
[e87ab6d] | 111 | async_event_dispatch(int fd __attribute__((__unused__)),
|
---|
| 112 | short ev __attribute__((__unused__)), void *arg)
|
---|
[b4049f9] | 113 | {
|
---|
[e87ab6d] | 114 | struct async_event *aev = arg;
|
---|
| 115 | struct async_res ar;
|
---|
| 116 | int r;
|
---|
| 117 | struct timeval tv;
|
---|
| 118 |
|
---|
| 119 | while ((r = asr_async_run(aev->async, &ar)) == ASYNC_YIELD)
|
---|
| 120 | aev->callback(r, &ar, aev->arg);
|
---|
| 121 |
|
---|
| 122 | event_del(&aev->ev);
|
---|
| 123 | if (r == ASYNC_COND) {
|
---|
| 124 | event_set(&aev->ev, ar.ar_fd,
|
---|
| 125 | ar.ar_cond == ASYNC_READ ? EV_READ : EV_WRITE,
|
---|
| 126 | async_event_dispatch, aev);
|
---|
| 127 | tv.tv_sec = ar.ar_timeout / 1000;
|
---|
| 128 | tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
|
---|
| 129 | event_add(&aev->ev, &tv);
|
---|
| 130 | } else { /* ASYNC_DONE */
|
---|
| 131 | aev->callback(r, &ar, aev->arg);
|
---|
| 132 | free(aev);
|
---|
[cd7b81d] | 133 | }
|
---|
| 134 | }
|
---|