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