]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-stream.h
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / resolve / resolved-dns-stream.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 Copyright 2014 Lennart Poettering
6 ***/
7
8 #include "socket-util.h"
9
10 typedef struct DnsStream DnsStream;
11
12 #include "resolved-dns-packet.h"
13 #include "resolved-dns-transaction.h"
14 #include "resolved-manager.h"
15
16 #if HAVE_GNUTLS
17 #include <gnutls/gnutls.h>
18 #endif
19
20 /* Streams are used by three subsystems:
21 *
22 * 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP
23 * 2. The LLMNR logic when accepting a TCP-based lookup
24 * 3. The DNS stub logic when accepting a TCP-based lookup
25 */
26
27 struct DnsStream {
28 Manager *manager;
29 int n_ref;
30
31 DnsProtocol protocol;
32
33 int fd;
34 union sockaddr_union peer;
35 socklen_t peer_salen;
36 union sockaddr_union local;
37 socklen_t local_salen;
38 int ifindex;
39 uint32_t ttl;
40 bool identified;
41
42 /* only when using TCP fast open */
43 union sockaddr_union tfo_address;
44 socklen_t tfo_salen;
45
46 #if HAVE_GNUTLS
47 gnutls_session_t tls_session;
48 int tls_handshake;
49 bool tls_bye;
50 #endif
51
52 sd_event_source *io_event_source;
53 sd_event_source *timeout_event_source;
54
55 be16_t write_size, read_size;
56 DnsPacket *write_packet, *read_packet;
57 size_t n_written, n_read;
58 OrderedSet *write_queue;
59
60 int (*on_connection)(DnsStream *s);
61 int (*on_packet)(DnsStream *s);
62 int (*complete)(DnsStream *s, int error);
63
64 LIST_HEAD(DnsTransaction, transactions); /* when used by the transaction logic */
65 DnsServer *server; /* when used by the transaction logic */
66 DnsQuery *query; /* when used by the DNS stub logic */
67
68 /* used when DNS-over-TLS is enabled */
69 bool encrypted:1;
70
71 LIST_FIELDS(DnsStream, streams);
72 };
73
74 int dns_stream_new(Manager *m, DnsStream **s, DnsProtocol protocol, int fd, const union sockaddr_union *tfo_address);
75 #if HAVE_GNUTLS
76 int dns_stream_connect_tls(DnsStream *s, gnutls_session_t tls_session);
77 #endif
78 DnsStream *dns_stream_unref(DnsStream *s);
79 DnsStream *dns_stream_ref(DnsStream *s);
80
81 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref);
82
83 int dns_stream_write_packet(DnsStream *s, DnsPacket *p);
84
85 static inline bool DNS_STREAM_QUEUED(DnsStream *s) {
86 assert(s);
87
88 if (s->fd < 0) /* already stopped? */
89 return false;
90
91 return !!s->write_packet;
92 }