dtstream.c revision 1.1.1.2 1 1.1 christos /*
2 1.1 christos * dnstap/dtstream.c - Frame Streams thread for unbound DNSTAP
3 1.1 christos *
4 1.1 christos * Copyright (c) 2020, NLnet Labs. All rights reserved.
5 1.1 christos *
6 1.1 christos * This software is open source.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * Redistributions of source code must retain the above copyright notice,
13 1.1 christos * this list of conditions and the following disclaimer.
14 1.1 christos *
15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice,
16 1.1 christos * this list of conditions and the following disclaimer in the documentation
17 1.1 christos * and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1 christos * be used to endorse or promote products derived from this software without
21 1.1 christos * specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos *
35 1.1 christos */
36 1.1 christos
37 1.1 christos /**
38 1.1 christos * \file
39 1.1 christos *
40 1.1 christos * An implementation of the Frame Streams data transport protocol for
41 1.1 christos * the Unbound DNSTAP message logging facility.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "config.h"
45 1.1 christos #include "dnstap/dtstream.h"
46 1.1 christos #include "dnstap/dnstap_fstrm.h"
47 1.1 christos #include "util/config_file.h"
48 1.1 christos #include "util/ub_event.h"
49 1.1 christos #include "util/net_help.h"
50 1.1 christos #include "services/outside_network.h"
51 1.1 christos #include "sldns/sbuffer.h"
52 1.1 christos #ifdef HAVE_SYS_UN_H
53 1.1 christos #include <sys/un.h>
54 1.1 christos #endif
55 1.1 christos #include <fcntl.h>
56 1.1 christos #ifdef HAVE_OPENSSL_SSL_H
57 1.1 christos #include <openssl/ssl.h>
58 1.1 christos #endif
59 1.1 christos #ifdef HAVE_OPENSSL_ERR_H
60 1.1 christos #include <openssl/err.h>
61 1.1 christos #endif
62 1.1 christos
63 1.1 christos /** number of messages to process in one output callback */
64 1.1 christos #define DTIO_MESSAGES_PER_CALLBACK 100
65 1.1 christos /** the msec to wait for reconnect (if not immediate, the first attempt) */
66 1.1 christos #define DTIO_RECONNECT_TIMEOUT_MIN 10
67 1.1 christos /** the msec to wait for reconnect max after backoff */
68 1.1 christos #define DTIO_RECONNECT_TIMEOUT_MAX 1000
69 1.1 christos /** the msec to wait for reconnect slow, to stop busy spinning on reconnect */
70 1.1 christos #define DTIO_RECONNECT_TIMEOUT_SLOW 1000
71 1.1 christos /** number of messages before wakeup of thread */
72 1.1 christos #define DTIO_MSG_FOR_WAKEUP 32
73 1.1 christos
74 1.1 christos /** maximum length of received frame */
75 1.1 christos #define DTIO_RECV_FRAME_MAX_LEN 1000
76 1.1 christos
77 1.1 christos struct stop_flush_info;
78 1.1 christos /** DTIO command channel commands */
79 1.1 christos enum {
80 1.1 christos /** DTIO command channel stop */
81 1.1 christos DTIO_COMMAND_STOP = 0,
82 1.1 christos /** DTIO command channel wakeup */
83 1.1 christos DTIO_COMMAND_WAKEUP = 1
84 1.1 christos } dtio_channel_command;
85 1.1 christos
86 1.1 christos /** open the output channel */
87 1.1 christos static void dtio_open_output(struct dt_io_thread* dtio);
88 1.1 christos /** add output event for read and write */
89 1.1 christos static int dtio_add_output_event_write(struct dt_io_thread* dtio);
90 1.1 christos /** start reconnection attempts */
91 1.1 christos static void dtio_reconnect_enable(struct dt_io_thread* dtio);
92 1.1 christos /** stop from stop_flush event loop */
93 1.1 christos static void dtio_stop_flush_exit(struct stop_flush_info* info);
94 1.1 christos /** setup a start control message */
95 1.1 christos static int dtio_control_start_send(struct dt_io_thread* dtio);
96 1.1 christos #ifdef HAVE_SSL
97 1.1 christos /** enable briefly waiting for a read event, for SSL negotiation */
98 1.1 christos static int dtio_enable_brief_read(struct dt_io_thread* dtio);
99 1.1 christos /** enable briefly waiting for a write event, for SSL negotiation */
100 1.1 christos static int dtio_enable_brief_write(struct dt_io_thread* dtio);
101 1.1 christos #endif
102 1.1 christos
103 1.1 christos struct dt_msg_queue*
104 1.1 christos dt_msg_queue_create(struct comm_base* base)
105 1.1 christos {
106 1.1 christos struct dt_msg_queue* mq = calloc(1, sizeof(*mq));
107 1.1 christos if(!mq) return NULL;
108 1.1 christos mq->maxsize = 1*1024*1024; /* set max size of buffer, per worker,
109 1.1 christos about 1 M should contain 64K messages with some overhead,
110 1.1 christos or a whole bunch smaller ones */
111 1.1 christos mq->wakeup_timer = comm_timer_create(base, mq_wakeup_cb, mq);
112 1.1 christos if(!mq->wakeup_timer) {
113 1.1 christos free(mq);
114 1.1 christos return NULL;
115 1.1 christos }
116 1.1 christos lock_basic_init(&mq->lock);
117 1.1 christos lock_protect(&mq->lock, mq, sizeof(*mq));
118 1.1 christos return mq;
119 1.1 christos }
120 1.1 christos
121 1.1 christos /** clear the message list, caller must hold the lock */
122 1.1 christos static void
123 1.1 christos dt_msg_queue_clear(struct dt_msg_queue* mq)
124 1.1 christos {
125 1.1 christos struct dt_msg_entry* e = mq->first, *next=NULL;
126 1.1 christos while(e) {
127 1.1 christos next = e->next;
128 1.1 christos free(e->buf);
129 1.1 christos free(e);
130 1.1 christos e = next;
131 1.1 christos }
132 1.1 christos mq->first = NULL;
133 1.1 christos mq->last = NULL;
134 1.1 christos mq->cursize = 0;
135 1.1 christos mq->msgcount = 0;
136 1.1 christos }
137 1.1 christos
138 1.1 christos void
139 1.1 christos dt_msg_queue_delete(struct dt_msg_queue* mq)
140 1.1 christos {
141 1.1 christos if(!mq) return;
142 1.1 christos lock_basic_destroy(&mq->lock);
143 1.1 christos dt_msg_queue_clear(mq);
144 1.1 christos comm_timer_delete(mq->wakeup_timer);
145 1.1 christos free(mq);
146 1.1 christos }
147 1.1 christos
148 1.1 christos /** make the dtio wake up by sending a wakeup command */
149 1.1 christos static void dtio_wakeup(struct dt_io_thread* dtio)
150 1.1 christos {
151 1.1 christos uint8_t cmd = DTIO_COMMAND_WAKEUP;
152 1.1 christos if(!dtio) return;
153 1.1 christos if(!dtio->started) return;
154 1.1 christos
155 1.1 christos while(1) {
156 1.1 christos ssize_t r = write(dtio->commandpipe[1], &cmd, sizeof(cmd));
157 1.1 christos if(r == -1) {
158 1.1 christos #ifndef USE_WINSOCK
159 1.1 christos if(errno == EINTR || errno == EAGAIN)
160 1.1 christos continue;
161 1.1 christos #else
162 1.1 christos if(WSAGetLastError() == WSAEINPROGRESS)
163 1.1 christos continue;
164 1.1 christos if(WSAGetLastError() == WSAEWOULDBLOCK)
165 1.1 christos continue;
166 1.1 christos #endif
167 1.1 christos log_err("dnstap io wakeup: write: %s",
168 1.1 christos sock_strerror(errno));
169 1.1 christos break;
170 1.1 christos }
171 1.1 christos break;
172 1.1 christos }
173 1.1 christos }
174 1.1 christos
175 1.1 christos void
176 1.1 christos mq_wakeup_cb(void* arg)
177 1.1 christos {
178 1.1 christos struct dt_msg_queue* mq = (struct dt_msg_queue*)arg;
179 1.1 christos /* even if the dtio is already active, because perhaps much
180 1.1 christos * traffic suddenly, we leave the timer running to save on
181 1.1 christos * managing it, the once a second timer is less work then
182 1.1 christos * starting and stopping the timer frequently */
183 1.1 christos lock_basic_lock(&mq->dtio->wakeup_timer_lock);
184 1.1 christos mq->dtio->wakeup_timer_enabled = 0;
185 1.1 christos lock_basic_unlock(&mq->dtio->wakeup_timer_lock);
186 1.1 christos dtio_wakeup(mq->dtio);
187 1.1 christos }
188 1.1 christos
189 1.1 christos /** start timer to wakeup dtio because there is content in the queue */
190 1.1 christos static void
191 1.1.1.2 christos dt_msg_queue_start_timer(struct dt_msg_queue* mq, int wakeupnow)
192 1.1 christos {
193 1.1.1.2 christos struct timeval tv = {0};
194 1.1 christos /* Start a timer to process messages to be logged.
195 1.1 christos * If we woke up the dtio thread for every message, the wakeup
196 1.1 christos * messages take up too much processing power. If the queue
197 1.1 christos * fills up the wakeup happens immediately. The timer wakes it up
198 1.1 christos * if there are infrequent messages to log. */
199 1.1 christos
200 1.1 christos /* we cannot start a timer in dtio thread, because it is a different
201 1.1 christos * thread and its event base is in use by the other thread, it would
202 1.1 christos * give race conditions if we tried to modify its event base,
203 1.1 christos * and locks would wait until it woke up, and this is what we do. */
204 1.1 christos
205 1.1 christos /* do not start the timer if a timer already exists, perhaps
206 1.1 christos * in another worker. So this variable is protected by a lock in
207 1.1.1.2 christos * dtio. */
208 1.1.1.2 christos
209 1.1.1.2 christos /* If we need to wakeupnow, 0 the timer to force the callback. */
210 1.1 christos lock_basic_lock(&mq->dtio->wakeup_timer_lock);
211 1.1 christos if(mq->dtio->wakeup_timer_enabled) {
212 1.1.1.2 christos if(wakeupnow) {
213 1.1.1.2 christos comm_timer_set(mq->wakeup_timer, &tv);
214 1.1.1.2 christos }
215 1.1 christos lock_basic_unlock(&mq->dtio->wakeup_timer_lock);
216 1.1 christos return;
217 1.1 christos }
218 1.1 christos mq->dtio->wakeup_timer_enabled = 1; /* we are going to start one */
219 1.1 christos
220 1.1 christos /* start the timer, in mq, in the event base of our worker */
221 1.1.1.2 christos if(!wakeupnow) {
222 1.1.1.2 christos tv.tv_sec = 1;
223 1.1.1.2 christos tv.tv_usec = 0;
224 1.1.1.2 christos }
225 1.1 christos comm_timer_set(mq->wakeup_timer, &tv);
226 1.1.1.2 christos lock_basic_unlock(&mq->dtio->wakeup_timer_lock);
227 1.1 christos }
228 1.1 christos
229 1.1 christos void
230 1.1 christos dt_msg_queue_submit(struct dt_msg_queue* mq, void* buf, size_t len)
231 1.1 christos {
232 1.1 christos int wakeupnow = 0, wakeupstarttimer = 0;
233 1.1 christos struct dt_msg_entry* entry;
234 1.1 christos
235 1.1 christos /* check conditions */
236 1.1 christos if(!buf) return;
237 1.1 christos if(len == 0) {
238 1.1 christos /* it is not possible to log entries with zero length,
239 1.1 christos * because the framestream protocol does not carry it.
240 1.1 christos * However the protobuf serialization does not create zero
241 1.1 christos * length datagrams for dnstap, so this should not happen. */
242 1.1 christos free(buf);
243 1.1 christos return;
244 1.1 christos }
245 1.1 christos if(!mq) {
246 1.1 christos free(buf);
247 1.1 christos return;
248 1.1 christos }
249 1.1 christos
250 1.1 christos /* allocate memory for queue entry */
251 1.1 christos entry = malloc(sizeof(*entry));
252 1.1 christos if(!entry) {
253 1.1 christos log_err("out of memory logging dnstap");
254 1.1 christos free(buf);
255 1.1 christos return;
256 1.1 christos }
257 1.1 christos entry->next = NULL;
258 1.1 christos entry->buf = buf;
259 1.1 christos entry->len = len;
260 1.1 christos
261 1.1.1.2 christos /* acquire lock */
262 1.1 christos lock_basic_lock(&mq->lock);
263 1.1 christos /* if list was empty, start timer for (eventual) wakeup */
264 1.1 christos if(mq->first == NULL)
265 1.1 christos wakeupstarttimer = 1;
266 1.1 christos /* if list contains more than wakeupnum elements, wakeup now,
267 1.1 christos * or if list is (going to be) almost full */
268 1.1 christos if(mq->msgcount == DTIO_MSG_FOR_WAKEUP ||
269 1.1 christos (mq->cursize < mq->maxsize * 9 / 10 &&
270 1.1 christos mq->cursize+len >= mq->maxsize * 9 / 10))
271 1.1 christos wakeupnow = 1;
272 1.1 christos /* see if it is going to fit */
273 1.1 christos if(mq->cursize + len > mq->maxsize) {
274 1.1 christos /* buffer full, or congested. */
275 1.1 christos /* drop */
276 1.1 christos lock_basic_unlock(&mq->lock);
277 1.1 christos free(buf);
278 1.1 christos free(entry);
279 1.1 christos return;
280 1.1 christos }
281 1.1 christos mq->cursize += len;
282 1.1 christos mq->msgcount ++;
283 1.1 christos /* append to list */
284 1.1 christos if(mq->last) {
285 1.1 christos mq->last->next = entry;
286 1.1 christos } else {
287 1.1 christos mq->first = entry;
288 1.1 christos }
289 1.1 christos mq->last = entry;
290 1.1 christos /* release lock */
291 1.1 christos lock_basic_unlock(&mq->lock);
292 1.1 christos
293 1.1.1.2 christos if(wakeupnow || wakeupstarttimer) {
294 1.1.1.2 christos dt_msg_queue_start_timer(mq, wakeupnow);
295 1.1 christos }
296 1.1 christos }
297 1.1 christos
298 1.1 christos struct dt_io_thread* dt_io_thread_create(void)
299 1.1 christos {
300 1.1 christos struct dt_io_thread* dtio = calloc(1, sizeof(*dtio));
301 1.1 christos lock_basic_init(&dtio->wakeup_timer_lock);
302 1.1 christos lock_protect(&dtio->wakeup_timer_lock, &dtio->wakeup_timer_enabled,
303 1.1 christos sizeof(dtio->wakeup_timer_enabled));
304 1.1 christos return dtio;
305 1.1 christos }
306 1.1 christos
307 1.1 christos void dt_io_thread_delete(struct dt_io_thread* dtio)
308 1.1 christos {
309 1.1 christos struct dt_io_list_item* item, *nextitem;
310 1.1 christos if(!dtio) return;
311 1.1 christos lock_basic_destroy(&dtio->wakeup_timer_lock);
312 1.1 christos item=dtio->io_list;
313 1.1 christos while(item) {
314 1.1 christos nextitem = item->next;
315 1.1 christos free(item);
316 1.1 christos item = nextitem;
317 1.1 christos }
318 1.1 christos free(dtio->socket_path);
319 1.1 christos free(dtio->ip_str);
320 1.1 christos free(dtio->tls_server_name);
321 1.1 christos free(dtio->client_key_file);
322 1.1 christos free(dtio->client_cert_file);
323 1.1 christos if(dtio->ssl_ctx) {
324 1.1 christos #ifdef HAVE_SSL
325 1.1 christos SSL_CTX_free(dtio->ssl_ctx);
326 1.1 christos #endif
327 1.1 christos }
328 1.1 christos free(dtio);
329 1.1 christos }
330 1.1 christos
331 1.1 christos int dt_io_thread_apply_cfg(struct dt_io_thread* dtio, struct config_file *cfg)
332 1.1 christos {
333 1.1 christos if(!cfg->dnstap) {
334 1.1 christos log_warn("cannot setup dnstap because dnstap-enable is no");
335 1.1 christos return 0;
336 1.1 christos }
337 1.1 christos
338 1.1 christos /* what type of connectivity do we have */
339 1.1 christos if(cfg->dnstap_ip && cfg->dnstap_ip[0]) {
340 1.1 christos if(cfg->dnstap_tls)
341 1.1 christos dtio->upstream_is_tls = 1;
342 1.1 christos else dtio->upstream_is_tcp = 1;
343 1.1 christos } else {
344 1.1 christos dtio->upstream_is_unix = 1;
345 1.1 christos }
346 1.1 christos dtio->is_bidirectional = cfg->dnstap_bidirectional;
347 1.1 christos
348 1.1 christos if(dtio->upstream_is_unix) {
349 1.1 christos char* nm;
350 1.1 christos if(!cfg->dnstap_socket_path ||
351 1.1 christos cfg->dnstap_socket_path[0]==0) {
352 1.1 christos log_err("dnstap setup: no dnstap-socket-path for "
353 1.1 christos "socket connect");
354 1.1 christos return 0;
355 1.1 christos }
356 1.1 christos nm = cfg->dnstap_socket_path;
357 1.1 christos if(cfg->chrootdir && cfg->chrootdir[0] && strncmp(nm,
358 1.1 christos cfg->chrootdir, strlen(cfg->chrootdir)) == 0)
359 1.1 christos nm += strlen(cfg->chrootdir);
360 1.1 christos free(dtio->socket_path);
361 1.1 christos dtio->socket_path = strdup(nm);
362 1.1 christos if(!dtio->socket_path) {
363 1.1 christos log_err("dnstap setup: malloc failure");
364 1.1 christos return 0;
365 1.1 christos }
366 1.1 christos }
367 1.1 christos
368 1.1 christos if(dtio->upstream_is_tcp || dtio->upstream_is_tls) {
369 1.1 christos if(!cfg->dnstap_ip || cfg->dnstap_ip[0] == 0) {
370 1.1 christos log_err("dnstap setup: no dnstap-ip for TCP connect");
371 1.1 christos return 0;
372 1.1 christos }
373 1.1 christos free(dtio->ip_str);
374 1.1 christos dtio->ip_str = strdup(cfg->dnstap_ip);
375 1.1 christos if(!dtio->ip_str) {
376 1.1 christos log_err("dnstap setup: malloc failure");
377 1.1 christos return 0;
378 1.1 christos }
379 1.1 christos }
380 1.1 christos
381 1.1 christos if(dtio->upstream_is_tls) {
382 1.1 christos #ifdef HAVE_SSL
383 1.1 christos if(cfg->dnstap_tls_server_name &&
384 1.1 christos cfg->dnstap_tls_server_name[0]) {
385 1.1 christos free(dtio->tls_server_name);
386 1.1 christos dtio->tls_server_name = strdup(
387 1.1 christos cfg->dnstap_tls_server_name);
388 1.1 christos if(!dtio->tls_server_name) {
389 1.1 christos log_err("dnstap setup: malloc failure");
390 1.1 christos return 0;
391 1.1 christos }
392 1.1 christos if(!check_auth_name_for_ssl(dtio->tls_server_name))
393 1.1 christos return 0;
394 1.1 christos }
395 1.1 christos if(cfg->dnstap_tls_client_key_file &&
396 1.1 christos cfg->dnstap_tls_client_key_file[0]) {
397 1.1 christos dtio->use_client_certs = 1;
398 1.1 christos free(dtio->client_key_file);
399 1.1 christos dtio->client_key_file = strdup(
400 1.1 christos cfg->dnstap_tls_client_key_file);
401 1.1 christos if(!dtio->client_key_file) {
402 1.1 christos log_err("dnstap setup: malloc failure");
403 1.1 christos return 0;
404 1.1 christos }
405 1.1 christos if(!cfg->dnstap_tls_client_cert_file ||
406 1.1 christos cfg->dnstap_tls_client_cert_file[0]==0) {
407 1.1 christos log_err("dnstap setup: client key "
408 1.1 christos "authentication enabled with "
409 1.1 christos "dnstap-tls-client-key-file, but "
410 1.1 christos "no dnstap-tls-client-cert-file "
411 1.1 christos "is given");
412 1.1 christos return 0;
413 1.1 christos }
414 1.1 christos free(dtio->client_cert_file);
415 1.1 christos dtio->client_cert_file = strdup(
416 1.1 christos cfg->dnstap_tls_client_cert_file);
417 1.1 christos if(!dtio->client_cert_file) {
418 1.1 christos log_err("dnstap setup: malloc failure");
419 1.1 christos return 0;
420 1.1 christos }
421 1.1 christos } else {
422 1.1 christos dtio->use_client_certs = 0;
423 1.1 christos dtio->client_key_file = NULL;
424 1.1 christos dtio->client_cert_file = NULL;
425 1.1 christos }
426 1.1 christos
427 1.1 christos if(cfg->dnstap_tls_cert_bundle) {
428 1.1 christos dtio->ssl_ctx = connect_sslctx_create(
429 1.1 christos dtio->client_key_file,
430 1.1 christos dtio->client_cert_file,
431 1.1 christos cfg->dnstap_tls_cert_bundle, 0);
432 1.1 christos } else {
433 1.1 christos dtio->ssl_ctx = connect_sslctx_create(
434 1.1 christos dtio->client_key_file,
435 1.1 christos dtio->client_cert_file,
436 1.1 christos cfg->tls_cert_bundle, cfg->tls_win_cert);
437 1.1 christos }
438 1.1 christos if(!dtio->ssl_ctx) {
439 1.1 christos log_err("could not setup SSL CTX");
440 1.1 christos return 0;
441 1.1 christos }
442 1.1 christos dtio->tls_use_sni = cfg->tls_use_sni;
443 1.1 christos #endif /* HAVE_SSL */
444 1.1 christos }
445 1.1 christos return 1;
446 1.1 christos }
447 1.1 christos
448 1.1 christos int dt_io_thread_register_queue(struct dt_io_thread* dtio,
449 1.1 christos struct dt_msg_queue* mq)
450 1.1 christos {
451 1.1 christos struct dt_io_list_item* item = malloc(sizeof(*item));
452 1.1 christos if(!item) return 0;
453 1.1 christos lock_basic_lock(&mq->lock);
454 1.1 christos mq->dtio = dtio;
455 1.1 christos lock_basic_unlock(&mq->lock);
456 1.1 christos item->queue = mq;
457 1.1 christos item->next = dtio->io_list;
458 1.1 christos dtio->io_list = item;
459 1.1 christos dtio->io_list_iter = NULL;
460 1.1 christos return 1;
461 1.1 christos }
462 1.1 christos
463 1.1 christos void dt_io_thread_unregister_queue(struct dt_io_thread* dtio,
464 1.1 christos struct dt_msg_queue* mq)
465 1.1 christos {
466 1.1 christos struct dt_io_list_item* item, *prev=NULL;
467 1.1 christos if(!dtio) return;
468 1.1 christos item = dtio->io_list;
469 1.1 christos while(item) {
470 1.1 christos if(item->queue == mq) {
471 1.1 christos /* found it */
472 1.1 christos if(prev) prev->next = item->next;
473 1.1 christos else dtio->io_list = item->next;
474 1.1 christos /* the queue itself only registered, not deleted */
475 1.1 christos lock_basic_lock(&item->queue->lock);
476 1.1 christos item->queue->dtio = NULL;
477 1.1 christos lock_basic_unlock(&item->queue->lock);
478 1.1 christos free(item);
479 1.1 christos dtio->io_list_iter = NULL;
480 1.1 christos return;
481 1.1 christos }
482 1.1 christos prev = item;
483 1.1 christos item = item->next;
484 1.1 christos }
485 1.1 christos }
486 1.1 christos
487 1.1 christos /** pick a message from the queue, the routine locks and unlocks,
488 1.1 christos * returns true if there is a message */
489 1.1 christos static int dt_msg_queue_pop(struct dt_msg_queue* mq, void** buf,
490 1.1 christos size_t* len)
491 1.1 christos {
492 1.1 christos lock_basic_lock(&mq->lock);
493 1.1 christos if(mq->first) {
494 1.1 christos struct dt_msg_entry* entry = mq->first;
495 1.1 christos mq->first = entry->next;
496 1.1 christos if(!entry->next) mq->last = NULL;
497 1.1 christos mq->cursize -= entry->len;
498 1.1 christos mq->msgcount --;
499 1.1 christos lock_basic_unlock(&mq->lock);
500 1.1 christos
501 1.1 christos *buf = entry->buf;
502 1.1 christos *len = entry->len;
503 1.1 christos free(entry);
504 1.1 christos return 1;
505 1.1 christos }
506 1.1 christos lock_basic_unlock(&mq->lock);
507 1.1 christos return 0;
508 1.1 christos }
509 1.1 christos
510 1.1 christos /** find message in queue, false if no message, true if message to send */
511 1.1 christos static int dtio_find_in_queue(struct dt_io_thread* dtio,
512 1.1 christos struct dt_msg_queue* mq)
513 1.1 christos {
514 1.1 christos void* buf=NULL;
515 1.1 christos size_t len=0;
516 1.1 christos if(dt_msg_queue_pop(mq, &buf, &len)) {
517 1.1 christos dtio->cur_msg = buf;
518 1.1 christos dtio->cur_msg_len = len;
519 1.1 christos dtio->cur_msg_done = 0;
520 1.1 christos dtio->cur_msg_len_done = 0;
521 1.1 christos return 1;
522 1.1 christos }
523 1.1 christos return 0;
524 1.1 christos }
525 1.1 christos
526 1.1 christos /** find a new message to write, search message queues, false if none */
527 1.1 christos static int dtio_find_msg(struct dt_io_thread* dtio)
528 1.1 christos {
529 1.1 christos struct dt_io_list_item *spot, *item;
530 1.1 christos
531 1.1 christos spot = dtio->io_list_iter;
532 1.1 christos /* use the next queue for the next message lookup,
533 1.1 christos * if we hit the end(NULL) the NULL restarts the iter at start. */
534 1.1 christos if(spot)
535 1.1 christos dtio->io_list_iter = spot->next;
536 1.1 christos else if(dtio->io_list)
537 1.1 christos dtio->io_list_iter = dtio->io_list->next;
538 1.1 christos
539 1.1 christos /* scan from spot to end-of-io_list */
540 1.1 christos item = spot;
541 1.1 christos while(item) {
542 1.1 christos if(dtio_find_in_queue(dtio, item->queue))
543 1.1 christos return 1;
544 1.1 christos item = item->next;
545 1.1 christos }
546 1.1 christos /* scan starting at the start-of-list (to wrap around the end) */
547 1.1 christos item = dtio->io_list;
548 1.1 christos while(item) {
549 1.1 christos if(dtio_find_in_queue(dtio, item->queue))
550 1.1 christos return 1;
551 1.1 christos item = item->next;
552 1.1 christos }
553 1.1 christos return 0;
554 1.1 christos }
555 1.1 christos
556 1.1 christos /** callback for the dnstap reconnect, to start reconnecting to output */
557 1.1 christos void dtio_reconnect_timeout_cb(int ATTR_UNUSED(fd),
558 1.1 christos short ATTR_UNUSED(bits), void* arg)
559 1.1 christos {
560 1.1 christos struct dt_io_thread* dtio = (struct dt_io_thread*)arg;
561 1.1 christos dtio->reconnect_is_added = 0;
562 1.1 christos verbose(VERB_ALGO, "dnstap io: reconnect timer");
563 1.1 christos
564 1.1 christos dtio_open_output(dtio);
565 1.1 christos if(dtio->event) {
566 1.1 christos if(!dtio_add_output_event_write(dtio))
567 1.1 christos return;
568 1.1 christos /* nothing wrong so far, wait on the output event */
569 1.1 christos return;
570 1.1 christos }
571 1.1 christos /* exponential backoff and retry on timer */
572 1.1 christos dtio_reconnect_enable(dtio);
573 1.1 christos }
574 1.1 christos
575 1.1 christos /** attempt to reconnect to the output, after a timeout */
576 1.1 christos static void dtio_reconnect_enable(struct dt_io_thread* dtio)
577 1.1 christos {
578 1.1 christos struct timeval tv;
579 1.1 christos int msec;
580 1.1 christos if(dtio->want_to_exit) return;
581 1.1 christos if(dtio->reconnect_is_added)
582 1.1 christos return; /* already done */
583 1.1 christos
584 1.1 christos /* exponential backoff, store the value for next timeout */
585 1.1 christos msec = dtio->reconnect_timeout;
586 1.1 christos if(msec == 0) {
587 1.1 christos dtio->reconnect_timeout = DTIO_RECONNECT_TIMEOUT_MIN;
588 1.1 christos } else {
589 1.1 christos dtio->reconnect_timeout = msec*2;
590 1.1 christos if(dtio->reconnect_timeout > DTIO_RECONNECT_TIMEOUT_MAX)
591 1.1 christos dtio->reconnect_timeout = DTIO_RECONNECT_TIMEOUT_MAX;
592 1.1 christos }
593 1.1 christos verbose(VERB_ALGO, "dnstap io: set reconnect attempt after %d msec",
594 1.1 christos msec);
595 1.1 christos
596 1.1 christos /* setup wait timer */
597 1.1 christos memset(&tv, 0, sizeof(tv));
598 1.1 christos tv.tv_sec = msec/1000;
599 1.1 christos tv.tv_usec = (msec%1000)*1000;
600 1.1 christos if(ub_timer_add(dtio->reconnect_timer, dtio->event_base,
601 1.1 christos &dtio_reconnect_timeout_cb, dtio, &tv) != 0) {
602 1.1 christos log_err("dnstap io: could not reconnect ev timer add");
603 1.1 christos return;
604 1.1 christos }
605 1.1 christos dtio->reconnect_is_added = 1;
606 1.1 christos }
607 1.1 christos
608 1.1 christos /** remove dtio reconnect timer */
609 1.1 christos static void dtio_reconnect_del(struct dt_io_thread* dtio)
610 1.1 christos {
611 1.1 christos if(!dtio->reconnect_is_added)
612 1.1 christos return;
613 1.1 christos ub_timer_del(dtio->reconnect_timer);
614 1.1 christos dtio->reconnect_is_added = 0;
615 1.1 christos }
616 1.1 christos
617 1.1 christos /** clear the reconnect exponential backoff timer.
618 1.1 christos * We have successfully connected so we can try again with short timeouts. */
619 1.1 christos static void dtio_reconnect_clear(struct dt_io_thread* dtio)
620 1.1 christos {
621 1.1 christos dtio->reconnect_timeout = 0;
622 1.1 christos dtio_reconnect_del(dtio);
623 1.1 christos }
624 1.1 christos
625 1.1 christos /** reconnect slowly, because we already know we have to wait for a bit */
626 1.1 christos static void dtio_reconnect_slow(struct dt_io_thread* dtio, int msec)
627 1.1 christos {
628 1.1 christos dtio_reconnect_del(dtio);
629 1.1 christos dtio->reconnect_timeout = msec;
630 1.1 christos dtio_reconnect_enable(dtio);
631 1.1 christos }
632 1.1 christos
633 1.1 christos /** delete the current message in the dtio, and reset counters */
634 1.1 christos static void dtio_cur_msg_free(struct dt_io_thread* dtio)
635 1.1 christos {
636 1.1 christos free(dtio->cur_msg);
637 1.1 christos dtio->cur_msg = NULL;
638 1.1 christos dtio->cur_msg_len = 0;
639 1.1 christos dtio->cur_msg_done = 0;
640 1.1 christos dtio->cur_msg_len_done = 0;
641 1.1 christos }
642 1.1 christos
643 1.1 christos /** delete the buffer and counters used to read frame */
644 1.1 christos static void dtio_read_frame_free(struct dt_frame_read_buf* rb)
645 1.1 christos {
646 1.1 christos if(rb->buf) {
647 1.1 christos free(rb->buf);
648 1.1 christos rb->buf = NULL;
649 1.1 christos }
650 1.1 christos rb->buf_count = 0;
651 1.1 christos rb->buf_cap = 0;
652 1.1 christos rb->frame_len = 0;
653 1.1 christos rb->frame_len_done = 0;
654 1.1 christos rb->control_frame = 0;
655 1.1 christos }
656 1.1 christos
657 1.1 christos /** del the output file descriptor event for listening */
658 1.1 christos static void dtio_del_output_event(struct dt_io_thread* dtio)
659 1.1 christos {
660 1.1 christos if(!dtio->event_added)
661 1.1 christos return;
662 1.1 christos ub_event_del(dtio->event);
663 1.1 christos dtio->event_added = 0;
664 1.1 christos dtio->event_added_is_write = 0;
665 1.1 christos }
666 1.1 christos
667 1.1 christos /** close dtio socket and set it to -1 */
668 1.1 christos static void dtio_close_fd(struct dt_io_thread* dtio)
669 1.1 christos {
670 1.1 christos sock_close(dtio->fd);
671 1.1 christos dtio->fd = -1;
672 1.1 christos }
673 1.1 christos
674 1.1 christos /** close and stop the output file descriptor event */
675 1.1 christos static void dtio_close_output(struct dt_io_thread* dtio)
676 1.1 christos {
677 1.1 christos if(!dtio->event)
678 1.1 christos return;
679 1.1 christos ub_event_free(dtio->event);
680 1.1 christos dtio->event = NULL;
681 1.1 christos if(dtio->ssl) {
682 1.1 christos #ifdef HAVE_SSL
683 1.1 christos SSL_shutdown(dtio->ssl);
684 1.1 christos SSL_free(dtio->ssl);
685 1.1 christos dtio->ssl = NULL;
686 1.1 christos #endif
687 1.1 christos }
688 1.1 christos dtio_close_fd(dtio);
689 1.1 christos
690 1.1 christos /* if there is a (partial) message, discard it
691 1.1 christos * we cannot send (the remainder of) it, and a new
692 1.1 christos * connection needs to start with a control frame. */
693 1.1 christos if(dtio->cur_msg) {
694 1.1 christos dtio_cur_msg_free(dtio);
695 1.1 christos }
696 1.1 christos
697 1.1 christos dtio->ready_frame_sent = 0;
698 1.1 christos dtio->accept_frame_received = 0;
699 1.1 christos dtio_read_frame_free(&dtio->read_frame);
700 1.1 christos
701 1.1 christos dtio_reconnect_enable(dtio);
702 1.1 christos }
703 1.1 christos
704 1.1 christos /** check for pending nonblocking connect errors,
705 1.1 christos * returns 1 if it is okay. -1 on error (close it), 0 to try later */
706 1.1 christos static int dtio_check_nb_connect(struct dt_io_thread* dtio)
707 1.1 christos {
708 1.1 christos int error = 0;
709 1.1 christos socklen_t len = (socklen_t)sizeof(error);
710 1.1 christos if(!dtio->check_nb_connect)
711 1.1 christos return 1; /* everything okay */
712 1.1 christos if(getsockopt(dtio->fd, SOL_SOCKET, SO_ERROR, (void*)&error,
713 1.1 christos &len) < 0) {
714 1.1 christos #ifndef USE_WINSOCK
715 1.1 christos error = errno; /* on solaris errno is error */
716 1.1 christos #else
717 1.1 christos error = WSAGetLastError();
718 1.1 christos #endif
719 1.1 christos }
720 1.1 christos #ifndef USE_WINSOCK
721 1.1 christos #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
722 1.1 christos if(error == EINPROGRESS || error == EWOULDBLOCK)
723 1.1 christos return 0; /* try again later */
724 1.1 christos #endif
725 1.1 christos #else
726 1.1 christos if(error == WSAEINPROGRESS) {
727 1.1 christos return 0; /* try again later */
728 1.1 christos } else if(error == WSAEWOULDBLOCK) {
729 1.1 christos ub_winsock_tcp_wouldblock((dtio->stop_flush_event?
730 1.1 christos dtio->stop_flush_event:dtio->event), UB_EV_WRITE);
731 1.1 christos return 0; /* try again later */
732 1.1 christos }
733 1.1 christos #endif
734 1.1 christos if(error != 0) {
735 1.1 christos char* to = dtio->socket_path;
736 1.1 christos if(!to) to = dtio->ip_str;
737 1.1 christos if(!to) to = "";
738 1.1 christos log_err("dnstap io: failed to connect to \"%s\": %s",
739 1.1 christos to, sock_strerror(error));
740 1.1 christos return -1; /* error, close it */
741 1.1 christos }
742 1.1 christos
743 1.1 christos if(dtio->ip_str)
744 1.1 christos verbose(VERB_DETAIL, "dnstap io: connected to %s",
745 1.1 christos dtio->ip_str);
746 1.1 christos else if(dtio->socket_path)
747 1.1 christos verbose(VERB_DETAIL, "dnstap io: connected to \"%s\"",
748 1.1 christos dtio->socket_path);
749 1.1 christos dtio_reconnect_clear(dtio);
750 1.1 christos dtio->check_nb_connect = 0;
751 1.1 christos return 1; /* everything okay */
752 1.1 christos }
753 1.1 christos
754 1.1 christos #ifdef HAVE_SSL
755 1.1 christos /** write to ssl output
756 1.1 christos * returns number of bytes written, 0 if nothing happened,
757 1.1 christos * try again later, or -1 if the channel is to be closed. */
758 1.1 christos static int dtio_write_ssl(struct dt_io_thread* dtio, uint8_t* buf,
759 1.1 christos size_t len)
760 1.1 christos {
761 1.1 christos int r;
762 1.1 christos ERR_clear_error();
763 1.1 christos r = SSL_write(dtio->ssl, buf, len);
764 1.1 christos if(r <= 0) {
765 1.1 christos int want = SSL_get_error(dtio->ssl, r);
766 1.1 christos if(want == SSL_ERROR_ZERO_RETURN) {
767 1.1 christos /* closed */
768 1.1 christos return -1;
769 1.1 christos } else if(want == SSL_ERROR_WANT_READ) {
770 1.1 christos /* we want a brief read event */
771 1.1 christos dtio_enable_brief_read(dtio);
772 1.1 christos return 0;
773 1.1 christos } else if(want == SSL_ERROR_WANT_WRITE) {
774 1.1 christos /* write again later */
775 1.1 christos return 0;
776 1.1 christos } else if(want == SSL_ERROR_SYSCALL) {
777 1.1 christos #ifdef EPIPE
778 1.1 christos if(errno == EPIPE && verbosity < 2)
779 1.1 christos return -1; /* silence 'broken pipe' */
780 1.1 christos #endif
781 1.1 christos #ifdef ECONNRESET
782 1.1 christos if(errno == ECONNRESET && verbosity < 2)
783 1.1 christos return -1; /* silence reset by peer */
784 1.1 christos #endif
785 1.1 christos if(errno != 0) {
786 1.1 christos log_err("dnstap io, SSL_write syscall: %s",
787 1.1 christos strerror(errno));
788 1.1 christos }
789 1.1 christos return -1;
790 1.1 christos }
791 1.1 christos log_crypto_err("dnstap io, could not SSL_write");
792 1.1 christos return -1;
793 1.1 christos }
794 1.1 christos return r;
795 1.1 christos }
796 1.1 christos #endif /* HAVE_SSL */
797 1.1 christos
798 1.1 christos /** write buffer to output.
799 1.1 christos * returns number of bytes written, 0 if nothing happened,
800 1.1 christos * try again later, or -1 if the channel is to be closed. */
801 1.1 christos static int dtio_write_buf(struct dt_io_thread* dtio, uint8_t* buf,
802 1.1 christos size_t len)
803 1.1 christos {
804 1.1 christos ssize_t ret;
805 1.1 christos if(dtio->fd == -1)
806 1.1 christos return -1;
807 1.1 christos #ifdef HAVE_SSL
808 1.1 christos if(dtio->ssl)
809 1.1 christos return dtio_write_ssl(dtio, buf, len);
810 1.1 christos #endif
811 1.1 christos ret = send(dtio->fd, (void*)buf, len, 0);
812 1.1 christos if(ret == -1) {
813 1.1 christos #ifndef USE_WINSOCK
814 1.1 christos if(errno == EINTR || errno == EAGAIN)
815 1.1 christos return 0;
816 1.1 christos #else
817 1.1 christos if(WSAGetLastError() == WSAEINPROGRESS)
818 1.1 christos return 0;
819 1.1 christos if(WSAGetLastError() == WSAEWOULDBLOCK) {
820 1.1 christos ub_winsock_tcp_wouldblock((dtio->stop_flush_event?
821 1.1 christos dtio->stop_flush_event:dtio->event),
822 1.1 christos UB_EV_WRITE);
823 1.1 christos return 0;
824 1.1 christos }
825 1.1 christos #endif
826 1.1 christos log_err("dnstap io: failed send: %s", sock_strerror(errno));
827 1.1 christos return -1;
828 1.1 christos }
829 1.1 christos return ret;
830 1.1 christos }
831 1.1 christos
832 1.1 christos #ifdef HAVE_WRITEV
833 1.1 christos /** write with writev, len and message, in one write, if possible.
834 1.1 christos * return true if message is done, false if incomplete */
835 1.1 christos static int dtio_write_with_writev(struct dt_io_thread* dtio)
836 1.1 christos {
837 1.1 christos uint32_t sendlen = htonl(dtio->cur_msg_len);
838 1.1 christos struct iovec iov[2];
839 1.1 christos ssize_t r;
840 1.1 christos iov[0].iov_base = ((uint8_t*)&sendlen)+dtio->cur_msg_len_done;
841 1.1 christos iov[0].iov_len = sizeof(sendlen)-dtio->cur_msg_len_done;
842 1.1 christos iov[1].iov_base = dtio->cur_msg;
843 1.1 christos iov[1].iov_len = dtio->cur_msg_len;
844 1.1 christos log_assert(iov[0].iov_len > 0);
845 1.1 christos r = writev(dtio->fd, iov, 2);
846 1.1 christos if(r == -1) {
847 1.1 christos #ifndef USE_WINSOCK
848 1.1 christos if(errno == EINTR || errno == EAGAIN)
849 1.1 christos return 0;
850 1.1 christos #else
851 1.1 christos if(WSAGetLastError() == WSAEINPROGRESS)
852 1.1 christos return 0;
853 1.1 christos if(WSAGetLastError() == WSAEWOULDBLOCK) {
854 1.1 christos ub_winsock_tcp_wouldblock((dtio->stop_flush_event?
855 1.1 christos dtio->stop_flush_event:dtio->event),
856 1.1 christos UB_EV_WRITE);
857 1.1 christos return 0;
858 1.1 christos }
859 1.1 christos #endif
860 1.1 christos log_err("dnstap io: failed writev: %s", sock_strerror(errno));
861 1.1 christos /* close the channel */
862 1.1 christos dtio_del_output_event(dtio);
863 1.1 christos dtio_close_output(dtio);
864 1.1 christos return 0;
865 1.1 christos }
866 1.1 christos /* written r bytes */
867 1.1 christos dtio->cur_msg_len_done += r;
868 1.1 christos if(dtio->cur_msg_len_done < 4)
869 1.1 christos return 0;
870 1.1 christos if(dtio->cur_msg_len_done > 4) {
871 1.1 christos dtio->cur_msg_done = dtio->cur_msg_len_done-4;
872 1.1 christos dtio->cur_msg_len_done = 4;
873 1.1 christos }
874 1.1 christos if(dtio->cur_msg_done < dtio->cur_msg_len)
875 1.1 christos return 0;
876 1.1 christos return 1;
877 1.1 christos }
878 1.1 christos #endif /* HAVE_WRITEV */
879 1.1 christos
880 1.1 christos /** write more of the length, preceding the data frame.
881 1.1 christos * return true if message is done, false if incomplete. */
882 1.1 christos static int dtio_write_more_of_len(struct dt_io_thread* dtio)
883 1.1 christos {
884 1.1 christos uint32_t sendlen;
885 1.1 christos int r;
886 1.1 christos if(dtio->cur_msg_len_done >= 4)
887 1.1 christos return 1;
888 1.1 christos #ifdef HAVE_WRITEV
889 1.1 christos if(!dtio->ssl) {
890 1.1 christos /* we try writev for everything.*/
891 1.1 christos return dtio_write_with_writev(dtio);
892 1.1 christos }
893 1.1 christos #endif /* HAVE_WRITEV */
894 1.1 christos sendlen = htonl(dtio->cur_msg_len);
895 1.1 christos r = dtio_write_buf(dtio,
896 1.1 christos ((uint8_t*)&sendlen)+dtio->cur_msg_len_done,
897 1.1 christos sizeof(sendlen)-dtio->cur_msg_len_done);
898 1.1 christos if(r == -1) {
899 1.1 christos /* close the channel */
900 1.1 christos dtio_del_output_event(dtio);
901 1.1 christos dtio_close_output(dtio);
902 1.1 christos return 0;
903 1.1 christos } else if(r == 0) {
904 1.1 christos /* try again later */
905 1.1 christos return 0;
906 1.1 christos }
907 1.1 christos dtio->cur_msg_len_done += r;
908 1.1 christos if(dtio->cur_msg_len_done < 4)
909 1.1 christos return 0;
910 1.1 christos return 1;
911 1.1 christos }
912 1.1 christos
913 1.1 christos /** write more of the data frame.
914 1.1 christos * return true if message is done, false if incomplete. */
915 1.1 christos static int dtio_write_more_of_data(struct dt_io_thread* dtio)
916 1.1 christos {
917 1.1 christos int r;
918 1.1 christos if(dtio->cur_msg_done >= dtio->cur_msg_len)
919 1.1 christos return 1;
920 1.1 christos r = dtio_write_buf(dtio,
921 1.1 christos ((uint8_t*)dtio->cur_msg)+dtio->cur_msg_done,
922 1.1 christos dtio->cur_msg_len - dtio->cur_msg_done);
923 1.1 christos if(r == -1) {
924 1.1 christos /* close the channel */
925 1.1 christos dtio_del_output_event(dtio);
926 1.1 christos dtio_close_output(dtio);
927 1.1 christos return 0;
928 1.1 christos } else if(r == 0) {
929 1.1 christos /* try again later */
930 1.1 christos return 0;
931 1.1 christos }
932 1.1 christos dtio->cur_msg_done += r;
933 1.1 christos if(dtio->cur_msg_done < dtio->cur_msg_len)
934 1.1 christos return 0;
935 1.1 christos return 1;
936 1.1 christos }
937 1.1 christos
938 1.1.1.2 christos /** write more of the current message. false if incomplete, true if
939 1.1 christos * the message is done */
940 1.1 christos static int dtio_write_more(struct dt_io_thread* dtio)
941 1.1 christos {
942 1.1 christos if(dtio->cur_msg_len_done < 4) {
943 1.1 christos if(!dtio_write_more_of_len(dtio))
944 1.1 christos return 0;
945 1.1 christos }
946 1.1 christos if(dtio->cur_msg_done < dtio->cur_msg_len) {
947 1.1 christos if(!dtio_write_more_of_data(dtio))
948 1.1 christos return 0;
949 1.1 christos }
950 1.1 christos return 1;
951 1.1 christos }
952 1.1 christos
953 1.1 christos /** Receive bytes from dtio->fd, store in buffer. Returns 0: closed,
954 1.1 christos * -1: continue, >0: number of bytes read into buffer */
955 1.1 christos static ssize_t receive_bytes(struct dt_io_thread* dtio, void* buf, size_t len) {
956 1.1 christos ssize_t r;
957 1.1 christos r = recv(dtio->fd, (void*)buf, len, 0);
958 1.1 christos if(r == -1) {
959 1.1 christos char* to = dtio->socket_path;
960 1.1 christos if(!to) to = dtio->ip_str;
961 1.1 christos if(!to) to = "";
962 1.1 christos #ifndef USE_WINSOCK
963 1.1 christos if(errno == EINTR || errno == EAGAIN)
964 1.1 christos return -1; /* try later */
965 1.1 christos #else
966 1.1 christos if(WSAGetLastError() == WSAEINPROGRESS) {
967 1.1 christos return -1; /* try later */
968 1.1 christos } else if(WSAGetLastError() == WSAEWOULDBLOCK) {
969 1.1 christos ub_winsock_tcp_wouldblock(
970 1.1 christos (dtio->stop_flush_event?
971 1.1 christos dtio->stop_flush_event:dtio->event),
972 1.1 christos UB_EV_READ);
973 1.1 christos return -1; /* try later */
974 1.1 christos }
975 1.1 christos #endif
976 1.1 christos if(dtio->reconnect_timeout > DTIO_RECONNECT_TIMEOUT_MIN &&
977 1.1 christos verbosity < 4)
978 1.1 christos return 0; /* no log retries on low verbosity */
979 1.1 christos log_err("dnstap io: output closed, recv %s: %s", to,
980 1.1 christos strerror(errno));
981 1.1 christos /* and close below */
982 1.1 christos return 0;
983 1.1 christos }
984 1.1 christos if(r == 0) {
985 1.1 christos if(dtio->reconnect_timeout > DTIO_RECONNECT_TIMEOUT_MIN &&
986 1.1 christos verbosity < 4)
987 1.1 christos return 0; /* no log retries on low verbosity */
988 1.1 christos verbose(VERB_DETAIL, "dnstap io: output closed by the other side");
989 1.1 christos /* and close below */
990 1.1 christos return 0;
991 1.1 christos }
992 1.1 christos /* something was received */
993 1.1 christos return r;
994 1.1 christos }
995 1.1 christos
996 1.1 christos #ifdef HAVE_SSL
997 1.1 christos /** Receive bytes over TLS from dtio->fd, store in buffer. Returns 0: closed,
998 1.1 christos * -1: continue, >0: number of bytes read into buffer */
999 1.1 christos static int ssl_read_bytes(struct dt_io_thread* dtio, void* buf, size_t len)
1000 1.1 christos {
1001 1.1 christos int r;
1002 1.1 christos ERR_clear_error();
1003 1.1 christos r = SSL_read(dtio->ssl, buf, len);
1004 1.1 christos if(r <= 0) {
1005 1.1 christos int want = SSL_get_error(dtio->ssl, r);
1006 1.1 christos if(want == SSL_ERROR_ZERO_RETURN) {
1007 1.1 christos if(dtio->reconnect_timeout > DTIO_RECONNECT_TIMEOUT_MIN &&
1008 1.1 christos verbosity < 4)
1009 1.1 christos return 0; /* no log retries on low verbosity */
1010 1.1 christos verbose(VERB_DETAIL, "dnstap io: output closed by the "
1011 1.1 christos "other side");
1012 1.1 christos return 0;
1013 1.1 christos } else if(want == SSL_ERROR_WANT_READ) {
1014 1.1 christos /* continue later */
1015 1.1 christos return -1;
1016 1.1 christos } else if(want == SSL_ERROR_WANT_WRITE) {
1017 1.1 christos (void)dtio_enable_brief_write(dtio);
1018 1.1 christos return -1;
1019 1.1 christos } else if(want == SSL_ERROR_SYSCALL) {
1020 1.1 christos #ifdef ECONNRESET
1021 1.1 christos if(dtio->reconnect_timeout > DTIO_RECONNECT_TIMEOUT_MIN &&
1022 1.1 christos errno == ECONNRESET && verbosity < 4)
1023 1.1 christos return 0; /* silence reset by peer */
1024 1.1 christos #endif
1025 1.1 christos if(errno != 0)
1026 1.1 christos log_err("SSL_read syscall: %s",
1027 1.1 christos strerror(errno));
1028 1.1 christos verbose(VERB_DETAIL, "dnstap io: output closed by the "
1029 1.1 christos "other side");
1030 1.1 christos return 0;
1031 1.1 christos }
1032 1.1 christos log_crypto_err("could not SSL_read");
1033 1.1 christos verbose(VERB_DETAIL, "dnstap io: output closed by the "
1034 1.1 christos "other side");
1035 1.1 christos return 0;
1036 1.1 christos }
1037 1.1 christos return r;
1038 1.1 christos }
1039 1.1 christos #endif /* HAVE_SSL */
1040 1.1 christos
1041 1.1 christos /** check if the output fd has been closed,
1042 1.1 christos * it returns false if the stream is closed. */
1043 1.1 christos static int dtio_check_close(struct dt_io_thread* dtio)
1044 1.1 christos {
1045 1.1 christos /* we don't want to read any packets, but if there are we can
1046 1.1 christos * discard the input (ignore it). Ignore of unknown (control)
1047 1.1 christos * packets is okay for the framestream protocol. And also, the
1048 1.1 christos * read call can return that the stream has been closed by the
1049 1.1 christos * other side. */
1050 1.1 christos uint8_t buf[1024];
1051 1.1 christos int r = -1;
1052 1.1 christos
1053 1.1 christos
1054 1.1 christos if(dtio->fd == -1) return 0;
1055 1.1 christos
1056 1.1 christos while(r != 0) {
1057 1.1 christos /* not interested in buffer content, overwrite */
1058 1.1 christos r = receive_bytes(dtio, (void*)buf, sizeof(buf));
1059 1.1 christos if(r == -1)
1060 1.1 christos return 1;
1061 1.1 christos }
1062 1.1 christos /* the other end has been closed */
1063 1.1 christos /* close the channel */
1064 1.1 christos dtio_del_output_event(dtio);
1065 1.1 christos dtio_close_output(dtio);
1066 1.1 christos return 0;
1067 1.1 christos }
1068 1.1 christos
1069 1.1 christos /** Read accept frame. Returns -1: continue reading, 0: closed,
1070 1.1 christos * 1: valid accept received. */
1071 1.1 christos static int dtio_read_accept_frame(struct dt_io_thread* dtio)
1072 1.1 christos {
1073 1.1 christos int r;
1074 1.1 christos size_t read_frame_done;
1075 1.1 christos while(dtio->read_frame.frame_len_done < 4) {
1076 1.1 christos #ifdef HAVE_SSL
1077 1.1 christos if(dtio->ssl) {
1078 1.1 christos r = ssl_read_bytes(dtio,
1079 1.1 christos (uint8_t*)&dtio->read_frame.frame_len+
1080 1.1 christos dtio->read_frame.frame_len_done,
1081 1.1 christos 4-dtio->read_frame.frame_len_done);
1082 1.1 christos } else {
1083 1.1 christos #endif
1084 1.1 christos r = receive_bytes(dtio,
1085 1.1 christos (uint8_t*)&dtio->read_frame.frame_len+
1086 1.1 christos dtio->read_frame.frame_len_done,
1087 1.1 christos 4-dtio->read_frame.frame_len_done);
1088 1.1 christos #ifdef HAVE_SSL
1089 1.1 christos }
1090 1.1 christos #endif
1091 1.1 christos if(r == -1)
1092 1.1 christos return -1; /* continue reading */
1093 1.1 christos if(r == 0) {
1094 1.1 christos /* connection closed */
1095 1.1 christos goto close_connection;
1096 1.1 christos }
1097 1.1 christos dtio->read_frame.frame_len_done += r;
1098 1.1 christos if(dtio->read_frame.frame_len_done < 4)
1099 1.1 christos return -1; /* continue reading */
1100 1.1 christos
1101 1.1 christos if(dtio->read_frame.frame_len == 0) {
1102 1.1 christos dtio->read_frame.frame_len_done = 0;
1103 1.1 christos dtio->read_frame.control_frame = 1;
1104 1.1 christos continue;
1105 1.1 christos }
1106 1.1 christos dtio->read_frame.frame_len = ntohl(dtio->read_frame.frame_len);
1107 1.1 christos if(dtio->read_frame.frame_len > DTIO_RECV_FRAME_MAX_LEN) {
1108 1.1 christos verbose(VERB_OPS, "dnstap: received frame exceeds max "
1109 1.1 christos "length of %d bytes, closing connection",
1110 1.1 christos DTIO_RECV_FRAME_MAX_LEN);
1111 1.1 christos goto close_connection;
1112 1.1 christos }
1113 1.1 christos dtio->read_frame.buf = calloc(1, dtio->read_frame.frame_len);
1114 1.1 christos dtio->read_frame.buf_cap = dtio->read_frame.frame_len;
1115 1.1 christos if(!dtio->read_frame.buf) {
1116 1.1 christos log_err("dnstap io: out of memory (creating read "
1117 1.1 christos "buffer)");
1118 1.1 christos goto close_connection;
1119 1.1 christos }
1120 1.1 christos }
1121 1.1 christos if(dtio->read_frame.buf_count < dtio->read_frame.frame_len) {
1122 1.1 christos #ifdef HAVE_SSL
1123 1.1 christos if(dtio->ssl) {
1124 1.1 christos r = ssl_read_bytes(dtio, dtio->read_frame.buf+
1125 1.1 christos dtio->read_frame.buf_count,
1126 1.1 christos dtio->read_frame.buf_cap-
1127 1.1 christos dtio->read_frame.buf_count);
1128 1.1 christos } else {
1129 1.1 christos #endif
1130 1.1 christos r = receive_bytes(dtio, dtio->read_frame.buf+
1131 1.1 christos dtio->read_frame.buf_count,
1132 1.1 christos dtio->read_frame.buf_cap-
1133 1.1 christos dtio->read_frame.buf_count);
1134 1.1 christos #ifdef HAVE_SSL
1135 1.1 christos }
1136 1.1 christos #endif
1137 1.1 christos if(r == -1)
1138 1.1 christos return -1; /* continue reading */
1139 1.1 christos if(r == 0) {
1140 1.1 christos /* connection closed */
1141 1.1 christos goto close_connection;
1142 1.1 christos }
1143 1.1 christos dtio->read_frame.buf_count += r;
1144 1.1 christos if(dtio->read_frame.buf_count < dtio->read_frame.frame_len)
1145 1.1 christos return -1; /* continue reading */
1146 1.1 christos }
1147 1.1 christos
1148 1.1 christos /* Complete frame received, check if this is a valid ACCEPT control
1149 1.1 christos * frame. */
1150 1.1 christos if(dtio->read_frame.frame_len < 4) {
1151 1.1 christos verbose(VERB_OPS, "dnstap: invalid data received");
1152 1.1 christos goto close_connection;
1153 1.1 christos }
1154 1.1 christos if(sldns_read_uint32(dtio->read_frame.buf) !=
1155 1.1 christos FSTRM_CONTROL_FRAME_ACCEPT) {
1156 1.1 christos verbose(VERB_ALGO, "dnstap: invalid control type received, "
1157 1.1 christos "ignored");
1158 1.1 christos dtio->ready_frame_sent = 0;
1159 1.1 christos dtio->accept_frame_received = 0;
1160 1.1 christos dtio_read_frame_free(&dtio->read_frame);
1161 1.1 christos return -1;
1162 1.1 christos }
1163 1.1 christos read_frame_done = 4; /* control frame type */
1164 1.1 christos
1165 1.1 christos /* Iterate over control fields, ignore unknown types.
1166 1.1 christos * Need to be able to read at least 8 bytes (control field type +
1167 1.1 christos * length). */
1168 1.1 christos while(read_frame_done+8 < dtio->read_frame.frame_len) {
1169 1.1 christos uint32_t type = sldns_read_uint32(dtio->read_frame.buf +
1170 1.1 christos read_frame_done);
1171 1.1 christos uint32_t len = sldns_read_uint32(dtio->read_frame.buf +
1172 1.1 christos read_frame_done + 4);
1173 1.1 christos if(type == FSTRM_CONTROL_FIELD_TYPE_CONTENT_TYPE) {
1174 1.1 christos if(len == strlen(DNSTAP_CONTENT_TYPE) &&
1175 1.1 christos read_frame_done+8+len <=
1176 1.1 christos dtio->read_frame.frame_len &&
1177 1.1 christos memcmp(dtio->read_frame.buf + read_frame_done +
1178 1.1 christos + 8, DNSTAP_CONTENT_TYPE, len) == 0) {
1179 1.1 christos if(!dtio_control_start_send(dtio)) {
1180 1.1 christos verbose(VERB_OPS, "dnstap io: out of "
1181 1.1 christos "memory while sending START frame");
1182 1.1 christos goto close_connection;
1183 1.1 christos }
1184 1.1 christos dtio->accept_frame_received = 1;
1185 1.1 christos if(!dtio_add_output_event_write(dtio))
1186 1.1 christos goto close_connection;
1187 1.1 christos return 1;
1188 1.1 christos } else {
1189 1.1.1.2 christos /* unknown content type */
1190 1.1 christos verbose(VERB_ALGO, "dnstap: ACCEPT frame "
1191 1.1 christos "contains unknown content type, "
1192 1.1 christos "closing connection");
1193 1.1 christos goto close_connection;
1194 1.1 christos }
1195 1.1 christos }
1196 1.1 christos /* unknown option, try next */
1197 1.1 christos read_frame_done += 8+len;
1198 1.1 christos }
1199 1.1 christos
1200 1.1 christos
1201 1.1 christos close_connection:
1202 1.1 christos dtio_del_output_event(dtio);
1203 1.1 christos dtio_reconnect_slow(dtio, DTIO_RECONNECT_TIMEOUT_SLOW);
1204 1.1 christos dtio_close_output(dtio);
1205 1.1 christos return 0;
1206 1.1 christos }
1207 1.1 christos
1208 1.1 christos /** add the output file descriptor event for listening, read only */
1209 1.1 christos static int dtio_add_output_event_read(struct dt_io_thread* dtio)
1210 1.1 christos {
1211 1.1 christos if(!dtio->event)
1212 1.1 christos return 0;
1213 1.1 christos if(dtio->event_added && !dtio->event_added_is_write)
1214 1.1 christos return 1;
1215 1.1 christos /* we have to (re-)register the event */
1216 1.1 christos if(dtio->event_added)
1217 1.1 christos ub_event_del(dtio->event);
1218 1.1 christos ub_event_del_bits(dtio->event, UB_EV_WRITE);
1219 1.1 christos if(ub_event_add(dtio->event, NULL) != 0) {
1220 1.1 christos log_err("dnstap io: out of memory (adding event)");
1221 1.1 christos dtio->event_added = 0;
1222 1.1 christos dtio->event_added_is_write = 0;
1223 1.1 christos /* close output and start reattempts to open it */
1224 1.1 christos dtio_close_output(dtio);
1225 1.1 christos return 0;
1226 1.1 christos }
1227 1.1 christos dtio->event_added = 1;
1228 1.1 christos dtio->event_added_is_write = 0;
1229 1.1 christos return 1;
1230 1.1 christos }
1231 1.1 christos
1232 1.1 christos /** add the output file descriptor event for listening, read and write */
1233 1.1 christos static int dtio_add_output_event_write(struct dt_io_thread* dtio)
1234 1.1 christos {
1235 1.1 christos if(!dtio->event)
1236 1.1 christos return 0;
1237 1.1 christos if(dtio->event_added && dtio->event_added_is_write)
1238 1.1 christos return 1;
1239 1.1 christos /* we have to (re-)register the event */
1240 1.1 christos if(dtio->event_added)
1241 1.1 christos ub_event_del(dtio->event);
1242 1.1 christos ub_event_add_bits(dtio->event, UB_EV_WRITE);
1243 1.1 christos if(ub_event_add(dtio->event, NULL) != 0) {
1244 1.1 christos log_err("dnstap io: out of memory (adding event)");
1245 1.1 christos dtio->event_added = 0;
1246 1.1 christos dtio->event_added_is_write = 0;
1247 1.1 christos /* close output and start reattempts to open it */
1248 1.1 christos dtio_close_output(dtio);
1249 1.1 christos return 0;
1250 1.1 christos }
1251 1.1 christos dtio->event_added = 1;
1252 1.1 christos dtio->event_added_is_write = 1;
1253 1.1 christos return 1;
1254 1.1 christos }
1255 1.1 christos
1256 1.1 christos /** put the dtio thread to sleep */
1257 1.1 christos static void dtio_sleep(struct dt_io_thread* dtio)
1258 1.1 christos {
1259 1.1 christos /* unregister the event polling for write, because there is
1260 1.1 christos * nothing to be written */
1261 1.1 christos (void)dtio_add_output_event_read(dtio);
1262 1.1 christos }
1263 1.1 christos
1264 1.1 christos #ifdef HAVE_SSL
1265 1.1 christos /** enable the brief read condition */
1266 1.1 christos static int dtio_enable_brief_read(struct dt_io_thread* dtio)
1267 1.1 christos {
1268 1.1 christos dtio->ssl_brief_read = 1;
1269 1.1 christos if(dtio->stop_flush_event) {
1270 1.1 christos ub_event_del(dtio->stop_flush_event);
1271 1.1 christos ub_event_del_bits(dtio->stop_flush_event, UB_EV_WRITE);
1272 1.1 christos if(ub_event_add(dtio->stop_flush_event, NULL) != 0) {
1273 1.1 christos log_err("dnstap io, stop flush, could not ub_event_add");
1274 1.1 christos return 0;
1275 1.1 christos }
1276 1.1 christos return 1;
1277 1.1 christos }
1278 1.1 christos return dtio_add_output_event_read(dtio);
1279 1.1 christos }
1280 1.1 christos #endif /* HAVE_SSL */
1281 1.1 christos
1282 1.1 christos #ifdef HAVE_SSL
1283 1.1 christos /** disable the brief read condition */
1284 1.1 christos static int dtio_disable_brief_read(struct dt_io_thread* dtio)
1285 1.1 christos {
1286 1.1 christos dtio->ssl_brief_read = 0;
1287 1.1 christos if(dtio->stop_flush_event) {
1288 1.1 christos ub_event_del(dtio->stop_flush_event);
1289 1.1 christos ub_event_add_bits(dtio->stop_flush_event, UB_EV_WRITE);
1290 1.1 christos if(ub_event_add(dtio->stop_flush_event, NULL) != 0) {
1291 1.1 christos log_err("dnstap io, stop flush, could not ub_event_add");
1292 1.1 christos return 0;
1293 1.1 christos }
1294 1.1 christos return 1;
1295 1.1 christos }
1296 1.1 christos return dtio_add_output_event_write(dtio);
1297 1.1 christos }
1298 1.1 christos #endif /* HAVE_SSL */
1299 1.1 christos
1300 1.1 christos #ifdef HAVE_SSL
1301 1.1 christos /** enable the brief write condition */
1302 1.1 christos static int dtio_enable_brief_write(struct dt_io_thread* dtio)
1303 1.1 christos {
1304 1.1 christos dtio->ssl_brief_write = 1;
1305 1.1 christos return dtio_add_output_event_write(dtio);
1306 1.1 christos }
1307 1.1 christos #endif /* HAVE_SSL */
1308 1.1 christos
1309 1.1 christos #ifdef HAVE_SSL
1310 1.1 christos /** disable the brief write condition */
1311 1.1 christos static int dtio_disable_brief_write(struct dt_io_thread* dtio)
1312 1.1 christos {
1313 1.1 christos dtio->ssl_brief_write = 0;
1314 1.1 christos return dtio_add_output_event_read(dtio);
1315 1.1 christos }
1316 1.1 christos #endif /* HAVE_SSL */
1317 1.1 christos
1318 1.1 christos #ifdef HAVE_SSL
1319 1.1 christos /** check peer verification after ssl handshake connection, false if closed*/
1320 1.1 christos static int dtio_ssl_check_peer(struct dt_io_thread* dtio)
1321 1.1 christos {
1322 1.1 christos if((SSL_get_verify_mode(dtio->ssl)&SSL_VERIFY_PEER)) {
1323 1.1 christos /* verification */
1324 1.1 christos if(SSL_get_verify_result(dtio->ssl) == X509_V_OK) {
1325 1.1 christos X509* x = SSL_get_peer_certificate(dtio->ssl);
1326 1.1 christos if(!x) {
1327 1.1 christos verbose(VERB_ALGO, "dnstap io, %s, SSL "
1328 1.1 christos "connection failed no certificate",
1329 1.1 christos dtio->ip_str);
1330 1.1 christos return 0;
1331 1.1 christos }
1332 1.1 christos log_cert(VERB_ALGO, "dnstap io, peer certificate",
1333 1.1 christos x);
1334 1.1 christos #ifdef HAVE_SSL_GET0_PEERNAME
1335 1.1 christos if(SSL_get0_peername(dtio->ssl)) {
1336 1.1 christos verbose(VERB_ALGO, "dnstap io, %s, SSL "
1337 1.1 christos "connection to %s authenticated",
1338 1.1 christos dtio->ip_str,
1339 1.1 christos SSL_get0_peername(dtio->ssl));
1340 1.1 christos } else {
1341 1.1 christos #endif
1342 1.1 christos verbose(VERB_ALGO, "dnstap io, %s, SSL "
1343 1.1 christos "connection authenticated",
1344 1.1 christos dtio->ip_str);
1345 1.1 christos #ifdef HAVE_SSL_GET0_PEERNAME
1346 1.1 christos }
1347 1.1 christos #endif
1348 1.1 christos X509_free(x);
1349 1.1 christos } else {
1350 1.1 christos X509* x = SSL_get_peer_certificate(dtio->ssl);
1351 1.1 christos if(x) {
1352 1.1 christos log_cert(VERB_ALGO, "dnstap io, peer "
1353 1.1 christos "certificate", x);
1354 1.1 christos X509_free(x);
1355 1.1 christos }
1356 1.1 christos verbose(VERB_ALGO, "dnstap io, %s, SSL connection "
1357 1.1 christos "failed: failed to authenticate",
1358 1.1 christos dtio->ip_str);
1359 1.1 christos return 0;
1360 1.1 christos }
1361 1.1 christos } else {
1362 1.1 christos /* unauthenticated, the verify peer flag was not set
1363 1.1 christos * in ssl when the ssl object was created from ssl_ctx */
1364 1.1 christos verbose(VERB_ALGO, "dnstap io, %s, SSL connection",
1365 1.1 christos dtio->ip_str);
1366 1.1 christos }
1367 1.1 christos return 1;
1368 1.1 christos }
1369 1.1 christos #endif /* HAVE_SSL */
1370 1.1 christos
1371 1.1 christos #ifdef HAVE_SSL
1372 1.1 christos /** perform ssl handshake, returns 1 if okay, 0 to stop */
1373 1.1 christos static int dtio_ssl_handshake(struct dt_io_thread* dtio,
1374 1.1 christos struct stop_flush_info* info)
1375 1.1 christos {
1376 1.1 christos int r;
1377 1.1 christos if(dtio->ssl_brief_read) {
1378 1.1 christos /* assume the brief read condition is satisfied,
1379 1.1 christos * if we need more or again, we can set it again */
1380 1.1 christos if(!dtio_disable_brief_read(dtio)) {
1381 1.1 christos if(info) dtio_stop_flush_exit(info);
1382 1.1 christos return 0;
1383 1.1 christos }
1384 1.1 christos }
1385 1.1 christos if(dtio->ssl_handshake_done)
1386 1.1 christos return 1;
1387 1.1 christos
1388 1.1 christos ERR_clear_error();
1389 1.1 christos r = SSL_do_handshake(dtio->ssl);
1390 1.1 christos if(r != 1) {
1391 1.1 christos int want = SSL_get_error(dtio->ssl, r);
1392 1.1 christos if(want == SSL_ERROR_WANT_READ) {
1393 1.1 christos /* we want to read on the connection */
1394 1.1 christos if(!dtio_enable_brief_read(dtio)) {
1395 1.1 christos if(info) dtio_stop_flush_exit(info);
1396 1.1 christos return 0;
1397 1.1 christos }
1398 1.1 christos return 0;
1399 1.1 christos } else if(want == SSL_ERROR_WANT_WRITE) {
1400 1.1 christos /* we want to write on the connection */
1401 1.1 christos return 0;
1402 1.1 christos } else if(r == 0) {
1403 1.1 christos /* closed */
1404 1.1 christos if(info) dtio_stop_flush_exit(info);
1405 1.1 christos dtio_del_output_event(dtio);
1406 1.1 christos dtio_reconnect_slow(dtio, DTIO_RECONNECT_TIMEOUT_SLOW);
1407 1.1 christos dtio_close_output(dtio);
1408 1.1 christos return 0;
1409 1.1 christos } else if(want == SSL_ERROR_SYSCALL) {
1410 1.1 christos /* SYSCALL and errno==0 means closed uncleanly */
1411 1.1 christos int silent = 0;
1412 1.1 christos #ifdef EPIPE
1413 1.1 christos if(errno == EPIPE && verbosity < 2)
1414 1.1 christos silent = 1; /* silence 'broken pipe' */
1415 1.1 christos #endif
1416 1.1 christos #ifdef ECONNRESET
1417 1.1 christos if(errno == ECONNRESET && verbosity < 2)
1418 1.1 christos silent = 1; /* silence reset by peer */
1419 1.1 christos #endif
1420 1.1 christos if(errno == 0)
1421 1.1 christos silent = 1;
1422 1.1 christos if(!silent)
1423 1.1 christos log_err("dnstap io, SSL_handshake syscall: %s",
1424 1.1 christos strerror(errno));
1425 1.1 christos /* closed */
1426 1.1 christos if(info) dtio_stop_flush_exit(info);
1427 1.1 christos dtio_del_output_event(dtio);
1428 1.1 christos dtio_reconnect_slow(dtio, DTIO_RECONNECT_TIMEOUT_SLOW);
1429 1.1 christos dtio_close_output(dtio);
1430 1.1 christos return 0;
1431 1.1 christos } else {
1432 1.1 christos unsigned long err = ERR_get_error();
1433 1.1 christos if(!squelch_err_ssl_handshake(err)) {
1434 1.1 christos log_crypto_err_code("dnstap io, ssl handshake failed",
1435 1.1 christos err);
1436 1.1 christos verbose(VERB_OPS, "dnstap io, ssl handshake failed "
1437 1.1 christos "from %s", dtio->ip_str);
1438 1.1 christos }
1439 1.1 christos /* closed */
1440 1.1 christos if(info) dtio_stop_flush_exit(info);
1441 1.1 christos dtio_del_output_event(dtio);
1442 1.1 christos dtio_reconnect_slow(dtio, DTIO_RECONNECT_TIMEOUT_SLOW);
1443 1.1 christos dtio_close_output(dtio);
1444 1.1 christos return 0;
1445 1.1 christos }
1446 1.1 christos
1447 1.1 christos }
1448 1.1 christos /* check peer verification */
1449 1.1 christos dtio->ssl_handshake_done = 1;
1450 1.1 christos
1451 1.1 christos if(!dtio_ssl_check_peer(dtio)) {
1452 1.1 christos /* closed */
1453 1.1 christos if(info) dtio_stop_flush_exit(info);
1454 1.1 christos dtio_del_output_event(dtio);
1455 1.1 christos dtio_reconnect_slow(dtio, DTIO_RECONNECT_TIMEOUT_SLOW);
1456 1.1 christos dtio_close_output(dtio);
1457 1.1 christos return 0;
1458 1.1 christos }
1459 1.1 christos return 1;
1460 1.1 christos }
1461 1.1 christos #endif /* HAVE_SSL */
1462 1.1 christos
1463 1.1 christos /** callback for the dnstap events, to write to the output */
1464 1.1 christos void dtio_output_cb(int ATTR_UNUSED(fd), short bits, void* arg)
1465 1.1 christos {
1466 1.1 christos struct dt_io_thread* dtio = (struct dt_io_thread*)arg;
1467 1.1 christos int i;
1468 1.1 christos
1469 1.1 christos if(dtio->check_nb_connect) {
1470 1.1 christos int connect_err = dtio_check_nb_connect(dtio);
1471 1.1 christos if(connect_err == -1) {
1472 1.1 christos /* close the channel */
1473 1.1 christos dtio_del_output_event(dtio);
1474 1.1 christos dtio_close_output(dtio);
1475 1.1 christos return;
1476 1.1 christos } else if(connect_err == 0) {
1477 1.1 christos /* try again later */
1478 1.1 christos return;
1479 1.1 christos }
1480 1.1 christos /* nonblocking connect check passed, continue */
1481 1.1 christos }
1482 1.1 christos
1483 1.1 christos #ifdef HAVE_SSL
1484 1.1 christos if(dtio->ssl &&
1485 1.1 christos (!dtio->ssl_handshake_done || dtio->ssl_brief_read)) {
1486 1.1 christos if(!dtio_ssl_handshake(dtio, NULL))
1487 1.1 christos return;
1488 1.1 christos }
1489 1.1 christos #endif
1490 1.1 christos
1491 1.1 christos if((bits&UB_EV_READ || dtio->ssl_brief_write)) {
1492 1.1 christos if(dtio->ssl_brief_write)
1493 1.1 christos (void)dtio_disable_brief_write(dtio);
1494 1.1 christos if(dtio->ready_frame_sent && !dtio->accept_frame_received) {
1495 1.1 christos if(dtio_read_accept_frame(dtio) <= 0)
1496 1.1 christos return;
1497 1.1 christos } else if(!dtio_check_close(dtio))
1498 1.1 christos return;
1499 1.1 christos }
1500 1.1 christos
1501 1.1 christos /* loop to process a number of messages. This improves throughput,
1502 1.1 christos * because selecting on write-event if not needed for busy messages
1503 1.1 christos * (dnstap log) generation and if they need to all be written back.
1504 1.1 christos * The write event is usually not blocked up. But not forever,
1505 1.1 christos * because the event loop needs to stay responsive for other events.
1506 1.1 christos * If there are no (more) messages, or if the output buffers get
1507 1.1 christos * full, it returns out of the loop. */
1508 1.1 christos for(i=0; i<DTIO_MESSAGES_PER_CALLBACK; i++) {
1509 1.1 christos /* see if there are messages that need writing */
1510 1.1 christos if(!dtio->cur_msg) {
1511 1.1 christos if(!dtio_find_msg(dtio)) {
1512 1.1 christos if(i == 0) {
1513 1.1 christos /* no messages on the first iteration,
1514 1.1 christos * the queues are all empty */
1515 1.1 christos dtio_sleep(dtio);
1516 1.1 christos }
1517 1.1 christos return; /* nothing to do */
1518 1.1 christos }
1519 1.1 christos }
1520 1.1 christos
1521 1.1 christos /* write it */
1522 1.1 christos if(dtio->cur_msg_done < dtio->cur_msg_len) {
1523 1.1 christos if(!dtio_write_more(dtio))
1524 1.1 christos return;
1525 1.1 christos }
1526 1.1 christos
1527 1.1 christos /* done with the current message */
1528 1.1 christos dtio_cur_msg_free(dtio);
1529 1.1 christos
1530 1.1 christos /* If this is a bidirectional stream the first message will be
1531 1.1 christos * the READY control frame. We can only continue writing after
1532 1.1 christos * receiving an ACCEPT control frame. */
1533 1.1 christos if(dtio->is_bidirectional && !dtio->ready_frame_sent) {
1534 1.1 christos dtio->ready_frame_sent = 1;
1535 1.1 christos (void)dtio_add_output_event_read(dtio);
1536 1.1 christos break;
1537 1.1 christos }
1538 1.1 christos }
1539 1.1 christos }
1540 1.1 christos
1541 1.1 christos /** callback for the dnstap commandpipe, to stop the dnstap IO */
1542 1.1 christos void dtio_cmd_cb(int fd, short ATTR_UNUSED(bits), void* arg)
1543 1.1 christos {
1544 1.1 christos struct dt_io_thread* dtio = (struct dt_io_thread*)arg;
1545 1.1 christos uint8_t cmd;
1546 1.1 christos ssize_t r;
1547 1.1 christos if(dtio->want_to_exit)
1548 1.1 christos return;
1549 1.1 christos r = read(fd, &cmd, sizeof(cmd));
1550 1.1 christos if(r == -1) {
1551 1.1 christos #ifndef USE_WINSOCK
1552 1.1 christos if(errno == EINTR || errno == EAGAIN)
1553 1.1 christos return; /* ignore this */
1554 1.1 christos #else
1555 1.1 christos if(WSAGetLastError() == WSAEINPROGRESS)
1556 1.1 christos return;
1557 1.1 christos if(WSAGetLastError() == WSAEWOULDBLOCK)
1558 1.1 christos return;
1559 1.1 christos #endif
1560 1.1 christos log_err("dnstap io: failed to read: %s", sock_strerror(errno));
1561 1.1 christos /* and then fall through to quit the thread */
1562 1.1 christos } else if(r == 0) {
1563 1.1 christos verbose(VERB_ALGO, "dnstap io: cmd channel closed");
1564 1.1 christos } else if(r == 1 && cmd == DTIO_COMMAND_STOP) {
1565 1.1 christos verbose(VERB_ALGO, "dnstap io: cmd channel cmd quit");
1566 1.1 christos } else if(r == 1 && cmd == DTIO_COMMAND_WAKEUP) {
1567 1.1 christos verbose(VERB_ALGO, "dnstap io: cmd channel cmd wakeup");
1568 1.1 christos
1569 1.1 christos if(dtio->is_bidirectional && !dtio->accept_frame_received) {
1570 1.1 christos verbose(VERB_ALGO, "dnstap io: cmd wakeup ignored, "
1571 1.1 christos "waiting for ACCEPT control frame");
1572 1.1 christos return;
1573 1.1 christos }
1574 1.1 christos
1575 1.1 christos /* reregister event */
1576 1.1 christos if(!dtio_add_output_event_write(dtio))
1577 1.1 christos return;
1578 1.1 christos return;
1579 1.1 christos } else if(r == 1) {
1580 1.1 christos verbose(VERB_ALGO, "dnstap io: cmd channel unknown command");
1581 1.1 christos }
1582 1.1 christos dtio->want_to_exit = 1;
1583 1.1 christos if(ub_event_base_loopexit((struct ub_event_base*)dtio->event_base)
1584 1.1 christos != 0) {
1585 1.1 christos log_err("dnstap io: could not loopexit");
1586 1.1 christos }
1587 1.1 christos }
1588 1.1 christos
1589 1.1 christos #ifndef THREADS_DISABLED
1590 1.1 christos /** setup the event base for the dnstap io thread */
1591 1.1 christos static void dtio_setup_base(struct dt_io_thread* dtio, time_t* secs,
1592 1.1 christos struct timeval* now)
1593 1.1 christos {
1594 1.1 christos memset(now, 0, sizeof(*now));
1595 1.1 christos dtio->event_base = ub_default_event_base(0, secs, now);
1596 1.1 christos if(!dtio->event_base) {
1597 1.1 christos fatal_exit("dnstap io: could not create event_base");
1598 1.1 christos }
1599 1.1 christos }
1600 1.1 christos #endif /* THREADS_DISABLED */
1601 1.1 christos
1602 1.1 christos /** setup the cmd event for dnstap io */
1603 1.1 christos static void dtio_setup_cmd(struct dt_io_thread* dtio)
1604 1.1 christos {
1605 1.1 christos struct ub_event* cmdev;
1606 1.1 christos fd_set_nonblock(dtio->commandpipe[0]);
1607 1.1 christos cmdev = ub_event_new(dtio->event_base, dtio->commandpipe[0],
1608 1.1 christos UB_EV_READ | UB_EV_PERSIST, &dtio_cmd_cb, dtio);
1609 1.1 christos if(!cmdev) {
1610 1.1 christos fatal_exit("dnstap io: out of memory");
1611 1.1 christos }
1612 1.1 christos dtio->command_event = cmdev;
1613 1.1 christos if(ub_event_add(cmdev, NULL) != 0) {
1614 1.1 christos fatal_exit("dnstap io: out of memory (adding event)");
1615 1.1 christos }
1616 1.1 christos }
1617 1.1 christos
1618 1.1 christos /** setup the reconnect event for dnstap io */
1619 1.1 christos static void dtio_setup_reconnect(struct dt_io_thread* dtio)
1620 1.1 christos {
1621 1.1 christos dtio_reconnect_clear(dtio);
1622 1.1 christos dtio->reconnect_timer = ub_event_new(dtio->event_base, -1,
1623 1.1 christos UB_EV_TIMEOUT, &dtio_reconnect_timeout_cb, dtio);
1624 1.1 christos if(!dtio->reconnect_timer) {
1625 1.1 christos fatal_exit("dnstap io: out of memory");
1626 1.1 christos }
1627 1.1 christos }
1628 1.1 christos
1629 1.1 christos /**
1630 1.1 christos * structure to keep track of information during stop flush
1631 1.1 christos */
1632 1.1 christos struct stop_flush_info {
1633 1.1 christos /** the event base during stop flush */
1634 1.1 christos struct ub_event_base* base;
1635 1.1 christos /** did we already want to exit this stop-flush event base */
1636 1.1 christos int want_to_exit_flush;
1637 1.1 christos /** has the timer fired */
1638 1.1 christos int timer_done;
1639 1.1 christos /** the dtio */
1640 1.1 christos struct dt_io_thread* dtio;
1641 1.1 christos /** the stop control frame */
1642 1.1 christos void* stop_frame;
1643 1.1 christos /** length of the stop frame */
1644 1.1 christos size_t stop_frame_len;
1645 1.1 christos /** how much we have done of the stop frame */
1646 1.1 christos size_t stop_frame_done;
1647 1.1 christos };
1648 1.1 christos
1649 1.1 christos /** exit the stop flush base */
1650 1.1 christos static void dtio_stop_flush_exit(struct stop_flush_info* info)
1651 1.1 christos {
1652 1.1 christos if(info->want_to_exit_flush)
1653 1.1 christos return;
1654 1.1 christos info->want_to_exit_flush = 1;
1655 1.1 christos if(ub_event_base_loopexit(info->base) != 0) {
1656 1.1 christos log_err("dnstap io: could not loopexit");
1657 1.1 christos }
1658 1.1 christos }
1659 1.1 christos
1660 1.1 christos /** send the stop control,
1661 1.1 christos * return true if completed the frame. */
1662 1.1 christos static int dtio_control_stop_send(struct stop_flush_info* info)
1663 1.1 christos {
1664 1.1 christos struct dt_io_thread* dtio = info->dtio;
1665 1.1 christos int r;
1666 1.1 christos if(info->stop_frame_done >= info->stop_frame_len)
1667 1.1 christos return 1;
1668 1.1 christos r = dtio_write_buf(dtio, ((uint8_t*)info->stop_frame) +
1669 1.1 christos info->stop_frame_done, info->stop_frame_len -
1670 1.1 christos info->stop_frame_done);
1671 1.1 christos if(r == -1) {
1672 1.1 christos verbose(VERB_ALGO, "dnstap io: stop flush: output closed");
1673 1.1 christos dtio_stop_flush_exit(info);
1674 1.1 christos return 0;
1675 1.1 christos }
1676 1.1 christos if(r == 0) {
1677 1.1 christos /* try again later, or timeout */
1678 1.1 christos return 0;
1679 1.1 christos }
1680 1.1 christos info->stop_frame_done += r;
1681 1.1 christos if(info->stop_frame_done < info->stop_frame_len)
1682 1.1 christos return 0; /* not done yet */
1683 1.1 christos return 1;
1684 1.1 christos }
1685 1.1 christos
1686 1.1 christos void dtio_stop_timer_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(bits),
1687 1.1 christos void* arg)
1688 1.1 christos {
1689 1.1 christos struct stop_flush_info* info = (struct stop_flush_info*)arg;
1690 1.1 christos if(info->want_to_exit_flush)
1691 1.1 christos return;
1692 1.1 christos verbose(VERB_ALGO, "dnstap io: stop flush timer expired, stop flush");
1693 1.1 christos info->timer_done = 1;
1694 1.1 christos dtio_stop_flush_exit(info);
1695 1.1 christos }
1696 1.1 christos
1697 1.1 christos void dtio_stop_ev_cb(int ATTR_UNUSED(fd), short bits, void* arg)
1698 1.1 christos {
1699 1.1 christos struct stop_flush_info* info = (struct stop_flush_info*)arg;
1700 1.1 christos struct dt_io_thread* dtio = info->dtio;
1701 1.1 christos if(info->want_to_exit_flush)
1702 1.1 christos return;
1703 1.1 christos if(dtio->check_nb_connect) {
1704 1.1 christos /* we don't start the stop_flush if connect still
1705 1.1 christos * in progress, but the check code is here, just in case */
1706 1.1 christos int connect_err = dtio_check_nb_connect(dtio);
1707 1.1 christos if(connect_err == -1) {
1708 1.1 christos /* close the channel, exit the stop flush */
1709 1.1 christos dtio_stop_flush_exit(info);
1710 1.1 christos dtio_del_output_event(dtio);
1711 1.1 christos dtio_close_output(dtio);
1712 1.1 christos return;
1713 1.1 christos } else if(connect_err == 0) {
1714 1.1 christos /* try again later */
1715 1.1 christos return;
1716 1.1 christos }
1717 1.1 christos /* nonblocking connect check passed, continue */
1718 1.1 christos }
1719 1.1 christos #ifdef HAVE_SSL
1720 1.1 christos if(dtio->ssl &&
1721 1.1 christos (!dtio->ssl_handshake_done || dtio->ssl_brief_read)) {
1722 1.1 christos if(!dtio_ssl_handshake(dtio, info))
1723 1.1 christos return;
1724 1.1 christos }
1725 1.1 christos #endif
1726 1.1 christos
1727 1.1 christos if((bits&UB_EV_READ)) {
1728 1.1 christos if(!dtio_check_close(dtio)) {
1729 1.1 christos if(dtio->fd == -1) {
1730 1.1 christos verbose(VERB_ALGO, "dnstap io: "
1731 1.1 christos "stop flush: output closed");
1732 1.1 christos dtio_stop_flush_exit(info);
1733 1.1 christos }
1734 1.1 christos return;
1735 1.1 christos }
1736 1.1 christos }
1737 1.1 christos /* write remainder of last frame */
1738 1.1 christos if(dtio->cur_msg) {
1739 1.1 christos if(dtio->cur_msg_done < dtio->cur_msg_len) {
1740 1.1 christos if(!dtio_write_more(dtio)) {
1741 1.1 christos if(dtio->fd == -1) {
1742 1.1 christos verbose(VERB_ALGO, "dnstap io: "
1743 1.1 christos "stop flush: output closed");
1744 1.1 christos dtio_stop_flush_exit(info);
1745 1.1 christos }
1746 1.1 christos return;
1747 1.1 christos }
1748 1.1 christos }
1749 1.1 christos verbose(VERB_ALGO, "dnstap io: stop flush completed "
1750 1.1 christos "last frame");
1751 1.1 christos dtio_cur_msg_free(dtio);
1752 1.1 christos }
1753 1.1 christos /* write stop frame */
1754 1.1 christos if(info->stop_frame_done < info->stop_frame_len) {
1755 1.1 christos if(!dtio_control_stop_send(info))
1756 1.1 christos return;
1757 1.1 christos verbose(VERB_ALGO, "dnstap io: stop flush completed "
1758 1.1 christos "stop control frame");
1759 1.1 christos }
1760 1.1 christos /* when last frame and stop frame are sent, exit */
1761 1.1 christos dtio_stop_flush_exit(info);
1762 1.1 christos }
1763 1.1 christos
1764 1.1 christos /** flush at end, last packet and stop control */
1765 1.1 christos static void dtio_control_stop_flush(struct dt_io_thread* dtio)
1766 1.1 christos {
1767 1.1 christos /* briefly attempt to flush the previous packet to the output,
1768 1.1 christos * this could be a partial packet, or even the start control frame */
1769 1.1 christos time_t secs = 0;
1770 1.1 christos struct timeval now;
1771 1.1 christos struct stop_flush_info info;
1772 1.1 christos struct timeval tv;
1773 1.1 christos struct ub_event* timer, *stopev;
1774 1.1 christos
1775 1.1 christos if(dtio->fd == -1 || dtio->check_nb_connect) {
1776 1.1 christos /* no connection or we have just connected, so nothing is
1777 1.1 christos * sent yet, so nothing to stop or flush */
1778 1.1 christos return;
1779 1.1 christos }
1780 1.1 christos if(dtio->ssl && !dtio->ssl_handshake_done) {
1781 1.1 christos /* no SSL connection has been established yet */
1782 1.1 christos return;
1783 1.1 christos }
1784 1.1 christos
1785 1.1 christos memset(&info, 0, sizeof(info));
1786 1.1 christos memset(&now, 0, sizeof(now));
1787 1.1 christos info.dtio = dtio;
1788 1.1 christos info.base = ub_default_event_base(0, &secs, &now);
1789 1.1 christos if(!info.base) {
1790 1.1 christos log_err("dnstap io: malloc failure");
1791 1.1 christos return;
1792 1.1 christos }
1793 1.1 christos timer = ub_event_new(info.base, -1, UB_EV_TIMEOUT,
1794 1.1 christos &dtio_stop_timer_cb, &info);
1795 1.1 christos if(!timer) {
1796 1.1 christos log_err("dnstap io: malloc failure");
1797 1.1 christos ub_event_base_free(info.base);
1798 1.1 christos return;
1799 1.1 christos }
1800 1.1 christos memset(&tv, 0, sizeof(tv));
1801 1.1 christos tv.tv_sec = 2;
1802 1.1 christos if(ub_timer_add(timer, info.base, &dtio_stop_timer_cb, &info,
1803 1.1 christos &tv) != 0) {
1804 1.1 christos log_err("dnstap io: cannot event_timer_add");
1805 1.1 christos ub_event_free(timer);
1806 1.1 christos ub_event_base_free(info.base);
1807 1.1 christos return;
1808 1.1 christos }
1809 1.1 christos stopev = ub_event_new(info.base, dtio->fd, UB_EV_READ |
1810 1.1 christos UB_EV_WRITE | UB_EV_PERSIST, &dtio_stop_ev_cb, &info);
1811 1.1 christos if(!stopev) {
1812 1.1 christos log_err("dnstap io: malloc failure");
1813 1.1 christos ub_timer_del(timer);
1814 1.1 christos ub_event_free(timer);
1815 1.1 christos ub_event_base_free(info.base);
1816 1.1 christos return;
1817 1.1 christos }
1818 1.1 christos if(ub_event_add(stopev, NULL) != 0) {
1819 1.1 christos log_err("dnstap io: cannot event_add");
1820 1.1 christos ub_event_free(stopev);
1821 1.1 christos ub_timer_del(timer);
1822 1.1 christos ub_event_free(timer);
1823 1.1 christos ub_event_base_free(info.base);
1824 1.1 christos return;
1825 1.1 christos }
1826 1.1 christos info.stop_frame = fstrm_create_control_frame_stop(
1827 1.1 christos &info.stop_frame_len);
1828 1.1 christos if(!info.stop_frame) {
1829 1.1 christos log_err("dnstap io: malloc failure");
1830 1.1 christos ub_event_del(stopev);
1831 1.1 christos ub_event_free(stopev);
1832 1.1 christos ub_timer_del(timer);
1833 1.1 christos ub_event_free(timer);
1834 1.1 christos ub_event_base_free(info.base);
1835 1.1 christos return;
1836 1.1 christos }
1837 1.1 christos dtio->stop_flush_event = stopev;
1838 1.1 christos
1839 1.1 christos /* wait briefly, or until finished */
1840 1.1 christos verbose(VERB_ALGO, "dnstap io: stop flush started");
1841 1.1 christos if(ub_event_base_dispatch(info.base) < 0) {
1842 1.1 christos log_err("dnstap io: dispatch flush failed, errno is %s",
1843 1.1 christos strerror(errno));
1844 1.1 christos }
1845 1.1 christos verbose(VERB_ALGO, "dnstap io: stop flush ended");
1846 1.1 christos free(info.stop_frame);
1847 1.1 christos dtio->stop_flush_event = NULL;
1848 1.1 christos ub_event_del(stopev);
1849 1.1 christos ub_event_free(stopev);
1850 1.1 christos ub_timer_del(timer);
1851 1.1 christos ub_event_free(timer);
1852 1.1 christos ub_event_base_free(info.base);
1853 1.1 christos }
1854 1.1 christos
1855 1.1 christos /** perform desetup and free stuff when the dnstap io thread exits */
1856 1.1 christos static void dtio_desetup(struct dt_io_thread* dtio)
1857 1.1 christos {
1858 1.1 christos dtio_control_stop_flush(dtio);
1859 1.1 christos dtio_del_output_event(dtio);
1860 1.1 christos dtio_close_output(dtio);
1861 1.1 christos ub_event_del(dtio->command_event);
1862 1.1 christos ub_event_free(dtio->command_event);
1863 1.1 christos #ifndef USE_WINSOCK
1864 1.1 christos close(dtio->commandpipe[0]);
1865 1.1 christos #else
1866 1.1 christos _close(dtio->commandpipe[0]);
1867 1.1 christos #endif
1868 1.1 christos dtio->commandpipe[0] = -1;
1869 1.1 christos dtio_reconnect_del(dtio);
1870 1.1 christos ub_event_free(dtio->reconnect_timer);
1871 1.1 christos dtio_cur_msg_free(dtio);
1872 1.1 christos #ifndef THREADS_DISABLED
1873 1.1 christos ub_event_base_free(dtio->event_base);
1874 1.1 christos #endif
1875 1.1 christos }
1876 1.1 christos
1877 1.1 christos /** setup a start control message */
1878 1.1 christos static int dtio_control_start_send(struct dt_io_thread* dtio)
1879 1.1 christos {
1880 1.1 christos log_assert(dtio->cur_msg == NULL && dtio->cur_msg_len == 0);
1881 1.1 christos dtio->cur_msg = fstrm_create_control_frame_start(DNSTAP_CONTENT_TYPE,
1882 1.1 christos &dtio->cur_msg_len);
1883 1.1 christos if(!dtio->cur_msg) {
1884 1.1 christos return 0;
1885 1.1 christos }
1886 1.1 christos /* setup to send the control message */
1887 1.1 christos /* set that the buffer needs to be sent, but the length
1888 1.1 christos * of that buffer is already written, that way the buffer can
1889 1.1 christos * start with 0 length and then the length of the control frame
1890 1.1 christos * in it */
1891 1.1 christos dtio->cur_msg_done = 0;
1892 1.1 christos dtio->cur_msg_len_done = 4;
1893 1.1 christos return 1;
1894 1.1 christos }
1895 1.1 christos
1896 1.1 christos /** setup a ready control message */
1897 1.1 christos static int dtio_control_ready_send(struct dt_io_thread* dtio)
1898 1.1 christos {
1899 1.1 christos log_assert(dtio->cur_msg == NULL && dtio->cur_msg_len == 0);
1900 1.1 christos dtio->cur_msg = fstrm_create_control_frame_ready(DNSTAP_CONTENT_TYPE,
1901 1.1 christos &dtio->cur_msg_len);
1902 1.1 christos if(!dtio->cur_msg) {
1903 1.1 christos return 0;
1904 1.1 christos }
1905 1.1 christos /* setup to send the control message */
1906 1.1 christos /* set that the buffer needs to be sent, but the length
1907 1.1 christos * of that buffer is already written, that way the buffer can
1908 1.1 christos * start with 0 length and then the length of the control frame
1909 1.1 christos * in it */
1910 1.1 christos dtio->cur_msg_done = 0;
1911 1.1 christos dtio->cur_msg_len_done = 4;
1912 1.1 christos return 1;
1913 1.1 christos }
1914 1.1 christos
1915 1.1 christos /** open the output file descriptor for af_local */
1916 1.1 christos static int dtio_open_output_local(struct dt_io_thread* dtio)
1917 1.1 christos {
1918 1.1 christos #ifdef HAVE_SYS_UN_H
1919 1.1 christos struct sockaddr_un s;
1920 1.1 christos dtio->fd = socket(AF_LOCAL, SOCK_STREAM, 0);
1921 1.1 christos if(dtio->fd == -1) {
1922 1.1 christos log_err("dnstap io: failed to create socket: %s",
1923 1.1 christos sock_strerror(errno));
1924 1.1 christos return 0;
1925 1.1 christos }
1926 1.1 christos memset(&s, 0, sizeof(s));
1927 1.1 christos #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1928 1.1 christos /* this member exists on BSDs, not Linux */
1929 1.1 christos s.sun_len = (unsigned)sizeof(s);
1930 1.1 christos #endif
1931 1.1 christos s.sun_family = AF_LOCAL;
1932 1.1 christos /* length is 92-108, 104 on FreeBSD */
1933 1.1 christos (void)strlcpy(s.sun_path, dtio->socket_path, sizeof(s.sun_path));
1934 1.1 christos fd_set_nonblock(dtio->fd);
1935 1.1 christos if(connect(dtio->fd, (struct sockaddr*)&s, (socklen_t)sizeof(s))
1936 1.1 christos == -1) {
1937 1.1 christos char* to = dtio->socket_path;
1938 1.1 christos if(dtio->reconnect_timeout > DTIO_RECONNECT_TIMEOUT_MIN &&
1939 1.1 christos verbosity < 4) {
1940 1.1 christos dtio_close_fd(dtio);
1941 1.1 christos return 0; /* no log retries on low verbosity */
1942 1.1 christos }
1943 1.1 christos log_err("dnstap io: failed to connect to \"%s\": %s",
1944 1.1 christos to, sock_strerror(errno));
1945 1.1 christos dtio_close_fd(dtio);
1946 1.1 christos return 0;
1947 1.1 christos }
1948 1.1 christos return 1;
1949 1.1 christos #else
1950 1.1 christos log_err("cannot create af_local socket");
1951 1.1 christos return 0;
1952 1.1 christos #endif /* HAVE_SYS_UN_H */
1953 1.1 christos }
1954 1.1 christos
1955 1.1 christos /** open the output file descriptor for af_inet and af_inet6 */
1956 1.1 christos static int dtio_open_output_tcp(struct dt_io_thread* dtio)
1957 1.1 christos {
1958 1.1 christos struct sockaddr_storage addr;
1959 1.1 christos socklen_t addrlen;
1960 1.1 christos memset(&addr, 0, sizeof(addr));
1961 1.1 christos addrlen = (socklen_t)sizeof(addr);
1962 1.1 christos
1963 1.1 christos if(!extstrtoaddr(dtio->ip_str, &addr, &addrlen)) {
1964 1.1 christos log_err("could not parse IP '%s'", dtio->ip_str);
1965 1.1 christos return 0;
1966 1.1 christos }
1967 1.1 christos dtio->fd = socket(addr.ss_family, SOCK_STREAM, 0);
1968 1.1 christos if(dtio->fd == -1) {
1969 1.1 christos log_err("can't create socket: %s", sock_strerror(errno));
1970 1.1 christos return 0;
1971 1.1 christos }
1972 1.1 christos fd_set_nonblock(dtio->fd);
1973 1.1 christos if(connect(dtio->fd, (struct sockaddr*)&addr, addrlen) == -1) {
1974 1.1 christos if(errno == EINPROGRESS)
1975 1.1 christos return 1; /* wait until connect done*/
1976 1.1 christos if(dtio->reconnect_timeout > DTIO_RECONNECT_TIMEOUT_MIN &&
1977 1.1 christos verbosity < 4) {
1978 1.1 christos dtio_close_fd(dtio);
1979 1.1 christos return 0; /* no log retries on low verbosity */
1980 1.1 christos }
1981 1.1 christos #ifndef USE_WINSOCK
1982 1.1 christos if(tcp_connect_errno_needs_log(
1983 1.1 christos (struct sockaddr *)&addr, addrlen)) {
1984 1.1 christos log_err("dnstap io: failed to connect to %s: %s",
1985 1.1 christos dtio->ip_str, strerror(errno));
1986 1.1 christos }
1987 1.1 christos #else
1988 1.1 christos if(WSAGetLastError() == WSAEINPROGRESS ||
1989 1.1 christos WSAGetLastError() == WSAEWOULDBLOCK)
1990 1.1 christos return 1; /* wait until connect done*/
1991 1.1 christos if(tcp_connect_errno_needs_log(
1992 1.1 christos (struct sockaddr *)&addr, addrlen)) {
1993 1.1 christos log_err("dnstap io: failed to connect to %s: %s",
1994 1.1 christos dtio->ip_str, wsa_strerror(WSAGetLastError()));
1995 1.1 christos }
1996 1.1 christos #endif
1997 1.1 christos dtio_close_fd(dtio);
1998 1.1 christos return 0;
1999 1.1 christos }
2000 1.1 christos return 1;
2001 1.1 christos }
2002 1.1 christos
2003 1.1 christos /** setup the SSL structure for new connection */
2004 1.1 christos static int dtio_setup_ssl(struct dt_io_thread* dtio)
2005 1.1 christos {
2006 1.1 christos dtio->ssl = outgoing_ssl_fd(dtio->ssl_ctx, dtio->fd);
2007 1.1 christos if(!dtio->ssl) return 0;
2008 1.1 christos dtio->ssl_handshake_done = 0;
2009 1.1 christos dtio->ssl_brief_read = 0;
2010 1.1 christos
2011 1.1 christos if(!set_auth_name_on_ssl(dtio->ssl, dtio->tls_server_name,
2012 1.1 christos dtio->tls_use_sni)) {
2013 1.1 christos return 0;
2014 1.1 christos }
2015 1.1 christos return 1;
2016 1.1 christos }
2017 1.1 christos
2018 1.1 christos /** open the output file descriptor */
2019 1.1 christos static void dtio_open_output(struct dt_io_thread* dtio)
2020 1.1 christos {
2021 1.1 christos struct ub_event* ev;
2022 1.1 christos if(dtio->upstream_is_unix) {
2023 1.1 christos if(!dtio_open_output_local(dtio)) {
2024 1.1 christos dtio_reconnect_enable(dtio);
2025 1.1 christos return;
2026 1.1 christos }
2027 1.1 christos } else if(dtio->upstream_is_tcp || dtio->upstream_is_tls) {
2028 1.1 christos if(!dtio_open_output_tcp(dtio)) {
2029 1.1 christos dtio_reconnect_enable(dtio);
2030 1.1 christos return;
2031 1.1 christos }
2032 1.1 christos if(dtio->upstream_is_tls) {
2033 1.1 christos if(!dtio_setup_ssl(dtio)) {
2034 1.1 christos dtio_close_fd(dtio);
2035 1.1 christos dtio_reconnect_enable(dtio);
2036 1.1 christos return;
2037 1.1 christos }
2038 1.1 christos }
2039 1.1 christos }
2040 1.1 christos dtio->check_nb_connect = 1;
2041 1.1 christos
2042 1.1 christos /* the EV_READ is to read ACCEPT control messages, and catch channel
2043 1.1 christos * close. EV_WRITE is to write packets */
2044 1.1 christos ev = ub_event_new(dtio->event_base, dtio->fd,
2045 1.1 christos UB_EV_READ | UB_EV_WRITE | UB_EV_PERSIST, &dtio_output_cb,
2046 1.1 christos dtio);
2047 1.1 christos if(!ev) {
2048 1.1 christos log_err("dnstap io: out of memory");
2049 1.1 christos if(dtio->ssl) {
2050 1.1 christos #ifdef HAVE_SSL
2051 1.1 christos SSL_free(dtio->ssl);
2052 1.1 christos dtio->ssl = NULL;
2053 1.1 christos #endif
2054 1.1 christos }
2055 1.1 christos dtio_close_fd(dtio);
2056 1.1 christos dtio_reconnect_enable(dtio);
2057 1.1 christos return;
2058 1.1 christos }
2059 1.1 christos dtio->event = ev;
2060 1.1 christos
2061 1.1 christos /* setup protocol control message to start */
2062 1.1 christos if((!dtio->is_bidirectional && !dtio_control_start_send(dtio)) ||
2063 1.1 christos (dtio->is_bidirectional && !dtio_control_ready_send(dtio)) ) {
2064 1.1 christos log_err("dnstap io: out of memory");
2065 1.1 christos ub_event_free(dtio->event);
2066 1.1 christos dtio->event = NULL;
2067 1.1 christos if(dtio->ssl) {
2068 1.1 christos #ifdef HAVE_SSL
2069 1.1 christos SSL_free(dtio->ssl);
2070 1.1 christos dtio->ssl = NULL;
2071 1.1 christos #endif
2072 1.1 christos }
2073 1.1 christos dtio_close_fd(dtio);
2074 1.1 christos dtio_reconnect_enable(dtio);
2075 1.1 christos return;
2076 1.1 christos }
2077 1.1 christos }
2078 1.1 christos
2079 1.1 christos /** perform the setup of the writer thread on the established event_base */
2080 1.1 christos static void dtio_setup_on_base(struct dt_io_thread* dtio)
2081 1.1 christos {
2082 1.1 christos dtio_setup_cmd(dtio);
2083 1.1 christos dtio_setup_reconnect(dtio);
2084 1.1 christos dtio_open_output(dtio);
2085 1.1 christos if(!dtio_add_output_event_write(dtio))
2086 1.1 christos return;
2087 1.1 christos }
2088 1.1 christos
2089 1.1 christos #ifndef THREADS_DISABLED
2090 1.1 christos /** the IO thread function for the DNSTAP IO */
2091 1.1 christos static void* dnstap_io(void* arg)
2092 1.1 christos {
2093 1.1 christos struct dt_io_thread* dtio = (struct dt_io_thread*)arg;
2094 1.1 christos time_t secs = 0;
2095 1.1 christos struct timeval now;
2096 1.1 christos log_thread_set(&dtio->threadnum);
2097 1.1 christos
2098 1.1 christos /* setup */
2099 1.1 christos verbose(VERB_ALGO, "start dnstap io thread");
2100 1.1 christos dtio_setup_base(dtio, &secs, &now);
2101 1.1 christos dtio_setup_on_base(dtio);
2102 1.1 christos
2103 1.1 christos /* run */
2104 1.1 christos if(ub_event_base_dispatch(dtio->event_base) < 0) {
2105 1.1 christos log_err("dnstap io: dispatch failed, errno is %s",
2106 1.1 christos strerror(errno));
2107 1.1 christos }
2108 1.1 christos
2109 1.1 christos /* cleanup */
2110 1.1 christos verbose(VERB_ALGO, "stop dnstap io thread");
2111 1.1 christos dtio_desetup(dtio);
2112 1.1 christos return NULL;
2113 1.1 christos }
2114 1.1 christos #endif /* THREADS_DISABLED */
2115 1.1 christos
2116 1.1 christos int dt_io_thread_start(struct dt_io_thread* dtio, void* event_base_nothr,
2117 1.1 christos int numworkers)
2118 1.1 christos {
2119 1.1 christos /* set up the thread, can fail */
2120 1.1 christos #ifndef USE_WINSOCK
2121 1.1 christos if(pipe(dtio->commandpipe) == -1) {
2122 1.1 christos log_err("failed to create pipe: %s", strerror(errno));
2123 1.1 christos return 0;
2124 1.1 christos }
2125 1.1 christos #else
2126 1.1 christos if(_pipe(dtio->commandpipe, 4096, _O_BINARY) == -1) {
2127 1.1 christos log_err("failed to create _pipe: %s",
2128 1.1 christos wsa_strerror(WSAGetLastError()));
2129 1.1 christos return 0;
2130 1.1 christos }
2131 1.1 christos #endif
2132 1.1 christos
2133 1.1 christos /* start the thread */
2134 1.1 christos dtio->threadnum = numworkers+1;
2135 1.1 christos dtio->started = 1;
2136 1.1 christos #ifndef THREADS_DISABLED
2137 1.1 christos ub_thread_create(&dtio->tid, dnstap_io, dtio);
2138 1.1 christos (void)event_base_nothr;
2139 1.1 christos #else
2140 1.1 christos dtio->event_base = event_base_nothr;
2141 1.1 christos dtio_setup_on_base(dtio);
2142 1.1 christos #endif
2143 1.1 christos return 1;
2144 1.1 christos }
2145 1.1 christos
2146 1.1 christos void dt_io_thread_stop(struct dt_io_thread* dtio)
2147 1.1 christos {
2148 1.1 christos #ifndef THREADS_DISABLED
2149 1.1 christos uint8_t cmd = DTIO_COMMAND_STOP;
2150 1.1 christos #endif
2151 1.1 christos if(!dtio) return;
2152 1.1 christos if(!dtio->started) return;
2153 1.1 christos verbose(VERB_ALGO, "dnstap io: send stop cmd");
2154 1.1 christos
2155 1.1 christos #ifndef THREADS_DISABLED
2156 1.1 christos while(1) {
2157 1.1 christos ssize_t r = write(dtio->commandpipe[1], &cmd, sizeof(cmd));
2158 1.1 christos if(r == -1) {
2159 1.1 christos #ifndef USE_WINSOCK
2160 1.1 christos if(errno == EINTR || errno == EAGAIN)
2161 1.1 christos continue;
2162 1.1 christos #else
2163 1.1 christos if(WSAGetLastError() == WSAEINPROGRESS)
2164 1.1 christos continue;
2165 1.1 christos if(WSAGetLastError() == WSAEWOULDBLOCK)
2166 1.1 christos continue;
2167 1.1 christos #endif
2168 1.1 christos log_err("dnstap io stop: write: %s",
2169 1.1 christos sock_strerror(errno));
2170 1.1 christos break;
2171 1.1 christos }
2172 1.1 christos break;
2173 1.1 christos }
2174 1.1 christos dtio->started = 0;
2175 1.1 christos #endif /* THREADS_DISABLED */
2176 1.1 christos
2177 1.1 christos #ifndef USE_WINSOCK
2178 1.1 christos close(dtio->commandpipe[1]);
2179 1.1 christos #else
2180 1.1 christos _close(dtio->commandpipe[1]);
2181 1.1 christos #endif
2182 1.1 christos dtio->commandpipe[1] = -1;
2183 1.1 christos #ifndef THREADS_DISABLED
2184 1.1 christos ub_thread_join(dtio->tid);
2185 1.1 christos #else
2186 1.1 christos dtio->want_to_exit = 1;
2187 1.1 christos dtio_desetup(dtio);
2188 1.1 christos #endif
2189 1.1 christos }
2190