Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill out remote and local addresses on TCP server connection #4537

Merged
merged 5 commits into from
Sep 9, 2024
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
32 changes: 30 additions & 2 deletions src/platform/datapath_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ CxPlatSocketContextInitialize(
// Only set SO_REUSEPORT on a server socket, otherwise the client could be
// assigned a server port (unless it's forcing sharing).
//
if ((Config->Flags & CXPLAT_SOCKET_FLAG_SHARE || Config->RemoteAddress == NULL) &&
if ((Config->Flags & CXPLAT_SOCKET_FLAG_SHARE || Config->RemoteAddress == NULL) &&
SocketContext->Binding->Datapath->PartitionCount > 1) {
//
// The port is shared across processors.
Expand Down Expand Up @@ -1538,7 +1538,12 @@ CxPlatSocketContextAcceptCompletion(
goto Error;
}

SocketContext->AcceptSocket->SocketContexts[0].SocketFd = accept(SocketContext->SocketFd, NULL, NULL);
socklen_t AssignedRemoteAddressLength = sizeof(SocketContext->AcceptSocket->RemoteAddress);
SocketContext->AcceptSocket->SocketContexts[0].SocketFd =
accept(
SocketContext->SocketFd,
(struct sockaddr*)&SocketContext->AcceptSocket->RemoteAddress,
&AssignedRemoteAddressLength);
if (SocketContext->AcceptSocket->SocketContexts[0].SocketFd == INVALID_SOCKET) {
Status = errno;
QuicTraceEvent(
Expand All @@ -1550,6 +1555,29 @@ CxPlatSocketContextAcceptCompletion(
goto Error;
}

socklen_t AssignedLocalAddressLength = sizeof(SocketContext->AcceptSocket->LocalAddress);
int Result =
getsockname(
SocketContext->AcceptSocket->SocketContexts[0].SocketFd,
(struct sockaddr*)&SocketContext->AcceptSocket->LocalAddress,
&AssignedLocalAddressLength);
if (Result == SOCKET_ERROR) {
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
SocketContext->Binding,
Status,
"getsockname failed");
goto Error;
}

CxPlatConvertFromMappedV6(
&SocketContext->AcceptSocket->LocalAddress,
&SocketContext->AcceptSocket->LocalAddress);
CxPlatConvertFromMappedV6(
&SocketContext->AcceptSocket->RemoteAddress,
&SocketContext->AcceptSocket->RemoteAddress);

CxPlatSocketContextSetEvents(&SocketContext->AcceptSocket->SocketContexts[0], EPOLL_CTL_ADD, EPOLLIN);
SocketContext->AcceptSocket->SocketContexts[0].IoStarted = TRUE;
Datapath->TcpHandlers.Accept(
Expand Down
11 changes: 11 additions & 0 deletions src/platform/datapath_winuser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,17 @@ CxPlatDataPathSocketProcessAcceptCompletion(
SOCKET_PROCESSOR_AFFINITY RssAffinity = { 0 };
uint16_t PartitionIndex = 0;

ListenerSocketProc->AcceptSocket->LocalAddress =
*(const QUIC_ADDR*)ListenerSocketProc->AcceptAddrSpace;
ListenerSocketProc->AcceptSocket->RemoteAddress =
*(const QUIC_ADDR*)(ListenerSocketProc->AcceptAddrSpace + (sizeof(SOCKADDR_INET) + 16));
CxPlatConvertFromMappedV6(
&ListenerSocketProc->AcceptSocket->LocalAddress,
&ListenerSocketProc->AcceptSocket->LocalAddress);
CxPlatConvertFromMappedV6(
&ListenerSocketProc->AcceptSocket->RemoteAddress,
&ListenerSocketProc->AcceptSocket->RemoteAddress);

QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Expand Down
7 changes: 7 additions & 0 deletions src/platform/unittest/DataPathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,13 @@ TEST_P(DataPathTest, TcpConnect)
ASSERT_TRUE(CxPlatEventWaitWithTimeout(ClientContext.ConnectEvent, 500));
ASSERT_TRUE(CxPlatEventWaitWithTimeout(ListenerContext.AcceptEvent, 500));
ASSERT_NE(nullptr, ListenerContext.Server);
QUIC_ADDR ServerRemote = {};
CxPlatSocketGetRemoteAddress(ListenerContext.Server, &ServerRemote);
QUIC_ADDR ServerLocal = {};
CxPlatSocketGetLocalAddress(ListenerContext.Server, &ServerLocal);
ASSERT_NE(ServerRemote.Ipv4.sin_port, (uint16_t)0);
ASSERT_NE(ServerLocal.Ipv4.sin_port, (uint16_t)0);
ASSERT_EQ(ServerRemote.Ipv4.sin_port, Client.GetLocalAddress().Ipv4.sin_port);

ListenerContext.DeleteSocket();

Expand Down
Loading