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 typedef enum DnsStreamType
{
19 DNS_STREAM_LOOKUP
, /* Outgoing connection to a classic DNS server */
20 DNS_STREAM_LLMNR_SEND
, /* Outgoing LLMNR TCP lookup */
21 DNS_STREAM_LLMNR_RECV
, /* Incoming LLMNR TCP lookup */
22 DNS_STREAM_STUB
, /* Incoming DNS stub connection */
24 _DNS_STREAM_TYPE_INVALID
= -1,
27 #define DNS_STREAM_WRITE_TLS_DATA 1
29 /* Streams are used by three subsystems:
31 * 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP
32 * 2. The LLMNR logic when accepting a TCP-based lookup
33 * 3. The DNS stub logic when accepting a TCP-based lookup
44 union sockaddr_union peer
;
46 union sockaddr_union local
;
47 socklen_t local_salen
;
52 /* only when using TCP fast open */
53 union sockaddr_union tfo_address
;
56 #if ENABLE_DNS_OVER_TLS
57 DnsTlsStreamData dnstls_data
;
61 sd_event_source
*io_event_source
;
62 sd_event_source
*timeout_event_source
;
64 be16_t write_size
, read_size
;
65 DnsPacket
*write_packet
, *read_packet
;
66 size_t n_written
, n_read
;
67 OrderedSet
*write_queue
;
69 int (*on_packet
)(DnsStream
*s
);
70 int (*complete
)(DnsStream
*s
, int error
);
72 LIST_HEAD(DnsTransaction
, transactions
); /* when used by the transaction logic */
73 DnsServer
*server
; /* when used by the transaction logic */
74 Set
*queries
; /* when used by the DNS stub logic */
76 /* used when DNS-over-TLS is enabled */
79 DnsStubListenerExtra
*stub_listener_extra
;
81 LIST_FIELDS(DnsStream
, streams
);
84 int dns_stream_new(Manager
*m
, DnsStream
**s
, DnsStreamType type
, DnsProtocol protocol
, int fd
, const union sockaddr_union
*tfo_address
);
85 #if ENABLE_DNS_OVER_TLS
86 int dns_stream_connect_tls(DnsStream
*s
, void *tls_session
);
88 DnsStream
*dns_stream_unref(DnsStream
*s
);
89 DnsStream
*dns_stream_ref(DnsStream
*s
);
91 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream
*, dns_stream_unref
);
93 int dns_stream_write_packet(DnsStream
*s
, DnsPacket
*p
);
94 ssize_t
dns_stream_writev(DnsStream
*s
, const struct iovec
*iov
, size_t iovcnt
, int flags
);
96 static inline bool DNS_STREAM_QUEUED(DnsStream
*s
) {
99 if (s
->fd
< 0) /* already stopped? */
102 return !!s
->write_packet
;
105 DnsPacket
*dns_stream_take_read_packet(DnsStream
*s
);
107 void dns_stream_detach(DnsStream
*s
);