Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions localization/strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,10 @@ Usage:
<data name="MessageWslaSessionNotFound" xml:space="preserve">
<value>Session not found: '{}'</value>
<comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
</data>
<data name="MessageInvalidIp" xml:space="preserve">
<value>Invalid IP address '{}'</value>
<comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
</data>
<data name="MessageWslaOpenSessionFailed" xml:space="preserve">
<value>OpenSessionByName('{}') failed</value>
Expand Down
8 changes: 5 additions & 3 deletions src/windows/WslcSDK/wslcsdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,13 @@ try

convertedPort.HostPort = internalPort.windowsPort;
convertedPort.ContainerPort = internalPort.containerPort;
// TODO: Only other supported value right now is AF_INET6; no user access.

// TODO: Ipv6 & custom binding address support.
convertedPort.Family = AF_INET;

// TODO: Unused protocol?
// TODO: Unused windowsAddress?
// TODO: Consider using standard protocol numbers instead of our own enum.
convertedPort.Protocol = internalPort.protocol == WSLC_PORT_PROTOCOL_TCP ? IPPROTO_TCP : IPPROTO_UDP;
convertedPort.BindingAddress = "127.0.0.1";
}
containerOptions.Ports = convertedPorts.get();
containerOptions.PortsCount = static_cast<ULONG>(internalContainerSettings->portsCount);
Expand Down
1 change: 0 additions & 1 deletion src/windows/WslcSDK/wslcsdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ typedef enum WslcPortProtocol

typedef struct WslcContainerPortMapping
{

_In_ uint16_t windowsPort; // Port on Windows host
_In_ uint16_t containerPort; // Port inside container
_In_ WslcPortProtocol protocol; // TCP or UDP
Expand Down
12 changes: 10 additions & 2 deletions src/windows/common/WSLAContainerLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,17 @@ WSLAContainerLauncher::WSLAContainerLauncher(
{
}

void WSLAContainerLauncher::AddPort(uint16_t WindowsPort, uint16_t ContainerPort, int Family)
void WSLAContainerLauncher::AddPort(uint16_t WindowsPort, uint16_t ContainerPort, int Family, int Protocol, const std::optional<std::string>& BindingAddress)
{
m_ports.emplace_back(WSLAPortMapping{.HostPort = WindowsPort, .ContainerPort = ContainerPort, .Family = Family});
THROW_HR_IF(E_INVALIDARG, Family != AF_INET && Family != AF_INET6);

auto& inserted = m_bindingAddressStorage.emplace_back(BindingAddress.value_or(Family == AF_INET ? "127.0.0.1" : "::1"));
m_ports.emplace_back(WSLAPortMapping{
.HostPort = WindowsPort,
.ContainerPort = ContainerPort,
.Family = Family,
.Protocol = Protocol,
.BindingAddress = inserted.c_str()});
}

void WSLAContainerLauncher::SetName(std::string&& Name)
Expand Down
3 changes: 2 additions & 1 deletion src/windows/common/WSLAContainerLauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class WSLAContainerLauncher : private WSLAProcessLauncher
WSLAProcessFlags Flags = WSLAProcessFlagsNone);

void AddVolume(const std::wstring& HostPath, const std::string& ContainerPath, bool ReadOnly);
void AddPort(uint16_t WindowsPort, uint16_t ContainerPort, int Family);
void AddPort(uint16_t WindowsPort, uint16_t ContainerPort, int Family, int Protocol = IPPROTO_TCP, const std::optional<std::string>& BindingAddress = {});
void AddLabel(const std::string& Key, const std::string& Value);
void AddTmpfs(const std::string& ContainerPath, const std::string& Options);

Expand All @@ -84,6 +84,7 @@ class WSLAContainerLauncher : private WSLAProcessLauncher
private:
std::string m_image;
std::string m_name;
std::deque<std::string> m_bindingAddressStorage;
std::vector<WSLAPortMapping> m_ports;
std::vector<WSLAVolume> m_volumes;
std::deque<std::wstring> m_hostPaths;
Expand Down
16 changes: 16 additions & 0 deletions src/windows/common/wslutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,22 @@ wil::unique_handle wsl::windows::common::wslutil::OpenCallingProcess(_In_ DWORD
return caller;
}

void wsl::windows::common::wslutil::ParseIpv4Address(const char* Address, in_addr& Result)
{
if (inet_pton(AF_INET, Address, &Result) != 1)
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidIp(Address));
}
}

void wsl::windows::common::wslutil::ParseIpv6Address(const char* Address, in_addr6& Result)
{
if (inet_pton(AF_INET6, Address, &Result) != 1)
{
THROW_HR_WITH_USER_ERROR(E_INVALIDARG, wsl::shared::Localization::MessageInvalidIp(Address));
}
}

std::tuple<uint32_t, uint32_t, uint32_t> wsl::windows::common::wslutil::ParseWslPackageVersion(_In_ const std::wstring& Version)
{
const std::wregex pattern(L"(\\d+)\\.(\\d+)\\.(\\d+).*");
Expand Down
4 changes: 4 additions & 0 deletions src/windows/common/wslutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ std::pair<wil::unique_hfile, wil::unique_hfile> OpenAnonymousPipe(DWORD Size, bo

wil::unique_handle OpenCallingProcess(_In_ DWORD access);

void ParseIpv4Address(const char* Address, in_addr& Result);

void ParseIpv6Address(const char* Address, in_addr6& Result);

std::tuple<uint32_t, uint32_t, uint32_t> ParseWslPackageVersion(_In_ const std::wstring& Version);

void PrintSystemError(_In_ HRESULT result, _Inout_ FILE* stream = stdout);
Expand Down
6 changes: 4 additions & 2 deletions src/windows/service/inc/wslaservice.idl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ typedef struct _WSLAPortMapping
USHORT HostPort;
USHORT ContainerPort;
int Family;
int Protocol;
[string] LPCSTR BindingAddress;
} WSLAPortMapping;

typedef struct _WSLATmpfsMount
Expand Down Expand Up @@ -542,8 +544,8 @@ interface IWSLASession : IUnknown
// Used only for testing. TODO: Think about moving them to a dedicated testing-only interface.
HRESULT MountWindowsFolder([in, ref] LPCWSTR WindowsPath, [in, ref] LPCSTR LinuxPath, [in] BOOL ReadOnly);
HRESULT UnmountWindowsFolder([in, ref] LPCSTR LinuxPath);
HRESULT MapVmPort([in] int Family, [in] short WindowsPort, [in] short LinuxPort);
HRESULT UnmapVmPort([in] int Family, [in] short WindowsPort, [in] short LinuxPort);
HRESULT MapVmPort([in] int Family, [in] unsigned short WindowsPort, [in] unsigned short LinuxPort);
HRESULT UnmapVmPort([in] int Family, [in] unsigned short WindowsPort, [in] unsigned short LinuxPort);

// Session initialization - called by SYSTEM service after launching per-user process.
// Returns a handle to this COM server process (used to add to job object).
Expand Down
Loading