| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | localhost.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains localhost port relay function declarations. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | |
| 17 | #include <winsock2.h> |
| 18 | #include <map> |
| 19 | #include <string_view> |
| 20 | #include <tuple> |
| 21 | #include "SocketChannel.h" |
| 22 | |
| 23 | typedef struct _LX_PORT_LISTENER_THREAD_CONTEXT |
| 24 | { |
| 25 | GUID VmId; |
| 26 | unsigned short Family; |
| 27 | unsigned short Port; |
| 28 | int HvSocketPort; |
| 29 | int Count = 1; |
| 30 | wil::unique_event ExitEvent; |
| 31 | wil::unique_socket ListenSocket; |
| 32 | } LX_PORT_LISTENER_THREAD_CONTEXT, *PLX_PORT_LISTENER_THREAD_CONTEXT; |
| 33 | |
| 34 | typedef struct _LX_PORT_LISTENER_CONTEXT |
| 35 | { |
| 36 | ~_LX_PORT_LISTENER_CONTEXT() |
| 37 | { |
| 38 | if (Worker.joinable()) |
| 39 | { |
| 40 | Worker.join(); |
| 41 | } |
| 42 | }; |
| 43 | std::shared_ptr<LX_PORT_LISTENER_THREAD_CONTEXT> ThreadContext; |
| 44 | std::thread Worker; |
| 45 | } LX_PORT_LISTENER_CONTEXT, *PLX_PORT_LISTENER_CONTEXT; |
| 46 | |
| 47 | namespace wsl::windows::wslrelay::localhost { |
| 48 | void RelayWorker(_In_ wsl::shared::SocketChannel& SocketChannel, _In_ const GUID& VmId); |
| 49 | |
| 50 | void RunWSLCPortRelay(const GUID& VmId, uint32_t RelayPort, HANDLE ExitEvent); |
| 51 | |
| 52 | class Relay |
| 53 | { |
| 54 | public: |
| 55 | ~Relay(); |
| 56 | |
| 57 | int StartPortListener(_In_ const GUID& VmId, _In_ unsigned short Family, _In_ unsigned short Port, _In_ int HvSocketPort); |
| 58 | |
| 59 | void StopPortListener(_In_ unsigned short Family, _In_ unsigned short Port); |
| 60 | |
| 61 | private: |
| 62 | wil::srwlock m_lock; |
| 63 | _Guarded_by_(m_lock) std::map<std::tuple<unsigned short, unsigned short>, std::shared_ptr<LX_PORT_LISTENER_CONTEXT>> m_RelayThreads; |
| 64 | }; |
| 65 | } // namespace wsl::windows::wslrelay::localhost |