]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-stream.h
add ipv6 range element creation test cases
[thirdparty/systemd.git] / src / resolve / resolved-dns-stream.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 typedef struct DnsStubListenerExtra DnsStubListenerExtra;
14
15 #include "resolved-dns-packet.h"
16 #include "resolved-dnstls.h"
17
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 */
23 _DNS_STREAM_TYPE_MAX,
24 _DNS_STREAM_TYPE_INVALID = -1,
25 } DnsStreamType;
26
27 #define DNS_STREAM_WRITE_TLS_DATA 1
28
29 /* Streams are used by three subsystems:
30 *
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
34 */
35
36 struct DnsStream {
37 Manager *manager;
38 unsigned n_ref;
39
40 DnsStreamType type;
41 DnsProtocol protocol;
42
43 int fd;
44 union sockaddr_union peer;
45 socklen_t peer_salen;
46 union sockaddr_union local;
47 socklen_t local_salen;
48 int ifindex;
49 uint32_t ttl;
50 bool identified;
51
52 /* only when using TCP fast open */
53 union sockaddr_union tfo_address;
54 socklen_t tfo_salen;
55
56 #if ENABLE_DNS_OVER_TLS
57 DnsTlsStreamData dnstls_data;
58 int dnstls_events;
59 #endif
60
61 sd_event_source *io_event_source;
62 sd_event_source *timeout_event_source;
63
64 be16_t write_size, read_size;
65 DnsPacket *write_packet, *read_packet;
66 size_t n_written, n_read;
67 OrderedSet *write_queue;
68
69 int (*on_packet)(DnsStream *s);
70 int (*complete)(DnsStream *s, int error);
71
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 */
75
76 /* used when DNS-over-TLS is enabled */
77 bool encrypted:1;
78
79 DnsStubListenerExtra *stub_listener_extra;
80
81 LIST_FIELDS(DnsStream, streams);
82 };
83
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);
87 #endif
88 DnsStream *dns_stream_unref(DnsStream *s);
89 DnsStream *dns_stream_ref(DnsStream *s);
90
91 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref);
92
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);
95
96 static inline bool DNS_STREAM_QUEUED(DnsStream *s) {
97 assert(s);
98
99 if (s->fd < 0) /* already stopped? */
100 return false;
101
102 return !!s->write_packet;
103 }
104
105 DnsPacket *dns_stream_take_read_packet(DnsStream *s);
106
107 void dns_stream_detach(DnsStream *s);