source: code/dns.c@ e80f9fc

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

Convert dns_* functions to use icbd_log instead of syslog

  • 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 *async_run_event(struct async *,
40 void (*)(int, struct async_res *, void *), void *);
41void dns_done(int, struct async_res *, void *);
42
43extern int dodns;
44
45void
46dns_done(int ev __attribute__((__unused__)), struct async_res *ar, void *arg)
47{
48 struct icb_session *is = arg;
49
50 if (ar->ar_gai_errno == 0) {
51 icbd_log(is, LOG_DEBUG, "dns resolved %s to %s", is->host,
52 is->hostname);
53 if (strncmp(is->hostname, "localhost",
54 sizeof "localhost" - 1) == 0)
55 strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
56 else if (strlen(is->hostname) < ICB_MAXHOSTLEN)
57 strlcpy(is->host, is->hostname, ICB_MAXHOSTLEN);
58 } else
59 icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
60 gai_strerror(ar->ar_gai_errno));
61}
62
63void
64dns_rresolv(struct icb_session *is, struct sockaddr *sa)
65{
66 struct async *as;
67
68 if (!dodns)
69 return;
70
71 if (verbose)
72 icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
73
74 as = getnameinfo_async(sa, sa->sa_len, is->hostname,
75 sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
76 async_run_event(as, dns_done, is);
77}
78
79/* Generic libevent glue for asr */
80
81struct async_event {
82 struct async *async;
83 struct event ev;
84 void (*callback)(int, struct async_res *, void *);
85 void *arg;
86};
87
88void async_event_dispatch(int, short, void *);
89
90struct async_event *
91async_run_event(struct async * async,
92 void (*cb)(int, struct async_res *, void *), void *arg)
93{
94 struct async_event *aev;
95 struct timeval tv;
96
97 aev = calloc(1, sizeof *aev);
98 if (aev == NULL)
99 return (NULL);
100 aev->async = async;
101 aev->callback = cb;
102 aev->arg = arg;
103 tv.tv_sec = 0;
104 tv.tv_usec = 0;
105 evtimer_set(&aev->ev, async_event_dispatch, aev);
106 evtimer_add(&aev->ev, &tv);
107 return (aev);
108}
109
110void
111async_event_dispatch(int fd __attribute__((__unused__)),
112 short ev __attribute__((__unused__)), void *arg)
113{
114 struct async_event *aev = arg;
115 struct async_res ar;
116 int r;
117 struct timeval tv;
118
119 while ((r = asr_async_run(aev->async, &ar)) == ASYNC_YIELD)
120 aev->callback(r, &ar, aev->arg);
121
122 event_del(&aev->ev);
123 if (r == ASYNC_COND) {
124 event_set(&aev->ev, ar.ar_fd,
125 ar.ar_cond == ASYNC_READ ? EV_READ : EV_WRITE,
126 async_event_dispatch, aev);
127 tv.tv_sec = ar.ar_timeout / 1000;
128 tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
129 event_add(&aev->ev, &tv);
130 } else { /* ASYNC_DONE */
131 aev->callback(r, &ar, aev->arg);
132 free(aev);
133 }
134}
Note: See TracBrowser for help on using the repository browser.