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