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 |
|
---|
35 | void dns_done(struct asr_result *, void *);
|
---|
36 |
|
---|
37 | extern int dodns;
|
---|
38 |
|
---|
39 | void
|
---|
40 | dns_done(struct asr_result *ar, void *arg)
|
---|
41 | {
|
---|
42 | struct icb_session *is = arg;
|
---|
43 |
|
---|
44 | if (ar->ar_gai_errno == 0) {
|
---|
45 | icbd_log(is, LOG_DEBUG, "dns resolved %s to %s", is->host,
|
---|
46 | is->hostname);
|
---|
47 | if (strncmp(is->hostname, "localhost",
|
---|
48 | sizeof "localhost" - 1) == 0)
|
---|
49 | strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
|
---|
50 | else if (strlen(is->hostname) < ICB_MAXHOSTLEN)
|
---|
51 | strlcpy(is->host, is->hostname, ICB_MAXHOSTLEN);
|
---|
52 | } else
|
---|
53 | icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
|
---|
54 | gai_strerror(ar->ar_gai_errno));
|
---|
55 | }
|
---|
56 |
|
---|
57 | void
|
---|
58 | dns_rresolv(struct icb_session *is, struct sockaddr *sa)
|
---|
59 | {
|
---|
60 | struct asr_query *as;
|
---|
61 |
|
---|
62 | if (!dodns)
|
---|
63 | return;
|
---|
64 |
|
---|
65 | if (verbose)
|
---|
66 | icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
|
---|
67 |
|
---|
68 | as = getnameinfo_async(sa, sa->sa_len, is->hostname,
|
---|
69 | sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
|
---|
70 | event_asr_run(as, dns_done, is);
|
---|
71 | }
|
---|