master
h 94 lines 3.72 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2
3 #pragma once
4
5 #include "INetworkingEngine.h"
6 #include "GnsChannel.h"
7 #include "WslCoreHostDnsInfo.h"
8 #include "GnsPortTrackerChannel.h"
9 #include "GuestDeviceManager.h"
10
11 namespace wsl::core {
12
13 enum class ConsommeNetworkingFlags
14 {
15 None = 0x0,
16 LocalhostRelay = 0x1,
17 DnsTunneling = 0x2,
18 Ipv6 = 0x4,
19 LoopbackClientIp = 0x8,
20 };
21 DEFINE_ENUM_FLAG_OPERATORS(ConsommeNetworkingFlags);
22
23 class ConsommeNetworking : public INetworkingEngine
24 {
25 public:
26 ConsommeNetworking(
27 GnsChannel&& gnsChannel,
28 ConsommeNetworkingFlags flags,
29 LPCWSTR dnsOptions,
30 LPCSTR hostLoopback,
31 std::shared_ptr<GuestDeviceManager> guestDeviceManager,
32 wil::shared_handle userToken);
33
34 ~ConsommeNetworking() override;
35
36 // Note: This class cannot be moved because m_networkNotifyHandle captures a 'this' pointer.
37 ConsommeNetworking(const ConsommeNetworking&) = delete;
38 ConsommeNetworking(ConsommeNetworking&&) = delete;
39 ConsommeNetworking& operator=(const ConsommeNetworking&) = delete;
40 ConsommeNetworking& operator=(ConsommeNetworking&&) = delete;
41
42 // INetworkingEngine
43 void Initialize() override;
44 void TraceLoggingRundown() noexcept override;
45 void FillInitialConfiguration(LX_MINI_INIT_NETWORKING_CONFIGURATION& message) override;
46 void StartPortTracker(wil::unique_socket&& socket) override;
47
48 HRESULT MapPort(_In_ const SOCKADDR_INET& ListenAddress, _In_ USHORT GuestPort, _In_ int Protocol, _Out_ USHORT* AllocatedHostPort) const;
49
50 HRESULT UnmapPort(_In_ const SOCKADDR_INET& ListenAddress, _In_ USHORT GuestPort, _In_ int Protocol) const;
51
52 private:
53 static void NETIOAPI_API_ OnNetworkConnectivityChange(PVOID context, NL_NETWORK_CONNECTIVITY_HINT hint);
54
55 uint16_t HandlePortNotification(const SOCKADDR_INET& addr, int protocol, uint16_t guestPort, bool allocate) const;
56 uint16_t ModifyOpenPorts(
57 _In_ PCWSTR tag, _In_ const SOCKADDR_INET& hostAddress, _In_ uint16_t HostPort, _In_ uint16_t GuestPort, _In_ int protocol, _In_ bool isOpen) const;
58 void RefreshGuestConnection();
59 void SetupHostLoopback();
60 void SetupLoopbackDevice();
61 void SendDefaultRoute(const std::wstring& gateway, wsl::shared::hns::ModifyRequestType requestType);
62 void SendIpv6Address(const networking::EndpointIpAddress& ipAddress, wsl::shared::hns::ModifyRequestType requestType);
63 void UpdateDefaultRoute(const std::wstring& gateway);
64 void UpdateDnsSettings(const networking::DnsInfo& dns);
65 void UpdateIpv4Address(const networking::EndpointIpAddress& ipAddress);
66 void UpdateIpv6Address(const networking::EndpointIpAddress& ipAddress);
67 void UpdateMtu(std::optional<ULONG> mtu);
68
69 mutable wil::srwlock m_lock;
70
71 std::shared_ptr<GuestDeviceManager> m_guestDeviceManager;
72 wil::shared_handle m_userToken;
73 GnsChannel m_gnsChannel;
74 std::optional<GnsPortTrackerChannel> m_gnsPortTrackerChannel;
75 std::shared_ptr<networking::NetworkSettings> m_networkSettings;
76 ConsommeNetworkingFlags m_flags = ConsommeNetworkingFlags::None;
77 LPCWSTR m_dnsOptions = nullptr;
78 std::string m_hostLoopback;
79 std::optional<GUID> m_localhostAdapterId;
80 std::optional<GUID> m_adapterId;
81 std::optional<WslVirtioNetConfig> m_virtioNetConfig;
82 std::vector<IpAddress> m_virtioNetNameservers;
83
84 ULONG m_networkMtu = 0;
85 networking::EndpointIpAddress m_trackedIpv4Address{};
86 networking::EndpointIpAddress m_trackedIpv6Address{};
87 std::wstring m_trackedDefaultRoute;
88 networking::DnsInfo m_trackedDnsSettings{};
89
90 // Note: this field must be destroyed first to stop the callbacks before any other field is destroyed.
91 networking::unique_notify_handle m_networkNotifyHandle;
92 };
93
94 } // namespace wsl::core