1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "ordered-set.h"
7 #include "socket-util.h"
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
;
15 #include "resolved-dns-packet.h"
16 #include "resolved-dnstls.h"
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)
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)
24 /* In opportunistic TLS mode, lower timeouts */
25 #define DNS_STREAM_OPPORTUNISTIC_TLS_TIMEOUT_USEC (3 * USEC_PER_SEC)
27 /* Once connections are established apply this timeout once nothing happens anymore */
28 #define DNS_STREAM_ESTABLISHED_TIMEOUT_USEC (10 * USEC_PER_SEC)
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 */
36 _DNS_STREAM_TYPE_INVALID
= -EINVAL
,
39 #define DNS_STREAM_WRITE_TLS_DATA 1
41 /* Streams are used by three subsystems:
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
56 union sockaddr_union peer
;
58 union sockaddr_union local
;
59 socklen_t local_salen
;
63 bool packet_received
; /* At least one packet is received. Used by LLMNR. */
64 uint32_t requested_events
;
66 /* only when using TCP fast open */
67 union sockaddr_union tfo_address
;
70 #if ENABLE_DNS_OVER_TLS
71 DnsTlsStreamData dnstls_data
;
72 uint32_t dnstls_events
;
75 sd_event_source
*io_event_source
;
76 sd_event_source
*timeout_event_source
;
78 be16_t write_size
, read_size
;
79 DnsPacket
*write_packet
, *read_packet
;
80 size_t n_written
, n_read
;
81 OrderedSet
*write_queue
;
83 int (*on_packet
)(DnsStream
*s
, DnsPacket
*p
);
84 int (*complete
)(DnsStream
*s
, int error
);
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 */
90 /* used when DNS-over-TLS is enabled */
93 DnsStubListenerExtra
*stub_listener_extra
;
95 LIST_FIELDS(DnsStream
, streams
);
102 DnsProtocol protocol
,
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
);
111 DnsStream
*dns_stream_unref(DnsStream
*s
);
112 DnsStream
*dns_stream_ref(DnsStream
*s
);
114 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream
*, dns_stream_unref
);
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
);
119 static inline bool DNS_STREAM_QUEUED(DnsStream
*s
) {
122 if (s
->fd
< 0) /* already stopped? */
125 return !!s
->write_packet
;
128 void dns_stream_detach(DnsStream
*s
);
129 int dns_stream_disconnect_all(Manager
*m
);