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

[core] Fixed thread safety using WSAOVERLAPPED in WSASendTo (Update) #2929

Merged
merged 2 commits into from
Apr 17, 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
72 changes: 43 additions & 29 deletions srtcore/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,6 @@ srt::CChannel::CChannel()
, m_bBindMasked(true)
#endif
{
#ifdef _WIN32
SecureZeroMemory((PVOID)&m_SendOverlapped, sizeof(WSAOVERLAPPED));
m_SendOverlapped.hEvent = WSACreateEvent();
if (m_SendOverlapped.hEvent == NULL) {
LOGC(kmlog.Error, log << CONID() << "IPE: WSACreateEvent failed with error: " << NET_ERROR);
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR);
}
#endif
#ifdef SRT_ENABLE_PKTINFO
// Do the check for ancillary data buffer size, kinda assertion
static const size_t CMSG_MAX_SPACE = sizeof (CMSGNodeIPv4) + sizeof (CMSGNodeIPv6);
Expand All @@ -165,12 +157,7 @@ srt::CChannel::CChannel()
#endif
}

srt::CChannel::~CChannel()
{
#ifdef _WIN32
WSACloseEvent(m_SendOverlapped.hEvent);
#endif
}
srt::CChannel::~CChannel() {}

void srt::CChannel::createSocket(int family)
{
Expand Down Expand Up @@ -787,34 +774,61 @@ int srt::CChannel::sendto(const sockaddr_any& addr, CPacket& packet, const socka

const int res = (int)::sendmsg(m_iSocket, &mh, 0);
#else
DWORD size = (DWORD)(CPacket::HDR_SIZE + packet.getLength());
int addrsize = addr.size();
class WSAEventRef
{
public:
WSAEventRef()
: e(::WSACreateEvent())
{
}
~WSAEventRef()
{
::WSACloseEvent(e);
e = NULL;
}
void reset()
{
::WSAResetEvent(e);
}
WSAEVENT Handle()
{
return e;
}

private:
WSAEVENT e;
};
#if !defined(__MINGW32__) && defined(ENABLE_CXX11)
thread_local WSAEventRef lEvent;
#else
WSAEventRef lEvent;
#endif
WSAOVERLAPPED overlapped;
::SecureZeroMemory(&overlapped, sizeof(overlapped));
overlapped.hEvent = lEvent.Handle();

int res = ::WSASendTo(m_iSocket, (LPWSABUF)packet.m_PacketVector, 2, &size, 0, addr.get(), addrsize, &m_SendOverlapped, NULL);
DWORD size = (DWORD)(packet.m_PacketVector[0].size() + packet.m_PacketVector[1].size());
int addrsize = addr.size();
int res = ::WSASendTo(m_iSocket, (LPWSABUF)packet.m_PacketVector, 2, &size, 0, addr.get(), addrsize, &overlapped, NULL);

if (res == SOCKET_ERROR)
{
if (NET_ERROR == WSA_IO_PENDING)
{
DWORD res_wait = WSAWaitForMultipleEvents(1, &m_SendOverlapped.hEvent, TRUE, 100 /*ms*/, FALSE);
if (res_wait == WAIT_FAILED)
{
LOGC(kslog.Warn, log << "CChannel::WSAWaitForMultipleEvents: failed with " << NET_ERROR);
res = -1;
}
DWORD dwFlags = 0;
const bool bCompleted = WSAGetOverlappedResult(m_iSocket, &overlapped, &size, TRUE, &dwFlags);
if (bCompleted)
res = 0;
else
{
DWORD dwFlags = 0;
const bool bCompleted = WSAGetOverlappedResult(m_iSocket, &m_SendOverlapped, &size, false, &dwFlags);
res = bCompleted ? 0 : -1;
}
LOGC(kslog.Warn, log << "CChannel::sendto call on ::WSAGetOverlappedResult failed with error: " << NET_ERROR);
lEvent.reset();
}
else
{
LOGC(kmlog.Error, log << CONID() << "WSASendTo failed with error: " << NET_ERROR);
}
}
WSAResetEvent(m_SendOverlapped.hEvent);

res = (0 == res) ? size : -1;
#endif

Expand Down
3 changes: 0 additions & 3 deletions srtcore/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ class CChannel

private:
UDPSOCKET m_iSocket; // socket descriptor
#ifdef _WIN32
mutable WSAOVERLAPPED m_SendOverlapped;
#endif

// Mutable because when querying original settings
// this comprises the cache for extracted values,
Expand Down
Loading