Merge pull request #19310 from ANR2MERefork/adhoc_socket

[Adhoc] Attempt to prevent sockets lingering in TIME_WAIT state after closure
This commit is contained in:
Henrik Rydgård 2024-06-30 01:11:02 +02:00 committed by GitHub
commit c737eca1a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 3 deletions

View File

@ -429,7 +429,11 @@ void deleteAllAdhocSockets() {
if (fd > 0) {
// Close Socket
shutdown(fd, SD_BOTH);
struct linger sl {};
sl.l_onoff = 1; // non-zero value enables linger option in kernel
sl.l_linger = 0; // timeout interval in seconds
setsockopt(fd, SOL_SOCKET, SO_LINGER, (const char*)&sl, sizeof(sl));
shutdown(fd, SD_RECEIVE);
closesocket(fd);
}
// Free Memory

View File

@ -111,6 +111,10 @@ inline bool isDisconnected(int errcode) { return (errcode == EPIPE || errcode ==
#define POLLPRI POLL_PRI
#endif
#ifndef SD_RECEIVE
#define SD_RECEIVE SHUT_RD //0x00
#endif
#ifndef SD_BOTH
#define SD_BOTH SHUT_RDWR //0x02
#endif

View File

@ -2212,7 +2212,7 @@ int NetAdhocPdp_Delete(int id, int unknown) {
sl.l_onoff = 1; // non-zero value enables linger option in kernel
sl.l_linger = 0; // timeout interval in seconds
setsockopt(sock->data.pdp.id, SOL_SOCKET, SO_LINGER, (const char*)&sl, sizeof(sl));
shutdown(sock->data.pdp.id, SD_BOTH);
shutdown(sock->data.pdp.id, SD_RECEIVE);
closesocket(sock->data.pdp.id);
// Remove Port Forward from Router
@ -3922,7 +3922,7 @@ int NetAdhocPtp_Close(int id, int unknown) {
sl.l_onoff = 1; // non-zero value enables linger option in kernel
sl.l_linger = 0; // timeout interval in seconds
setsockopt(socket->data.ptp.id, SOL_SOCKET, SO_LINGER, (const char*)&sl, sizeof(sl));
shutdown(socket->data.ptp.id, SD_BOTH);
shutdown(socket->data.ptp.id, SD_RECEIVE);
closesocket(socket->data.ptp.id);
// Remove Port Forward from Router