[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>
|
---|
[ee0e95f] | 26 | #include <netdb.h>
|
---|
[cd7b81d] | 27 | #include <errno.h>
|
---|
[e87ab6d] | 28 | #include <event.h>
|
---|
| 29 | #include <resolv.h>
|
---|
[cd7b81d] | 30 | #include <stdlib.h>
|
---|
[b4049f9] | 31 | #include <string.h>
|
---|
[cd7b81d] | 32 | #include <syslog.h>
|
---|
[e87ab6d] | 33 |
|
---|
[ee0e95f] | 34 | #include <asr.h>
|
---|
[cd7b81d] | 35 |
|
---|
| 36 | #include "icb.h"
|
---|
| 37 | #include "icbd.h"
|
---|
| 38 |
|
---|
[e87ab6d] | 39 | struct async_event;
|
---|
[ee0e95f] | 40 | struct async_event *async_run_event(struct asr_query *,
|
---|
| 41 | void (*)(struct asr_result *, void *), void *);
|
---|
| 42 | void dns_done(struct asr_result *, void *);
|
---|
[460786f] | 43 |
|
---|
| 44 | extern int dodns;
|
---|
| 45 |
|
---|
[e87ab6d] | 46 | void
|
---|
[ee0e95f] | 47 | dns_done(struct asr_result *ar, void *arg)
|
---|
[cd7b81d] | 48 | {
|
---|
[e87ab6d] | 49 | struct icb_session *is = arg;
|
---|
| 50 |
|
---|
| 51 | if (ar->ar_gai_errno == 0) {
|
---|
[e80f9fc] | 52 | icbd_log(is, LOG_DEBUG, "dns resolved %s to %s", is->host,
|
---|
[e87ab6d] | 53 | is->hostname);
|
---|
| 54 | if (strncmp(is->hostname, "localhost",
|
---|
| 55 | sizeof "localhost" - 1) == 0)
|
---|
| 56 | strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
|
---|
| 57 | else if (strlen(is->hostname) < ICB_MAXHOSTLEN)
|
---|
| 58 | strlcpy(is->host, is->hostname, ICB_MAXHOSTLEN);
|
---|
| 59 | } else
|
---|
[e80f9fc] | 60 | icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
|
---|
[e87ab6d] | 61 | gai_strerror(ar->ar_gai_errno));
|
---|
[cd7b81d] | 62 | }
|
---|
| 63 |
|
---|
| 64 | void
|
---|
[e87ab6d] | 65 | dns_rresolv(struct icb_session *is, struct sockaddr *sa)
|
---|
[cd7b81d] | 66 | {
|
---|
[ee0e95f] | 67 | struct asr_query *as;
|
---|
[cd7b81d] | 68 |
|
---|
[e87ab6d] | 69 | if (!dodns)
|
---|
[cd7b81d] | 70 | return;
|
---|
| 71 |
|
---|
| 72 | if (verbose)
|
---|
[e80f9fc] | 73 | icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
|
---|
[cd7b81d] | 74 |
|
---|
[e87ab6d] | 75 | as = getnameinfo_async(sa, sa->sa_len, is->hostname,
|
---|
| 76 | sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
|
---|
| 77 | async_run_event(as, dns_done, is);
|
---|
[cd7b81d] | 78 | }
|
---|
| 79 |
|
---|
[e87ab6d] | 80 | /* Generic libevent glue for asr */
|
---|
[b4049f9] | 81 |
|
---|
[e87ab6d] | 82 | struct async_event {
|
---|
[ee0e95f] | 83 | struct asr_query *async;
|
---|
[e87ab6d] | 84 | struct event ev;
|
---|
[ee0e95f] | 85 | void (*callback)(struct asr_result *, void *);
|
---|
[e87ab6d] | 86 | void *arg;
|
---|
| 87 | };
|
---|
[b4049f9] | 88 |
|
---|
[e87ab6d] | 89 | void async_event_dispatch(int, short, void *);
|
---|
[cd7b81d] | 90 |
|
---|
[e87ab6d] | 91 | struct async_event *
|
---|
[ee0e95f] | 92 | async_run_event(struct asr_query *async,
|
---|
| 93 | void (*cb)(struct asr_result *, void *), void *arg)
|
---|
[e87ab6d] | 94 | {
|
---|
| 95 | struct async_event *aev;
|
---|
| 96 | struct timeval tv;
|
---|
| 97 |
|
---|
| 98 | aev = calloc(1, sizeof *aev);
|
---|
| 99 | if (aev == NULL)
|
---|
| 100 | return (NULL);
|
---|
| 101 | aev->async = async;
|
---|
| 102 | aev->callback = cb;
|
---|
| 103 | aev->arg = arg;
|
---|
| 104 | tv.tv_sec = 0;
|
---|
| 105 | tv.tv_usec = 0;
|
---|
| 106 | evtimer_set(&aev->ev, async_event_dispatch, aev);
|
---|
| 107 | evtimer_add(&aev->ev, &tv);
|
---|
| 108 | return (aev);
|
---|
[b4049f9] | 109 | }
|
---|
| 110 |
|
---|
[460786f] | 111 | void
|
---|
[e87ab6d] | 112 | async_event_dispatch(int fd __attribute__((__unused__)),
|
---|
| 113 | short ev __attribute__((__unused__)), void *arg)
|
---|
[b4049f9] | 114 | {
|
---|
[e87ab6d] | 115 | struct async_event *aev = arg;
|
---|
[ee0e95f] | 116 | struct asr_result ar;
|
---|
[e87ab6d] | 117 | struct timeval tv;
|
---|
| 118 |
|
---|
| 119 | event_del(&aev->ev);
|
---|
[ee0e95f] | 120 |
|
---|
| 121 | if (asr_run(aev->async, &ar) == 0) {
|
---|
[e87ab6d] | 122 | event_set(&aev->ev, ar.ar_fd,
|
---|
[ee0e95f] | 123 | ar.ar_cond == ASR_WANT_READ ? EV_READ : EV_WRITE,
|
---|
| 124 | async_event_dispatch, aev);
|
---|
[e87ab6d] | 125 | tv.tv_sec = ar.ar_timeout / 1000;
|
---|
| 126 | tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
|
---|
| 127 | event_add(&aev->ev, &tv);
|
---|
[ee0e95f] | 128 | } else { /* done */
|
---|
| 129 | aev->callback(&ar, aev->arg);
|
---|
[e87ab6d] | 130 | free(aev);
|
---|
[cd7b81d] | 131 | }
|
---|
| 132 | }
|
---|