1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
;
14 #include "resolved-dns-packet.h"
15 #include "resolved-dnstls.h"
17 typedef enum DnsStreamType
{
18 DNS_STREAM_LOOKUP
, /* Outgoing connection to a classic DNS server */
19 DNS_STREAM_LLMNR_SEND
, /* Outgoing LLMNR TCP lookup */
20 DNS_STREAM_LLMNR_RECV
, /* Incoming LLMNR TCP lookup */
21 DNS_STREAM_STUB
, /* Incoming DNS stub connection */
23 _DNS_STREAM_TYPE_INVALID
= -1,
26 #define DNS_STREAM_WRITE_TLS_DATA 1
28 /* Streams are used by three subsystems:
30 * 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP
31 * 2. The LLMNR logic when accepting a TCP-based lookup
32 * 3. The DNS stub logic when accepting a TCP-based lookup
43 union sockaddr_union peer
;
45 union sockaddr_union local
;
46 socklen_t local_salen
;
51 /* only when using TCP fast open */
52 union sockaddr_union tfo_address
;
55 #if ENABLE_DNS_OVER_TLS
56 DnsTlsStreamData dnstls_data
;
60 sd_event_source
*io_event_source
;
61 sd_event_source
*timeout_event_source
;
63 be16_t write_size
, read_size
;
64 DnsPacket
*write_packet
, *read_packet
;
65 size_t n_written
, n_read
;
66 OrderedSet
*write_queue
;
68 int (*on_packet
)(DnsStream
*s
);
69 int (*complete
)(DnsStream
*s
, int error
);
71 LIST_HEAD(DnsTransaction
, transactions
); /* when used by the transaction logic */
72 DnsServer
*server
; /* when used by the transaction logic */
73 Set
*queries
; /* when used by the DNS stub logic */
75 /* used when DNS-over-TLS is enabled */
78 LIST_FIELDS(DnsStream
, streams
);
81 int dns_stream_new(Manager
*m
, DnsStream
**s
, DnsStreamType type
, DnsProtocol protocol
, int fd
, const union sockaddr_union
*tfo_address
);
82 #if ENABLE_DNS_OVER_TLS
83 int dns_stream_connect_tls(DnsStream
*s
, void *tls_session
);
85 DnsStream
*dns_stream_unref(DnsStream
*s
);
86 DnsStream
*dns_stream_ref(DnsStream
*s
);
88 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream
*, dns_stream_unref
);
90 int dns_stream_write_packet(DnsStream
*s
, DnsPacket
*p
);
91 ssize_t
dns_stream_writev(DnsStream
*s
, const struct iovec
*iov
, size_t iovcnt
, int flags
);
93 static inline bool DNS_STREAM_QUEUED(DnsStream
*s
) {
96 if (s
->fd
< 0) /* already stopped? */
99 return !!s
->write_packet
;
102 DnsPacket
*dns_stream_take_read_packet(DnsStream
*s
);
104 void dns_stream_detach(DnsStream
*s
);