| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | #include <iostream> |
| 4 | #include <filesystem> |
| 5 | #include <fstream> |
| 6 | #include <netinet/in.h> |
| 7 | #include "common.h" |
| 8 | #include "DnsTunnelingManager.h" |
| 9 | |
| 10 | DnsTunnelingManager::DnsTunnelingManager(int hvsocketFd, const std::string& dnsTunnelingIpAddress) : |
| 11 | m_dnsChannel( |
| 12 | hvsocketFd, |
| 13 | [this](const gsl::span<gsl::byte> dnsBuffer, const LX_GNS_DNS_CLIENT_IDENTIFIER& dnsClientIdentifier) { |
| 14 | m_dnsServer.HandleDnsResponse(dnsBuffer, dnsClientIdentifier); |
| 15 | }), |
| 16 | m_dnsServer([this](const gsl::span<gsl::byte> dnsBuffer, const LX_GNS_DNS_CLIENT_IDENTIFIER& dnsClientIdentifier) { |
| 17 | if (m_stopped) |
| 18 | { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | m_dnsChannel.SendDnsMessage(dnsBuffer, dnsClientIdentifier); |
| 23 | }) |
| 24 | { |
| 25 | GNS_LOG_INFO("Using DNS server IP {}", dnsTunnelingIpAddress.c_str()); |
| 26 | |
| 27 | // Start DNS server used for tunneling. Server has both TCP and UDP support. |
| 28 | // |
| 29 | // Note: because DnsTunnelingManager runs as part of GNS daemon, which is started before GnsPortTracker, binding the DNS |
| 30 | // server will not be intercepted by the bind seccomp hook. This is ok because in FSE mode there is no need for host<->guest |
| 31 | // loopback communication to/from the DNS server (all traffic to/from DNS server will stay in the container). |
| 32 | m_dnsServer.Start(dnsTunnelingIpAddress); |
| 33 | } |
| 34 | |
| 35 | DnsTunnelingManager::~DnsTunnelingManager() |
| 36 | { |
| 37 | // Scoped m_dnsLock |
| 38 | { |
| 39 | // Set flag to signal object is stopping |
| 40 | m_stopped = true; |
| 41 | } |
| 42 | |
| 43 | // Stop channel first as it can call into the DNS server object |
| 44 | m_dnsChannel.Stop(); |
| 45 | |
| 46 | // Stop DNS server |
| 47 | m_dnsServer.Stop(); |
| 48 | } |