source: code/dns.c@ 4c2a775

Last change on this file since 4c2a775 was 1c6061c, checked in by Mike Belopuhov <mike@…>, 11 years ago

Don't waste time with DNS response if the session is going away.
Downgrade DNS failures to LOG_DEBUG while here.

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[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]35void dns_done_host(struct asr_result *, void *);
36void dns_done_reverse(struct asr_result *, void *);
[e68221b]37int cmp_addr(struct sockaddr *, struct sockaddr *);
[460786f]38
39extern int dodns;
40
[e87ab6d]41void
[9440414]42dns_done_host(struct asr_result *ar, void *arg)
[cd7b81d]43{
[e87ab6d]44 struct icb_session *is = arg;
[e68221b]45 struct addrinfo *res;
46 int found = 0;
[e87ab6d]47
[1c6061c]48 if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
49 if (ar->ar_addrinfo)
50 freeaddrinfo(ar->ar_addrinfo);
51 free(is);
52 return;
53 }
54
[e87ab6d]55 if (ar->ar_gai_errno == 0) {
56 if (strncmp(is->hostname, "localhost",
57 sizeof "localhost" - 1) == 0)
58 strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
[e68221b]59 else if (strlen(is->hostname) < ICB_MAXHOSTLEN) {
60 for (res = ar->ar_addrinfo; res; res = res->ai_next) {
61 if (cmp_addr(res->ai_addr, (struct sockaddr *)
62 &is->ss) == 0) {
63 strlcpy(is->host, is->hostname,
64 ICB_MAXHOSTLEN);
65 found = 1;
66 break;
67 }
68 }
69 if (!found)
70 icbd_log(is, LOG_WARNING, "hostname %s does "
71 "not resolve back to connecting ip %s",
72 is->hostname, is->host);
73 }
[e87ab6d]74 } else
[1c6061c]75 icbd_log(is, LOG_DEBUG, "dns resolution failed: %s",
[e87ab6d]76 gai_strerror(ar->ar_gai_errno));
[120eedd]77
[e68221b]78 if (ar->ar_addrinfo)
79 freeaddrinfo(ar->ar_addrinfo);
80
[120eedd]81 CLRF(is->flags, ICB_SF_DNSINPROGRESS);
[cd7b81d]82}
83
84void
[9440414]85dns_done_reverse(struct asr_result *ar, void *arg)
86{
87 struct icb_session *is = arg;
88 struct asr_query *as;
89 struct addrinfo hints;
90
[120eedd]91 if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
92 free(is);
93 return;
94 }
95
[9440414]96 if (ar->ar_gai_errno == 0) {
97 icbd_log(is, LOG_DEBUG, "reverse dns resolved %s to %s",
98 is->host, is->hostname);
99 /* try to verify that it resolves back */
100 memset(&hints, 0, sizeof(hints));
101 hints.ai_family = PF_UNSPEC;
102 as = getaddrinfo_async(is->hostname, NULL, &hints, NULL);
103 event_asr_run(as, dns_done_host, is);
[120eedd]104 } else {
[1c6061c]105 icbd_log(is, LOG_DEBUG, "reverse dns resolution failed: %s",
[9440414]106 gai_strerror(ar->ar_gai_errno));
[120eedd]107 CLRF(is->flags, ICB_SF_DNSINPROGRESS);
108 }
[9440414]109}
110
[e68221b]111int
112cmp_addr(struct sockaddr *a, struct sockaddr *b)
113{
114 if (a->sa_family != b->sa_family)
115 return (a->sa_family - b->sa_family);
116
117 if (a->sa_family == AF_INET)
118 return (((struct sockaddr_in *)a)->sin_addr.s_addr -
119 ((struct sockaddr_in *)b)->sin_addr.s_addr);
120
121 if (a->sa_family == AF_INET6)
122 return (memcmp(&((struct sockaddr_in6 *)a)->sin6_addr,
123 &((struct sockaddr_in6 *)b)->sin6_addr,
124 sizeof (struct in6_addr)));
125
126 return -1;
127}
128
[9440414]129void
[e68221b]130dns_resolve(struct icb_session *is)
[cd7b81d]131{
[ee0e95f]132 struct asr_query *as;
[cd7b81d]133
[e87ab6d]134 if (!dodns)
[cd7b81d]135 return;
136
[120eedd]137 SETF(is->flags, ICB_SF_DNSINPROGRESS);
138
[cd7b81d]139 if (verbose)
[e80f9fc]140 icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
[cd7b81d]141
[e68221b]142 as = getnameinfo_async((struct sockaddr *)&is->ss,
143 ((struct sockaddr *)&is->ss)->sa_len, is->hostname,
[e87ab6d]144 sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
[9440414]145 event_asr_run(as, dns_done_reverse, is);
[cd7b81d]146}
[9440414]147
Note: See TracBrowser for help on using the repository browser.