From 1827ed77d3446c305356e6639d0f053ed9347059 Mon Sep 17 00:00:00 2001 From: Matteo Federico Zazzetta Date: Mon, 22 Nov 2021 14:51:19 +0100 Subject: [PATCH 1/8] Added DesiredHostname as parameter of Hostname constructor. This is useful for systems where QHstInfo::localHostName() gives weird results --- src/include/qmdnsengine/hostname.h | 20 ++++++------ src/src/hostname.cpp | 50 ++++++++++++------------------ src/src/hostname_p.h | 17 +++++----- 3 files changed, 36 insertions(+), 51 deletions(-) diff --git a/src/include/qmdnsengine/hostname.h b/src/include/qmdnsengine/hostname.h index fe3c79b..90f4ec9 100644 --- a/src/include/qmdnsengine/hostname.h +++ b/src/include/qmdnsengine/hostname.h @@ -29,8 +29,7 @@ #include "qmdnsengine_export.h" -namespace QMdnsEngine -{ +namespace QMdnsEngine { class AbstractServer; @@ -51,17 +50,20 @@ class QMDNSENGINE_EXPORT HostnamePrivate; * }); * @endcode */ -class QMDNSENGINE_EXPORT Hostname : public QObject -{ +class QMDNSENGINE_EXPORT Hostname : public QObject { Q_OBJECT -public: - + public: /** * @brief Create a new hostname */ Hostname(AbstractServer *server, QObject *parent = 0); + /** + * @brief Create a new hostname with desired value to register + */ + Hostname(AbstractServer *server, const QByteArray &desired, QObject *parent = 0); + /** * @brief Determine if a hostname has been registered * @@ -77,7 +79,7 @@ class QMDNSENGINE_EXPORT Hostname : public QObject */ QByteArray hostname() const; -Q_SIGNALS: + Q_SIGNALS: /** * @brief Indicate that the current hostname has changed @@ -85,11 +87,9 @@ class QMDNSENGINE_EXPORT Hostname : public QObject */ void hostnameChanged(const QByteArray &hostname); -private: - + private: HostnamePrivate *const d; }; - } #endif // QMDNSENGINE_HOSTNAME_H diff --git a/src/src/hostname.cpp b/src/src/hostname.cpp index 94d9931..a8c5a29 100644 --- a/src/src/hostname.cpp +++ b/src/src/hostname.cpp @@ -39,10 +39,10 @@ using namespace QMdnsEngine; HostnamePrivate::HostnamePrivate(Hostname *hostname, AbstractServer *server) - : QObject(hostname), - server(server), - q(hostname) -{ + : HostnamePrivate(hostname, QByteArray(), server) {} + +HostnamePrivate::HostnamePrivate(Hostname *hostname, const QByteArray &desired, AbstractServer *server) + : QObject(hostname), server(server), desiredHostname(desired), q(hostname) { connect(server, &AbstractServer::messageReceived, this, &HostnamePrivate::onMessageReceived); connect(®istrationTimer, &QTimer::timeout, this, &HostnamePrivate::onRegistrationTimeout); connect(&rebroadcastTimer, &QTimer::timeout, this, &HostnamePrivate::onRebroadcastTimeout); @@ -57,17 +57,16 @@ HostnamePrivate::HostnamePrivate(Hostname *hostname, AbstractServer *server) onRebroadcastTimeout(); } -void HostnamePrivate::assertHostname() -{ +void HostnamePrivate::assertHostname() { // Begin with the local hostname and replace any "." with "-" (I'm looking // at you, macOS) - QByteArray localHostname = QHostInfo::localHostName().toUtf8(); + QByteArray localHostname = desiredHostname.isEmpty() ? QHostInfo::localHostName().toUtf8() : desiredHostname; localHostname = localHostname.replace('.', '-'); // If the suffix > 1, then append a "-2", "-3", etc. to the hostname to // aid in finding one that is unique and not in use - hostname = (hostnameSuffix == 1 ? localHostname: - localHostname + "-" + QByteArray::number(hostnameSuffix)) + ".local."; + hostname = + (hostnameSuffix == 1 ? localHostname : localHostname + "-" + QByteArray::number(hostnameSuffix)) + ".local."; // Compose a query for A and AAAA records matching the hostname Query ipv4Query; @@ -86,8 +85,7 @@ void HostnamePrivate::assertHostname() registrationTimer.start(); } -bool HostnamePrivate::generateRecord(const QHostAddress &srcAddress, quint16 type, Record &record) -{ +bool HostnamePrivate::generateRecord(const QHostAddress &srcAddress, quint16 type, Record &record) { // Attempt to find the interface that corresponds with the provided // address and determine this device's address from the interface @@ -99,7 +97,7 @@ bool HostnamePrivate::generateRecord(const QHostAddress &srcAddress, quint16 typ for (const QNetworkAddressEntry &newEntry : entries) { QHostAddress address = newEntry.ip(); if ((address.protocol() == QAbstractSocket::IPv4Protocol && type == A) || - (address.protocol() == QAbstractSocket::IPv6Protocol && type == AAAA)) { + (address.protocol() == QAbstractSocket::IPv6Protocol && type == AAAA)) { record.setName(hostname); record.setType(type); record.setAddress(address); @@ -112,8 +110,7 @@ bool HostnamePrivate::generateRecord(const QHostAddress &srcAddress, quint16 typ return false; } -void HostnamePrivate::onMessageReceived(const Message &message) -{ +void HostnamePrivate::onMessageReceived(const Message &message) { if (message.isResponse()) { if (hostnameRegistered) { return; @@ -146,8 +143,7 @@ void HostnamePrivate::onMessageReceived(const Message &message) } } -void HostnamePrivate::onRegistrationTimeout() -{ +void HostnamePrivate::onRegistrationTimeout() { hostnameRegistered = true; if (hostname != hostnamePrev) { emit q->hostnameChanged(hostname); @@ -157,8 +153,7 @@ void HostnamePrivate::onRegistrationTimeout() rebroadcastTimer.start(); } -void HostnamePrivate::onRebroadcastTimeout() -{ +void HostnamePrivate::onRebroadcastTimeout() { hostnamePrev = hostname; hostnameRegistered = false; hostnameSuffix = 1; @@ -166,18 +161,11 @@ void HostnamePrivate::onRebroadcastTimeout() assertHostname(); } -Hostname::Hostname(AbstractServer *server, QObject *parent) - : QObject(parent), - d(new HostnamePrivate(this, server)) -{ -} +Hostname::Hostname(AbstractServer *server, QObject *parent) : QObject(parent), d(new HostnamePrivate(this, server)) {} -bool Hostname::isRegistered() const -{ - return d->hostnameRegistered; -} +Hostname::Hostname(AbstractServer *server, const QByteArray &desired, QObject *parent) + : QObject(parent), d(new HostnamePrivate(this, desired, server)) {} -QByteArray Hostname::hostname() const -{ - return d->hostname; -} +bool Hostname::isRegistered() const { return d->hostnameRegistered; } + +QByteArray Hostname::hostname() const { return d->hostname; } diff --git a/src/src/hostname_p.h b/src/src/hostname_p.h index 33261db..d1bfaa5 100644 --- a/src/src/hostname_p.h +++ b/src/src/hostname_p.h @@ -30,21 +30,19 @@ class QHostAddress; -namespace QMdnsEngine -{ +namespace QMdnsEngine { class AbstractServer; class Hostname; class Message; class Record; -class HostnamePrivate : public QObject -{ +class HostnamePrivate : public QObject { Q_OBJECT -public: - + public: HostnamePrivate(Hostname *hostname, AbstractServer *server); + HostnamePrivate(Hostname *hostname, const QByteArray &desired, AbstractServer *server); void assertHostname(); bool generateRecord(const QHostAddress &srcAddress, quint16 type, Record &record); @@ -53,23 +51,22 @@ class HostnamePrivate : public QObject QByteArray hostnamePrev; QByteArray hostname; + QByteArray desiredHostname; bool hostnameRegistered; int hostnameSuffix; QTimer registrationTimer; QTimer rebroadcastTimer; -private Q_SLOTS: + private Q_SLOTS: void onMessageReceived(const Message &message); void onRegistrationTimeout(); void onRebroadcastTimeout(); -private: - + private: Hostname *const q; }; - } #endif // QMDNSENGINE_HOSTNAME_P_H From 881f7fa2002a96e1b887114795aa4d5dab537fdd Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Thu, 14 Apr 2022 11:48:58 +0200 Subject: [PATCH 2/8] Zwift connects to QZ! (ip is static, I need to change it) --- src/src/provider.cpp | 47 ++++++++++++++++++-------------------------- src/src/provider_p.h | 14 ++++++------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/src/provider.cpp b/src/src/provider.cpp index 756d896..22a2313 100644 --- a/src/src/provider.cpp +++ b/src/src/provider.cpp @@ -36,13 +36,7 @@ using namespace QMdnsEngine; ProviderPrivate::ProviderPrivate(QObject *parent, AbstractServer *server, Hostname *hostname) - : QObject(parent), - server(server), - hostname(hostname), - prober(nullptr), - initialized(false), - confirmed(false) -{ + : QObject(parent), server(server), hostname(hostname), prober(nullptr), initialized(false), confirmed(false) { connect(server, &AbstractServer::messageReceived, this, &ProviderPrivate::onMessageReceived); connect(hostname, &Hostname::hostnameChanged, this, &ProviderPrivate::onHostnameChanged); @@ -53,15 +47,13 @@ ProviderPrivate::ProviderPrivate(QObject *parent, AbstractServer *server, Hostna txtProposed.setType(TXT); } -ProviderPrivate::~ProviderPrivate() -{ +ProviderPrivate::~ProviderPrivate() { if (confirmed) { farewell(); } } -void ProviderPrivate::announce() -{ +void ProviderPrivate::announce() { // Broadcast a message with each of the records Message message; @@ -72,8 +64,7 @@ void ProviderPrivate::announce() server->sendMessageToAll(message); } -void ProviderPrivate::confirm() -{ +void ProviderPrivate::confirm() { // Confirm that the desired name is unique through probing if (prober) { @@ -81,7 +72,6 @@ void ProviderPrivate::confirm() } prober = new Prober(server, srvProposed, this); connect(prober, &Prober::nameConfirmed, [this](const QByteArray &name) { - // If existing records were confirmed, indicate that they are no // longer valid if (confirmed) { @@ -103,8 +93,7 @@ void ProviderPrivate::confirm() }); } -void ProviderPrivate::farewell() -{ +void ProviderPrivate::farewell() { // Send a message indicating that the existing records are no longer valid // by setting their TTL to 0 @@ -114,19 +103,23 @@ void ProviderPrivate::farewell() announce(); } -void ProviderPrivate::publish() -{ +void ProviderPrivate::publish() { // Copy the proposed records over and announce them browsePtrRecord = browsePtrProposed; ptrRecord = ptrProposed; srvRecord = srvProposed; txtRecord = txtProposed; + + // zwift + ARecord.setType(A); + ARecord.setName(srvRecord.target()); + ARecord.setAddress(QHostAddress("172.31.100.161")); + announce(); } -void ProviderPrivate::onMessageReceived(const Message &message) -{ +void ProviderPrivate::onMessageReceived(const Message &message) { if (!confirmed || message.isResponse()) { return; } @@ -179,6 +172,9 @@ void ProviderPrivate::onMessageReceived(const Message &message) } if (sendSrv) { reply.addRecord(srvRecord); + + // zwift need it + reply.addRecord(ARecord); } if (sendTxt) { reply.addRecord(txtRecord); @@ -187,8 +183,7 @@ void ProviderPrivate::onMessageReceived(const Message &message) } } -void ProviderPrivate::onHostnameChanged(const QByteArray &newHostname) -{ +void ProviderPrivate::onHostnameChanged(const QByteArray &newHostname) { // Update the proposed SRV record srvProposed.setTarget(newHostname); @@ -199,13 +194,9 @@ void ProviderPrivate::onHostnameChanged(const QByteArray &newHostname) } Provider::Provider(AbstractServer *server, Hostname *hostname, QObject *parent) - : QObject(parent), - d(new ProviderPrivate(this, server, hostname)) -{ -} + : QObject(parent), d(new ProviderPrivate(this, server, hostname)) {} -void Provider::update(const Service &service) -{ +void Provider::update(const Service &service) { d->initialized = true; // Clean the service name diff --git a/src/src/provider_p.h b/src/src/provider_p.h index 28578f2..8dba42f 100644 --- a/src/src/provider_p.h +++ b/src/src/provider_p.h @@ -30,20 +30,17 @@ #include #include -namespace QMdnsEngine -{ +namespace QMdnsEngine { class AbstractServer; class Hostname; class Message; class Prober; -class ProviderPrivate : public QObject -{ +class ProviderPrivate : public QObject { Q_OBJECT -public: - + public: ProviderPrivate(QObject *parent, AbstractServer *server, Hostname *hostname); virtual ~ProviderPrivate(); @@ -64,18 +61,19 @@ class ProviderPrivate : public QObject Record ptrRecord; Record srvRecord; Record txtRecord; + Record ARecord; Record browsePtrProposed; Record ptrProposed; Record srvProposed; Record txtProposed; -private Q_SLOTS: + private Q_SLOTS: void onMessageReceived(const Message &message); void onHostnameChanged(const QByteArray &hostname); }; -} +} // namespace QMdnsEngine #endif // QMDNSENGINE_PROVIDER_P_H From a529188038c5b235cf3d58ca2635f227c86f5e49 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Thu, 14 Apr 2022 14:20:47 +0200 Subject: [PATCH 3/8] getting ip address dinamically on A record --- src/src/provider.cpp | 27 ++++++++++++++++++++++++++- src/src/provider_p.h | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/src/provider.cpp b/src/src/provider.cpp index 22a2313..2a0b6a9 100644 --- a/src/src/provider.cpp +++ b/src/src/provider.cpp @@ -22,6 +22,7 @@ * IN THE SOFTWARE. */ +#include #include #include #include @@ -114,11 +115,33 @@ void ProviderPrivate::publish() { // zwift ARecord.setType(A); ARecord.setName(srvRecord.target()); - ARecord.setAddress(QHostAddress("172.31.100.161")); + // set directly when we receive a message + // ARecord.setAddress(QHostAddress("172.31.100.161")); announce(); } +QHostAddress ProviderPrivate::getIP(const QHostAddress &srcAddress) { + // Attempt to find the interface that corresponds with the provided + // address and determine this device's address from the interface + + const auto interfaces = QNetworkInterface::allInterfaces(); + for (const QNetworkInterface &networkInterface : interfaces) { + const auto entries = networkInterface.addressEntries(); + for (const QNetworkAddressEntry &entry : entries) { + if (srcAddress.isInSubnet(entry.ip(), entry.prefixLength())) { + for (const QNetworkAddressEntry &newEntry : entries) { + QHostAddress address = newEntry.ip(); + if ((address.protocol() == QAbstractSocket::IPv4Protocol)) { + return address; + } + } + } + } + } + return QHostAddress(); +} + void ProviderPrivate::onMessageReceived(const Message &message) { if (!confirmed || message.isResponse()) { return; @@ -129,6 +152,8 @@ void ProviderPrivate::onMessageReceived(const Message &message) { bool sendSrv = false; bool sendTxt = false; + ARecord.setAddress(getIP(message.address())); + // Determine which records to send based on the queries const auto queries = message.queries(); for (const Query &query : queries) { diff --git a/src/src/provider_p.h b/src/src/provider_p.h index 8dba42f..1abac68 100644 --- a/src/src/provider_p.h +++ b/src/src/provider_p.h @@ -48,6 +48,7 @@ class ProviderPrivate : public QObject { void confirm(); void farewell(); void publish(); + QHostAddress getIP(const QHostAddress &srcAddress); AbstractServer *server; Hostname *hostname; From cb16afa468a5ed7bb44707485c97841e66bf0375 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Mon, 17 Oct 2022 10:57:23 +0200 Subject: [PATCH 4/8] fixing https://github.com/cagnulein/qdomyos-zwift/issues/983 https://github.com/cagnulein/qdomyos-zwift/issues/983 --- src/src/provider.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/src/provider.cpp b/src/src/provider.cpp index 2a0b6a9..5a15435 100644 --- a/src/src/provider.cpp +++ b/src/src/provider.cpp @@ -34,6 +34,12 @@ #include "provider_p.h" +#ifdef Q_OS_ANDROID +#include +#include +#include +#endif + using namespace QMdnsEngine; ProviderPrivate::ProviderPrivate(QObject *parent, AbstractServer *server, Hostname *hostname) @@ -121,6 +127,92 @@ void ProviderPrivate::publish() { announce(); } +#ifdef Q_OS_ANDROID + +/* + * Get WifiManager object + * Parameters: jCtxObj is Context object + */ +jobject getWifiManagerObj(JNIEnv *env, jobject jCtxObj) { + qDebug() << "gotWifiMangerObj "; + // Get the value of Context.WIFI_SERVICE + // jstring jstr_wifi_serveice = env->NewStringUTF("wifi"); + jclass jCtxClz = env->FindClass("android/content/Context"); + jfieldID fid_wifi_service = env->GetStaticFieldID(jCtxClz, "WIFI_SERVICE", "Ljava/lang/String;"); + jstring jstr_wifi_serveice = (jstring)env->GetStaticObjectField(jCtxClz, fid_wifi_service); + + jclass jclz = env->GetObjectClass(jCtxObj); + jmethodID mid_getSystemService = + env->GetMethodID(jclz, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); + jobject wifiManager = env->CallObjectMethod(jCtxObj, mid_getSystemService, jstr_wifi_serveice); + + // Because jclass inherits from jobject, it needs to be released; + // jfieldID and jmethodID are memory addresses, this memory is not allocated in our code, and we don’t need to + // release it. + env->DeleteLocalRef(jCtxClz); + env->DeleteLocalRef(jclz); + env->DeleteLocalRef(jstr_wifi_serveice); + + return wifiManager; +} + +/* + * Get the WifiInfo object + * Parameters: wifiMgrObj is the WifiManager object + */ +jobject getWifiInfoObj(JNIEnv *env, jobject wifiMgrObj) { + qDebug() << "getWifiInfoObj "; + if (wifiMgrObj == NULL) { + return NULL; + } + jclass jclz = env->GetObjectClass(wifiMgrObj); + jmethodID mid = env->GetMethodID(jclz, "getConnectionInfo", "()Landroid/net/wifi/WifiInfo;"); + jobject wifiInfo = env->CallObjectMethod(wifiMgrObj, mid); + + env->DeleteLocalRef(jclz); + return wifiInfo; +} + +/* + * Get MAC address + * Parameters: wifiInfoObj, WifiInfo object + */ +char *getMacAddress(JNIEnv *env, jobject wifiInfoObj) { + qDebug() << "getMacAddress.... "; + if (wifiInfoObj == NULL) { + return NULL; + } + jclass jclz = env->GetObjectClass(wifiInfoObj); + jmethodID mid = env->GetMethodID(jclz, "getMacAddress", "()Ljava/lang/String;"); + jstring jstr_mac = (jstring)env->CallObjectMethod(wifiInfoObj, mid); + if (jstr_mac == NULL) { + env->DeleteLocalRef(jclz); + return NULL; + } + + const char *tmp = env->GetStringUTFChars(jstr_mac, NULL); + char *mac = (char *)malloc(strlen(tmp) + 1); + memcpy(mac, tmp, strlen(tmp) + 1); + env->ReleaseStringUTFChars(jstr_mac, tmp); + env->DeleteLocalRef(jclz); + return mac; +} + +/* + * Get MAC address + * Parameters: wifiInfoObj, WifiInfo object + */ +int getIpAddress(JNIEnv *env, jobject wifiInfoObj) { + qDebug() << "getIpAddress.... "; + if (wifiInfoObj == NULL) { + return NULL; + } + jclass jclz = env->GetObjectClass(wifiInfoObj); + jmethodID mid = env->GetMethodID(jclz, "getIpAddress", "()I"); + return env->CallIntMethod(wifiInfoObj, mid); +} +#endif + QHostAddress ProviderPrivate::getIP(const QHostAddress &srcAddress) { // Attempt to find the interface that corresponds with the provided // address and determine this device's address from the interface @@ -139,6 +231,14 @@ QHostAddress ProviderPrivate::getIP(const QHostAddress &srcAddress) { } } } +#ifdef Q_OS_ANDROID + QAndroidJniEnvironment env; + jobject wifiManagerObj = getWifiManagerObj(env, QtAndroid::androidContext().object()); + jobject wifiInfoObj = getWifiInfoObj(env, wifiManagerObj); + int ip = getIpAddress(env, wifiInfoObj); + QHostAddress qip = QHostAddress(qFromBigEndian(ip)); + qDebug() << "getIP from JNI" << qip; +#endif return QHostAddress(); } From 9b501b026f8283603b260bbe2fdcc67a2a141fd8 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Mon, 17 Oct 2022 11:57:25 +0200 Subject: [PATCH 5/8] fixing return --- src/src/provider.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/src/provider.cpp b/src/src/provider.cpp index 5a15435..d556a2d 100644 --- a/src/src/provider.cpp +++ b/src/src/provider.cpp @@ -238,6 +238,7 @@ QHostAddress ProviderPrivate::getIP(const QHostAddress &srcAddress) { int ip = getIpAddress(env, wifiInfoObj); QHostAddress qip = QHostAddress(qFromBigEndian(ip)); qDebug() << "getIP from JNI" << qip; + return qip; #endif return QHostAddress(); } From 2be37188d0c85b7bfbf302ebaa3b82d7903909aa Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Fri, 28 Oct 2022 23:40:11 +0200 Subject: [PATCH 6/8] fixing iOS broadcast permission issue qmdnsengine doesn't work anymore with iOS 16 or Ventura #1011 https://github.com/cagnulein/qdomyos-zwift/issues/1011 --- src/src/message.cpp | 16 ++++++++++++++++ src/src/server.cpp | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/src/message.cpp b/src/src/message.cpp index 9fe2a9f..2ed7d8c 100644 --- a/src/src/message.cpp +++ b/src/src/message.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "message_p.h" @@ -135,7 +136,22 @@ void Message::reply(const Message &other) { if (other.port() == MdnsPort) { if (other.address().protocol() == QAbstractSocket::IPv4Protocol) { +#ifdef Q_OS_IOS + const auto interfaces = QNetworkInterface::allInterfaces(); + for (const QNetworkInterface &networkInterface : interfaces) { + const auto entries = networkInterface.addressEntries(); + for (const QNetworkAddressEntry &entry : entries) { + for (const QNetworkAddressEntry &newEntry : entries) { + QHostAddress address = newEntry.ip(); + if ((address.protocol() == QAbstractSocket::IPv4Protocol) && newEntry.ip() != QHostAddress::LocalHost) { + setAddress(QHostAddress(newEntry.ip().toIPv4Address() | ( ~ newEntry.netmask().toIPv4Address() ))); + } + } + } + } +#else setAddress(MdnsIpv4Address); +#endif } else { setAddress(MdnsIpv6Address); } diff --git a/src/src/server.cpp b/src/src/server.cpp index e91566e..68a2ccd 100644 --- a/src/src/server.cpp +++ b/src/src/server.cpp @@ -154,6 +154,22 @@ void Server::sendMessageToAll(const Message &message) { QByteArray packet; toPacket(message, packet); +#ifdef Q_OS_IOS + const auto interfaces = QNetworkInterface::allInterfaces(); + for (const QNetworkInterface &networkInterface : interfaces) { + const auto entries = networkInterface.addressEntries(); + for (const QNetworkAddressEntry &entry : entries) { + for (const QNetworkAddressEntry &newEntry : entries) { + QHostAddress address = newEntry.ip(); + if ((address.protocol() == QAbstractSocket::IPv4Protocol) && newEntry.ip() != QHostAddress::LocalHost) { + QHostAddress a = QHostAddress(newEntry.ip().toIPv4Address() | ( ~ newEntry.netmask().toIPv4Address() )); + qDebug() << "dircon advertising bytes sent" << packet << d->ipv4Socket.writeDatagram(packet, a, MdnsPort); + } + } + } + } +#else d->ipv4Socket.writeDatagram(packet, MdnsIpv4Address, MdnsPort); +#endif d->ipv6Socket.writeDatagram(packet, MdnsIpv6Address, MdnsPort); } From 71019847ffbb933186eb14b10a6840283bb85d30 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Thu, 10 Nov 2022 04:18:39 +0100 Subject: [PATCH 7/8] Revert "fixing iOS broadcast permission issue" This reverts commit 2be37188d0c85b7bfbf302ebaa3b82d7903909aa. --- src/src/message.cpp | 16 ---------------- src/src/server.cpp | 16 ---------------- 2 files changed, 32 deletions(-) diff --git a/src/src/message.cpp b/src/src/message.cpp index 2ed7d8c..9fe2a9f 100644 --- a/src/src/message.cpp +++ b/src/src/message.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include "message_p.h" @@ -136,22 +135,7 @@ void Message::reply(const Message &other) { if (other.port() == MdnsPort) { if (other.address().protocol() == QAbstractSocket::IPv4Protocol) { -#ifdef Q_OS_IOS - const auto interfaces = QNetworkInterface::allInterfaces(); - for (const QNetworkInterface &networkInterface : interfaces) { - const auto entries = networkInterface.addressEntries(); - for (const QNetworkAddressEntry &entry : entries) { - for (const QNetworkAddressEntry &newEntry : entries) { - QHostAddress address = newEntry.ip(); - if ((address.protocol() == QAbstractSocket::IPv4Protocol) && newEntry.ip() != QHostAddress::LocalHost) { - setAddress(QHostAddress(newEntry.ip().toIPv4Address() | ( ~ newEntry.netmask().toIPv4Address() ))); - } - } - } - } -#else setAddress(MdnsIpv4Address); -#endif } else { setAddress(MdnsIpv6Address); } diff --git a/src/src/server.cpp b/src/src/server.cpp index 68a2ccd..e91566e 100644 --- a/src/src/server.cpp +++ b/src/src/server.cpp @@ -154,22 +154,6 @@ void Server::sendMessageToAll(const Message &message) { QByteArray packet; toPacket(message, packet); -#ifdef Q_OS_IOS - const auto interfaces = QNetworkInterface::allInterfaces(); - for (const QNetworkInterface &networkInterface : interfaces) { - const auto entries = networkInterface.addressEntries(); - for (const QNetworkAddressEntry &entry : entries) { - for (const QNetworkAddressEntry &newEntry : entries) { - QHostAddress address = newEntry.ip(); - if ((address.protocol() == QAbstractSocket::IPv4Protocol) && newEntry.ip() != QHostAddress::LocalHost) { - QHostAddress a = QHostAddress(newEntry.ip().toIPv4Address() | ( ~ newEntry.netmask().toIPv4Address() )); - qDebug() << "dircon advertising bytes sent" << packet << d->ipv4Socket.writeDatagram(packet, a, MdnsPort); - } - } - } - } -#else d->ipv4Socket.writeDatagram(packet, MdnsIpv4Address, MdnsPort); -#endif d->ipv6Socket.writeDatagram(packet, MdnsIpv6Address, MdnsPort); } From 575d343444780a88a25ddf9bed8bda0c23502385 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Fri, 7 Apr 2023 13:01:07 +0200 Subject: [PATCH 8/8] added the localip function to the main QZ --- src/src/provider.cpp | 125 +------------------------------------------ src/src/provider_p.h | 3 +- 2 files changed, 3 insertions(+), 125 deletions(-) diff --git a/src/src/provider.cpp b/src/src/provider.cpp index d556a2d..4fff244 100644 --- a/src/src/provider.cpp +++ b/src/src/provider.cpp @@ -33,12 +33,7 @@ #include #include "provider_p.h" - -#ifdef Q_OS_ANDROID -#include -#include -#include -#endif +#include "localipaddress.h" using namespace QMdnsEngine; @@ -127,122 +122,6 @@ void ProviderPrivate::publish() { announce(); } -#ifdef Q_OS_ANDROID - -/* - * Get WifiManager object - * Parameters: jCtxObj is Context object - */ -jobject getWifiManagerObj(JNIEnv *env, jobject jCtxObj) { - qDebug() << "gotWifiMangerObj "; - // Get the value of Context.WIFI_SERVICE - // jstring jstr_wifi_serveice = env->NewStringUTF("wifi"); - jclass jCtxClz = env->FindClass("android/content/Context"); - jfieldID fid_wifi_service = env->GetStaticFieldID(jCtxClz, "WIFI_SERVICE", "Ljava/lang/String;"); - jstring jstr_wifi_serveice = (jstring)env->GetStaticObjectField(jCtxClz, fid_wifi_service); - - jclass jclz = env->GetObjectClass(jCtxObj); - jmethodID mid_getSystemService = - env->GetMethodID(jclz, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); - jobject wifiManager = env->CallObjectMethod(jCtxObj, mid_getSystemService, jstr_wifi_serveice); - - // Because jclass inherits from jobject, it needs to be released; - // jfieldID and jmethodID are memory addresses, this memory is not allocated in our code, and we don’t need to - // release it. - env->DeleteLocalRef(jCtxClz); - env->DeleteLocalRef(jclz); - env->DeleteLocalRef(jstr_wifi_serveice); - - return wifiManager; -} - -/* - * Get the WifiInfo object - * Parameters: wifiMgrObj is the WifiManager object - */ -jobject getWifiInfoObj(JNIEnv *env, jobject wifiMgrObj) { - qDebug() << "getWifiInfoObj "; - if (wifiMgrObj == NULL) { - return NULL; - } - jclass jclz = env->GetObjectClass(wifiMgrObj); - jmethodID mid = env->GetMethodID(jclz, "getConnectionInfo", "()Landroid/net/wifi/WifiInfo;"); - jobject wifiInfo = env->CallObjectMethod(wifiMgrObj, mid); - - env->DeleteLocalRef(jclz); - return wifiInfo; -} - -/* - * Get MAC address - * Parameters: wifiInfoObj, WifiInfo object - */ -char *getMacAddress(JNIEnv *env, jobject wifiInfoObj) { - qDebug() << "getMacAddress.... "; - if (wifiInfoObj == NULL) { - return NULL; - } - jclass jclz = env->GetObjectClass(wifiInfoObj); - jmethodID mid = env->GetMethodID(jclz, "getMacAddress", "()Ljava/lang/String;"); - jstring jstr_mac = (jstring)env->CallObjectMethod(wifiInfoObj, mid); - if (jstr_mac == NULL) { - env->DeleteLocalRef(jclz); - return NULL; - } - - const char *tmp = env->GetStringUTFChars(jstr_mac, NULL); - char *mac = (char *)malloc(strlen(tmp) + 1); - memcpy(mac, tmp, strlen(tmp) + 1); - env->ReleaseStringUTFChars(jstr_mac, tmp); - env->DeleteLocalRef(jclz); - return mac; -} - -/* - * Get MAC address - * Parameters: wifiInfoObj, WifiInfo object - */ -int getIpAddress(JNIEnv *env, jobject wifiInfoObj) { - qDebug() << "getIpAddress.... "; - if (wifiInfoObj == NULL) { - return NULL; - } - jclass jclz = env->GetObjectClass(wifiInfoObj); - jmethodID mid = env->GetMethodID(jclz, "getIpAddress", "()I"); - return env->CallIntMethod(wifiInfoObj, mid); -} -#endif - -QHostAddress ProviderPrivate::getIP(const QHostAddress &srcAddress) { - // Attempt to find the interface that corresponds with the provided - // address and determine this device's address from the interface - - const auto interfaces = QNetworkInterface::allInterfaces(); - for (const QNetworkInterface &networkInterface : interfaces) { - const auto entries = networkInterface.addressEntries(); - for (const QNetworkAddressEntry &entry : entries) { - if (srcAddress.isInSubnet(entry.ip(), entry.prefixLength())) { - for (const QNetworkAddressEntry &newEntry : entries) { - QHostAddress address = newEntry.ip(); - if ((address.protocol() == QAbstractSocket::IPv4Protocol)) { - return address; - } - } - } - } - } -#ifdef Q_OS_ANDROID - QAndroidJniEnvironment env; - jobject wifiManagerObj = getWifiManagerObj(env, QtAndroid::androidContext().object()); - jobject wifiInfoObj = getWifiInfoObj(env, wifiManagerObj); - int ip = getIpAddress(env, wifiInfoObj); - QHostAddress qip = QHostAddress(qFromBigEndian(ip)); - qDebug() << "getIP from JNI" << qip; - return qip; -#endif - return QHostAddress(); -} - void ProviderPrivate::onMessageReceived(const Message &message) { if (!confirmed || message.isResponse()) { return; @@ -253,7 +132,7 @@ void ProviderPrivate::onMessageReceived(const Message &message) { bool sendSrv = false; bool sendTxt = false; - ARecord.setAddress(getIP(message.address())); + ARecord.setAddress(localipaddress::getIP(message.address())); // Determine which records to send based on the queries const auto queries = message.queries(); diff --git a/src/src/provider_p.h b/src/src/provider_p.h index 1abac68..543f2f7 100644 --- a/src/src/provider_p.h +++ b/src/src/provider_p.h @@ -47,8 +47,7 @@ class ProviderPrivate : public QObject { void announce(); void confirm(); void farewell(); - void publish(); - QHostAddress getIP(const QHostAddress &srcAddress); + void publish(); AbstractServer *server; Hostname *hostname;