master
h 50 lines 1.65 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2
3 #pragma once
4
5 #include <wil/resource.h>
6 #include "lxinitshared.h"
7 #include "SocketChannel.h"
8
9 namespace wsl::core::networking {
10
11 using DnsTunnelingCallback = std::function<void(const gsl::span<gsl::byte>, const LX_GNS_DNS_CLIENT_IDENTIFIER&)>;
12
13 class DnsTunnelingChannel
14 {
15 public:
16 DnsTunnelingChannel(wil::unique_socket&& socket, DnsTunnelingCallback&& reportDnsRequest);
17 ~DnsTunnelingChannel();
18
19 DnsTunnelingChannel(const DnsTunnelingChannel&) = delete;
20 DnsTunnelingChannel& operator=(const DnsTunnelingChannel&) = delete;
21
22 DnsTunnelingChannel(DnsTunnelingChannel&&) = delete;
23 DnsTunnelingChannel& operator=(DnsTunnelingChannel&&) = delete;
24
25 // Construct and send a LX_GNS_DNS_TUNNELING_MESSAGE message on the channel.
26 // Note: Callers are responsible for sequencing calls to this method.
27 //
28 // Arguments:
29 // dnsBuffer - buffer containing DNS response.
30 // dnsClientIdentifier - struct containing protocol (TCP/UDP) and unique id of the Linux DNS client making the request.
31 void SendDnsMessage(const gsl::span<gsl::byte> dnsBuffer, const LX_GNS_DNS_CLIENT_IDENTIFIER& dnsClientIdentifier) noexcept;
32
33 // Stop the channel.
34 void Stop() noexcept;
35
36 private:
37 // Wait for messages on the channel from Linux side.
38 void ReceiveLoop() noexcept;
39
40 wil::unique_event m_stopEvent{wil::EventOptions::ManualReset};
41
42 wsl::shared::SocketChannel m_channel;
43
44 std::thread m_receiveWorkerThread;
45
46 // Callback used to notify when there is a new DNS request message on the channel.
47 DnsTunnelingCallback m_reportDnsRequest;
48 };
49
50 } // namespace wsl::core::networking