Rename things

This commit is contained in:
Albert Vaca Cintora 2023-07-10 12:11:38 +02:00
parent bbf053ed5a
commit c94215ce05
4 changed files with 35 additions and 26 deletions

View file

@ -16,6 +16,9 @@
#include <QNetworkInterface> #include <QNetworkInterface>
#include <QSocketNotifier> #include <QSocketNotifier>
namespace MdnsWrapper
{
const char *recordTypeToStr(int rtype) const char *recordTypeToStr(int rtype)
{ {
switch (rtype) { switch (rtype) {
@ -56,7 +59,7 @@ static int query_callback(int sock, const struct sockaddr* from, size_t addrlen,
//qCDebug(KDECONNECT_CORE) << "Received DNS record of type" << recordTypeToStr(record_type) << "from socket" << sock << "with type" << entryTypeToStr(entry_type); //qCDebug(KDECONNECT_CORE) << "Received DNS record of type" << recordTypeToStr(record_type) << "from socket" << sock << "with type" << entryTypeToStr(entry_type);
MdnsWrapper::MdnsService *discoveredService = (MdnsWrapper::MdnsService *)user_data; Discoverer::MdnsService *discoveredService = (Discoverer::MdnsService *)user_data;
switch (record_type) { switch (record_type) {
case MDNS_RECORDTYPE_PTR: { case MDNS_RECORDTYPE_PTR: {
@ -104,7 +107,7 @@ static int query_callback(int sock, const struct sockaddr* from, size_t addrlen,
return 0; return 0;
} }
void MdnsWrapper::startDiscovering(const QString &serviceType) void Discoverer::startDiscovering(const QString &serviceType)
{ {
int num_sockets = listenForQueryResponses(); int num_sockets = listenForQueryResponses();
if (num_sockets <= 0) { if (num_sockets <= 0) {
@ -114,12 +117,12 @@ void MdnsWrapper::startDiscovering(const QString &serviceType)
sendQuery(serviceType); sendQuery(serviceType);
} }
void MdnsWrapper::stopDiscovering() void Discoverer::stopDiscovering()
{ {
stopListeningForQueryResponses(); stopListeningForQueryResponses();
} }
void MdnsWrapper::stopListeningForQueryResponses() void Discoverer::stopListeningForQueryResponses()
{ {
qCDebug(KDECONNECT_CORE) << "Closing" << responseSocketNotifiers.size() << "sockets"; qCDebug(KDECONNECT_CORE) << "Closing" << responseSocketNotifiers.size() << "sockets";
for (QSocketNotifier *socketNotifier : responseSocketNotifiers) { for (QSocketNotifier *socketNotifier : responseSocketNotifiers) {
@ -129,7 +132,7 @@ void MdnsWrapper::stopListeningForQueryResponses()
responseSocketNotifiers.clear(); responseSocketNotifiers.clear();
} }
int MdnsWrapper::listenForQueryResponses() int Discoverer::listenForQueryResponses()
{ {
// Open a socket for each interface // Open a socket for each interface
QVector<int> sockets; QVector<int> sockets;
@ -176,7 +179,7 @@ int MdnsWrapper::listenForQueryResponses()
return sockets.size(); return sockets.size();
} }
void MdnsWrapper::sendQuery(const QString &serviceType) void Discoverer::sendQuery(const QString &serviceType)
{ {
qCDebug(KDECONNECT_CORE) << "Sending MDNS query for service" << serviceType; qCDebug(KDECONNECT_CORE) << "Sending MDNS query for service" << serviceType;
@ -204,7 +207,7 @@ static mdns_string_t createMdnsString(const QByteArray &str)
return mdns_string_t{str.constData(), (size_t)str.length()}; return mdns_string_t{str.constData(), (size_t)str.length()};
} }
static mdns_record_t createMdnsRecord(const MdnsServiceAnnouncer::AnnouncedInfo &self, static mdns_record_t createMdnsRecord(const Announcer::AnnouncedInfo &self,
mdns_record_type_t record_type, mdns_record_type_t record_type,
QHash<QByteArray, QByteArray>::const_iterator txtIterator = {}) QHash<QByteArray, QByteArray>::const_iterator txtIterator = {})
{ {
@ -252,9 +255,9 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle
Q_UNUSED(name_length); Q_UNUSED(name_length);
Q_UNUSED(record_offset); Q_UNUSED(record_offset);
Q_UNUSED(record_length); Q_UNUSED(record_length);
static char sendbuffer[2048]; static char sendbuffer[2024];
const MdnsServiceAnnouncer::AnnouncedInfo &self = *((MdnsServiceAnnouncer::AnnouncedInfo *)user_data); const Announcer::AnnouncedInfo &self = *((Announcer::AnnouncedInfo *)user_data);
if (entry_type != MDNS_ENTRYTYPE_QUESTION) { if (entry_type != MDNS_ENTRYTYPE_QUESTION) {
return 0; return 0;
@ -419,7 +422,7 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle
// Open sockets to listen to incoming mDNS queries on port 5353 // Open sockets to listen to incoming mDNS queries on port 5353
// When recieving, each socket can recieve data from all network interfaces // When recieving, each socket can recieve data from all network interfaces
// Thus we only need to open one socket for each address family // Thus we only need to open one socket for each address family
int MdnsServiceAnnouncer::listenForQueries() int Announcer::listenForQueries()
{ {
auto callback = [this](QSocketDescriptor socket) { auto callback = [this](QSocketDescriptor socket) {
static char buffer[2048]; static char buffer[2048];
@ -469,7 +472,7 @@ int MdnsServiceAnnouncer::listenForQueries()
return numSockets; return numSockets;
} }
MdnsServiceAnnouncer::MdnsServiceAnnouncer(const QString &serviceName, const QString &serviceType, uint16_t port) Announcer::Announcer(const QString &serviceName, const QString &serviceType, uint16_t port)
{ {
self.serviceType = serviceType.toLatin1(); self.serviceType = serviceType.toLatin1();
if (!self.serviceType.endsWith('.')) { if (!self.serviceType.endsWith('.')) {
@ -483,7 +486,7 @@ MdnsServiceAnnouncer::MdnsServiceAnnouncer(const QString &serviceName, const QSt
memset(&self.address_ipv6, 0, sizeof(struct sockaddr_in6)); memset(&self.address_ipv6, 0, sizeof(struct sockaddr_in6));
} }
void MdnsServiceAnnouncer::startAnnouncing() void Announcer::startAnnouncing()
{ {
int num_sockets = listenForQueries(); int num_sockets = listenForQueries();
if (num_sockets <= 0) { if (num_sockets <= 0) {
@ -493,14 +496,13 @@ void MdnsServiceAnnouncer::startAnnouncing()
sendMulticastAnnounce(false); sendMulticastAnnounce(false);
} }
void MdnsServiceAnnouncer::stopAnnouncing() void Announcer::stopAnnouncing()
{ {
sendMulticastAnnounce(true); sendMulticastAnnounce(true);
stopListeningForQueries(); stopListeningForQueries();
} }
void Announcer::stopListeningForQueries()
void MdnsServiceAnnouncer::stopListeningForQueries()
{ {
if (socketNotifier != nullptr) { if (socketNotifier != nullptr) {
delete socketNotifier; delete socketNotifier;
@ -512,7 +514,7 @@ void MdnsServiceAnnouncer::stopListeningForQueries()
} }
} }
void MdnsServiceAnnouncer::sendMulticastAnnounce(bool isGoodbye) void Announcer::sendMulticastAnnounce(bool isGoodbye)
{ {
mdns_record_t ptr_record = createMdnsRecord(self, MDNS_RECORDTYPE_PTR); mdns_record_t ptr_record = createMdnsRecord(self, MDNS_RECORDTYPE_PTR);
@ -541,3 +543,5 @@ void MdnsServiceAnnouncer::sendMulticastAnnounce(bool isGoodbye)
} }
} }
} // namespace MdnsWrapper

View file

@ -23,7 +23,10 @@
* A Qt wrapper for the mdns.h header-only library * A Qt wrapper for the mdns.h header-only library
* from https://github.com/mjansson/mdns * from https://github.com/mjansson/mdns
*/ */
class MdnsWrapper : public QObject namespace MdnsWrapper
{
class Discoverer : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -51,7 +54,7 @@ private:
QVector<QSocketNotifier *> responseSocketNotifiers; QVector<QSocketNotifier *> responseSocketNotifiers;
}; };
class MdnsServiceAnnouncer : public QObject class Announcer : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -60,14 +63,14 @@ public:
QByteArray serviceType; // ie: "<_service-type>._tcp.local." QByteArray serviceType; // ie: "<_service-type>._tcp.local."
QByteArray serviceInstance; // ie: "<service-name>.<_service-type>._tcp.local." QByteArray serviceInstance; // ie: "<service-name>.<_service-type>._tcp.local."
QByteArray hostname; // ie: "<hostname>.local." QByteArray hostname; // ie: "<hostname>.local."
uint16_t port;
QHash<QByteArray, QByteArray> txtRecords;
sockaddr_in address_ipv4; sockaddr_in address_ipv4;
sockaddr_in6 address_ipv6; sockaddr_in6 address_ipv6;
uint16_t port;
QHash<QByteArray, QByteArray> txtRecords;
}; };
// serviceType should be of the form _kdeconnect._udp.local // serviceType should be of the form _kdeconnect._udp.local
MdnsServiceAnnouncer(const QString &serviceName, const QString &serviceType, uint16_t port); Announcer(const QString &serviceName, const QString &serviceType, uint16_t port);
void putTxtRecord(const QString &key, const QString &value) void putTxtRecord(const QString &key, const QString &value)
{ {
@ -89,4 +92,6 @@ private:
QSocketNotifier *socketNotifierV6 = nullptr; QSocketNotifier *socketNotifierV6 = nullptr;
}; };
} // namespace MdnsWrapper
#endif #endif

View file

@ -24,7 +24,7 @@ MdnsDiscovery::MdnsDiscovery(LanLinkProvider *lanLinkProvider)
mdnsAnnouncer.putTxtRecord(QStringLiteral("type"), config.deviceType().toString()); mdnsAnnouncer.putTxtRecord(QStringLiteral("type"), config.deviceType().toString());
mdnsAnnouncer.putTxtRecord(QStringLiteral("protocol"), QString::number(NetworkPacket::s_protocolVersion)); mdnsAnnouncer.putTxtRecord(QStringLiteral("protocol"), QString::number(NetworkPacket::s_protocolVersion));
connect(&mdnsWrapper, &MdnsWrapper::serviceFound, [lanLinkProvider](const MdnsWrapper::MdnsService &service) { connect(&mdnsDiscoverer, &MdnsWrapper::Discoverer::serviceFound, [lanLinkProvider](const MdnsWrapper::Discoverer::MdnsService &service) {
if (KdeConnectConfig::instance().deviceId() == service.name) { if (KdeConnectConfig::instance().deviceId() == service.name) {
qCDebug(KDECONNECT_CORE) << "Discovered myself, ignoring"; qCDebug(KDECONNECT_CORE) << "Discovered myself, ignoring";
return; return;
@ -52,10 +52,10 @@ void MdnsDiscovery::stopAnnouncing()
void MdnsDiscovery::startDiscovering() void MdnsDiscovery::startDiscovering()
{ {
mdnsWrapper.startDiscovering(kServiceType); mdnsDiscoverer.startDiscovering(kServiceType);
} }
void MdnsDiscovery::stopDiscovering() void MdnsDiscovery::stopDiscovering()
{ {
mdnsWrapper.stopDiscovering(); mdnsDiscoverer.stopDiscovering();
} }

View file

@ -31,8 +31,8 @@ public:
private: private:
LanLinkProvider *lanLinkProvider = nullptr; LanLinkProvider *lanLinkProvider = nullptr;
MdnsWrapper mdnsWrapper; MdnsWrapper::Discoverer mdnsDiscoverer;
MdnsServiceAnnouncer mdnsAnnouncer; MdnsWrapper::Announcer mdnsAnnouncer;
}; };
#endif // KDECONNECT_SERVER_H #endif // KDECONNECT_SERVER_H