]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-stream.h
Merge pull request #9950 from yuwata/macro-ref-unref
[thirdparty/systemd.git] / src / resolve / resolved-dns-stream.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include "socket-util.h"
5
6 typedef struct DnsStream DnsStream;
7
8 #include "resolved-dns-packet.h"
9 #include "resolved-dns-transaction.h"
10 #include "resolved-manager.h"
11 #if ENABLE_DNS_OVER_TLS
12 #include "resolved-dnstls.h"
13 #endif
14
15 #define DNS_STREAM_WRITE_TLS_DATA 1
16
17 /* Streams are used by three subsystems:
18 *
19 * 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP
20 * 2. The LLMNR logic when accepting a TCP-based lookup
21 * 3. The DNS stub logic when accepting a TCP-based lookup
22 */
23
24 struct DnsStream {
25 Manager *manager;
26 unsigned n_ref;
27
28 DnsProtocol protocol;
29
30 int fd;
31 union sockaddr_union peer;
32 socklen_t peer_salen;
33 union sockaddr_union local;
34 socklen_t local_salen;
35 int ifindex;
36 uint32_t ttl;
37 bool identified;
38
39 /* only when using TCP fast open */
40 union sockaddr_union tfo_address;
41 socklen_t tfo_salen;
42
43 #if ENABLE_DNS_OVER_TLS
44 DnsTlsStreamData dnstls_data;
45 int dnstls_events;
46 #endif
47
48 sd_event_source *io_event_source;
49 sd_event_source *timeout_event_source;
50
51 be16_t write_size, read_size;
52 DnsPacket *write_packet, *read_packet;
53 size_t n_written, n_read;
54 OrderedSet *write_queue;
55
56 int (*on_connection)(DnsStream *s);
57 int (*on_packet)(DnsStream *s);
58 int (*complete)(DnsStream *s, int error);
59
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 */
63
64 /* used when DNS-over-TLS is enabled */
65 bool encrypted:1;
66
67 LIST_FIELDS(DnsStream, streams);
68 };
69
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, void *tls_session);
73 #endif
74 DnsStream *dns_stream_unref(DnsStream *s);
75 DnsStream *dns_stream_ref(DnsStream *s);
76
77 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref);
78
79 int dns_stream_write_packet(DnsStream *s, DnsPacket *p);
80 ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags);
81
82 static inline bool DNS_STREAM_QUEUED(DnsStream *s) {
83 assert(s);
84
85 if (s->fd < 0) /* already stopped? */
86 return false;
87
88 return !!s->write_packet;
89 }