[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 |
|
---|
[4a74b5b] | 30 | #ifdef __NetBSD__
|
---|
| 31 | #define NO_ASYNC
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 | #ifdef NO_ASYNC
|
---|
| 35 | struct asr_result {
|
---|
| 36 | struct addrinfo* ar_addrinfo;
|
---|
| 37 | int ar_gai_errno;
|
---|
| 38 | };
|
---|
| 39 | #else
|
---|
[ee0e95f] | 40 | #include <asr.h>
|
---|
[4a74b5b] | 41 | #endif
|
---|
[cd7b81d] | 42 |
|
---|
| 43 | #include "icb.h"
|
---|
| 44 | #include "icbd.h"
|
---|
| 45 |
|
---|
[9440414] | 46 | void dns_done_host(struct asr_result *, void *);
|
---|
| 47 | void dns_done_reverse(struct asr_result *, void *);
|
---|
[e68221b] | 48 | int cmp_addr(struct sockaddr *, struct sockaddr *);
|
---|
[460786f] | 49 |
|
---|
| 50 | extern int dodns;
|
---|
| 51 |
|
---|
[e87ab6d] | 52 | void
|
---|
[9440414] | 53 | dns_done_host(struct asr_result *ar, void *arg)
|
---|
[cd7b81d] | 54 | {
|
---|
[e87ab6d] | 55 | struct icb_session *is = arg;
|
---|
[e68221b] | 56 | struct addrinfo *res;
|
---|
| 57 | int found = 0;
|
---|
[e87ab6d] | 58 |
|
---|
[1c6061c] | 59 | if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
|
---|
| 60 | if (ar->ar_addrinfo)
|
---|
| 61 | freeaddrinfo(ar->ar_addrinfo);
|
---|
| 62 | free(is);
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[e87ab6d] | 66 | if (ar->ar_gai_errno == 0) {
|
---|
| 67 | if (strncmp(is->hostname, "localhost",
|
---|
| 68 | sizeof "localhost" - 1) == 0)
|
---|
| 69 | strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
|
---|
[e68221b] | 70 | else if (strlen(is->hostname) < ICB_MAXHOSTLEN) {
|
---|
| 71 | for (res = ar->ar_addrinfo; res; res = res->ai_next) {
|
---|
| 72 | if (cmp_addr(res->ai_addr, (struct sockaddr *)
|
---|
| 73 | &is->ss) == 0) {
|
---|
| 74 | strlcpy(is->host, is->hostname,
|
---|
| 75 | ICB_MAXHOSTLEN);
|
---|
| 76 | found = 1;
|
---|
| 77 | break;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | if (!found)
|
---|
| 81 | icbd_log(is, LOG_WARNING, "hostname %s does "
|
---|
| 82 | "not resolve back to connecting ip %s",
|
---|
| 83 | is->hostname, is->host);
|
---|
| 84 | }
|
---|
[e87ab6d] | 85 | } else
|
---|
[1c6061c] | 86 | icbd_log(is, LOG_DEBUG, "dns resolution failed: %s",
|
---|
[e87ab6d] | 87 | gai_strerror(ar->ar_gai_errno));
|
---|
[120eedd] | 88 |
|
---|
[e68221b] | 89 | if (ar->ar_addrinfo)
|
---|
| 90 | freeaddrinfo(ar->ar_addrinfo);
|
---|
| 91 |
|
---|
[120eedd] | 92 | CLRF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
[cd7b81d] | 93 | }
|
---|
| 94 |
|
---|
| 95 | void
|
---|
[9440414] | 96 | dns_done_reverse(struct asr_result *ar, void *arg)
|
---|
| 97 | {
|
---|
| 98 | struct icb_session *is = arg;
|
---|
[4a74b5b] | 99 | #ifdef NO_ASYNC
|
---|
| 100 | struct asr_result result;
|
---|
| 101 | #else
|
---|
[9440414] | 102 | struct asr_query *as;
|
---|
[4a74b5b] | 103 | #endif
|
---|
[9440414] | 104 | struct addrinfo hints;
|
---|
| 105 |
|
---|
[120eedd] | 106 | if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
|
---|
| 107 | free(is);
|
---|
| 108 | return;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[9440414] | 111 | if (ar->ar_gai_errno == 0) {
|
---|
| 112 | icbd_log(is, LOG_DEBUG, "reverse dns resolved %s to %s",
|
---|
| 113 | is->host, is->hostname);
|
---|
| 114 | /* try to verify that it resolves back */
|
---|
| 115 | memset(&hints, 0, sizeof(hints));
|
---|
| 116 | hints.ai_family = PF_UNSPEC;
|
---|
[4a74b5b] | 117 | #ifdef NO_ASYNC
|
---|
| 118 | getaddrinfo(is->hostname, NULL, &hints, &result.ar_addrinfo);
|
---|
| 119 | result.ar_gai_errno = errno;
|
---|
| 120 | dns_done_host(&result, is);
|
---|
| 121 | #else
|
---|
[9440414] | 122 | as = getaddrinfo_async(is->hostname, NULL, &hints, NULL);
|
---|
| 123 | event_asr_run(as, dns_done_host, is);
|
---|
[4a74b5b] | 124 | #endif
|
---|
[120eedd] | 125 | } else {
|
---|
[1c6061c] | 126 | icbd_log(is, LOG_DEBUG, "reverse dns resolution failed: %s",
|
---|
[9440414] | 127 | gai_strerror(ar->ar_gai_errno));
|
---|
[120eedd] | 128 | CLRF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
| 129 | }
|
---|
[9440414] | 130 | }
|
---|
| 131 |
|
---|
[e68221b] | 132 | int
|
---|
| 133 | cmp_addr(struct sockaddr *a, struct sockaddr *b)
|
---|
| 134 | {
|
---|
| 135 | if (a->sa_family != b->sa_family)
|
---|
| 136 | return (a->sa_family - b->sa_family);
|
---|
| 137 |
|
---|
| 138 | if (a->sa_family == AF_INET)
|
---|
| 139 | return (((struct sockaddr_in *)a)->sin_addr.s_addr -
|
---|
| 140 | ((struct sockaddr_in *)b)->sin_addr.s_addr);
|
---|
| 141 |
|
---|
| 142 | if (a->sa_family == AF_INET6)
|
---|
| 143 | return (memcmp(&((struct sockaddr_in6 *)a)->sin6_addr,
|
---|
| 144 | &((struct sockaddr_in6 *)b)->sin6_addr,
|
---|
| 145 | sizeof (struct in6_addr)));
|
---|
| 146 |
|
---|
| 147 | return -1;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[9440414] | 150 | void
|
---|
[e68221b] | 151 | dns_resolve(struct icb_session *is)
|
---|
[cd7b81d] | 152 | {
|
---|
[4a74b5b] | 153 | #ifdef NO_ASYNC
|
---|
| 154 | struct asr_result result;
|
---|
| 155 | #else
|
---|
[ee0e95f] | 156 | struct asr_query *as;
|
---|
[4a74b5b] | 157 | #endif
|
---|
[cd7b81d] | 158 |
|
---|
[e87ab6d] | 159 | if (!dodns)
|
---|
[cd7b81d] | 160 | return;
|
---|
| 161 |
|
---|
[120eedd] | 162 | SETF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
| 163 |
|
---|
[cd7b81d] | 164 | if (verbose)
|
---|
[e80f9fc] | 165 | icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
|
---|
[cd7b81d] | 166 |
|
---|
[4a74b5b] | 167 | #ifdef NO_ASYNC
|
---|
| 168 | getnameinfo((struct sockaddr *)&is->ss,
|
---|
| 169 | ((struct sockaddr *)&is->ss)->sa_len, is->hostname,
|
---|
| 170 | sizeof is->hostname, NULL, 0, NI_NOFQDN);
|
---|
| 171 | result.ar_gai_errno = errno;
|
---|
| 172 | dns_done_reverse(&result, is);
|
---|
| 173 | #else
|
---|
[e68221b] | 174 | as = getnameinfo_async((struct sockaddr *)&is->ss,
|
---|
| 175 | ((struct sockaddr *)&is->ss)->sa_len, is->hostname,
|
---|
[e87ab6d] | 176 | sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
|
---|
[9440414] | 177 | event_asr_run(as, dns_done_reverse, is);
|
---|
[4a74b5b] | 178 | #endif
|
---|
[cd7b81d] | 179 | }
|
---|
[9440414] | 180 |
|
---|