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