1 /*
2  * Copyright (C) 2007  Justin Karneges
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301  USA
18  *
19  */
20 
21 #ifndef QDNSSD_H
22 #define QDNSSD_H
23 
24 #include <QObject>
25 #include <QByteArray>
26 #include <QList>
27 
28 // DOR-compliant
29 class QDnsSd : public QObject
30 {
31 	Q_OBJECT
32 public:
33 	class LowLevelError
34 	{
35 	public:
36 		QString func;
37 		int code;
38 
LowLevelError()39 		LowLevelError() :
40 			code(0)
41 		{
42 		}
43 
LowLevelError(const QString & _func,int _code)44 		LowLevelError(const QString &_func, int _code) :
45 			func(_func),
46 			code(_code)
47 		{
48 		}
49 	};
50 
51 	class Record
52 	{
53 	public:
54 		bool added; // only used by QueryResult
55 
56 		QByteArray name;
57 		int rrtype;
58 		QByteArray rdata;
59 		quint32 ttl;
60 	};
61 
62 	class BrowseEntry
63 	{
64 	public:
65 		bool added;
66 		QByteArray serviceName;
67 
68 		// these may be different from request, see dns_sd docs
69 		QByteArray serviceType;
70 		QByteArray replyDomain;
71 	};
72 
73 	class QueryResult
74 	{
75 	public:
76 		bool success;
77 		LowLevelError lowLevelError;
78 
79 		QList<Record> records;
80 	};
81 
82 	class BrowseResult
83 	{
84 	public:
85 		bool success;
86 		LowLevelError lowLevelError;
87 
88 		QList<BrowseEntry> entries;
89 	};
90 
91 	class ResolveResult
92 	{
93 	public:
94 		bool success;
95 		LowLevelError lowLevelError;
96 
97 		QByteArray fullName;
98 		QByteArray hostTarget;
99 		int port; // host byte-order
100 		QByteArray txtRecord;
101 	};
102 
103 	class RegResult
104 	{
105 	public:
106 		enum Error
107 		{
108 			ErrorGeneric,
109 			ErrorConflict
110 		};
111 
112 		bool success;
113 		Error errorCode;
114 		LowLevelError lowLevelError;
115 
116 		QByteArray domain;
117 	};
118 
119 	QDnsSd(QObject *parent = 0);
120 	~QDnsSd();
121 
122 	int query(const QByteArray &name, int qType);
123 
124 	// domain may be empty
125 	int browse(const QByteArray &serviceType, const QByteArray &domain);
126 
127 	int resolve(const QByteArray &serviceName, const QByteArray &serviceType, const QByteArray &domain);
128 
129 	// domain may be empty
130 	int reg(const QByteArray &serviceName, const QByteArray &serviceType, const QByteArray &domain, int port, const QByteArray &txtRecord);
131 
132 	// return -1 on error, else a record id
133 	int recordAdd(int reg_id, const Record &rec, LowLevelError *lowLevelError = 0);
134 
135 	bool recordUpdate(int rec_id, const Record &rec, LowLevelError *lowLevelError = 0);
136 	bool recordUpdateTxt(int reg_id, const QByteArray &txtRecord, quint32 ttl, LowLevelError *lowLevelError = 0);
137 	void recordRemove(int rec_id);
138 
139 	void stop(int id);
140 
141 	// return empty array on error
142 	static QByteArray createTxtRecord(const QList<QByteArray> &strings);
143 
144 	// return empty list on error (note that it is possible to have a
145 	//   txt record with no entries, but in that case txtRecord will be
146 	//   empty and so you shouldn't call this function)
147 	static QList<QByteArray> parseTxtRecord(const QByteArray &txtRecord);
148 
149 signals:
150 	void queryResult(int id, const QDnsSd::QueryResult &result);
151 	void browseResult(int id, const QDnsSd::BrowseResult &result);
152 	void resolveResult(int id, const QDnsSd::ResolveResult &result);
153 	void regResult(int id, const QDnsSd::RegResult &result);
154 
155 private:
156 	class Private;
157 	friend class Private;
158 	Private *d;
159 };
160 
161 #endif
162