]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
623a4c97 LP |
2 | #pragma once |
3 | ||
623a4c97 LP |
4 | #include "socket-util.h" |
5 | ||
6 | typedef struct DnsStream DnsStream; | |
7 | ||
652ba568 LP |
8 | typedef enum DnsStreamType { |
9 | DNS_STREAM_LOOKUP, /* Outgoing connection to a classic DNS server */ | |
10 | DNS_STREAM_LLMNR_SEND, /* Outgoing LLMNR TCP lookup */ | |
11 | DNS_STREAM_LLMNR_RECV, /* Incoming LLMNR TCP lookup */ | |
12 | DNS_STREAM_STUB, /* Incoming DNS stub connection */ | |
13 | _DNS_STREAM_TYPE_MAX, | |
14 | _DNS_STREAM_TYPE_INVALID = -1, | |
15 | } DnsStreamType; | |
16 | ||
ec2c5e43 LP |
17 | #include "resolved-dns-packet.h" |
18 | #include "resolved-dns-transaction.h" | |
07f264e4 | 19 | #include "resolved-manager.h" |
56ddbf10 | 20 | #if ENABLE_DNS_OVER_TLS |
6016fcb0 | 21 | #include "resolved-dnstls.h" |
5d67a7ae IT |
22 | #endif |
23 | ||
6016fcb0 IT |
24 | #define DNS_STREAM_WRITE_TLS_DATA 1 |
25 | ||
b30bf55d LP |
26 | /* Streams are used by three subsystems: |
27 | * | |
28 | * 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP | |
29 | * 2. The LLMNR logic when accepting a TCP-based lookup | |
30 | * 3. The DNS stub logic when accepting a TCP-based lookup | |
31 | */ | |
32 | ||
623a4c97 LP |
33 | struct DnsStream { |
34 | Manager *manager; | |
cf4b2f99 | 35 | unsigned n_ref; |
623a4c97 | 36 | |
652ba568 | 37 | DnsStreamType type; |
623a4c97 LP |
38 | DnsProtocol protocol; |
39 | ||
40 | int fd; | |
41 | union sockaddr_union peer; | |
42 | socklen_t peer_salen; | |
43 | union sockaddr_union local; | |
44 | socklen_t local_salen; | |
45 | int ifindex; | |
46 | uint32_t ttl; | |
b914e211 | 47 | bool identified; |
623a4c97 | 48 | |
91ccab1e IT |
49 | /* only when using TCP fast open */ |
50 | union sockaddr_union tfo_address; | |
51 | socklen_t tfo_salen; | |
52 | ||
56ddbf10 | 53 | #if ENABLE_DNS_OVER_TLS |
6016fcb0 | 54 | DnsTlsStreamData dnstls_data; |
ba6aaf57 | 55 | int dnstls_events; |
5d67a7ae IT |
56 | #endif |
57 | ||
623a4c97 LP |
58 | sd_event_source *io_event_source; |
59 | sd_event_source *timeout_event_source; | |
60 | ||
61 | be16_t write_size, read_size; | |
62 | DnsPacket *write_packet, *read_packet; | |
63 | size_t n_written, n_read; | |
98767d75 | 64 | OrderedSet *write_queue; |
623a4c97 LP |
65 | |
66 | int (*on_packet)(DnsStream *s); | |
67 | int (*complete)(DnsStream *s, int error); | |
68 | ||
98767d75 IT |
69 | LIST_HEAD(DnsTransaction, transactions); /* when used by the transaction logic */ |
70 | DnsServer *server; /* when used by the transaction logic */ | |
51bc63fe | 71 | DnsQuery *query; /* when used by the DNS stub logic */ |
623a4c97 | 72 | |
5d67a7ae IT |
73 | /* used when DNS-over-TLS is enabled */ |
74 | bool encrypted:1; | |
75 | ||
623a4c97 LP |
76 | LIST_FIELDS(DnsStream, streams); |
77 | }; | |
78 | ||
652ba568 | 79 | int dns_stream_new(Manager *m, DnsStream **s, DnsStreamType type, DnsProtocol protocol, int fd, const union sockaddr_union *tfo_address); |
56ddbf10 | 80 | #if ENABLE_DNS_OVER_TLS |
6016fcb0 | 81 | int dns_stream_connect_tls(DnsStream *s, void *tls_session); |
5d67a7ae | 82 | #endif |
b30bf55d LP |
83 | DnsStream *dns_stream_unref(DnsStream *s); |
84 | DnsStream *dns_stream_ref(DnsStream *s); | |
623a4c97 | 85 | |
98767d75 IT |
86 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref); |
87 | ||
623a4c97 | 88 | int dns_stream_write_packet(DnsStream *s, DnsPacket *p); |
6016fcb0 | 89 | ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags); |
b30bf55d LP |
90 | |
91 | static inline bool DNS_STREAM_QUEUED(DnsStream *s) { | |
92 | assert(s); | |
93 | ||
94 | if (s->fd < 0) /* already stopped? */ | |
95 | return false; | |
96 | ||
97 | return !!s->write_packet; | |
98 | } | |
aa337a5e LP |
99 | |
100 | DnsPacket *dns_stream_take_read_packet(DnsStream *s); | |
808089ae LP |
101 | |
102 | void dns_stream_detach(DnsStream *s); |