OpenDNSSEC-signer  1.4.9
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 "shared/duration.h"
36 #include "shared/status.h"
37 
38 #include <errno.h>
39 #include <string.h>
40 
41 static const char* xfrh_str = "xfrhandler";
42 
43 static 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  if (!allocator) {
56  return NULL;
57  }
58  xfrh = (xfrhandler_type*) allocator_alloc(allocator,
59  sizeof(xfrhandler_type));
60  if (!xfrh) {
61  ods_log_error("[%s] unable to create xfrhandler: "
62  "allocator_alloc() failed", xfrh_str);
63  return NULL;
64  }
65  xfrh->allocator = allocator;
66  xfrh->engine = NULL;
67  xfrh->packet = NULL;
68  xfrh->netio = NULL;
69  xfrh->tcp_set = NULL;
70  xfrh->tcp_waiting_first = NULL;
71  xfrh->udp_waiting_first = NULL;
72  xfrh->udp_waiting_last = NULL;
73  xfrh->udp_use_num = 0;
74  xfrh->start_time = 0;
75  xfrh->current_time = 0;
76  xfrh->got_time = 0;
77  xfrh->need_to_exit = 0;
78  xfrh->started = 0;
79  /* notify */
80  xfrh->notify_waiting_first = NULL;
81  xfrh->notify_waiting_last = NULL;
82  xfrh->notify_udp_num = 0;
83  /* setup */
84  xfrh->netio = netio_create(allocator);
85  if (!xfrh->netio) {
86  ods_log_error("[%s] unable to create xfrhandler: "
87  "netio_create() failed", xfrh_str);
88  xfrhandler_cleanup(xfrh);
89  return NULL;
90  }
91  xfrh->packet = buffer_create(allocator, PACKET_BUFFER_SIZE);
92  if (!xfrh->packet) {
93  ods_log_error("[%s] unable to create xfrhandler: "
94  "buffer_create() failed", xfrh_str);
95  xfrhandler_cleanup(xfrh);
96  return NULL;
97  }
98  xfrh->tcp_set = tcp_set_create(allocator);
99  if (!xfrh->tcp_set) {
100  ods_log_error("[%s] unable to create xfrhandler: "
101  "tcp_set_create() failed", xfrh_str);
102  xfrhandler_cleanup(xfrh);
103  return NULL;
104  }
105  xfrh->dnshandler.fd = -1;
106  xfrh->dnshandler.user_data = (void*) xfrh;
107  xfrh->dnshandler.timeout = 0;
109  xfrh->dnshandler.event_handler = xfrhandler_handle_dns;
110  return xfrh;
111 }
112 
113 
118 void
120 {
121  ods_log_assert(xfrhandler);
122  ods_log_assert(xfrhandler->engine);
123  ods_log_debug("[%s] start", xfrh_str);
124  /* setup */
125  xfrhandler->start_time = time_now();
126  /* handlers */
127  netio_add_handler(xfrhandler->netio, &xfrhandler->dnshandler);
128  /* service */
129  while (xfrhandler->need_to_exit == 0) {
130  /* dispatch may block for a longer period, so current is gone */
131  xfrhandler->got_time = 0;
132  ods_log_deeebug("[%s] netio dispatch", xfrh_str);
133  if (netio_dispatch(xfrhandler->netio, NULL, NULL) == -1) {
134  if (errno != EINTR) {
135  ods_log_error("[%s] unable to dispatch netio: %s", xfrh_str,
136  strerror(errno));
137  }
138  }
139  }
140  /* shutdown */
141  ods_log_debug("[%s] shutdown", xfrh_str);
142  return;
143 
144 /*
145  xfrd_write_state(xfrd);
146 */
147  /* close tcp sockets */
148  /* close udp sockets */
149 }
150 
151 
156 time_t
158 {
159  if (!xfrhandler) {
160  return 0;
161  }
162  if (!xfrhandler->got_time) {
163  xfrhandler->current_time = time_now();
164  xfrhandler->got_time = 1;
165  }
166  return xfrhandler->current_time;
167 }
168 
169 
174 void
176 {
177  if (xfrhandler && xfrhandler->started) {
178  ods_thread_kill(xfrhandler->thread_id, SIGHUP);
179  }
180  return;
181 }
182 
183 
188 static void
189 xfrhandler_handle_dns(netio_type* ATTR_UNUSED(netio),
190  netio_handler_type* handler, netio_events_type event_types)
191 {
192  xfrhandler_type* xfrhandler = NULL;
193  uint8_t buf[MAX_PACKET_SIZE];
194  ssize_t received = 0;
195  if (!handler) {
196  return;
197  }
198  xfrhandler = (xfrhandler_type*) handler->user_data;
199  ods_log_assert(event_types & NETIO_EVENT_READ);
200  received = read(xfrhandler->dnshandler.fd, &buf, MAX_PACKET_SIZE);
201  ods_log_debug("[%s] read forwarded dns packet: %d bytes received",
202  xfrh_str, (int) received);
203  if (received == -1) {
204  ods_log_error("[%s] unable to forward dns packet: %s", xfrh_str,
205  strerror(errno));
206  }
207  return;
208 }
209 
210 
215 void
217 {
218  allocator_type* allocator = NULL;
219  if (!xfrhandler) {
220  return;
221  }
222  allocator = xfrhandler->allocator;
223  netio_cleanup(xfrhandler->netio);
224  buffer_cleanup(xfrhandler->packet, allocator);
225  tcp_set_cleanup(xfrhandler->tcp_set, allocator);
226  allocator_deallocate(allocator, (void*) xfrhandler);
227  return;
228 }
tcp_set_type * tcp_set_create(allocator_type *allocator)
Definition: tcpset.c:74
xfrd_type * udp_waiting_first
Definition: xfrhandler.h:62
xfrd_type * udp_waiting_last
Definition: xfrhandler.h:63
void ods_log_debug(const char *format,...)
Definition: log.c:270
time_t current_time
Definition: xfrhandler.h:56
xfrd_type * tcp_waiting_first
Definition: xfrhandler.h:61
void tcp_set_cleanup(tcp_set_type *set, allocator_type *allocator)
Definition: tcpset.c:251
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:66
void xfrhandler_cleanup(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:216
buffer_type * packet
Definition: xfrhandler.h:60
netio_type * netio
Definition: xfrhandler.h:58
unsigned need_to_exit
Definition: xfrhandler.h:70
enum netio_events_enum netio_events_type
Definition: netio.h:76
void ods_log_error(const char *format,...)
Definition: log.c:334
netio_handler_type dnshandler
Definition: xfrhandler.h:68
void * user_data
Definition: netio.h:119
buffer_type * buffer_create(allocator_type *allocator, size_t capacity)
Definition: buffer.c:78
void netio_add_handler(netio_type *netio, netio_handler_type *handler)
Definition: netio.c:58
unsigned got_time
Definition: xfrhandler.h:69
time_t xfrhandler_time(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:157
size_t udp_use_num
Definition: xfrhandler.h:64
netio_type * netio_create(allocator_type *allocator)
Definition: netio.c:39
void xfrhandler_start(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:119
notify_type * notify_waiting_first
Definition: xfrhandler.h:65
netio_event_handler_type event_handler
Definition: netio.h:131
tcp_set_type * tcp_set
Definition: xfrhandler.h:59
notify_type * notify_waiting_last
Definition: xfrhandler.h:66
void xfrhandler_signal(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:175
#define MAX_PACKET_SIZE
Definition: buffer.h:49
#define PACKET_BUFFER_SIZE
Definition: buffer.h:50
xfrhandler_type * xfrhandler_create(allocator_type *allocator)
Definition: xfrhandler.c:52
allocator_type * allocator
Definition: xfrhandler.h:50
netio_events_type event_types
Definition: netio.h:124
void ods_log_deeebug(const char *format,...)
Definition: log.c:254
unsigned started
Definition: xfrhandler.h:71
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:135
void buffer_cleanup(buffer_type *buffer, allocator_type *allocator)
Definition: buffer.c:1261
struct timespec * timeout
Definition: netio.h:115
#define ods_log_assert(x)
Definition: log.h:154
ods_thread_type thread_id
Definition: xfrhandler.h:52
time_t time_now(void)
Definition: duration.c:513
int netio_dispatch(netio_type *netio, const struct timespec *timeout, const sigset_t *sigmask)
Definition: netio.c:203
void netio_cleanup(netio_type *netio)
Definition: netio.c:352