OpenDNSSEC-signer
1.4.1
Main Page
Data Structures
Files
File List
Globals
signer
src
daemon
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
53
xfrhandler_type
*
54
xfrhandler_create
(
allocator_type
* allocator)
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;
109
xfrh->
dnshandler
.
event_types
=
NETIO_EVENT_READ
;
110
xfrh->
dnshandler
.
event_handler
= xfrhandler_handle_dns;
111
return
xfrh;
112
}
113
114
119
void
120
xfrhandler_start
(
xfrhandler_type
* xfrhandler)
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
158
xfrhandler_time
(
xfrhandler_type
* xfrhandler)
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
176
xfrhandler_signal
(
xfrhandler_type
* xfrhandler)
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
216
xfrhandler_cleanup
(
xfrhandler_type
* xfrhandler)
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
}
Generated on Wed Jul 17 2013 07:14:23 for OpenDNSSEC-signer by
1.8.4