OpenDNSSEC-signer  1.4.5
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  ods_log_debug("[%s] read forwarded dns packet", xfrh_str);
200  received = read(xfrhandler->dnshandler.fd, &buf, MAX_PACKET_SIZE);
201  if (received == -1) {
202  ods_log_error("[%s] unable to forward dns packet: %s", xfrh_str,
203  strerror(errno));
204  }
205  return;
206 }
207 
208 
213 void
215 {
216  allocator_type* allocator = NULL;
217  if (!xfrhandler) {
218  return;
219  }
220  allocator = xfrhandler->allocator;
221  netio_cleanup(xfrhandler->netio);
222  buffer_cleanup(xfrhandler->packet, allocator);
223  tcp_set_cleanup(xfrhandler->tcp_set, allocator);
224  allocator_deallocate(allocator, (void*) xfrhandler);
225  return;
226 }