1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include "socket-util.h"
6 typedef struct DnsStream DnsStream
;
8 #include "resolved-dns-packet.h"
9 #include "resolved-dns-transaction.h"
10 #include "resolved-manager.h"
12 #if ENABLE_DNS_OVER_TLS
13 #include <gnutls/gnutls.h>
16 /* Streams are used by three subsystems:
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
30 union sockaddr_union peer
;
32 union sockaddr_union local
;
33 socklen_t local_salen
;
38 /* only when using TCP fast open */
39 union sockaddr_union tfo_address
;
42 #if ENABLE_DNS_OVER_TLS
43 gnutls_session_t tls_session
;
48 sd_event_source
*io_event_source
;
49 sd_event_source
*timeout_event_source
;
51 be16_t write_size
, read_size
;
52 DnsPacket
*write_packet
, *read_packet
;
53 size_t n_written
, n_read
;
54 OrderedSet
*write_queue
;
56 int (*on_connection
)(DnsStream
*s
);
57 int (*on_packet
)(DnsStream
*s
);
58 int (*complete
)(DnsStream
*s
, int error
);
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 */
64 /* used when DNS-over-TLS is enabled */
67 LIST_FIELDS(DnsStream
, streams
);
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
);
74 DnsStream
*dns_stream_unref(DnsStream
*s
);
75 DnsStream
*dns_stream_ref(DnsStream
*s
);
77 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream
*, dns_stream_unref
);
79 int dns_stream_write_packet(DnsStream
*s
, DnsPacket
*p
);
81 static inline bool DNS_STREAM_QUEUED(DnsStream
*s
) {
84 if (s
->fd
< 0) /* already stopped? */
87 return !!s
->write_packet
;