]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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 | |
652ba568 LP |
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 | ||
6016fcb0 IT |
27 | #define DNS_STREAM_WRITE_TLS_DATA 1 |
28 | ||
b30bf55d LP |
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 | ||
623a4c97 LP |
36 | struct DnsStream { |
37 | Manager *manager; | |
cf4b2f99 | 38 | unsigned n_ref; |
623a4c97 | 39 | |
652ba568 | 40 | DnsStreamType type; |
623a4c97 LP |
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; | |
b914e211 | 50 | bool identified; |
623a4c97 | 51 | |
91ccab1e IT |
52 | /* only when using TCP fast open */ |
53 | union sockaddr_union tfo_address; | |
54 | socklen_t tfo_salen; | |
55 | ||
56ddbf10 | 56 | #if ENABLE_DNS_OVER_TLS |
6016fcb0 | 57 | DnsTlsStreamData dnstls_data; |
ba6aaf57 | 58 | int dnstls_events; |
5d67a7ae IT |
59 | #endif |
60 | ||
623a4c97 LP |
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; | |
98767d75 | 67 | OrderedSet *write_queue; |
623a4c97 LP |
68 | |
69 | int (*on_packet)(DnsStream *s); | |
70 | int (*complete)(DnsStream *s, int error); | |
71 | ||
98767d75 IT |
72 | LIST_HEAD(DnsTransaction, transactions); /* when used by the transaction logic */ |
73 | DnsServer *server; /* when used by the transaction logic */ | |
b412af57 | 74 | Set *queries; /* when used by the DNS stub logic */ |
623a4c97 | 75 | |
5d67a7ae IT |
76 | /* used when DNS-over-TLS is enabled */ |
77 | bool encrypted:1; | |
78 | ||
0354029b LP |
79 | DnsStubListenerExtra *stub_listener_extra; |
80 | ||
623a4c97 LP |
81 | LIST_FIELDS(DnsStream, streams); |
82 | }; | |
83 | ||
652ba568 | 84 | int dns_stream_new(Manager *m, DnsStream **s, DnsStreamType type, DnsProtocol protocol, int fd, const union sockaddr_union *tfo_address); |
56ddbf10 | 85 | #if ENABLE_DNS_OVER_TLS |
6016fcb0 | 86 | int dns_stream_connect_tls(DnsStream *s, void *tls_session); |
5d67a7ae | 87 | #endif |
b30bf55d LP |
88 | DnsStream *dns_stream_unref(DnsStream *s); |
89 | DnsStream *dns_stream_ref(DnsStream *s); | |
623a4c97 | 90 | |
98767d75 IT |
91 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref); |
92 | ||
623a4c97 | 93 | int dns_stream_write_packet(DnsStream *s, DnsPacket *p); |
6016fcb0 | 94 | ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags); |
b30bf55d LP |
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 | } | |
aa337a5e LP |
104 | |
105 | DnsPacket *dns_stream_take_read_packet(DnsStream *s); | |
808089ae LP |
106 | |
107 | void dns_stream_detach(DnsStream *s); |