From 41a161647dd8f3f0bfa5727ee1b357ea8605c348 Mon Sep 17 00:00:00 2001 From: Trond Abusdal Date: Mon, 9 Nov 2015 01:33:16 +0100 Subject: * Winsock UDP messages sent to an unavailable target causing WSAECONNRESET will no longer close the socket. * Ensured that unsigned<->signed conversion would not cause wrong buffer size values sent to recvfrom. --- platform/windows/packet_peer_udp_winsock.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'platform/windows') diff --git a/platform/windows/packet_peer_udp_winsock.cpp b/platform/windows/packet_peer_udp_winsock.cpp index 34192e318a..04d526e443 100644 --- a/platform/windows/packet_peer_udp_winsock.cpp +++ b/platform/windows/packet_peer_udp_winsock.cpp @@ -120,8 +120,10 @@ Error PacketPeerUDPWinsock::_poll(bool p_wait) { struct sockaddr_in from = {0}; int len = sizeof(struct sockaddr_in); + int rb_size = MAX(rb.space_left()-12, 0); + int buffer_size = MIN((int)sizeof(recv_buffer),rb_size); int ret; - while ( (ret = recvfrom(sockfd, (char*)recv_buffer, MIN(sizeof(recv_buffer),rb.space_left()-12), 0, (struct sockaddr*)&from, &len)) > 0) { + while ( (ret = recvfrom(sockfd, (char*)recv_buffer, buffer_size, 0, (struct sockaddr*)&from, &len)) > 0) { rb.write((uint8_t*)&from.sin_addr, 4); uint32_t port = ntohs(from.sin_port); rb.write((uint8_t*)&port, 4); @@ -132,8 +134,25 @@ Error PacketPeerUDPWinsock::_poll(bool p_wait) { ++queue_count; }; + if (ret == SOCKET_ERROR){ + int error = WSAGetLastError(); + + if (error == WSAEWOULDBLOCK){ + // Expected when doing non-blocking sockets, retry later. + } + else if (error == WSAECONNRESET){ + // If the remote target does not accept messages, this error may occur, but is harmless. + // Once the remote target gets available, this message will disappear for new messages. + } + else + { + close(); + return FAILED; + } + } + - if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) ) { + if (ret == 0) { close(); return FAILED; }; -- cgit v1.2.3