| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#ifndef SERVICEBROWSER_P_H |
| 34 |
#define SERVICEBROWSER_P_H |
| 35 |
|
| 36 |
#include "dns_sd_types.h" |
| 37 |
#include "servicebrowser.h" |
| 38 |
|
| 39 |
#include <QtCore/QCoreApplication> |
| 40 |
#include <QtCore/QAtomicInt> |
| 41 |
#include <QtCore/QHash> |
| 42 |
#include <QtCore/QList> |
| 43 |
#include <QtCore/QMap> |
| 44 |
#include <QtCore/QMutex> |
| 45 |
#include <QtCore/QSharedPointer> |
| 46 |
#include <QtCore/QStringList> |
| 47 |
#include <QtCore/QThread> |
| 48 |
|
| 49 |
class QHostInfo; |
| 50 |
|
| 51 |
namespace ZeroConf { |
| 52 |
namespace Internal { |
| 53 |
|
| 54 |
class ServiceGatherer; |
| 55 |
class ServiceBrowserPrivate; |
| 56 |
|
| 57 |
|
| 58 |
class ZConfLib { |
| 59 |
Q_DECLARE_TR_FUNCTIONS(ZeroConf) |
| 60 |
public: |
| 61 |
typedef void *ConnectionRef; |
| 62 |
typedef void *BrowserRef; |
| 63 |
enum ProcessStatus { |
| 64 |
ProcessedOk, |
| 65 |
ProcessedQuit, |
| 66 |
ProcessedError, |
| 67 |
ProcessedFailure |
| 68 |
}; |
| 69 |
ZConfLib *fallbackLib; |
| 70 |
|
| 71 |
ZConfLib(ZConfLib *fallBack); |
| 72 |
virtual ~ZConfLib(); |
| 73 |
|
| 74 |
virtual QString name(); |
| 75 |
|
| 76 |
virtual bool tryStartDaemon(); |
| 77 |
|
| 78 |
virtual void refDeallocate(DNSServiceRef sdRef) = 0; |
| 79 |
virtual void browserDeallocate(BrowserRef *sdRef) = 0; |
| 80 |
virtual DNSServiceErrorType resolve(ConnectionRef cRef, DNSServiceRef *sdRef, uint32_t interfaceIndex, |
| 81 |
const char *name, const char *regtype, const char *domain, |
| 82 |
ServiceGatherer *gatherer) = 0; |
| 83 |
virtual DNSServiceErrorType queryRecord(ConnectionRef cRef, DNSServiceRef *sdRef, uint32_t interfaceIndex, |
| 84 |
const char *fullname, ServiceGatherer *gatherer) = 0; |
| 85 |
virtual DNSServiceErrorType getAddrInfo(ConnectionRef cRef, DNSServiceRef *sdRef, uint32_t interfaceIndex, |
| 86 |
DNSServiceProtocol protocol, const char *hostname, |
| 87 |
ServiceGatherer *gatherer) = 0; |
| 88 |
|
| 89 |
virtual DNSServiceErrorType reconfirmRecord(ConnectionRef cRef, uint32_t interfaceIndex, |
| 90 |
const char *name, const char *type, const char *domain, |
| 91 |
const char *fullname) = 0; |
| 92 |
virtual DNSServiceErrorType browse(ConnectionRef cRef, BrowserRef *sdRef, uint32_t interfaceIndex, |
| 93 |
const char *regtype, const char *domain, ServiceBrowserPrivate *browser) = 0; |
| 94 |
virtual DNSServiceErrorType getProperty(const char *property, void *result, uint32_t *size) = 0; |
| 95 |
virtual ProcessStatus processResult(ConnectionRef sdRef) = 0; |
| 96 |
virtual DNSServiceErrorType createConnection(ConnectionRef *sdRef) = 0; |
| 97 |
virtual void stopConnection(ConnectionRef cRef) = 0; |
| 98 |
virtual void destroyConnection(ConnectionRef *sdRef) = 0; |
| 99 |
bool isOk(); |
| 100 |
QString errorMsg(); |
| 101 |
void setError(bool failure, const QString &eMsg); |
| 102 |
|
| 103 |
static ZConfLib *createEmbeddedLib(const QString &daemonPath, ZConfLib *fallback=0); |
| 104 |
static ZConfLib *createNativeLib(const QString &libName, ZConfLib *fallback=0); |
| 105 |
static ZConfLib *createAvahiLib(const QString &libName, ZConfLib *fallback=0); |
| 106 |
static ZConfLib *defaultLib(); |
| 107 |
protected: |
| 108 |
bool m_isOk; |
| 109 |
QString m_errorMsg; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
class ServiceGatherer { |
| 114 |
public: |
| 115 |
QHash<QString, QString> txtRecord; |
| 116 |
QString hostName; |
| 117 |
ServiceBrowserPrivate *serviceBrowser; |
| 118 |
QHostInfo *host; |
| 119 |
Service::Ptr publishedService; |
| 120 |
Service *currentService; |
| 121 |
|
| 122 |
typedef QSharedPointer<ServiceGatherer> Ptr; |
| 123 |
|
| 124 |
enum Status{ |
| 125 |
ResolveConnectionFailed = 1 << 0, |
| 126 |
ResolveConnectionActive = 1 << 1, |
| 127 |
ResolveConnectionSuccess = 1 << 2, |
| 128 |
TxtConnectionFailed = 1 << 3, |
| 129 |
TxtConnectionActive = 1 << 4, |
| 130 |
TxtConnectionSuccess = 1 << 5, |
| 131 |
AddrConnectionFailed = 1 << 6, |
| 132 |
AddrConnectionActive = 1 << 7, |
| 133 |
AddrConnectionSuccess = 1 << 8 |
| 134 |
}; |
| 135 |
|
| 136 |
QString fullName(); |
| 137 |
void enactServiceChange(); |
| 138 |
void retireService(); |
| 139 |
Ptr gatherer(); |
| 140 |
|
| 141 |
void stopResolve(); |
| 142 |
void stopTxt(); |
| 143 |
void stopHostResolution(); |
| 144 |
void restartResolve(); |
| 145 |
void restartTxt(); |
| 146 |
void restartHostResolution(); |
| 147 |
|
| 148 |
bool currentServiceCanBePublished(); |
| 149 |
|
| 150 |
~ServiceGatherer(); |
| 151 |
static Ptr createGatherer(const QString &newService, const QString &newType, const QString &newDomain, |
| 152 |
const QString &fullName, uint32_t interfaceIndex, ServiceBrowserPrivate *serviceBrowser); |
| 153 |
|
| 154 |
void serviceResolveReply(DNSServiceFlags flags, uint32_t interfaceIndex, |
| 155 |
DNSServiceErrorType errorCode, const char *hosttarget, |
| 156 |
const QString &port, uint16_t txtLen, |
| 157 |
const unsigned char *rawTxtRecord); |
| 158 |
|
| 159 |
void txtRecordReply(DNSServiceFlags flags, DNSServiceErrorType errorCode, |
| 160 |
uint16_t txtLen, const void *rawTxtRecord, uint32_t ttl); |
| 161 |
|
| 162 |
void addrReply(DNSServiceFlags flags, DNSServiceErrorType errorCode, const char *hostname, const struct sockaddr *address, |
| 163 |
uint32_t ttl); |
| 164 |
void maybeRemove(); |
| 165 |
void stop(); |
| 166 |
void reload(qint32 interfaceIndex=0); |
| 167 |
void remove(); |
| 168 |
void reconfirm(); |
| 169 |
ZConfLib *lib(); |
| 170 |
private: |
| 171 |
ServiceGatherer(const QString &newService, const QString &newType, const QString &newDomain, |
| 172 |
const QString &fullName, uint32_t interfaceIndex, ServiceBrowserPrivate *serviceBrowser); |
| 173 |
|
| 174 |
DNSServiceRef resolveConnection; |
| 175 |
DNSServiceRef txtConnection; |
| 176 |
DNSServiceRef addrConnection; |
| 177 |
uint32_t interfaceIndex; |
| 178 |
|
| 179 |
quint32 status; |
| 180 |
QWeakPointer<ServiceGatherer> self; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
class ConnectionThread; |
| 186 |
|
| 187 |
class MainConnection{ |
| 188 |
Q_DECLARE_TR_FUNCTIONS(ZeroConf) |
| 189 |
public: |
| 190 |
enum RequestFlowStatus { |
| 191 |
NormalRFS, |
| 192 |
MoreComingRFS, |
| 193 |
ForceUpdateRFS |
| 194 |
}; |
| 195 |
RequestFlowStatus flowStatus; |
| 196 |
|
| 197 |
enum Status { |
| 198 |
Starting, |
| 199 |
Started, |
| 200 |
Running, |
| 201 |
Stopping, |
| 202 |
Stopped |
| 203 |
}; |
| 204 |
ZConfLib *lib; |
| 205 |
|
| 206 |
MainConnection(); |
| 207 |
~MainConnection(); |
| 208 |
QMutex *lock(); |
| 209 |
void waitStartup(); |
| 210 |
void stop(bool wait=true); |
| 211 |
void addBrowser(ServiceBrowserPrivate *browser); |
| 212 |
void removeBrowser(ServiceBrowserPrivate *browser); |
| 213 |
void updateFlowStatusForCancel(); |
| 214 |
void updateFlowStatusForFlags(DNSServiceFlags flags); |
| 215 |
void maybeUpdateLists(); |
| 216 |
void gotoValidLib(); |
| 217 |
void abortLib(); |
| 218 |
void createConnection(); |
| 219 |
void destroyConnection(); |
| 220 |
ZConfLib::ProcessStatus handleEvent(); |
| 221 |
bool increaseStatusTo(int s); |
| 222 |
void handleEvents(); |
| 223 |
ZConfLib::ConnectionRef mainRef(); |
| 224 |
|
| 225 |
QStringList errors(); |
| 226 |
void clearErrors(); |
| 227 |
void appendError(const QStringList &msgs,bool fullFailure); |
| 228 |
bool isOk(); |
| 229 |
private: |
| 230 |
mutable QMutex m_lock; |
| 231 |
QList<ServiceBrowserPrivate *> m_browsers; |
| 232 |
ZConfLib::ConnectionRef m_mainRef; |
| 233 |
bool m_failed; |
| 234 |
ConnectionThread *m_thread; |
| 235 |
QAtomicInt m_status; |
| 236 |
int m_nErrs; |
| 237 |
QStringList m_errors; |
| 238 |
}; |
| 239 |
|
| 240 |
class ServiceBrowserPrivate { |
| 241 |
friend class ServiceBrowser; |
| 242 |
public: |
| 243 |
ServiceBrowser *q; |
| 244 |
QString serviceType; |
| 245 |
QString domain; |
| 246 |
MainConnectionPtr mainConnection; |
| 247 |
ZConfLib::BrowserRef serviceConnection; |
| 248 |
DNSServiceFlags flags; |
| 249 |
uint32_t interfaceIndex; |
| 250 |
QList<QString> knownServices; |
| 251 |
QMap<QString, ServiceGatherer::Ptr> gatherers; |
| 252 |
QList<Service::ConstPtr> activeServices; |
| 253 |
QList<Service::ConstPtr> nextActiveServices; |
| 254 |
QList<ServiceGatherer::Ptr> pendingGatherers; |
| 255 |
bool failed; |
| 256 |
bool browsing; |
| 257 |
bool autoResolveAddresses; |
| 258 |
bool requireAddresses; |
| 259 |
|
| 260 |
ZConfLib::ConnectionRef mainRef(); |
| 261 |
void updateFlowStatusForCancel(); |
| 262 |
void updateFlowStatusForFlags(DNSServiceFlags flags); |
| 263 |
|
| 264 |
void pendingGathererAdd(ServiceGatherer::Ptr gatherer); |
| 265 |
|
| 266 |
ServiceBrowserPrivate(const QString &serviceType, const QString &domain, bool requireAddresses, |
| 267 |
MainConnectionPtr conn); |
| 268 |
~ServiceBrowserPrivate(); |
| 269 |
|
| 270 |
void insertGatherer(const QString &fullName); |
| 271 |
void maybeUpdateLists(); |
| 272 |
|
| 273 |
bool startBrowsing(quint32 interfaceIndex); |
| 274 |
void stopBrowsing(); |
| 275 |
void reconfirmService(Service::ConstPtr s); |
| 276 |
|
| 277 |
void browseReply(DNSServiceFlags flags, |
| 278 |
uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *serviceName, |
| 279 |
const char *regtype, const char *replyDomain); |
| 280 |
void serviceChanged(const Service::ConstPtr &oldService, const Service::ConstPtr &newService, ServiceBrowser *browser); |
| 281 |
void serviceAdded(const Service::ConstPtr &service, ServiceBrowser *browser); |
| 282 |
void serviceRemoved(const Service::ConstPtr &service, ServiceBrowser *browser); |
| 283 |
void servicesUpdated(ServiceBrowser *browser); |
| 284 |
void hadError(QStringList errorMsgs, bool completeFailure); |
| 285 |
}; |
| 286 |
|
| 287 |
class ConnectionThread: public QThread{ |
| 288 |
MainConnection &connection; |
| 289 |
|
| 290 |
void run(); |
| 291 |
public: |
| 292 |
ConnectionThread(MainConnection &mc, QObject *parent=0); |
| 293 |
}; |
| 294 |
|
| 295 |
extern "C" void DNSSD_API cServiceResolveReply(DNSServiceRef sdRef, DNSServiceFlags flags, |
| 296 |
uint32_t interfaceIndex, DNSServiceErrorType errorCode, |
| 297 |
const char *fullname, const char *hosttarget, uint16_t port, |
| 298 |
uint16_t txtLen, const unsigned char *txtRecord, void *context); |
| 299 |
extern "C" void DNSSD_API cTxtRecordReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, |
| 300 |
DNSServiceErrorType errorCode, const char *fullname, |
| 301 |
uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, |
| 302 |
const void *rdata, uint32_t ttl, void *context); |
| 303 |
extern "C" void DNSSD_API cAddrReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, |
| 304 |
DNSServiceErrorType errorCode, const char *hostname, |
| 305 |
const struct sockaddr *address, uint32_t ttl, void *context); |
| 306 |
extern "C" void DNSSD_API cBrowseReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, |
| 307 |
DNSServiceErrorType errorCode, const char *serviceName, |
| 308 |
const char *regtype, const char *replyDomain, void *context); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#endif |