File indexing completed on 2024-05-19 04:06:39

0001 /*
0002  * Copyright (C) 2005,2006  Justin Karneges
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the
0006  * "Software"), to deal in the Software without restriction, including
0007  * without limitation the rights to use, copy, modify, merge, publish,
0008  * distribute, sublicense, and/or sell copies of the Software, and to
0009  * permit persons to whom the Software is furnished to do so, subject to
0010  * the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included
0013  * in all copies or substantial portions of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0018  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0019  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0020  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0021  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0022  */
0023 
0024 // this is the Qt wrapper to jdns.  it requires Qt 4.1+
0025 
0026 #ifndef QJDNS_H
0027 #define QJDNS_H
0028 
0029 #include <QtCore>
0030 #include <QtNetwork>
0031 
0032 class QJDns : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036     enum Mode
0037     {
0038         Unicast,
0039         Multicast
0040     };
0041 
0042     enum PublishMode
0043     {
0044         Unique,
0045         Shared
0046     };
0047 
0048     enum Type
0049     {
0050         A       = 1,
0051         Aaaa    = 28,
0052         Mx      = 15,
0053         Srv     = 33,
0054         Cname   = 5,
0055         Ptr     = 12,
0056         Txt     = 16,
0057         Hinfo   = 13,
0058         Ns      = 2,
0059         Any     = 255
0060     };
0061 
0062     enum Error
0063     {
0064         ErrorGeneric,
0065         ErrorNXDomain, // query only
0066         ErrorTimeout,  // query only
0067         ErrorConflict  // publish only
0068     };
0069 
0070     class NameServer
0071     {
0072     public:
0073         QHostAddress address;
0074         int port;
0075 
0076         NameServer();
0077     };
0078 
0079     class DnsHost
0080     {
0081     public:
0082         QByteArray name;
0083         QHostAddress address;
0084     };
0085 
0086     class SystemInfo
0087     {
0088     public:
0089         QList<NameServer> nameServers;
0090         QList<QByteArray> domains;
0091         QList<DnsHost> hosts;
0092     };
0093 
0094     class Record
0095     {
0096     public:
0097         QByteArray owner;
0098         int ttl;
0099         int type;
0100         QByteArray rdata;
0101         bool haveKnown;
0102 
0103         // known
0104         QHostAddress address;    // for A, Aaaa
0105         QByteArray name;         // for Mx, Srv, Cname, Ptr, Ns
0106         int priority;            // for Mx, Srv
0107         int weight;              // for Srv
0108         int port;                // for Srv
0109         QList<QByteArray> texts; // for Txt
0110         QByteArray cpu;          // for Hinfo
0111         QByteArray os;           // for Hinfo
0112 
0113         Record();
0114         bool verify() const;
0115     };
0116 
0117     class Response
0118     {
0119     public:
0120         QList<Record> answerRecords;
0121         QList<Record> authorityRecords;
0122         QList<Record> additionalRecords;
0123     };
0124 
0125     QJDns(QObject *parent = 0);
0126     ~QJDns() override;
0127 
0128     bool init(Mode mode, const QHostAddress &address);
0129     void shutdown();
0130     QStringList debugLines();
0131 
0132     static SystemInfo systemInfo();
0133     static QHostAddress detectPrimaryMulticast(const QHostAddress &address);
0134 
0135     void setNameServers(const QList<NameServer> &list);
0136 
0137     int queryStart(const QByteArray &name, int type);
0138     void queryCancel(int id);
0139 
0140     // for multicast mode only
0141     int publishStart(PublishMode m, const Record &record);
0142     void publishUpdate(int id, const Record &record);
0143     void publishCancel(int id);
0144 
0145 signals:
0146     void resultsReady(int id, const QJDns::Response &results);
0147     void published(int id);
0148     void error(int id, QJDns::Error e);
0149     void shutdownFinished();
0150     void debugLinesReady();
0151 
0152 private:
0153     class Private;
0154     friend class Private;
0155     Private *d;
0156 };
0157 
0158 #endif