Torc  0.1
torcbonjour.h
Go to the documentation of this file.
1 #ifndef TORCBONJOUR_H
2 #define TORCBONJOUR_H
3 
4 // Qt
5 #include <QMutex>
6 #include <QObject>
7 #include <QHostInfo>
8 #include <QSocketNotifier>
9 
10 // DNS Service Discovery
11 #ifndef WIN32
12 #include <dns_sd.h>
13 #endif
14 
15 #define TORC_BONJOUR_HOST QStringLiteral("host")
16 #define TORC_BONJOUR_ADDRESSES QStringLiteral("addresses")
17 #define TORC_BONJOUR_TXT QStringLiteral("txtrecords")
18 #define TORC_BONJOUR_NAME QStringLiteral("name")
19 #define TORC_BONJOUR_TYPE QStringLiteral("type")
20 #define TORC_BONJOUR_PORT QStringLiteral("port")
21 
22 class TorcBonjour;
23 
25 {
26  public:
28  {
32  };
33 
34  public:
37 #ifndef WIN32
38  TorcBonjourService(ServiceType BonjourType, DNSServiceRef DNSSRef, const QByteArray &Name, const QByteArray &Type);
39 #endif
40  TorcBonjourService(ServiceType BonjourType, const QByteArray &Name, const QByteArray &Type, const QByteArray &Domain, uint32_t InterfaceIndex);
42  ~TorcBonjourService() = default;
43  void SetFileDescriptor(int FileDescriptor, TorcBonjour *Object);
44  bool IsResolved(void);
45  void Deregister(void);
46 
47  public:
49 #ifndef WIN32
50  DNSServiceRef m_dnssRef;
51 #endif
52  QByteArray m_name;
53  QByteArray m_type;
54  QByteArray m_txt;
55  QByteArray m_domain;
56  uint32_t m_interfaceIndex;
57  QByteArray m_host;
58  QList<QHostAddress> m_ipAddresses;
59  int m_port;
61  int m_fd;
62  QSocketNotifier *m_socketNotifier;
63 };
64 
65 class TorcBonjour : public QObject
66 {
67  Q_OBJECT
68 
69  public:
70  static TorcBonjour* Instance (void);
71  static void Suspend (bool Suspend);
72  static void TearDown (void);
73  static QByteArray MapToTxtRecord (const QMap<QByteArray,QByteArray> &Map);
74  static QMap<QByteArray,QByteArray> TxtRecordToMap(const QByteArray &TxtRecord);
75 
76  quint32 Register (quint16 Port, const QByteArray &Type,
77  const QByteArray &Name, const QMap<QByteArray,QByteArray> &TxtRecords, quint32 Reference = 0);
78  quint32 Register (quint16 Port, const QByteArray &Type,
79  const QByteArray &Name, const QByteArray &Txt, quint32 Reference = 0);
80  quint32 Browse (const QByteArray &Type, quint32 Reference = 0);
81  void Deregister (quint32 Reference);
82 
83  // callback handlers
84 #ifndef WIN32
85  void AddBrowseResult(DNSServiceRef Reference, const TorcBonjourService &Service);
86  void RemoveBrowseResult(DNSServiceRef Reference, const TorcBonjourService &Service);
87  void Resolve(DNSServiceRef Reference, DNSServiceErrorType ErrorType, const char *Fullname,
88  const char *HostTarget, uint16_t Port, uint16_t TxtLen,
89  const unsigned char *TxtRecord);
90 #endif
91  public slots:
92  void socketReadyRead (int Socket);
93 
94  private slots:
95  void SuspendPriv (bool Suspend);
96  void HostLookup (const QHostInfo &HostInfo);
97 
98 
99  protected:
100  TorcBonjour();
101  ~TorcBonjour();
102 
103  private:
104  bool m_suspended;
105  QMutex m_serviceLock;
106  QMap<quint32,TorcBonjourService> m_services;
107  QMutex m_discoveredLock;
108  QMap<quint32,TorcBonjourService> m_discoveredServices;
109 };
110 
111 #endif
QList< QHostAddress > m_ipAddresses
Definition: torcbonjour.h:58
Address resolution for a discovered service.
Definition: torcbonjour.h:31
uint32_t m_interfaceIndex
Definition: torcbonjour.h:56
~TorcBonjourService()=default
DNSServiceRef m_dnssRef
Definition: torcbonjour.h:50
QSocketNotifier * m_socketNotifier
Definition: torcbonjour.h:62
bool IsResolved(void)
Returns true when the service has been fully resolved to an IP address and port.
QByteArray m_type
Definition: torcbonjour.h:53
QByteArray m_txt
Definition: torcbonjour.h:54
QByteArray m_name
Definition: torcbonjour.h:52
void Deregister(void)
Release all resources associated with this service.
QByteArray m_domain
Definition: torcbonjour.h:55
QByteArray m_host
Definition: torcbonjour.h:57
Wrapper around a DNS service reference, either advertised or discovered.
Definition: torcbonjour.h:24
A service being advertised by this application.
Definition: torcbonjour.h:29
TorcBonjourService & operator=(const TorcBonjourService &Other)
Advertises and searches for services via Bonjour (aka Zero Configuration Networking/Avahi) ...
Definition: torcbonjour.h:65
An external service which we are actively trying to discover.
Definition: torcbonjour.h:30
void SetFileDescriptor(int FileDescriptor, TorcBonjour *Object)
Sets the file descriptor and creates a QSocketNotifier to listen for socket events.
ServiceType m_serviceType
Definition: torcbonjour.h:48