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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ set( IXWEBSOCKET_HEADERS

option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" OFF)
option(USE_TLS "Enable TLS support" FALSE)
option(USE_ASYNC_DNS "Resolve hostnames on a cancellable background thread" ON)
option(USE_NETWORK_FRAMEWORK "Use Apple's Network.framework TLS backend (macOS 10.15+)" OFF)
if (USE_NETWORK_FRAMEWORK)
if (NOT APPLE OR NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
Expand Down Expand Up @@ -229,6 +230,10 @@ else()
)
endif()

if (NOT USE_ASYNC_DNS)
target_compile_definitions(ixwebsocket PRIVATE IXWEBSOCKET_USE_SYNC_DNS)
endif()

set(IXWEBSOCKET_PRIVATE_LIBS "")
include(CheckAtomic)

Expand Down
1 change: 1 addition & 0 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Options for building:
* `-DUSE_NETWORK_FRAMEWORK=ON` enables the experimental native macOS TLS backend and also enables `USE_TLS`. It requires macOS 10.15 or later and links Apple's Network, Security, and CoreFoundation frameworks, with no OpenSSL library dependency. It cannot be combined with another TLS backend; the make target below overrides cached backend selections. Client and server TLS, including mutual TLS, are supported. The existing Apple backend remains the default.
* `-DUSE_MBED_TLS=1` will use [mbedlts](https://tls.mbed.org/) for the TLS support
* `-DUSE_WS=1` will build the ws interactive command line tool
* `-DUSE_ASYNC_DNS=OFF` resolves hostnames synchronously, for static-glibc deployments that cannot use the default resolver worker. This is an opt-in workaround: hostname resolution cannot be cancelled until the system resolver returns, so `stop()` can wait for it. The default is `ON`. Numeric IP addresses never need a DNS worker.
* `-DUSE_TEST=1` will build the unittest
* `-DIXWEBSOCKET_WARNINGS_AS_ERRORS=ON` treats library and CLI compiler warnings as errors. It is off by default; `make -f makefile.dev test WARNINGS_AS_ERRORS=ON` enables it and builds both targets, as used by Linux CI. Unix GCC/Clang builds enable `-Wshadow`. Warning flags are private to these targets and do not apply to vendored dependencies or consumers.

Expand Down
61 changes: 47 additions & 14 deletions ixwebsocket/IXDNSLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@
#endif
#endif

namespace
{
bool isNumericHost(const std::string& hostname)
{
struct in6_addr address;
return ix::inet_pton(AF_INET, hostname.c_str(), &address) == 1 ||
ix::inet_pton(AF_INET6, hostname.c_str(), &address) == 1;
}
} // namespace

namespace ix
{
const int64_t DNSLookup::kDefaultWait = 1; // ms
Expand Down Expand Up @@ -72,9 +82,7 @@ namespace ix
// the whole process inside getaddrinfo (issue #560, reproduced with
// "127.0.0.1") and that also fails to resolve loopback when the machine is
// offline (issue #524).
struct in6_addr dummy;
bool numericHost = ix::inet_pton(AF_INET, hostname.c_str(), &dummy) == 1 ||
ix::inet_pton(AF_INET6, hostname.c_str(), &dummy) == 1;
const bool numericHost = isNumericHost(hostname);
if (numericHost)
{
hints.ai_flags |= AI_NUMERICHOST;
Expand Down Expand Up @@ -111,6 +119,27 @@ namespace ix
const CancellationRequest& isCancellationRequested,
bool cancellable)
{
// AI_NUMERICHOST | AI_NUMERICSERV performs no name/service lookup, and
// getAddrInfo deliberately omits the interface-querying AI_ADDRCONFIG here.
if (isNumericHost(_hostname))
{
errMsg.clear();
if (isCancellationRequested && isCancellationRequested())
{
errMsg = "cancellation requested";
return nullptr;
}
auto result = getAddrInfo(_hostname, _port, errMsg);
if (isCancellationRequested && isCancellationRequested())
{
errMsg = "cancellation requested";
return nullptr;
}
return result;
}
#ifdef IXWEBSOCKET_USE_SYNC_DNS
cancellable = false;
#endif
return cancellable ? resolveCancellable(errMsg, isCancellationRequested)
: resolveUnCancellable(errMsg, isCancellationRequested);
}
Expand All @@ -121,7 +150,7 @@ namespace ix
errMsg = "no error";

// Maybe a cancellation request got in before the background thread terminated ?
if (isCancellationRequested())
if (isCancellationRequested && isCancellationRequested())
{
errMsg = "cancellation requested";
return nullptr;
Expand All @@ -135,6 +164,12 @@ namespace ix
{
errMsg = "no error";

if (isCancellationRequested && isCancellationRequested())
{
errMsg = "cancellation requested";
return nullptr;
}

// Can only be called once, otherwise we would have to manage a pool
// of background thread which is overkill for our usage.
if (_done)
Expand All @@ -153,11 +188,9 @@ namespace ix
int port = _port;
std::string hostname(_hostname);

// We make the background thread doing the work a shared pointer
// instead of a member variable, because it can keep running when
// this object goes out of scope, in case of cancellation
auto t = std::make_shared<std::thread>(&DNSLookup::run, this, self, hostname, port);
t->detach();
// The detached worker owns its arguments and only accesses the lookup
// after acquiring a strong reference from self.
std::thread(&DNSLookup::run, self, hostname, port).detach();

while (!_done)
{
Expand All @@ -167,15 +200,15 @@ namespace ix
std::this_thread::sleep_for(std::chrono::milliseconds(_wait));

// Were we cancelled ?
if (isCancellationRequested())
if (isCancellationRequested && isCancellationRequested())
{
errMsg = "cancellation requested";
return nullptr;
}
}

// Maybe a cancellation request got in before the bg terminated ?
if (isCancellationRequested())
if (isCancellationRequested && isCancellationRequested())
{
errMsg = "cancellation requested";
return nullptr;
Expand All @@ -198,10 +231,10 @@ namespace ix
if (auto lock = self.lock())
{
// Copy result into the member variables
setRes(res);
setErrMsg(errMsg);
lock->setRes(res);
lock->setErrMsg(errMsg);

_done = true;
lock->_done = true;
}
}

Expand Down
8 changes: 3 additions & 5 deletions ixwebsocket/IXDNSLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
*
* Resolve a hostname+port to a struct addrinfo obtained with getaddrinfo
* Does this in a background thread so that it can be cancelled, since
* Numeric addresses resolve inline. Hostnames normally use a cancellable worker, since
* getaddrinfo is a blocking call, and we don't want to block the main thread on Mobile.
*/

Expand Down Expand Up @@ -40,11 +40,9 @@ namespace ix
AddrInfoPtr resolveUnCancellable(std::string& errMsg,
const CancellationRequest& isCancellationRequested);

AddrInfoPtr getAddrInfo(const std::string& hostname,
int port,
std::string& errMsg);
static AddrInfoPtr getAddrInfo(const std::string& hostname, int port, std::string& errMsg);

void run(std::weak_ptr<DNSLookup> self, std::string hostname, int port); // thread runner
static void run(std::weak_ptr<DNSLookup> self, std::string hostname, int port);

void setErrMsg(const std::string& errMsg);
const std::string& getErrMsg();
Expand Down
8 changes: 8 additions & 0 deletions ixwebsocket/IXSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ namespace ix
#endif
}

// Darwin TSan can confuse reused per-thread errno storage with shared memory.
// These accessors touch only OS thread-local error state; keep callers instrumented.
#if defined(__APPLE__) && defined(__clang__)
__attribute__((no_sanitize("thread")))
#endif
int Socket::getErrno()
{
int err;
Expand All @@ -319,6 +324,9 @@ namespace ix
return err;
}

#if defined(__APPLE__) && defined(__clang__)
__attribute__((no_sanitize("thread")))
#endif
void Socket::setErrno(int err)
{
#ifdef _WIN32
Expand Down
37 changes: 37 additions & 0 deletions test/IXDNSLookupTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,40 @@ TEST_CASE("dns", "[net]")
REQUIRE(res == nullptr);
}
}

TEST_CASE("Numeric addresses do not require a DNS worker", "[net][numeric_dns]")
{
const std::string host = GENERATE(std::string("127.0.0.1"), std::string("::1"));
DNSLookup lookup(host, 80);
std::string error;
auto result = lookup.resolve(error, CancellationRequest());
REQUIRE(result != nullptr);
CHECK(error.empty());
CHECK(result->ai_family == (host == "::1" ? AF_INET6 : AF_INET));
}

TEST_CASE("Already cancelled DNS lookups do not start a worker", "[net][numeric_dns]")
{
DNSLookup lookup("ixwebsocket-test.invalid.", 80);
std::string error;
CHECK(lookup.resolve(error, [] { return true; }) == nullptr);
CHECK(error == "cancellation requested");
}

TEST_CASE("Numeric resolution preserves cancellation after parsing", "[net][numeric_dns]")
{
DNSLookup lookup("127.0.0.1", 80);
unsigned checks = 0;
std::string error;
CHECK(lookup.resolve(error, [&] { return ++checks > 1; }) == nullptr);
CHECK(checks == 2);
CHECK(error == "cancellation requested");
}

TEST_CASE("Synchronous hostname resolution accepts an empty cancellation callback",
"[net][numeric_dns]")
{
DNSLookup lookup("localhost", 80);
std::string error;
CHECK(lookup.resolve(error, CancellationRequest(), false) != nullptr);
}
24 changes: 24 additions & 0 deletions test/IXSocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXSocketFactory.h>
#include <string.h>
#include <thread>

using namespace ix;

Expand Down Expand Up @@ -107,6 +108,29 @@ TEST_CASE("Empty and cancelled socket reads do not receive data", "[socket][read
CHECK(socket.reads == 0);
}

TEST_CASE("Socket error state remains local when threads are reused", "[socket][errno]")
{
std::atomic<bool> isolated {true};
auto churn = [&](int error)
{
for (int i = 0; i < 16; ++i)
{
std::thread worker(
[&, error]
{
Socket::setErrno(error);
if (Socket::getErrno() != error) isolated = false;
});
worker.join();
}
};
std::thread a(churn, EWOULDBLOCK);
std::thread b(churn, EINVAL);
a.join();
b.join();
CHECK(isolated.load());
}

namespace ix
{
void testSocket(const std::string& host,
Expand Down
Loading