]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
623a4c97 LP |
2 | #pragma once |
3 | ||
be28f72d DDM |
4 | #include "sd-event.h" |
5 | ||
6 | #include "ordered-set.h" | |
623a4c97 LP |
7 | #include "socket-util.h" |
8 | ||
be28f72d | 9 | typedef struct DnsServer DnsServer; |
623a4c97 | 10 | typedef struct DnsStream DnsStream; |
be28f72d DDM |
11 | typedef struct DnsTransaction DnsTransaction; |
12 | typedef struct Manager Manager; | |
0354029b | 13 | typedef struct DnsStubListenerExtra DnsStubListenerExtra; |
be28f72d DDM |
14 | |
15 | #include "resolved-dns-packet.h" | |
16 | #include "resolved-dnstls.h" | |
623a4c97 | 17 | |
e1158539 LP |
18 | /* Various timeouts for establishing TCP connections. First the default time-out for that. */ |
19 | #define DNS_STREAM_DEFAULT_TIMEOUT_USEC (10 * USEC_PER_SEC) | |
20 | ||
21 | /* In the DNS stub, be more friendly for incoming connections, than we are to ourselves for outgoing ones */ | |
22 | #define DNS_STREAM_STUB_TIMEOUT_USEC (30 * USEC_PER_SEC) | |
23 | ||
24 | /* In opportunistic TLS mode, lower timeouts */ | |
25 | #define DNS_STREAM_OPPORTUNISTIC_TLS_TIMEOUT_USEC (3 * USEC_PER_SEC) | |
26 | ||
27 | /* Once connections are established apply this timeout once nothing happens anymore */ | |
28 | #define DNS_STREAM_ESTABLISHED_TIMEOUT_USEC (10 * USEC_PER_SEC) | |
29 | ||
652ba568 LP |
30 | typedef enum DnsStreamType { |
31 | DNS_STREAM_LOOKUP, /* Outgoing connection to a classic DNS server */ | |
32 | DNS_STREAM_LLMNR_SEND, /* Outgoing LLMNR TCP lookup */ | |
33 | DNS_STREAM_LLMNR_RECV, /* Incoming LLMNR TCP lookup */ | |
34 | DNS_STREAM_STUB, /* Incoming DNS stub connection */ | |
35 | _DNS_STREAM_TYPE_MAX, | |
2d93c20e | 36 | _DNS_STREAM_TYPE_INVALID = -EINVAL, |
652ba568 LP |
37 | } DnsStreamType; |
38 | ||
6016fcb0 IT |
39 | #define DNS_STREAM_WRITE_TLS_DATA 1 |
40 | ||
b30bf55d LP |
41 | /* Streams are used by three subsystems: |
42 | * | |
43 | * 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP | |
44 | * 2. The LLMNR logic when accepting a TCP-based lookup | |
45 | * 3. The DNS stub logic when accepting a TCP-based lookup | |
46 | */ | |
47 | ||
623a4c97 LP |
48 | struct DnsStream { |
49 | Manager *manager; | |
cf4b2f99 | 50 | unsigned n_ref; |
623a4c97 | 51 | |
652ba568 | 52 | DnsStreamType type; |
623a4c97 LP |
53 | DnsProtocol protocol; |
54 | ||
55 | int fd; | |
56 | union sockaddr_union peer; | |
57 | socklen_t peer_salen; | |
58 | union sockaddr_union local; | |
59 | socklen_t local_salen; | |
60 | int ifindex; | |
61 | uint32_t ttl; | |
b914e211 | 62 | bool identified; |
a5e2a488 | 63 | bool packet_received; /* At least one packet is received. Used by LLMNR. */ |
eff10773 | 64 | uint32_t requested_events; |
623a4c97 | 65 | |
91ccab1e IT |
66 | /* only when using TCP fast open */ |
67 | union sockaddr_union tfo_address; | |
68 | socklen_t tfo_salen; | |
69 | ||
56ddbf10 | 70 | #if ENABLE_DNS_OVER_TLS |
6016fcb0 | 71 | DnsTlsStreamData dnstls_data; |
eff10773 | 72 | uint32_t dnstls_events; |
5d67a7ae IT |
73 | #endif |
74 | ||
623a4c97 LP |
75 | sd_event_source *io_event_source; |
76 | sd_event_source *timeout_event_source; | |
77 | ||
78 | be16_t write_size, read_size; | |
79 | DnsPacket *write_packet, *read_packet; | |
80 | size_t n_written, n_read; | |
98767d75 | 81 | OrderedSet *write_queue; |
623a4c97 | 82 | |
624f907e | 83 | int (*on_packet)(DnsStream *s, DnsPacket *p); |
623a4c97 LP |
84 | int (*complete)(DnsStream *s, int error); |
85 | ||
98767d75 IT |
86 | LIST_HEAD(DnsTransaction, transactions); /* when used by the transaction logic */ |
87 | DnsServer *server; /* when used by the transaction logic */ | |
b412af57 | 88 | Set *queries; /* when used by the DNS stub logic */ |
623a4c97 | 89 | |
5d67a7ae IT |
90 | /* used when DNS-over-TLS is enabled */ |
91 | bool encrypted:1; | |
92 | ||
0354029b LP |
93 | DnsStubListenerExtra *stub_listener_extra; |
94 | ||
623a4c97 LP |
95 | LIST_FIELDS(DnsStream, streams); |
96 | }; | |
97 | ||
18230451 YW |
98 | int dns_stream_new( |
99 | Manager *m, | |
100 | DnsStream **ret, | |
101 | DnsStreamType type, | |
102 | DnsProtocol protocol, | |
103 | int fd, | |
104 | const union sockaddr_union *tfo_address, | |
624f907e | 105 | int (on_packet)(DnsStream*, DnsPacket*), |
18230451 YW |
106 | int (complete)(DnsStream*, int), /* optional */ |
107 | usec_t connect_timeout_usec); | |
56ddbf10 | 108 | #if ENABLE_DNS_OVER_TLS |
6016fcb0 | 109 | int dns_stream_connect_tls(DnsStream *s, void *tls_session); |
5d67a7ae | 110 | #endif |
b30bf55d LP |
111 | DnsStream *dns_stream_unref(DnsStream *s); |
112 | DnsStream *dns_stream_ref(DnsStream *s); | |
623a4c97 | 113 | |
98767d75 IT |
114 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref); |
115 | ||
623a4c97 | 116 | int dns_stream_write_packet(DnsStream *s, DnsPacket *p); |
6016fcb0 | 117 | ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags); |
b30bf55d LP |
118 | |
119 | static inline bool DNS_STREAM_QUEUED(DnsStream *s) { | |
120 | assert(s); | |
121 | ||
122 | if (s->fd < 0) /* already stopped? */ | |
123 | return false; | |
124 | ||
125 | return !!s->write_packet; | |
126 | } | |
aa337a5e | 127 | |
808089ae | 128 | void dns_stream_detach(DnsStream *s); |
7addc530 | 129 | int dns_stream_disconnect_all(Manager *m); |