Log socket error code on failure
Issue #643. Change-Id: I8adfb9910e1e43294738abb7783a64645a4f42c3
This commit is contained in:
parent
b8ee20df1d
commit
3ec2975e36
|
@ -11,6 +11,7 @@
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#define close closesocket
|
#define close closesocket
|
||||||
|
#define EINTR_CODE WSAEINTR
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
@ -21,6 +22,7 @@
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#define INVALID_SOCKET -1
|
#define INVALID_SOCKET -1
|
||||||
|
#define EINTR_CODE EINTR
|
||||||
|
|
||||||
// IP_MULTICAST_ALL has been supported since kernel version 2.6.31 but we may be
|
// 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.
|
// 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;
|
return (ntohl(addr.s_addr) & 0xf0000000) == 0xe0000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetSocketErrorCode() {
|
||||||
|
#if defined(OS_WIN)
|
||||||
|
return WSAGetLastError();
|
||||||
|
#else
|
||||||
|
return errno;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
|
||||||
UdpFile::UdpFile(const char* file_name)
|
UdpFile::UdpFile(const char* file_name)
|
||||||
|
@ -71,7 +81,7 @@ int64_t UdpFile::Read(void* buffer, uint64_t length) {
|
||||||
do {
|
do {
|
||||||
result =
|
result =
|
||||||
recvfrom(socket_, reinterpret_cast<char*>(buffer), length, 0, NULL, 0);
|
recvfrom(socket_, reinterpret_cast<char*>(buffer), length, 0, NULL, 0);
|
||||||
} while ((result == -1) && (errno == EINTR));
|
} while (result == -1 && GetSocketErrorCode() == EINTR_CODE);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -165,7 +175,7 @@ bool UdpFile::Open() {
|
||||||
|
|
||||||
ScopedSocket new_socket(socket(AF_INET, SOCK_DGRAM, 0));
|
ScopedSocket new_socket(socket(AF_INET, SOCK_DGRAM, 0));
|
||||||
if (new_socket.get() == INVALID_SOCKET) {
|
if (new_socket.get() == INVALID_SOCKET) {
|
||||||
LOG(ERROR) << "Could not allocate socket.";
|
LOG(ERROR) << "Could not allocate socket, error = " << GetSocketErrorCode();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,8 +201,9 @@ bool UdpFile::Open() {
|
||||||
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_REUSEADDR,
|
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_REUSEADDR,
|
||||||
reinterpret_cast<const char*>(&optval),
|
reinterpret_cast<const char*>(&optval),
|
||||||
sizeof(optval)) < 0) {
|
sizeof(optval)) < 0) {
|
||||||
LOG(ERROR)
|
LOG(ERROR) << "Could not apply the SO_REUSEADDR property to the UDP "
|
||||||
<< "Could not apply the SO_REUSEADDR property to the UDP socket";
|
"socket, error = "
|
||||||
|
<< GetSocketErrorCode();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,7 +211,7 @@ bool UdpFile::Open() {
|
||||||
if (bind(new_socket.get(),
|
if (bind(new_socket.get(),
|
||||||
reinterpret_cast<struct sockaddr*>(&local_sock_addr),
|
reinterpret_cast<struct sockaddr*>(&local_sock_addr),
|
||||||
sizeof(local_sock_addr)) < 0) {
|
sizeof(local_sock_addr)) < 0) {
|
||||||
LOG(ERROR) << "Could not bind UDP socket";
|
LOG(ERROR) << "Could not bind UDP socket, error = " << GetSocketErrorCode();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,7 +240,8 @@ bool UdpFile::Open() {
|
||||||
IP_ADD_SOURCE_MEMBERSHIP,
|
IP_ADD_SOURCE_MEMBERSHIP,
|
||||||
reinterpret_cast<const char*>(&source_multicast_group),
|
reinterpret_cast<const char*>(&source_multicast_group),
|
||||||
sizeof(source_multicast_group)) < 0) {
|
sizeof(source_multicast_group)) < 0) {
|
||||||
LOG(ERROR) << "Failed to join multicast group.";
|
LOG(ERROR) << "Failed to join multicast group, error = "
|
||||||
|
<< GetSocketErrorCode();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -248,10 +260,10 @@ bool UdpFile::Open() {
|
||||||
if (setsockopt(new_socket.get(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
if (setsockopt(new_socket.get(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
||||||
reinterpret_cast<const char*>(&multicast_group),
|
reinterpret_cast<const char*>(&multicast_group),
|
||||||
sizeof(multicast_group)) < 0) {
|
sizeof(multicast_group)) < 0) {
|
||||||
LOG(ERROR) << "Failed to join multicast group.";
|
LOG(ERROR) << "Failed to join multicast group, error = "
|
||||||
|
<< GetSocketErrorCode();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
|
@ -261,8 +273,9 @@ bool UdpFile::Open() {
|
||||||
if (setsockopt(new_socket.get(), IPPROTO_IP, IP_MULTICAST_ALL,
|
if (setsockopt(new_socket.get(), IPPROTO_IP, IP_MULTICAST_ALL,
|
||||||
reinterpret_cast<const char*>(&optval_zero),
|
reinterpret_cast<const char*>(&optval_zero),
|
||||||
sizeof(optval_zero)) < 0 &&
|
sizeof(optval_zero)) < 0 &&
|
||||||
errno != ENOPROTOOPT) {
|
GetSocketErrorCode() != ENOPROTOOPT) {
|
||||||
LOG(ERROR) << "Failed to disable IP_MULTICAST_ALL option.";
|
LOG(ERROR) << "Failed to disable IP_MULTICAST_ALL option, error = "
|
||||||
|
<< GetSocketErrorCode();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif // #if defined(__linux__)
|
#endif // #if defined(__linux__)
|
||||||
|
@ -275,7 +288,8 @@ bool UdpFile::Open() {
|
||||||
tv.tv_usec = options->timeout_us() % 1000000;
|
tv.tv_usec = options->timeout_us() % 1000000;
|
||||||
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVTIMEO,
|
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVTIMEO,
|
||||||
reinterpret_cast<const char*>(&tv), sizeof(tv)) < 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -285,8 +299,8 @@ bool UdpFile::Open() {
|
||||||
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVBUF,
|
if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVBUF,
|
||||||
reinterpret_cast<const char*>(&receive_buffer_size),
|
reinterpret_cast<const char*>(&receive_buffer_size),
|
||||||
sizeof(receive_buffer_size)) < 0) {
|
sizeof(receive_buffer_size)) < 0) {
|
||||||
LOG(ERROR) << "Failed to set the maximum receive buffer size: "
|
LOG(ERROR) << "Failed to set the maximum receive buffer size, error = "
|
||||||
<< strerror(errno);
|
<< GetSocketErrorCode();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue