Log socket error code on failure

Issue #643.

Change-Id: I8adfb9910e1e43294738abb7783a64645a4f42c3
This commit is contained in:
KongQun Yang 2019-09-05 14:30:36 -07:00
parent b8ee20df1d
commit 3ec2975e36
1 changed files with 30 additions and 16 deletions

View File

@ -11,6 +11,7 @@
#include <windows.h>
#include <ws2tcpip.h>
#define close closesocket
#define EINTR_CODE WSAEINTR
#else
@ -21,6 +22,7 @@
#include <sys/time.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define EINTR_CODE EINTR
// IP_MULTICAST_ALL has been supported since kernel version 2.6.31 but we may be
// building on a machine that is older than that.
@ -43,6 +45,14 @@ bool IsIpv4MulticastAddress(const struct in_addr& addr) {
return (ntohl(addr.s_addr) & 0xf0000000) == 0xe0000000;
}
int GetSocketErrorCode() {
#if defined(OS_WIN)
return WSAGetLastError();
#else
return errno;
#endif
}
} // anonymous namespace
UdpFile::UdpFile(const char* file_name)
@ -71,7 +81,7 @@ int64_t UdpFile::Read(void* buffer, uint64_t length) {
do {
result =
recvfrom(socket_, reinterpret_cast<char*>(buffer), length, 0, NULL, 0);
} while ((result == -1) && (errno == EINTR));
} while (result == -1 && GetSocketErrorCode() == EINTR_CODE);
return result;
}
@ -165,7 +175,7 @@ bool UdpFile::Open() {
ScopedSocket new_socket(socket(AF_INET, SOCK_DGRAM, 0));
if (new_socket.get() == INVALID_SOCKET) {
LOG(ERROR) << "Could not allocate socket.";
LOG(ERROR) << "Could not allocate socket, error = " << GetSocketErrorCode();
return false;
}
@ -191,8 +201,9 @@ bool UdpFile::Open() {
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const char*>(&optval),
sizeof(optval)) < 0) {
LOG(ERROR)
<< "Could not apply the SO_REUSEADDR property to the UDP socket";
LOG(ERROR) << "Could not apply the SO_REUSEADDR property to the UDP "
"socket, error = "
<< GetSocketErrorCode();
return false;
}
}
@ -200,7 +211,7 @@ bool UdpFile::Open() {
if (bind(new_socket.get(),
reinterpret_cast<struct sockaddr*>(&local_sock_addr),
sizeof(local_sock_addr)) < 0) {
LOG(ERROR) << "Could not bind UDP socket";
LOG(ERROR) << "Could not bind UDP socket, error = " << GetSocketErrorCode();
return false;
}
@ -229,8 +240,9 @@ bool UdpFile::Open() {
IP_ADD_SOURCE_MEMBERSHIP,
reinterpret_cast<const char*>(&source_multicast_group),
sizeof(source_multicast_group)) < 0) {
LOG(ERROR) << "Failed to join multicast group.";
return false;
LOG(ERROR) << "Failed to join multicast group, error = "
<< GetSocketErrorCode();
return false;
}
} else {
// this is a v2 join without a specific source.
@ -246,12 +258,12 @@ bool UdpFile::Open() {
}
if (setsockopt(new_socket.get(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
reinterpret_cast<const char*>(&multicast_group),
sizeof(multicast_group)) < 0) {
LOG(ERROR) << "Failed to join multicast group.";
reinterpret_cast<const char*>(&multicast_group),
sizeof(multicast_group)) < 0) {
LOG(ERROR) << "Failed to join multicast group, error = "
<< GetSocketErrorCode();
return false;
}
}
#if defined(__linux__)
@ -261,8 +273,9 @@ bool UdpFile::Open() {
if (setsockopt(new_socket.get(), IPPROTO_IP, IP_MULTICAST_ALL,
reinterpret_cast<const char*>(&optval_zero),
sizeof(optval_zero)) < 0 &&
errno != ENOPROTOOPT) {
LOG(ERROR) << "Failed to disable IP_MULTICAST_ALL option.";
GetSocketErrorCode() != ENOPROTOOPT) {
LOG(ERROR) << "Failed to disable IP_MULTICAST_ALL option, error = "
<< GetSocketErrorCode();
return false;
}
#endif // #if defined(__linux__)
@ -275,7 +288,8 @@ bool UdpFile::Open() {
tv.tv_usec = options->timeout_us() % 1000000;
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<const char*>(&tv), sizeof(tv)) < 0) {
LOG(ERROR) << "Failed to set socket timeout.";
LOG(ERROR) << "Failed to set socket timeout, error = "
<< GetSocketErrorCode();
return false;
}
}
@ -285,8 +299,8 @@ bool UdpFile::Open() {
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char*>(&receive_buffer_size),
sizeof(receive_buffer_size)) < 0) {
LOG(ERROR) << "Failed to set the maximum receive buffer size: "
<< strerror(errno);
LOG(ERROR) << "Failed to set the maximum receive buffer size, error = "
<< GetSocketErrorCode();
return false;
}
}