]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-stream.h
Merge pull request #9193 from keszybz/coverity
[thirdparty/systemd.git] / src / resolve / resolved-dns-stream.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2014 Lennart Poettering
8 ***/
9
10 #include "socket-util.h"
11
12 typedef struct DnsStream DnsStream;
13
14 #include "resolved-dns-packet.h"
15 #include "resolved-dns-transaction.h"
16 #include "resolved-manager.h"
17
18 #if HAVE_GNUTLS
19 #include <gnutls/gnutls.h>
20 #endif
21
22 /* Streams are used by three subsystems:
23 *
24 * 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP
25 * 2. The LLMNR logic when accepting a TCP-based lookup
26 * 3. The DNS stub logic when accepting a TCP-based lookup
27 */
28
29 struct DnsStream {
30 Manager *manager;
31 int n_ref;
32
33 DnsProtocol protocol;
34
35 int fd;
36 union sockaddr_union peer;
37 socklen_t peer_salen;
38 union sockaddr_union local;
39 socklen_t local_salen;
40 int ifindex;
41 uint32_t ttl;
42 bool identified;
43
44 /* only when using TCP fast open */
45 union sockaddr_union tfo_address;
46 socklen_t tfo_salen;
47
48 #if HAVE_GNUTLS
49 gnutls_session_t tls_session;
50 int tls_handshake;
51 bool tls_bye;
52 #endif
53
54 sd_event_source *io_event_source;
55 sd_event_source *timeout_event_source;
56
57 be16_t write_size, read_size;
58 DnsPacket *write_packet, *read_packet;
59 size_t n_written, n_read;
60 OrderedSet *write_queue;
61
62 int (*on_connection)(DnsStream *s);
63 int (*on_packet)(DnsStream *s);
64 int (*complete)(DnsStream *s, int error);
65
66 LIST_HEAD(DnsTransaction, transactions); /* when used by the transaction logic */
67 DnsServer *server; /* when used by the transaction logic */
68 DnsQuery *query; /* when used by the DNS stub logic */
69
70 /* used when DNS-over-TLS is enabled */
71 bool encrypted:1;
72
73 LIST_FIELDS(DnsStream, streams);
74 };
75
76 int dns_stream_new(Manager *m, DnsStream **s, DnsProtocol protocol, int fd, const union sockaddr_union *tfo_address);
77 #if HAVE_GNUTLS
78 int dns_stream_connect_tls(DnsStream *s, gnutls_session_t tls_session);
79 #endif
80 DnsStream *dns_stream_unref(DnsStream *s);
81 DnsStream *dns_stream_ref(DnsStream *s);
82
83 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref);
84
85 int dns_stream_write_packet(DnsStream *s, DnsPacket *p);
86
87 static inline bool DNS_STREAM_QUEUED(DnsStream *s) {
88 assert(s);
89
90 if (s->fd < 0) /* already stopped? */
91 return false;
92
93 return !!s->write_packet;
94 }