Skip to content

Commit 5e666a3

Browse files
authored
Fail bind() requests that fail in Windows (#41856)
1 parent 52143d8 commit 5e666a3

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎src/windows/common/ConsommeNetworking.cpp‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,30 @@ void ConsommeNetworking::StartPortTracker(wil::unique_socket&& socket)
195195
m_gnsPortTrackerChannel.emplace(
196196
std::move(socket),
197197
[&](const SOCKADDR_INET& addr, int protocol, bool allocate) {
198-
return wil::ResultFromException([&]() {
198+
const auto result = wil::ResultFromException([&]() {
199199
HandlePortNotification(addr, protocol, INETADDR_PORT(reinterpret_cast<const SOCKADDR*>(&addr)), allocate);
200200
});
201+
202+
LOG_IF_FAILED_MSG(
203+
result,
204+
"Port mapping failed: address=%hs port=%u protocol=%d allocate=%d",
205+
wsl::windows::common::string::SockAddrInetToString(addr).c_str(),
206+
INETADDR_PORT(reinterpret_cast<const SOCKADDR*>(&addr)),
207+
protocol,
208+
allocate);
209+
210+
switch (result)
211+
{
212+
case S_OK:
213+
return 0;
214+
215+
case HRESULT_FROM_WIN32(WSAEADDRINUSE):
216+
case HRESULT_FROM_WIN32(WSAEACCES):
217+
return -LX_EADDRINUSE;
218+
219+
default:
220+
return -LX_EINVAL;
221+
}
201222
},
202223
[](const std::string&, bool) {}); // TODO: reconsider if InterfaceStateCallback is needed.
203224
}

‎test/windows/wsl/NetworkTests.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5842,6 +5842,19 @@ class ConsommeTests
58425842
// Make sure the VM doesn't time out
58435843
WslKeepAlive keepAlive;
58445844

5845+
{
5846+
auto hostPort = NetworkTests::BindHostPort(1234, SOCK_STREAM, IPPROTO_TCP, true, false, false, std::chrono::minutes(2));
5847+
VERIFY_ARE_EQUAL(listen(hostPort.get(), SOMAXCONN), 0);
5848+
VERIFY_ARE_EQUAL(
5849+
LxsstuLaunchWsl(L"perl -MSocket -MErrno=EADDRINUSE -e '"
5850+
L"socket(my $s,AF_INET,SOCK_STREAM,0) or die \"socket: $!\\n\";"
5851+
L"bind($s,sockaddr_in(1234,INADDR_ANY)) and die \"bind unexpectedly succeeded\\n\";"
5852+
L"$!{EADDRINUSE} or die \"expected EADDRINUSE: $!\\n\";"
5853+
L"my ($port)=sockaddr_in(getsockname($s));"
5854+
L"$port==0 or die \"failed bind assigned port $port\\n\";'"),
5855+
0L);
5856+
}
5857+
58455858
{
58465859
auto guestProcess = NetworkTests::BindGuestPort(L"TCP4-LISTEN:1234", true);
58475860
NetworkTests::BindHostPort(1234, SOCK_STREAM, IPPROTO_TCP, false);

0 commit comments

Comments
 (0)