OpenDNSSEC-signer 2.1.12
xfrhandler.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
32#include "config.h"
33#include "daemon/engine.h"
34#include "daemon/xfrhandler.h"
35#include "duration.h"
36#include "status.h"
37
38#include <errno.h>
39#include <string.h>
40
41static const char* xfrh_str = "xfrhandler";
42
43static void xfrhandler_handle_dns(netio_type* netio,
44 netio_handler_type* handler, netio_events_type event_types);
45
46
53{
54 xfrhandler_type* xfrh = NULL;
55 CHECKALLOC(xfrh = (xfrhandler_type*) malloc(sizeof(xfrhandler_type)));
56 xfrh->engine = NULL;
57 xfrh->packet = NULL;
58 xfrh->netio = NULL;
59 xfrh->tcp_set = NULL;
60 xfrh->tcp_waiting_first = NULL;
61 xfrh->udp_waiting_first = NULL;
62 xfrh->udp_waiting_last = NULL;
63 xfrh->udp_use_num = 0;
64 xfrh->start_time = 0;
65 xfrh->current_time = 0;
66 xfrh->got_time = 0;
67 xfrh->need_to_exit = 0;
68 xfrh->started = 0;
69 /* notify */
70 xfrh->notify_waiting_first = NULL;
71 xfrh->notify_waiting_last = NULL;
72 xfrh->notify_udp_num = 0;
73 /* setup */
74 xfrh->netio = netio_create();
75 if (!xfrh->netio) {
76 ods_log_error("[%s] unable to create xfrhandler: "
77 "netio_create() failed", xfrh_str);
79 return NULL;
80 }
82 if (!xfrh->packet) {
83 ods_log_error("[%s] unable to create xfrhandler: "
84 "buffer_create() failed", xfrh_str);
86 return NULL;
87 }
88 xfrh->tcp_set = tcp_set_create();
89 if (!xfrh->tcp_set) {
90 ods_log_error("[%s] unable to create xfrhandler: "
91 "tcp_set_create() failed", xfrh_str);
93 return NULL;
94 }
95 xfrh->dnshandler.fd = -1;
96 xfrh->dnshandler.user_data = (void*) xfrh;
97 xfrh->dnshandler.timeout = 0;
99 xfrh->dnshandler.event_handler = xfrhandler_handle_dns;
100 xfrh->dnshandler.free_handler = 0;
101 return xfrh;
102}
103
104
109void
111{
112 ods_log_assert(xfrhandler);
113 ods_log_assert(xfrhandler->engine);
114 ods_log_debug("[%s] start", xfrh_str);
115 /* setup */
116 xfrhandler->start_time = time_now();
117 /* handlers */
118 netio_add_handler(xfrhandler->netio, &xfrhandler->dnshandler);
119 /* service */
120 while (xfrhandler->need_to_exit == 0) {
121 /* dispatch may block for a longer period, so current is gone */
122 xfrhandler->got_time = 0;
123 ods_log_deeebug("[%s] netio dispatch", xfrh_str);
124 if (netio_dispatch(xfrhandler->netio, NULL, NULL) == -1) {
125 if (errno != EINTR) {
126 ods_log_error("[%s] unable to dispatch netio: %s", xfrh_str,
127 strerror(errno));
128 }
129 }
130 }
131 /* shutdown */
132 ods_log_debug("[%s] shutdown", xfrh_str);
133}
134
135
140time_t
142{
143 if (!xfrhandler) {
144 return 0;
145 }
146 if (!xfrhandler->got_time) {
147 xfrhandler->current_time = time_now();
148 xfrhandler->got_time = 1;
149 }
150 return xfrhandler->current_time;
151}
152
153
158void
160{
161 if (xfrhandler && xfrhandler->started) {
162 janitor_thread_signal(xfrhandler->thread_id);
163 }
164}
165
166
171static void
172xfrhandler_handle_dns(netio_type* ATTR_UNUSED(netio),
173 netio_handler_type* handler, netio_events_type event_types)
174{
175 xfrhandler_type* xfrhandler = NULL;
176 uint8_t buf[MAX_PACKET_SIZE];
177 ssize_t received = 0;
178 if (!handler) {
179 return;
180 }
181 xfrhandler = (xfrhandler_type*) handler->user_data;
182 ods_log_assert(event_types & NETIO_EVENT_READ);
183 received = read(xfrhandler->dnshandler.fd, &buf, MAX_PACKET_SIZE);
184 ods_log_debug("[%s] read forwarded dns packet: %d bytes received",
185 xfrh_str, (int) received);
186 if (received == -1) {
187 ods_log_error("[%s] unable to forward dns packet: %s", xfrh_str,
188 strerror(errno));
189 }
190}
191
192
197void
199{
200 if (!xfrhandler) {
201 return;
202 }
203 netio_cleanup_shallow(xfrhandler->netio);
204 buffer_cleanup(xfrhandler->packet);
205 tcp_set_cleanup(xfrhandler->tcp_set);
206 free(xfrhandler);
207}
void buffer_cleanup(buffer_type *buffer)
Definition: buffer.c:1145
buffer_type * buffer_create(size_t capacity)
Definition: buffer.c:78
#define MAX_PACKET_SIZE
Definition: buffer.h:49
#define PACKET_BUFFER_SIZE
Definition: buffer.h:50
void netio_cleanup_shallow(netio_type *netio)
Definition: netio.c:355
int netio_dispatch(netio_type *netio, const struct timespec *timeout, const sigset_t *sigmask)
Definition: netio.c:187
void netio_add_handler(netio_type *netio, netio_handler_type *handler)
Definition: netio.c:53
netio_type * netio_create()
Definition: netio.c:39
enum netio_events_enum netio_events_type
Definition: netio.h:76
@ NETIO_EVENT_READ
Definition: netio.h:71
struct timespec * timeout
Definition: netio.h:115
netio_events_type event_types
Definition: netio.h:124
netio_event_handler_type event_handler
Definition: netio.h:131
void * user_data
Definition: netio.h:119
buffer_type * packet
Definition: xfrhandler.h:62
unsigned got_time
Definition: xfrhandler.h:71
netio_type * netio
Definition: xfrhandler.h:60
tcp_set_type * tcp_set
Definition: xfrhandler.h:61
time_t current_time
Definition: xfrhandler.h:58
unsigned started
Definition: xfrhandler.h:73
janitor_thread_t thread_id
Definition: xfrhandler.h:54
engine_type * engine
Definition: xfrhandler.h:55
unsigned need_to_exit
Definition: xfrhandler.h:72
xfrd_type * udp_waiting_first
Definition: xfrhandler.h:64
notify_type * notify_waiting_last
Definition: xfrhandler.h:68
notify_type * notify_waiting_first
Definition: xfrhandler.h:67
netio_handler_type dnshandler
Definition: xfrhandler.h:70
size_t udp_use_num
Definition: xfrhandler.h:66
xfrd_type * udp_waiting_last
Definition: xfrhandler.h:65
xfrd_type * tcp_waiting_first
Definition: xfrhandler.h:63
void tcp_set_cleanup(tcp_set_type *set)
Definition: tcpset.c:242
tcp_set_type * tcp_set_create()
Definition: tcpset.c:67
void xfrhandler_cleanup(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:198
void xfrhandler_start(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:110
xfrhandler_type * xfrhandler_create()
Definition: xfrhandler.c:52
void xfrhandler_signal(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:159
time_t xfrhandler_time(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:141