]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-stream.h
resolved: explicitly disconnect all left-over TCP connections when coming back from...
[thirdparty/systemd.git] / src / resolve / resolved-dns-stream.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "sd-event.h"
5
6 #include "ordered-set.h"
7 #include "socket-util.h"
8
9 typedef struct DnsServer DnsServer;
10 typedef struct DnsStream DnsStream;
11 typedef struct DnsTransaction DnsTransaction;
12 typedef struct Manager Manager;
13 typedef struct DnsStubListenerExtra DnsStubListenerExtra;
14
15 #include "resolved-dns-packet.h"
16 #include "resolved-dnstls.h"
17
18 /* Various timeouts for establishing TCP connections. First the default time-out for that. */
19 #define DNS_STREAM_DEFAULT_TIMEOUT_USEC (10 * USEC_PER_SEC)
20
21 /* In the DNS stub, be more friendly for incoming connections, than we are to ourselves for outgoing ones */
22 #define DNS_STREAM_STUB_TIMEOUT_USEC (30 * USEC_PER_SEC)
23
24 /* In opportunistic TLS mode, lower timeouts */
25 #define DNS_STREAM_OPPORTUNISTIC_TLS_TIMEOUT_USEC (3 * USEC_PER_SEC)
26
27 /* Once connections are established apply this timeout once nothing happens anymore */
28 #define DNS_STREAM_ESTABLISHED_TIMEOUT_USEC (10 * USEC_PER_SEC)
29
30 typedef enum DnsStreamType {
31 DNS_STREAM_LOOKUP, /* Outgoing connection to a classic DNS server */
32 DNS_STREAM_LLMNR_SEND, /* Outgoing LLMNR TCP lookup */
33 DNS_STREAM_LLMNR_RECV, /* Incoming LLMNR TCP lookup */
34 DNS_STREAM_STUB, /* Incoming DNS stub connection */
35 _DNS_STREAM_TYPE_MAX,
36 _DNS_STREAM_TYPE_INVALID = -EINVAL,
37 } DnsStreamType;
38
39 #define DNS_STREAM_WRITE_TLS_DATA 1
40
41 /* Streams are used by three subsystems:
42 *
43 * 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP
44 * 2. The LLMNR logic when accepting a TCP-based lookup
45 * 3. The DNS stub logic when accepting a TCP-based lookup
46 */
47
48 struct DnsStream {
49 Manager *manager;
50 unsigned n_ref;
51
52 DnsStreamType type;
53 DnsProtocol protocol;
54
55 int fd;
56 union sockaddr_union peer;
57 socklen_t peer_salen;
58 union sockaddr_union local;
59 socklen_t local_salen;
60 int ifindex;
61 uint32_t ttl;
62 bool identified;
63 bool packet_received; /* At least one packet is received. Used by LLMNR. */
64 uint32_t requested_events;
65
66 /* only when using TCP fast open */
67 union sockaddr_union tfo_address;
68 socklen_t tfo_salen;
69
70 #if ENABLE_DNS_OVER_TLS
71 DnsTlsStreamData dnstls_data;
72 uint32_t dnstls_events;
73 #endif
74
75 sd_event_source *io_event_source;
76 sd_event_source *timeout_event_source;
77
78 be16_t write_size, read_size;
79 DnsPacket *write_packet, *read_packet;
80 size_t n_written, n_read;
81 OrderedSet *write_queue;
82
83 int (*on_packet)(DnsStream *s, DnsPacket *p);
84 int (*complete)(DnsStream *s, int error);
85
86 LIST_HEAD(DnsTransaction, transactions); /* when used by the transaction logic */
87 DnsServer *server; /* when used by the transaction logic */
88 Set *queries; /* when used by the DNS stub logic */
89
90 /* used when DNS-over-TLS is enabled */
91 bool encrypted:1;
92
93 DnsStubListenerExtra *stub_listener_extra;
94
95 LIST_FIELDS(DnsStream, streams);
96 };
97
98 int dns_stream_new(
99 Manager *m,
100 DnsStream **ret,
101 DnsStreamType type,
102 DnsProtocol protocol,
103 int fd,
104 const union sockaddr_union *tfo_address,
105 int (on_packet)(DnsStream*, DnsPacket*),
106 int (complete)(DnsStream*, int), /* optional */
107 usec_t connect_timeout_usec);
108 #if ENABLE_DNS_OVER_TLS
109 int dns_stream_connect_tls(DnsStream *s, void *tls_session);
110 #endif
111 DnsStream *dns_stream_unref(DnsStream *s);
112 DnsStream *dns_stream_ref(DnsStream *s);
113
114 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref);
115
116 int dns_stream_write_packet(DnsStream *s, DnsPacket *p);
117 ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags);
118
119 static inline bool DNS_STREAM_QUEUED(DnsStream *s) {
120 assert(s);
121
122 if (s->fd < 0) /* already stopped? */
123 return false;
124
125 return !!s->write_packet;
126 }
127
128 void dns_stream_detach(DnsStream *s);
129 int dns_stream_disconnect_all(Manager *m);