source: code/dns.c@ 9440414

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

check that reverse dns entry can be resolved

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2 * Copyright (c) 2014 Mike Belopuhov
3 * Copyright (c) 2014 Eric Faurot <eric@faurot.net>
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
18#include <sys/types.h>
19#include <sys/socket.h>
20#include <sys/time.h>
21#include <netinet/in.h>
22#include <netdb.h>
23#include <errno.h>
24#include <event.h>
25#include <resolv.h>
26#include <stdlib.h>
27#include <string.h>
28#include <syslog.h>
29
30#include <asr.h>
31
32#include "icb.h"
33#include "icbd.h"
34
35void dns_done_host(struct asr_result *, void *);
36void dns_done_reverse(struct asr_result *, void *);
37
38extern int dodns;
39
40void
41dns_done_host(struct asr_result *ar, void *arg)
42{
43 struct icb_session *is = arg;
44
45 if (ar->ar_addrinfo)
46 freeaddrinfo(ar->ar_addrinfo);
47
48 /* just check that there's no error */
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
56 icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
57 gai_strerror(ar->ar_gai_errno));
58}
59
60void
61dns_done_reverse(struct asr_result *ar, void *arg)
62{
63 struct icb_session *is = arg;
64 struct asr_query *as;
65 struct addrinfo hints;
66
67 if (ar->ar_gai_errno == 0) {
68 icbd_log(is, LOG_DEBUG, "reverse dns resolved %s to %s",
69 is->host, is->hostname);
70 /* try to verify that it resolves back */
71 memset(&hints, 0, sizeof(hints));
72 hints.ai_family = PF_UNSPEC;
73 as = getaddrinfo_async(is->hostname, NULL, &hints, NULL);
74 event_asr_run(as, dns_done_host, is);
75 } else
76 icbd_log(is, LOG_WARNING, "reverse dns resolution failed: %s",
77 gai_strerror(ar->ar_gai_errno));
78}
79
80void
81dns_resolve(struct icb_session *is, struct sockaddr *sa)
82{
83 struct asr_query *as;
84
85 if (!dodns)
86 return;
87
88 if (verbose)
89 icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
90
91 as = getnameinfo_async(sa, sa->sa_len, is->hostname,
92 sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
93 event_asr_run(as, dns_done_reverse, is);
94}
95
Note: See TracBrowser for help on using the repository browser.