source: code/dns.c@ dcbd425

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

Convert DNS code to use ASR

  • Property mode set to 100644
File size: 3.6 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 <errno.h>
27#include <event.h>
28#include <resolv.h>
29#include <stdlib.h>
30#include <string.h>
31#include <syslog.h>
32
33#include "asr.h"
34
35#include "icb.h"
36#include "icbd.h"
37
38struct async_event;
39struct async_event *
40 async_run_event(struct async *,
41 void (*)(int, struct async_res *, void *), void *);
42void dns_done(int, struct async_res *, void *);
43
44extern int dodns;
45
46void
47dns_done(int ev __attribute__((__unused__)), struct async_res *ar, void *arg)
48{
49 struct icb_session *is = arg;
50
51 if (ar->ar_gai_errno == 0) {
52 syslog(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 syslog(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 async *as;
68
69 if (!dodns)
70 return;
71
72 if (verbose)
73 syslog(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 async *async;
84 struct event ev;
85 void (*callback)(int, struct async_res *, void *);
86 void *arg;
87};
88
89void async_event_dispatch(int, short, void *);
90
91struct async_event *
92async_run_event(struct async * async,
93 void (*cb)(int, struct async_res *, 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 async_res ar;
117 int r;
118 struct timeval tv;
119
120 while ((r = asr_async_run(aev->async, &ar)) == ASYNC_YIELD)
121 aev->callback(r, &ar, aev->arg);
122
123 event_del(&aev->ev);
124 if (r == ASYNC_COND) {
125 event_set(&aev->ev, ar.ar_fd,
126 ar.ar_cond == ASYNC_READ ? EV_READ : EV_WRITE,
127 async_event_dispatch, aev);
128 tv.tv_sec = ar.ar_timeout / 1000;
129 tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
130 event_add(&aev->ev, &tv);
131 } else { /* ASYNC_DONE */
132 aev->callback(r, &ar, aev->arg);
133 free(aev);
134 }
135}
Note: See TracBrowser for help on using the repository browser.