diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index b9cc7211..6ad1b436 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -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(-1) && readyToRead && fds[0].revents & POLLIN) { pollResult = PollResultType::ReadyForRead; } - else if (sockfd != -1 && !readyToRead && fds[0].revents & POLLOUT) + else if (sockfd != static_cast(-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(&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(-1) && + (fds[0].revents & POLLERR || fds[0].revents & POLLHUP || + fds[0].revents & POLLNVAL)) { pollResult = PollResultType::Error; } diff --git a/ixwebsocket/IXSocketConnect.cpp b/ixwebsocket/IXSocketConnect.cpp index 942865db..2a411c4d 100644 --- a/ixwebsocket/IXSocketConnect.cpp +++ b/ixwebsocket/IXSocketConnect.cpp @@ -14,6 +14,7 @@ #include #include #include +#include // Android needs extra headers for TCP_NODELAY and IPPROTO_TCP #ifdef ANDROID @@ -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(-1)) { errMsg = "Cannot create a socket"; return -1; @@ -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; } @@ -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) @@ -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; } } diff --git a/test/IXSocketConnectTest.cpp b/test/IXSocketConnectTest.cpp index 805dacda..f602d168 100644 --- a/test/IXSocketConnectTest.cpp +++ b/test/IXSocketConnectTest.cpp @@ -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(-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]") {