kdeconnect-kde/core/backends/lan/mdns_wrapper.h

93 lines
2.2 KiB
C
Raw Normal View History

/**
* SPDX-FileCopyrightText: 2023 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#ifndef KDECONNECT_MDNS_WRAPPER_H
#define KDECONNECT_MDNS_WRAPPER_H
#include <QHostAddress>
#include <QMap>
#include <QSocketNotifier>
#include <QString>
#include <QVector>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif
/*
* A Qt wrapper for the mdns.h header-only library
* from https://github.com/mjansson/mdns
*/
class MdnsWrapper : public QObject
{
Q_OBJECT
public:
struct MdnsService {
2023-07-07 17:03:01 +01:00
QString name;
uint16_t port;
QHostAddress address;
QMap<QString, QString> txtRecords;
};
2023-07-08 12:22:45 +01:00
// serviceType should be of the form _kdeconnect._udp.local
void startDiscovering(const QString &serviceType);
void stopDiscovering();
2023-07-08 12:22:45 +01:00
void sendQuery(const QString &serviceName);
Q_SIGNALS:
void serviceFound(const MdnsService &service);
private:
int listenForQueryResponses();
void stopListeningForQueryResponses();
QVector<QSocketNotifier *> responseSocketNotifiers;
};
2023-07-08 12:22:45 +01:00
class MdnsServiceAnnouncer : public QObject
{
Q_OBJECT
public:
struct AnnouncedInfo {
QByteArray serviceType; // ie: "<_service-type>._tcp.local."
QByteArray serviceInstance; // ie: "<service-name>.<_service-type>._tcp.local."
QByteArray hostname; // ie: "<hostname>.local."
uint16_t port;
QHash<QByteArray, QByteArray> txtRecords;
sockaddr_in address_ipv4;
sockaddr_in6 address_ipv6;
2023-07-08 12:22:45 +01:00
};
// serviceType should be of the form _kdeconnect._udp.local
MdnsServiceAnnouncer(const QString &serviceName, const QString &serviceType, uint16_t port);
2023-07-08 12:22:45 +01:00
void putTxtRecord(const QString &key, const QString &value)
{
self.txtRecords[key.toLatin1()] = value.toLatin1();
2023-07-08 12:22:45 +01:00
}
void startAnnouncing();
void stopAnnouncing();
void sendMulticastAnnounce(bool isGoodbye);
private:
int listenForQueries();
void stopListeningForQueries();
AnnouncedInfo self;
2023-07-08 12:22:45 +01:00
QSocketNotifier *socketNotifier = nullptr;
QSocketNotifier *socketNotifierV6 = nullptr;
};
#endif