Throw error exceptions if the recvfrom within SocketImpl::available causes an error.

This commit is contained in:
Andrew Auclair 2024-04-24 20:44:57 -04:00
parent 6998c66539
commit 5dabc6515c
No known key found for this signature in database
GPG Key ID: D9A9B2C2A07C3344
3 changed files with 32 additions and 0 deletions

View File

@ -653,6 +653,8 @@ int SocketImpl::available()
{
std::vector<char> buf(result);
result = recvfrom(sockfd(), &buf[0], result, MSG_PEEK, nullptr, nullptr);
if (result < 0) error();
}
#endif
return result;

View File

@ -804,6 +804,32 @@ void DatagramSocketTest::testGatherScatterVariableUNIX()
}
void DatagramSocketTest::testLocalUDPConnectionResetWin()
{
#if defined(POCO_OS_FAMILY_WINDOWS)
// create a local UDP socket and connect to another local port which doesn't have a UDP socket
DatagramSocket socket(SocketAddress("127.0.0.1", 0), true, true);
socket.connect(SocketAddress("127.0.0.1", 2345));
// send some data to the socket
unsigned char values[5]{};
socket.sendBytes(values, 5);
try
{
// available calls recvfrom which can cause a "Connection Reset by Peer" error for UDP sockets in Windows due to ICMP packets for remote destination not existing
socket.available();
fail();
}
catch (const Poco::Net::ConnectionResetException&)
{
}
#endif
}
void DatagramSocketTest::setUp()
{
}
@ -832,5 +858,7 @@ CppUnit::Test* DatagramSocketTest::suite()
CppUnit_addTest(pSuite, DatagramSocketTest, testGatherScatterFixed);
CppUnit_addTest(pSuite, DatagramSocketTest, testGatherScatterVariable);
CppUnit_addTest(pSuite, DatagramSocketTest, testLocalUDPConnectionResetWin);
return pSuite;
}

View File

@ -55,6 +55,8 @@ private:
void testGatherScatterSTRFFixedUNIX();
void testGatherScatterVariableUNIX();
void testGatherScatterSTRFVariableUNIX();
void testLocalUDPConnectionResetWin();
};