[cd7b81d] | 1 | /*
|
---|
[626f420] | 2 | * Copyright (c) 2014 Mike Belopuhov
|
---|
[dab4135] | 3 | * Copyright (c) 2014 Eric Faurot <eric@faurot.net>
|
---|
[cd7b81d] | 4 | *
|
---|
| 5 | * Permission to use, copy, modify, and distribute this software for any
|
---|
| 6 | * purpose with or without fee is hereby granted, provided that the above
|
---|
| 7 | * copyright notice and this permission notice appear in all copies.
|
---|
| 8 | *
|
---|
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
---|
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
---|
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
---|
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
---|
| 13 | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
|
---|
| 14 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
---|
| 15 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
[e87ab6d] | 18 | #include <sys/types.h>
|
---|
[cd7b81d] | 19 | #include <sys/socket.h>
|
---|
| 20 | #include <sys/time.h>
|
---|
| 21 | #include <netinet/in.h>
|
---|
[ee0e95f] | 22 | #include <netdb.h>
|
---|
[cd7b81d] | 23 | #include <errno.h>
|
---|
[e87ab6d] | 24 | #include <event.h>
|
---|
| 25 | #include <resolv.h>
|
---|
[cd7b81d] | 26 | #include <stdlib.h>
|
---|
[b4049f9] | 27 | #include <string.h>
|
---|
[cd7b81d] | 28 | #include <syslog.h>
|
---|
[e87ab6d] | 29 |
|
---|
[ee0e95f] | 30 | #include <asr.h>
|
---|
[cd7b81d] | 31 |
|
---|
| 32 | #include "icb.h"
|
---|
| 33 | #include "icbd.h"
|
---|
| 34 |
|
---|
[9440414] | 35 | void dns_done_host(struct asr_result *, void *);
|
---|
| 36 | void dns_done_reverse(struct asr_result *, void *);
|
---|
[460786f] | 37 |
|
---|
| 38 | extern int dodns;
|
---|
| 39 |
|
---|
[e87ab6d] | 40 | void
|
---|
[9440414] | 41 | dns_done_host(struct asr_result *ar, void *arg)
|
---|
[cd7b81d] | 42 | {
|
---|
[e87ab6d] | 43 | struct icb_session *is = arg;
|
---|
| 44 |
|
---|
[9440414] | 45 | if (ar->ar_addrinfo)
|
---|
| 46 | freeaddrinfo(ar->ar_addrinfo);
|
---|
| 47 |
|
---|
| 48 | /* just check that there's no error */
|
---|
[e87ab6d] | 49 | if (ar->ar_gai_errno == 0) {
|
---|
| 50 | if (strncmp(is->hostname, "localhost",
|
---|
| 51 | sizeof "localhost" - 1) == 0)
|
---|
| 52 | strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
|
---|
| 53 | else if (strlen(is->hostname) < ICB_MAXHOSTLEN)
|
---|
| 54 | strlcpy(is->host, is->hostname, ICB_MAXHOSTLEN);
|
---|
| 55 | } else
|
---|
[e80f9fc] | 56 | icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
|
---|
[e87ab6d] | 57 | gai_strerror(ar->ar_gai_errno));
|
---|
[120eedd] | 58 |
|
---|
| 59 | if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
|
---|
| 60 | free(is);
|
---|
| 61 | return;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | CLRF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
[cd7b81d] | 65 | }
|
---|
| 66 |
|
---|
| 67 | void
|
---|
[9440414] | 68 | dns_done_reverse(struct asr_result *ar, void *arg)
|
---|
| 69 | {
|
---|
| 70 | struct icb_session *is = arg;
|
---|
| 71 | struct asr_query *as;
|
---|
| 72 | struct addrinfo hints;
|
---|
| 73 |
|
---|
[120eedd] | 74 | if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
|
---|
| 75 | free(is);
|
---|
| 76 | return;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[9440414] | 79 | if (ar->ar_gai_errno == 0) {
|
---|
| 80 | icbd_log(is, LOG_DEBUG, "reverse dns resolved %s to %s",
|
---|
| 81 | is->host, is->hostname);
|
---|
| 82 | /* try to verify that it resolves back */
|
---|
| 83 | memset(&hints, 0, sizeof(hints));
|
---|
| 84 | hints.ai_family = PF_UNSPEC;
|
---|
| 85 | as = getaddrinfo_async(is->hostname, NULL, &hints, NULL);
|
---|
| 86 | event_asr_run(as, dns_done_host, is);
|
---|
[120eedd] | 87 | } else {
|
---|
[9440414] | 88 | icbd_log(is, LOG_WARNING, "reverse dns resolution failed: %s",
|
---|
| 89 | gai_strerror(ar->ar_gai_errno));
|
---|
[120eedd] | 90 | CLRF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
| 91 | }
|
---|
[9440414] | 92 | }
|
---|
| 93 |
|
---|
| 94 | void
|
---|
| 95 | dns_resolve(struct icb_session *is, struct sockaddr *sa)
|
---|
[cd7b81d] | 96 | {
|
---|
[ee0e95f] | 97 | struct asr_query *as;
|
---|
[cd7b81d] | 98 |
|
---|
[e87ab6d] | 99 | if (!dodns)
|
---|
[cd7b81d] | 100 | return;
|
---|
| 101 |
|
---|
[120eedd] | 102 | SETF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
| 103 |
|
---|
[cd7b81d] | 104 | if (verbose)
|
---|
[e80f9fc] | 105 | icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
|
---|
[cd7b81d] | 106 |
|
---|
[e87ab6d] | 107 | as = getnameinfo_async(sa, sa->sa_len, is->hostname,
|
---|
| 108 | sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
|
---|
[9440414] | 109 | event_asr_run(as, dns_done_reverse, is);
|
---|
[cd7b81d] | 110 | }
|
---|
[9440414] | 111 |
|
---|