source: code/dns.c@ ee0e95f

Last change on this file since ee0e95f was ee0e95f, checked in by Florian Obser <florian@…>, 11 years ago

new and public asr api; from eric@
OK mikeb

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2014 Mike Belopuhov
3 *
4 * ASR implementation and OpenSMTPD integration are copyright
5 * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
6 * Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
7 * Copyright (c) 2011-2012 Eric Faurot <eric@faurot.net>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
18 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <sys/time.h>
25#include <netinet/in.h>
26#include <netdb.h>
27#include <errno.h>
28#include <event.h>
29#include <resolv.h>
30#include <stdlib.h>
31#include <string.h>
32#include <syslog.h>
33
34#include <asr.h>
35
36#include "icb.h"
37#include "icbd.h"
38
39struct async_event;
40struct async_event *async_run_event(struct asr_query *,
41 void (*)(struct asr_result *, void *), void *);
42void dns_done(struct asr_result *, void *);
43
44extern int dodns;
45
46void
47dns_done(struct asr_result *ar, void *arg)
48{
49 struct icb_session *is = arg;
50
51 if (ar->ar_gai_errno == 0) {
52 icbd_log(is, LOG_DEBUG, "dns resolved %s to %s", is->host,
53 is->hostname);
54 if (strncmp(is->hostname, "localhost",
55 sizeof "localhost" - 1) == 0)
56 strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
57 else if (strlen(is->hostname) < ICB_MAXHOSTLEN)
58 strlcpy(is->host, is->hostname, ICB_MAXHOSTLEN);
59 } else
60 icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
61 gai_strerror(ar->ar_gai_errno));
62}
63
64void
65dns_rresolv(struct icb_session *is, struct sockaddr *sa)
66{
67 struct asr_query *as;
68
69 if (!dodns)
70 return;
71
72 if (verbose)
73 icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
74
75 as = getnameinfo_async(sa, sa->sa_len, is->hostname,
76 sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
77 async_run_event(as, dns_done, is);
78}
79
80/* Generic libevent glue for asr */
81
82struct async_event {
83 struct asr_query *async;
84 struct event ev;
85 void (*callback)(struct asr_result *, void *);
86 void *arg;
87};
88
89void async_event_dispatch(int, short, void *);
90
91struct async_event *
92async_run_event(struct asr_query *async,
93 void (*cb)(struct asr_result *, void *), void *arg)
94{
95 struct async_event *aev;
96 struct timeval tv;
97
98 aev = calloc(1, sizeof *aev);
99 if (aev == NULL)
100 return (NULL);
101 aev->async = async;
102 aev->callback = cb;
103 aev->arg = arg;
104 tv.tv_sec = 0;
105 tv.tv_usec = 0;
106 evtimer_set(&aev->ev, async_event_dispatch, aev);
107 evtimer_add(&aev->ev, &tv);
108 return (aev);
109}
110
111void
112async_event_dispatch(int fd __attribute__((__unused__)),
113 short ev __attribute__((__unused__)), void *arg)
114{
115 struct async_event *aev = arg;
116 struct asr_result ar;
117 struct timeval tv;
118
119 event_del(&aev->ev);
120
121 if (asr_run(aev->async, &ar) == 0) {
122 event_set(&aev->ev, ar.ar_fd,
123 ar.ar_cond == ASR_WANT_READ ? EV_READ : EV_WRITE,
124 async_event_dispatch, aev);
125 tv.tv_sec = ar.ar_timeout / 1000;
126 tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
127 event_add(&aev->ev, &tv);
128 } else { /* done */
129 aev->callback(&ar, aev->arg);
130 free(aev);
131 }
132}
Note: See TracBrowser for help on using the repository browser.