Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions ixwebsocket/IXSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,38 +117,39 @@ namespace ix
// The InterruptEvent was signaled
readSelectInterruptRequest(selectInterrupt, &pollResult);
}
else if (sockfd != -1 && readyToRead && fds[0].revents & POLLIN)
else if (sockfd != static_cast<socket_t>(-1) && readyToRead && fds[0].revents & POLLIN)
{
pollResult = PollResultType::ReadyForRead;
}
else if (sockfd != -1 && !readyToRead && fds[0].revents & POLLOUT)
else if (sockfd != static_cast<socket_t>(-1) && !readyToRead &&
(fds[0].revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)))
{
pollResult = PollResultType::ReadyForWrite;

#ifdef _WIN32
// On connect error, in async mode, windows will write to the exceptions fds
if (fds[0].revents & POLLERR)
int error = 0;
socklen_t length = sizeof(error);
if (getsockopt(
sockfd, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &length) != 0)
{
// Preserve the getsockopt error, rather than overwriting it with zero.
pollResult = PollResultType::Error;
}
#else
int optval = -1;
socklen_t optlen = sizeof(optval);

// getsockopt() puts the errno value for connect into optval so 0
// means no-error.
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1 || optval != 0)
else if (error != 0)
{
Socket::setErrno(error);
pollResult = PollResultType::Error;

// set errno to optval so that external callers can have an
// appropriate error description when calling strerror
errno = optval;
}
#endif
else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
{
Socket::setErrno((fds[0].revents & POLLNVAL) ? EBADF : ECONNRESET);
pollResult = PollResultType::Error;
}
else
{
pollResult = PollResultType::ReadyForWrite;
}
}
else if (sockfd != -1 && (fds[0].revents & POLLERR || fds[0].revents & POLLHUP ||
fds[0].revents & POLLNVAL))
else if (sockfd != static_cast<socket_t>(-1) &&
(fds[0].revents & POLLERR || fds[0].revents & POLLHUP ||
fds[0].revents & POLLNVAL))
{
pollResult = PollResultType::Error;
}
Expand Down
13 changes: 9 additions & 4 deletions ixwebsocket/IXSocketConnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <system_error>

// Android needs extra headers for TCP_NODELAY and IPPROTO_TCP
#ifdef ANDROID
Expand All @@ -36,7 +37,7 @@ namespace ix
errMsg = "no error";

socket_t fd = socket(address->ai_family, address->ai_socktype, address->ai_protocol);
if (fd < 0)
if (fd == static_cast<socket_t>(-1))
{
errMsg = "Cannot create a socket";
return -1;
Expand All @@ -50,7 +51,7 @@ namespace ix

if (res == -1 && !Socket::isWaitNeeded())
{
errMsg = strerror(Socket::getErrno());
errMsg = std::system_category().message(Socket::getErrno());
Socket::closeSocket(fd);
return -1;
}
Expand All @@ -74,8 +75,10 @@ namespace ix
}
else if (pollResult == PollResultType::Error)
{
const int error = Socket::getErrno();
Socket::closeSocket(fd);
errMsg = std::string("Connect error: ") + strerror(Socket::getErrno());
errMsg = error ? "Connect error: " + std::system_category().message(error)
: "Connect error: socket did not become writable";
return -1;
}
else if (pollResult == PollResultType::ReadyForWrite)
Expand All @@ -84,8 +87,10 @@ namespace ix
}
else
{
const int error = Socket::getErrno();
Socket::closeSocket(fd);
errMsg = std::string("Connect error: ") + strerror(Socket::getErrno());
errMsg = error ? "Connect error: " + std::system_category().message(error)
: "Connect error: socket did not become writable";
return -1;
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/IXSocketConnectTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

using namespace ix;

TEST_CASE("Write polling reports invalid socket errors", "[net][connect]")
{
const auto fd = ::socket(AF_INET, SOCK_STREAM, 0);
REQUIRE(fd != static_cast<socket_t>(-1));
Socket::closeSocket(fd);
Socket::setErrno(0);
CHECK(Socket::poll(false, 0, fd, nullptr) == PollResultType::Error);
CHECK(Socket::getErrno() != 0);
}


TEST_CASE("socket_connect", "[net]")
{
Expand Down
Loading