OpenDNSSEC-signer  1.4.1
xfrhandler.c
Go to the documentation of this file.
1 /*
2  * $Id: xfrhandler.c 4518 2011-02-24 15:39:09Z matthijs $
3  *
4  * Copyright (c) 2009 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "config.h"
35 #include "daemon/engine.h"
36 #include "daemon/xfrhandler.h"
37 #include "shared/duration.h"
38 #include "shared/status.h"
39 
40 #include <errno.h>
41 #include <string.h>
42 
43 static const char* xfrh_str = "xfrhandler";
44 
45 static void xfrhandler_handle_dns(netio_type* netio,
46  netio_handler_type* handler, netio_events_type event_types);
47 
48 
55 {
56  xfrhandler_type* xfrh = NULL;
57  if (!allocator) {
58  return NULL;
59  }
60  xfrh = (xfrhandler_type*) allocator_alloc(allocator,
61  sizeof(xfrhandler_type));
62  if (!xfrh) {
63  ods_log_error("[%s] unable to create xfrhandler: "
64  "allocator_alloc() failed", xfrh_str);
65  return NULL;
66  }
67  xfrh->allocator = allocator;
68  xfrh->engine = NULL;
69  xfrh->packet = NULL;
70  xfrh->netio = NULL;
71  xfrh->tcp_set = NULL;
72  xfrh->udp_waiting_first = NULL;
73  xfrh->udp_waiting_last = NULL;
74  xfrh->udp_use_num = 0;
75  xfrh->start_time = 0;
76  xfrh->current_time = 0;
77  xfrh->got_time = 0;
78  xfrh->need_to_exit = 0;
79  xfrh->started = 0;
80  /* notify */
81  xfrh->notify_waiting_first = NULL;
82  xfrh->notify_waiting_last = NULL;
83  xfrh->notify_udp_num = 0;
84  /* setup */
85  xfrh->netio = netio_create(allocator);
86  if (!xfrh->netio) {
87  ods_log_error("[%s] unable to create xfrhandler: "
88  "netio_create() failed", xfrh_str);
89  xfrhandler_cleanup(xfrh);
90  return NULL;
91  }
92  xfrh->packet = buffer_create(allocator, PACKET_BUFFER_SIZE);
93  if (!xfrh->packet) {
94  ods_log_error("[%s] unable to create xfrhandler: "
95  "buffer_create() failed", xfrh_str);
96  xfrhandler_cleanup(xfrh);
97  return NULL;
98  }
99  xfrh->tcp_set = tcp_set_create(allocator);
100  if (!xfrh->tcp_set) {
101  ods_log_error("[%s] unable to create xfrhandler: "
102  "tcp_set_create() failed", xfrh_str);
103  xfrhandler_cleanup(xfrh);
104  return NULL;
105  }
106  xfrh->dnshandler.fd = -1;
107  xfrh->dnshandler.user_data = (void*) xfrh;
108  xfrh->dnshandler.timeout = 0;
110  xfrh->dnshandler.event_handler = xfrhandler_handle_dns;
111  return xfrh;
112 }
113 
114 
119 void
121 {
122  ods_log_assert(xfrhandler);
123  ods_log_assert(xfrhandler->engine);
124  ods_log_debug("[%s] start", xfrh_str);
125  /* setup */
126  xfrhandler->start_time = time_now();
127  /* handlers */
128  netio_add_handler(xfrhandler->netio, &xfrhandler->dnshandler);
129  /* service */
130  while (xfrhandler->need_to_exit == 0) {
131  /* dispatch may block for a longer period, so current is gone */
132  xfrhandler->got_time = 0;
133  ods_log_deeebug("[%s] netio dispatch", xfrh_str);
134  if (netio_dispatch(xfrhandler->netio, NULL, NULL) == -1) {
135  if (errno != EINTR) {
136  ods_log_error("[%s] unable to dispatch netio: %s", xfrh_str,
137  strerror(errno));
138  }
139  }
140  }
141  /* shutdown */
142  ods_log_debug("[%s] shutdown", xfrh_str);
143  return;
144 
145 /*
146  xfrd_write_state(xfrd);
147 */
148  /* close tcp sockets */
149  /* close udp sockets */
150 }
151 
152 
157 time_t
159 {
160  if (!xfrhandler) {
161  return 0;
162  }
163  if (!xfrhandler->got_time) {
164  xfrhandler->current_time = time_now();
165  xfrhandler->got_time = 1;
166  }
167  return xfrhandler->current_time;
168 }
169 
170 
175 void
177 {
178  if (xfrhandler && xfrhandler->started) {
179  ods_thread_kill(xfrhandler->thread_id, SIGHUP);
180  }
181  return;
182 }
183 
184 
189 static void
190 xfrhandler_handle_dns(netio_type* ATTR_UNUSED(netio),
191  netio_handler_type* handler, netio_events_type event_types)
192 {
193  xfrhandler_type* xfrhandler = NULL;
194  uint8_t buf[MAX_PACKET_SIZE];
195  ssize_t received = 0;
196  if (!handler) {
197  return;
198  }
199  xfrhandler = (xfrhandler_type*) handler->user_data;
200  ods_log_assert(event_types & NETIO_EVENT_READ);
201  ods_log_debug("[%s] read forwarded dns packet", xfrh_str);
202  received = read(xfrhandler->dnshandler.fd, &buf, MAX_PACKET_SIZE);
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:76
xfrd_type * udp_waiting_first
Definition: xfrhandler.h:63
xfrd_type * udp_waiting_last
Definition: xfrhandler.h:64
void ods_log_debug(const char *format,...)
Definition: log.c:272
time_t current_time
Definition: xfrhandler.h:58
void tcp_set_cleanup(tcp_set_type *set, allocator_type *allocator)
Definition: tcpset.c:253
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:68
void xfrhandler_cleanup(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:216
buffer_type * packet
Definition: xfrhandler.h:62
netio_type * netio
Definition: xfrhandler.h:60
unsigned need_to_exit
Definition: xfrhandler.h:71
enum netio_events_enum netio_events_type
Definition: netio.h:78
void ods_log_error(const char *format,...)
Definition: log.c:336
netio_handler_type dnshandler
Definition: xfrhandler.h:69
void * user_data
Definition: netio.h:121
buffer_type * buffer_create(allocator_type *allocator, size_t capacity)
Definition: buffer.c:64
void netio_add_handler(netio_type *netio, netio_handler_type *handler)
Definition: netio.c:60
unsigned got_time
Definition: xfrhandler.h:70
time_t xfrhandler_time(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:158
size_t udp_use_num
Definition: xfrhandler.h:65
netio_type * netio_create(allocator_type *allocator)
Definition: netio.c:41
void xfrhandler_start(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:120
notify_type * notify_waiting_first
Definition: xfrhandler.h:66
netio_event_handler_type event_handler
Definition: netio.h:133
tcp_set_type * tcp_set
Definition: xfrhandler.h:61
notify_type * notify_waiting_last
Definition: xfrhandler.h:67
void xfrhandler_signal(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:176
#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:54
allocator_type * allocator
Definition: xfrhandler.h:52
netio_events_type event_types
Definition: netio.h:126
void ods_log_deeebug(const char *format,...)
Definition: log.c:256
unsigned started
Definition: xfrhandler.h:72
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:137
void buffer_cleanup(buffer_type *buffer, allocator_type *allocator)
Definition: buffer.c:1231
struct timespec * timeout
Definition: netio.h:117
#define ods_log_assert(x)
Definition: log.h:156
ods_thread_type thread_id
Definition: xfrhandler.h:54
time_t time_now(void)
Definition: duration.c:507
int netio_dispatch(netio_type *netio, const struct timespec *timeout, const sigset_t *sigmask)
Definition: netio.c:205
void netio_cleanup(netio_type *netio)
Definition: netio.c:354