OpenDNSSEC-signer  2.0.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 "duration.h"
36 #include "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  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);
78  xfrhandler_cleanup(xfrh);
79  return NULL;
80  }
82  if (!xfrh->packet) {
83  ods_log_error("[%s] unable to create xfrhandler: "
84  "buffer_create() failed", xfrh_str);
85  xfrhandler_cleanup(xfrh);
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);
92  xfrhandler_cleanup(xfrh);
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  return xfrh;
101 }
102 
103 
108 void
110 {
111  ods_log_assert(xfrhandler);
112  ods_log_assert(xfrhandler->engine);
113  ods_log_debug("[%s] start", xfrh_str);
114  /* setup */
115  xfrhandler->start_time = time_now();
116  /* handlers */
117  netio_add_handler(xfrhandler->netio, &xfrhandler->dnshandler);
118  /* service */
119  while (xfrhandler->need_to_exit == 0) {
120  /* dispatch may block for a longer period, so current is gone */
121  xfrhandler->got_time = 0;
122  ods_log_deeebug("[%s] netio dispatch", xfrh_str);
123  if (netio_dispatch(xfrhandler->netio, NULL, NULL) == -1) {
124  if (errno != EINTR) {
125  ods_log_error("[%s] unable to dispatch netio: %s", xfrh_str,
126  strerror(errno));
127  }
128  }
129  }
130  /* shutdown */
131  ods_log_debug("[%s] shutdown", xfrh_str);
132 }
133 
134 
139 time_t
141 {
142  if (!xfrhandler) {
143  return 0;
144  }
145  if (!xfrhandler->got_time) {
146  xfrhandler->current_time = time_now();
147  xfrhandler->got_time = 1;
148  }
149  return xfrhandler->current_time;
150 }
151 
152 
157 void
159 {
160  if (xfrhandler && xfrhandler->started) {
161  ods_thread_kill(xfrhandler->thread_id, SIGHUP);
162  }
163 }
164 
165 
170 static void
171 xfrhandler_handle_dns(netio_type* ATTR_UNUSED(netio),
172  netio_handler_type* handler, netio_events_type event_types)
173 {
174  xfrhandler_type* xfrhandler = NULL;
175  uint8_t buf[MAX_PACKET_SIZE];
176  ssize_t received = 0;
177  if (!handler) {
178  return;
179  }
180  xfrhandler = (xfrhandler_type*) handler->user_data;
181  ods_log_assert(event_types & NETIO_EVENT_READ);
182  received = read(xfrhandler->dnshandler.fd, &buf, MAX_PACKET_SIZE);
183  ods_log_debug("[%s] read forwarded dns packet: %d bytes received",
184  xfrh_str, (int) received);
185  if (received == -1) {
186  ods_log_error("[%s] unable to forward dns packet: %s", xfrh_str,
187  strerror(errno));
188  }
189 }
190 
191 
196 void
198 {
199  if (!xfrhandler) {
200  return;
201  }
202  netio_cleanup(xfrhandler->netio);
203  buffer_cleanup(xfrhandler->packet);
204  tcp_set_cleanup(xfrhandler->tcp_set);
205  free(xfrhandler);
206 }
xfrd_type * udp_waiting_first
Definition: xfrhandler.h:64
xfrd_type * udp_waiting_last
Definition: xfrhandler.h:65
time_t current_time
Definition: xfrhandler.h:58
xfrd_type * tcp_waiting_first
Definition: xfrhandler.h:63
void xfrhandler_cleanup(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:197
buffer_type * packet
Definition: xfrhandler.h:62
netio_type * netio
Definition: xfrhandler.h:60
unsigned need_to_exit
Definition: xfrhandler.h:72
engine_type * engine
Definition: xfrhandler.h:55
enum netio_events_enum netio_events_type
Definition: netio.h:76
netio_handler_type dnshandler
Definition: xfrhandler.h:70
void * user_data
Definition: netio.h:119
void netio_add_handler(netio_type *netio, netio_handler_type *handler)
Definition: netio.c:54
unsigned got_time
Definition: xfrhandler.h:71
time_t xfrhandler_time(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:140
size_t udp_use_num
Definition: xfrhandler.h:66
void buffer_cleanup(buffer_type *buffer)
Definition: buffer.c:1145
xfrhandler_type * xfrhandler_create()
Definition: xfrhandler.c:52
void xfrhandler_start(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:109
notify_type * notify_waiting_first
Definition: xfrhandler.h:67
netio_event_handler_type event_handler
Definition: netio.h:131
tcp_set_type * tcp_set
Definition: xfrhandler.h:61
notify_type * notify_waiting_last
Definition: xfrhandler.h:68
void tcp_set_cleanup(tcp_set_type *set)
Definition: tcpset.c:242
void xfrhandler_signal(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:158
#define MAX_PACKET_SIZE
Definition: buffer.h:49
#define PACKET_BUFFER_SIZE
Definition: buffer.h:50
netio_events_type event_types
Definition: netio.h:124
netio_type * netio_create()
Definition: netio.c:39
unsigned started
Definition: xfrhandler.h:73
struct timespec * timeout
Definition: netio.h:115
tcp_set_type * tcp_set_create()
Definition: tcpset.c:67
ods_thread_type thread_id
Definition: xfrhandler.h:54
buffer_type * buffer_create(size_t capacity)
Definition: buffer.c:78
int netio_dispatch(netio_type *netio, const struct timespec *timeout, const sigset_t *sigmask)
Definition: netio.c:193
void netio_cleanup(netio_type *netio)
Definition: netio.c:342