Ignore ICMP Port Unreachable messages during STUN

This commit is contained in:
Cameron Gutman 2020-09-06 15:12:16 -07:00
parent cdbaa0aff3
commit 83360d775b

View File

@ -100,6 +100,7 @@ bool getExternalAddressPortIP4(unsigned short localPort, PSOCKADDR_IN wanAddr)
} }
} }
for (;;) {
fd_set fds; fd_set fds;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
@ -110,19 +111,26 @@ bool getExternalAddressPortIP4(unsigned short localPort, PSOCKADDR_IN wanAddr)
int selectRes = select(0, &fds, nullptr, nullptr, &tv); int selectRes = select(0, &fds, nullptr, nullptr, &tv);
if (selectRes == 0) { if (selectRes == 0) {
// Timeout - continue looping // Timeout - break to the enclosing loop
continue; break;
} }
else if (selectRes == SOCKET_ERROR) { else if (selectRes == SOCKET_ERROR) {
fprintf(LOG_OUT, "select() failed: %d\n", WSAGetLastError()); fprintf(LOG_OUT, "select() failed: %d\n", WSAGetLastError());
goto Exit; goto Exit;
} }
// Error handling is below // recvfrom() will return WSAECONNRESET if the socket has received an ICMP Port Unreachable
// message from one of the IP addresses we've attempted communication with. The socket is still
// operable (and may even contain queued messages to receive). We should ignore that error and
// continue reading, otherwise exit the loop.
bytesRead = recvfrom(sock, resp.buf, sizeof(resp.buf), 0, NULL, NULL); bytesRead = recvfrom(sock, resp.buf, sizeof(resp.buf), 0, NULL, NULL);
break; if (bytesRead != SOCKET_ERROR || WSAGetLastError() != WSAECONNRESET) {
goto RecvDone;
}
}
} }
RecvDone:
if (bytesRead == 0) { if (bytesRead == 0) {
fprintf(LOG_OUT, "No response from STUN server\n"); fprintf(LOG_OUT, "No response from STUN server\n");
goto Exit; goto Exit;