| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <functional> |
| 6 | #include <optional> |
| 7 | #include <tuple> |
| 8 | #include "util.h" |
| 9 | #include "lxinitshared.h" |
| 10 | #include "NetworkManager.h" |
| 11 | #include "DnsTunnelingManager.h" |
| 12 | #include "hns_schema.h" |
| 13 | |
| 14 | class GnsEngine |
| 15 | { |
| 16 | public: |
| 17 | struct Message |
| 18 | { |
| 19 | LX_MESSAGE_TYPE MessageType; |
| 20 | std::string Json; |
| 21 | std::optional<GUID> AdapterId; |
| 22 | }; |
| 23 | |
| 24 | using NotificationRoutine = std::function<std::optional<Message>(wsl::shared::Transaction&)>; |
| 25 | using StatusRoutine = std::function<void(int, const std::string&, wsl::shared::Transaction&)>; |
| 26 | |
| 27 | GnsEngine( |
| 28 | wsl::shared::SocketChannel& channel, |
| 29 | const NotificationRoutine& notificationRoutine, |
| 30 | const StatusRoutine& statusRoutine, |
| 31 | NetworkManager& manager, |
| 32 | std::optional<int> dnsTunnelingFd, |
| 33 | const std::string& dnsTunnelingIpAddress); |
| 34 | |
| 35 | void setup(); |
| 36 | |
| 37 | void run(); |
| 38 | |
| 39 | private: |
| 40 | std::tuple<bool, int> ProcessNextMessage(wsl::shared::Transaction& transaction); |
| 41 | |
| 42 | void ProcessNotification(const nlohmann::json& payload, Interface& interface); |
| 43 | |
| 44 | void ProcessRouteChange(Interface& interface, const wsl::shared::hns::Route& route, wsl::shared::hns::ModifyRequestType type); |
| 45 | |
| 46 | void ProcessIpAddressChange(Interface& interface, const wsl::shared::hns::IPAddress& route, wsl::shared::hns::ModifyRequestType type); |
| 47 | |
| 48 | void ProcessMacAddressChange(Interface& interface, const wsl::shared::hns::MacAddress& mac, wsl::shared::hns::ModifyRequestType type); |
| 49 | |
| 50 | void ProcessDNSChange(Interface& interface, const wsl::shared::hns::DNS& dns, wsl::shared::hns::ModifyRequestType type); |
| 51 | |
| 52 | void ProcessLinkChange(Interface& interface, const wsl::shared::hns::NetworkInterface& link, wsl::shared::hns::ModifyRequestType type); |
| 53 | |
| 54 | static Interface OpenAdapter(const GUID& id); |
| 55 | |
| 56 | static Interface OpenAdapterImpl(const GUID& id); |
| 57 | |
| 58 | static Interface OpenInterface(const std::string& deviceName); |
| 59 | |
| 60 | static Interface OpenInterfaceImpl(const std::string& deviceName); |
| 61 | |
| 62 | static Interface OpenInterfaceOrAdapter(const std::wstring& nameOrId); |
| 63 | |
| 64 | static std::optional<GUID> GetAdapterId(const std::string& name); |
| 65 | |
| 66 | template <typename T> |
| 67 | static T Deserialize(const std::string& json); |
| 68 | |
| 69 | template <typename T> |
| 70 | void ProcessNotificationImpl( |
| 71 | Interface& interface, const nlohmann::json& payload, void (GnsEngine::*routine)(Interface&, const T&, wsl::shared::hns::ModifyRequestType)); |
| 72 | |
| 73 | wsl::shared::SocketChannel& channel; |
| 74 | const NotificationRoutine& notificationRoutine; |
| 75 | const StatusRoutine& statusRoutine; |
| 76 | NetworkManager& manager; |
| 77 | |
| 78 | std::optional<DnsTunnelingManager> dnsTunnelingManager; |
| 79 | }; |