diff options
author | Morten Johan Sørvig <morten.sorvig@qt.io> | 2021-10-01 13:11:41 +0200 |
---|---|---|
committer | Morten Johan Sørvig <morten.sorvig@qt.io> | 2021-12-15 17:06:11 +0100 |
commit | 76d12eea2252c5e537dff15b103bdc1f925cf760 (patch) | |
tree | 30bf5a17f560268d334cfa46228639f0ab77c68e /src/network/kernel/qhostinfo.cpp | |
parent | 292bdac4cfaebcc905975aa0cdc2f87782c389f6 (diff) |
wasm: implement host lookup for socket tunneling
Emscripten implements support for tunneling TCP and
UDP sockets through a WebSockets connection. This support
is implement for the BSD sockets API, which means that
Qt’s existing socket classes can be used, with some
adjustments.
For example, the flow for making a TCP connection to
example.com:1515 can look like this:
1) The application resolves “example.com”.
Emscripten creates an internal mapping to a private
IP and returns that IP: 172.29.1.0.
2) The application connects to 172.29.1.0:1515.
Emscripten makes a WebSocket connection to example.com:1515,
and forwards the TCP data over this connection
3) On example.com:1515, a WebSockify intermediate server
accepts the WebScoket connection and forwards the
TCP data to the target sever, as specified by the
WebSockify configuration.
Emscripten’s local getaddrinfo() implementation is fast,
which means don’t need caching or the thread pool. Instead,
special-case lookupHostImpl() for Q_OS_WASM. The implementation
calls QHostInfoAgent::lookup() and then posts resultReady
using QHostInfoResult.
Change-Id: Iaf31efb701ae7cc11752a63cc6b8346d4f09107e
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
Diffstat (limited to 'src/network/kernel/qhostinfo.cpp')
-rw-r--r-- | src/network/kernel/qhostinfo.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/network/kernel/qhostinfo.cpp b/src/network/kernel/qhostinfo.cpp index 6f508fb59b..a089351d60 100644 --- a/src/network/kernel/qhostinfo.cpp +++ b/src/network/kernel/qhostinfo.cpp @@ -59,7 +59,7 @@ # include <unistd.h> # include <netdb.h> # include <netinet/in.h> -# if defined(AI_ADDRCONFIG) +# if defined(AI_ADDRCONFIG) && !defined(Q_OS_WASM) # define Q_ADDRCONFIG AI_ADDRCONFIG # endif #elif defined Q_OS_WIN @@ -378,12 +378,17 @@ QHostInfo QHostInfo::fromName(const QString &name) qDebug("QHostInfo::fromName(\"%s\")",name.toLatin1().constData()); #endif +#ifdef Q_OS_WASM + return QHostInfoAgent::lookup(name); +#else QHostInfo hostInfo = QHostInfoAgent::fromName(name); QHostInfoLookupManager* manager = theHostInfoLookupManager(); manager->cache.put(name, hostInfo); return hostInfo; +#endif } + QHostInfo QHostInfoAgent::reverseLookup(const QHostAddress &address) { QHostInfo results; @@ -801,6 +806,20 @@ int QHostInfo::lookupHostImpl(const QString &name, return id; } +#ifdef Q_OS_WASM + // Resolve the host name directly without using a thread or cache, + // since Emscripten's host lookup is fast. Emscripten maintains an internal + // mapping of hosts and addresses for the purposes of WebSocket socket + // tunnelling, and does not perform an actual host lookup. + QHostInfo hostInfo = QHostInfoAgent::lookup(name); + hostInfo.setLookupId(id); + + QHostInfoResult result(receiver, slotObj); + if (receiver && member) + QObject::connect(&result, SIGNAL(resultsReady(QHostInfo)), + receiver, member, Qt::QueuedConnection); + result.postResultsReady(hostInfo); +#else QHostInfoLookupManager *manager = theHostInfoLookupManager(); if (Q_LIKELY(manager)) { @@ -827,6 +846,7 @@ int QHostInfo::lookupHostImpl(const QString &name, receiver, member, Qt::QueuedConnection); manager->scheduleLookup(runnable); } +#endif // Q_OS_WASM return id; } |