ArnLib  4.0.x
Active Registry Network
ArnZeroConf.hpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #ifndef ARNZEROCONF_HPP
33 #define ARNZEROCONF_HPP
34 
35 #include "ArnLib_global.hpp"
36 #include "XStringMap.hpp"
37 #include "MQFlags.hpp"
38 #include <QHostAddress>
39 #include <QObject>
40 #include <QStringList>
41 #include <QString>
42 #include <QAbstractSocket>
43 #include <QAtomicInt>
44 
45 typedef struct _DNSServiceRef_t *DNSServiceRef;
46 class QSocketNotifier;
47 class QTimer;
48 class QHostInfo;
49 
50 
51 namespace ArnZeroConf {
53 struct Error {
54  enum E {
56  Ok = 0,
58  Running = -1,
60  BadReqSeq = -2,
62  Timeout = -3,
64  UDnsFail = -4
65  };
67 };
68 
71 struct State {
72  enum E {
74  None = 0x0000,
76  Registering = 0x0100,
78  Registered = 0x0001,
80  Register = 0x0101,
82  Browsing = 0x0200,
84  Resolving = 0x0400,
86  Resolved = 0x0004,
88  Resolve = 0x0404,
90  LookingUp = 0x0800,
92  Lookuped = 0x0008,
94  Lookup = 0x0808,
96  InProgress = 0x0f00
97  };
99 };
100 } // ArnZeroConf::
101 
103 
104 
105 
112 class ARNLIBSHARED_EXPORT ArnZeroConfB : public QObject
113 {
114  Q_OBJECT
115 public:
116  ArnZeroConfB( QObject* parent = arnNullptr);
117  virtual ~ArnZeroConfB();
118 
120 
128  QAbstractSocket::SocketType socketType() const;
129 
131 
135  void setSocketType( QAbstractSocket::SocketType type);
136 
138 
141  QString serviceType() const;
142 
144 
151  void setServiceType( const QString& type);
152 
154 
157  QString domain() const;
158 
160 
164  void setDomain( const QString& domain);
165 
167 
169  ArnZeroConf::State state() const;
170 
172 
179  QString fullServiceType() const;
180 
182  QByteArray escapedFullDomain() const;
183 
184 protected slots:
185  void socketData();
186 
187 protected:
189 
193  QStringList subTypes() const;
194 
196 
201  void setSubTypes( const QStringList& subTypes);
202 
204 
208  void addSubType( const QString& subType);
209 
211 
214  quint16 port() const;
215 
217 
222  void setPort( quint16 port);
223 
225 
228  QString serviceName() const;
229 
231 
236  void setServiceName( const QString& name);
237 
239 
244  QString host() const;
245 
247 
252  void setHost( const QString& host);
253 
255 
264  bool getTxtRecordMap( Arn::XStringMap& xsm);
265 
267 
273  void setTxtRecordMap( const Arn::XStringMap& xsm);
274 
276 
280  QHostAddress hostAddr() const;
281 
283 
286  void setHostAddr( const QHostAddress &hostAddr);
287 
289 
296  QByteArray txtRecord() const;
297 
299 
305  void setTxtRecord( const QByteArray& txt);
306 
308 
310  static int getNextId();
311 
312  void parseFullDomain( const QByteArray& domainName);
313 
314  static QByteArray escapedName( const QByteArray& name);
315 
316  QString _serviceName;
317  QString _domain;
318  QAbstractSocket::SocketType _socketType;
319  QString _serviceType;
320  QString _host;
321  QHostAddress _hostAddr;
322  QByteArray _txtRec;
323 
324  ArnZeroConf::State _state;
325  _DNSServiceRef_t* _sdRef;
326  QStringList _serviceSubTypes;
327  int _iface;
328  QString _txtRecord;
329 
330  QSocketNotifier* _notifier;
331 
332  // Source for unique id to all discovered services ...
333  static QAtomicInt _idCount;
335 
336 private:
337  quint16 _port;
338 };
339 
340 
342 
367 {
368  friend class ArnZeroConfIntern;
369  Q_OBJECT
370 public:
372 
375  ArnZeroConfRegister( QObject* parent = arnNullptr);
376 
378 
384  ArnZeroConfRegister( const QString& serviceName, QObject* parent = arnNullptr);
385 
387 
397  ArnZeroConfRegister( const QString& serviceName, const QString& serviceType, quint16 port,
398  QObject* parent = arnNullptr);
399 
401 
403  virtual ~ArnZeroConfRegister();
404 
406 
410  QStringList subTypes() const
411  {return ArnZeroConfB::subTypes();}
412 
414 
419  void setSubTypes( const QStringList& subtypes)
420  {ArnZeroConfB::setSubTypes( subtypes);}
421 
423 
427  void addSubType( const QString& subtype)
428  {ArnZeroConfB::addSubType( subtype);}
429 
431 
434  quint16 port() const
435  {return ArnZeroConfB::port();}
436 
438 
443  void setPort( quint16 port)
444  {ArnZeroConfB::setPort( port);}
445 
447 
454  QString serviceName() const
455  {return ArnZeroConfB::serviceName();}
456 
458 
465  QString currentServiceName() const;
466 
468 
479  void setServiceName( const QString& name);
480 
482 
487  QString host() const
488  {return ArnZeroConfB::host();}
489 
491 
496  void setHost( const QString& host)
497  {ArnZeroConfB::setHost( host);}
498 
500 
510  {return ArnZeroConfB::getTxtRecordMap( xsm);}
511 
513 
520  {ArnZeroConfB::setTxtRecordMap( xsm);}
521 
523 
530  QByteArray txtRecord() const
531  {return ArnZeroConfB::txtRecord();}
532 
534 
540  void setTxtRecord( const QByteArray& txt)
541  {ArnZeroConfB::setTxtRecord( txt);}
542 
544 
553  void registerService( bool noAutoRename = false);
554 
556 
559  void releaseService();
560 
561 signals:
563 
570  void registered( const QString& serviceName);
571 
573 
576  void registrationError( int code);
577 
578 private:
579  void init();
580 
581  QString _currentServiceName;
582 };
583 
584 
586 
617 {
618  friend class ArnZeroConfIntern;
619  Q_OBJECT
620 public:
622 
624  ArnZeroConfResolve( QObject* parent = arnNullptr);
625 
627 
632  ArnZeroConfResolve( const QString& serviceName, QObject* parent = arnNullptr);
633 
635 
644  ArnZeroConfResolve( const QString& serviceName, const QString& serviceType, QObject* parent = arnNullptr);
645 
647 
649  virtual ~ArnZeroConfResolve();
650 
652 
655  int id() const;
656 
658 
664  void setId(int id);
665 
667 
670  QString host() const
671  {return ArnZeroConfB::host();}
672 
674 
676  quint16 port() const
677  {return ArnZeroConfB::port();}
678 
680 
682  QString serviceName() const
683  {return ArnZeroConfB::serviceName();}
684 
686 
691  void setServiceName( const QString& name)
692  {ArnZeroConfB::setServiceName( name);}
693 
695 
704  {return ArnZeroConfB::getTxtRecordMap( xsm);}
705 
707 
713  QByteArray txtRecord() const
714  {return ArnZeroConfB::txtRecord();}
715 
717 
727  void resolve( bool forceMulticast = false);
728 
730 
732  void releaseResolve();
733 
734 signals:
736 
740  void resolved( int id, const QByteArray& escFullDomain);
741 
743 
747  void resolveError( int id, int code);
748 
749 private slots:
750  void operationTimeout();
751 
752 private:
753  void init();
754 
755  int _id;
756  QTimer* _operationTimer;
757 };
758 
759 
761 
784 {
785  friend class ArnZeroConfIntern;
786  Q_OBJECT
787 public:
789 
791  ArnZeroConfLookup( QObject* parent = arnNullptr);
792 
794 
798  ArnZeroConfLookup( const QString& hostName, QObject* parent = arnNullptr);
799 
801 
803  virtual ~ArnZeroConfLookup();
804 
806 
809  int id() const;
810 
812 
818  void setId(int id);
819 
821 
824  QString host() const
825  {return ArnZeroConfB::host();}
826 
828 
832  void setHost( const QString& host)
833  {ArnZeroConfB::setHost( host);}
834 
836 
838  QHostAddress hostAddr() const
839  {return ArnZeroConfB::hostAddr();}
840 
842 
851  void lookup( bool forceMulticast = false);
852 
854 
856  void releaseLookup();
857 
859 
862  static bool isForceQtDnsLookup();
863 
865 
872  static void setForceQtDnsLookup( bool isForceQtDnsLookup);
873 
874 signals:
876 
879  void lookuped( int id);
880 
882 
886  void lookupError( int id, int code);
887 
888 private slots:
889  void operationTimeout();
890  void onIpLookup( const QHostInfo& host);
891 
892 private:
893  void init();
894 
895  int _id;
896  QTimer* _operationTimer;
897 
898  static bool _isForceQtDnsLookup;
899 };
900 
901 
903 
924 
937 {
938  friend class ArnZeroConfIntern;
939  Q_OBJECT
940 public:
942 
945  ArnZeroConfBrowser( QObject* parent = arnNullptr);
946 
948 
955  ArnZeroConfBrowser( const QString& serviceType, QObject* parent = arnNullptr);
956 
958 
960  virtual ~ArnZeroConfBrowser();
961 
963 
971  void setSubType( const QString& subtype);
972 
974 
978  QString subType();
979 
981 
984  QStringList activeServiceNames() const;
985 
987 
991  int serviceNameToId( const QString& name);
992 
994 
997  bool isBrowsing() const;
998 
1000 
1002  static int getNextId()
1003  {return ArnZeroConfB::getNextId();}
1004 
1005 public slots:
1007 
1011  void browse( bool enable = true);
1012 
1014 
1016  void stopBrowse();
1017 
1018 signals:
1020 
1029  void serviceChanged( bool isAdded, int id, const QString& serviceName, const QString& domain);
1030 
1032 
1039  void serviceAdded( int id, const QString& serviceName, const QString& domain);
1040 
1042 
1048  void serviceRemoved( int id, const QString& serviceName, const QString& domain);
1049 
1051 
1054  void browseError( int errorCode);
1055 
1056 private:
1057  void init();
1058 
1059  QMap<QString,int> _activeServiceNames;
1060 };
1061 
1062 #endif // ARNZEROCONF_HPP
QString serviceName() const
Returns the service name for this Zero Config.
Registering a ZeroConfig service.
isAny(): Operation in progress
Definition: ArnZeroConf.hpp:96
Lookup a host.
void setTxtRecordMap(const Arn::XStringMap &xsm)
Save a XStringMap with parameters to the Txt Record.
void setHost(const QString &host)
Set the host name for this Lookup.
static int getNextId()
Return the next id number for zero config objects.
QByteArray txtRecord() const
Return the Txt Record for this Zero Config.
void setPort(quint16 port)
Sets the port number for connecting to the service.
quint16 port() const
Returns the port number for connecting to the service.
QString serviceName() const
Returns the service name used for this resolv.
Container class with string representation for serialized data.
Definition: XStringMap.hpp:107
Operation in progress.
Definition: ArnZeroConf.hpp:58
void setServiceName(const QString &name)
Set the service name used for this resolv.
Browsing for ZeroConfig services.
Ok, defined as kDNSServiceErr_NoError in dns_sd.h.
Definition: ArnZeroConf.hpp:56
#define MQ_DECLARE_FLAGS(FEStruct)
Flags.
Definition: MQFlagsBase.hpp:49
Base class for Zero Config.
QString host() const
Returns the host name for this resolv.
Registering service in progress.
Definition: ArnZeroConf.hpp:76
Resolving service has finished sucessfully.
Definition: ArnZeroConf.hpp:86
void setTxtRecord(const QByteArray &txt)
Set the Txt Record for this Zero Config.
Inactive state.
Definition: ArnZeroConf.hpp:74
QByteArray txtRecord() const
Return the Txt Record for this Zero Config.
void setSubTypes(const QStringList &subtypes)
Sets the list of current subtypes.
isAny(): Resolving service in progress or has finished sucessfully
Definition: ArnZeroConf.hpp:88
QStringList subTypes() const
Returns the list of current subtypes.
Lookup host in progress.
Definition: ArnZeroConf.hpp:90
void addSubType(const QString &subtype)
Add a subtype to the list of current subtypes.
#define MQ_DECLARE_OPERATORS_FOR_FLAGS(FEStruct)
Definition: MQFlagsBase.hpp:65
struct _DNSServiceRef_t * DNSServiceRef
Definition: ArnZeroConf.hpp:45
QHostAddress hostAddr() const
Returns the host address for this Lookup.
Operation timeout.
Definition: ArnZeroConf.hpp:62
Lookup host has finished sucessfully.
Definition: ArnZeroConf.hpp:92
bool getTxtRecordMap(Arn::XStringMap &xsm)
Load a XStringMap with parameters from the Txt Record.
States of ZeroConfig, limited valid for each ArnZeroConfB subclass / These values must be synced with...
Definition: ArnZeroConf.hpp:71
Bad request sequence.
Definition: ArnZeroConf.hpp:60
void setHost(const QString &host)
Set the host name for this Zero Config.
Resolv a ZeroConfig service.
quint16 port() const
Returns the port number for connecting to the service.
Resolving service in progress.
Definition: ArnZeroConf.hpp:84
QString host() const
Returns the host name for this Zero Config.
Browsing for service in progress.
Definition: ArnZeroConf.hpp:82
#define ARNLIBSHARED_EXPORT
bool getTxtRecordMap(Arn::XStringMap &xsm)
Load a XStringMap with parameters from the Txt Record.
isAny(): Lookup host in progress or has finished sucessfully
Definition: ArnZeroConf.hpp:94
Unicast DNS lookup fail.
Definition: ArnZeroConf.hpp:64
#define MQ_DECLARE_ENUM(EStruct)
Enums.
Definition: MQFlagsBase.hpp:70
Errors of ZeroConfig, other values are defined in dns_sd.h.
Definition: ArnZeroConf.hpp:53
Registering service has finished sucessfully.
Definition: ArnZeroConf.hpp:78
isAny(): Registering service in progress or has finished sucessfully
Definition: ArnZeroConf.hpp:80
QString host() const
Returns the host name for this Lookup.