]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-stream.h
nspawn: rework how /run/host/ is set up
[thirdparty/systemd.git] / src / resolve / resolved-dns-stream.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include "sd-event.h"
5
6 #include "ordered-set.h"
7 #include "socket-util.h"
8
9 typedef struct DnsServer DnsServer;
10 typedef struct DnsStream DnsStream;
11 typedef struct DnsTransaction DnsTransaction;
12 typedef struct Manager Manager;
13
14 #include "resolved-dns-packet.h"
15 #include "resolved-dnstls.h"
16
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 */
22 _DNS_STREAM_TYPE_MAX,
23 _DNS_STREAM_TYPE_INVALID = -1,
24 } DnsStreamType;
25
26 #define DNS_STREAM_WRITE_TLS_DATA 1
27
28 /* Streams are used by three subsystems:
29 *
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
33 */
34
35 struct DnsStream {
36 Manager *manager;
37 unsigned n_ref;
38
39 DnsStreamType type;
40 DnsProtocol protocol;
41
42 int fd;
43 union sockaddr_union peer;
44 socklen_t peer_salen;
45 union sockaddr_union local;
46 socklen_t local_salen;
47 int ifindex;
48 uint32_t ttl;
49 bool identified;
50
51 /* only when using TCP fast open */
52 union sockaddr_union tfo_address;
53 socklen_t tfo_salen;
54
55 #if ENABLE_DNS_OVER_TLS
56 DnsTlsStreamData dnstls_data;
57 int dnstls_events;
58 #endif
59
60 sd_event_source *io_event_source;
61 sd_event_source *timeout_event_source;
62
63 be16_t write_size, read_size;
64 DnsPacket *write_packet, *read_packet;
65 size_t n_written, n_read;
66 OrderedSet *write_queue;
67
68 int (*on_packet)(DnsStream *s);
69 int (*complete)(DnsStream *s, int error);
70
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 */
74
75 /* used when DNS-over-TLS is enabled */
76 bool encrypted:1;
77
78 LIST_FIELDS(DnsStream, streams);
79 };
80
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);
84 #endif
85 DnsStream *dns_stream_unref(DnsStream *s);
86 DnsStream *dns_stream_ref(DnsStream *s);
87
88 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref);
89
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);
92
93 static inline bool DNS_STREAM_QUEUED(DnsStream *s) {
94 assert(s);
95
96 if (s->fd < 0) /* already stopped? */
97 return false;
98
99 return !!s->write_packet;
100 }
101
102 DnsPacket *dns_stream_take_read_packet(DnsStream *s);
103
104 void dns_stream_detach(DnsStream *s);