| 1 | /************************************************************************** |
| 2 | ** |
| 3 | ** This file is part of Qt Creator |
| 4 | ** |
| 5 | ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies). |
| 6 | ** |
| 7 | ** Contact: Nokia Corporation ([email protected]) |
| 8 | ** |
| 9 | ** |
| 10 | ** GNU Lesser General Public License Usage |
| 11 | ** |
| 12 | ** This file may be used under the terms of the GNU Lesser General Public |
| 13 | ** License version 2.1 as published by the Free Software Foundation and |
| 14 | ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
| 15 | ** Please review the following information to ensure the GNU Lesser General |
| 16 | ** Public License version 2.1 requirements will be met: |
| 17 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 18 | ** |
| 19 | ** In addition, as a special exception, Nokia gives you certain additional |
| 20 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
| 21 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 22 | ** |
| 23 | ** Other Usage |
| 24 | ** |
| 25 | ** Alternatively, this file may be used in accordance with the terms and |
| 26 | ** conditions contained in a signed written agreement between you and Nokia. |
| 27 | ** |
| 28 | ** If you have questions regarding the use of this file, please contact |
| 29 | ** Nokia at [email protected]. |
| 30 | ** |
| 31 | **************************************************************************/ |
| 32 | |
| 33 | #include "servicebrowser.h" |
| 34 | #include "servicebrowser_p.h" |
| 35 | |
| 36 | #include <QtCore/QCoreApplication> |
| 37 | #include <QtCore/QDebug> |
| 38 | #include <QtCore/QLibrary> |
| 39 | #include <QtCore/QString> |
| 40 | #include <QtCore/QStringList> |
| 41 | |
| 42 | #include <errno.h> |
| 43 | |
| 44 | #ifndef NO_NATIVE_LIB |
| 45 | |
| 46 | #ifdef Q_OS_MACX |
| 47 | #define ZCONF_MDNS_STATIC_LINKING |
| 48 | #endif |
| 49 | |
| 50 | #ifdef ZCONF_MDNS_STATIC_LINKING |
| 51 | extern "C" { |
| 52 | #include "dns_sd_funct.h" |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | #ifdef Q_OS_UNIX |
| 57 | #include <sys/types.h> |
| 58 | #include <sys/socket.h> |
| 59 | #include <netdb.h> |
| 60 | #endif |
| 61 | |
| 62 | namespace ZeroConf { |
| 63 | namespace Internal { |
| 64 | |
| 65 | extern "C" { |
| 66 | typedef void (DNSSD_API *RefDeallocatePtr)(DNSServiceRef sdRef); |
| 67 | typedef DNSServiceErrorType (DNSSD_API *ResolvePtr)(DNSServiceRef *sdRef, DNSServiceFlags flags, |
| 68 | uint32_t interfaceIndex, const char *name, |
| 69 | const char *regtype, const char *domain, |
| 70 | DNSServiceResolveReply callBack, void *context); |
| 71 | typedef DNSServiceErrorType (DNSSD_API *QueryRecordPtr)(DNSServiceRef *sdRef, DNSServiceFlags flags, |
| 72 | uint32_t interfaceIndex, const char *fullname, |
| 73 | uint16_t rrtype, uint16_t rrclass, |
| 74 | DNSServiceQueryRecordReply callBack, void *context); |
| 75 | typedef DNSServiceErrorType (DNSSD_API *GetAddrInfoPtr)(DNSServiceRef *sdRef, DNSServiceFlags flags, |
| 76 | uint32_t interfaceIndex, DNSServiceProtocol protocol, |
| 77 | const char *hostname, DNSServiceGetAddrInfoReply callBack, |
| 78 | void *context); |
| 79 | typedef DNSServiceErrorType (DNSSD_API *ReconfirmRecordPtr)(DNSServiceFlags flags, uint32_t interfaceIndex, |
| 80 | const char *fullname, uint16_t rrtype, |
| 81 | uint16_t rrclass, uint16_t rdlen, const void *rdata); |
| 82 | typedef DNSServiceErrorType (DNSSD_API *BrowsePtr)(DNSServiceRef *sdRef, DNSServiceFlags flags, |
| 83 | uint32_t interfaceIndex, const char *regtype, const char *domain, |
| 84 | DNSServiceBrowseReply callBack, void *context); |
| 85 | typedef DNSServiceErrorType (DNSSD_API *GetPropertyPtr)(const char *property, void *result, uint32_t *size); |
| 86 | typedef DNSServiceErrorType (DNSSD_API *ProcessResultPtr)(DNSServiceRef sdRef); |
| 87 | typedef DNSServiceErrorType (DNSSD_API *CreateConnectionPtr)(DNSServiceRef *sdRef); |
| 88 | typedef int (DNSSD_API *RefSockFDPtr)(DNSServiceRef sdRef); |
| 89 | } |
| 90 | |
| 91 | // represents a zero conf library exposing the dns-sd interface |
| 92 | class NativeZConfLib : public ZConfLib{ |
| 93 | Q_DECLARE_TR_FUNCTIONS(ZeroConf) |
| 94 | private: |
| 95 | RefDeallocatePtr m_refDeallocate; |
| 96 | ResolvePtr m_resolve; |
| 97 | QueryRecordPtr m_queryRecord; |
| 98 | GetAddrInfoPtr m_getAddrInfo; |
| 99 | ReconfirmRecordPtr m_reconfirmRecord; |
| 100 | BrowsePtr m_browse; |
| 101 | GetPropertyPtr m_getProperty; |
| 102 | ProcessResultPtr m_processResult; |
| 103 | CreateConnectionPtr m_createConnection; |
| 104 | RefSockFDPtr m_refSockFD; |
| 105 | QLibrary nativeLib; |
| 106 | public: |
| 107 | enum { |
| 108 | // Note: the select() implementation on Windows (Winsock2) fails with any timeout much larger than this |
| 109 | LONG_TIME = 100000000 |
| 110 | }; |
| 111 | |
| 112 | NativeZConfLib(QString libName = QLatin1String("dns_sd"), ZConfLib *fallBack = 0) : ZConfLib(fallBack), nativeLib(libName) |
| 113 | { |
| 114 | #ifndef ZCONF_MDNS_STATIC_LINKING |
| 115 | // dynamic linking |
| 116 | if (!nativeLib.load()) { |
| 117 | m_isOk = false; |
| 118 | m_errorMsg=tr("NativeZConfLib could not load native library"); |
| 119 | } |
| 120 | m_refDeallocate = reinterpret_cast<RefDeallocatePtr>(nativeLib.resolve("DNSServiceRefDeallocate")); |
| 121 | m_resolve = reinterpret_cast<ResolvePtr>(nativeLib.resolve("DNSServiceResolve")); |
| 122 | m_queryRecord = reinterpret_cast<QueryRecordPtr>(nativeLib.resolve("DNSServiceQueryRecord")); |
| 123 | m_getAddrInfo = reinterpret_cast<GetAddrInfoPtr>(nativeLib.resolve("DNSServiceGetAddrInfo")); |
| 124 | m_reconfirmRecord = reinterpret_cast<ReconfirmRecordPtr>(nativeLib.resolve("DNSServiceReconfirmRecord")); |
| 125 | m_browse = reinterpret_cast<BrowsePtr>(nativeLib.resolve("DNSServiceBrowse")); |
| 126 | m_getProperty = reinterpret_cast<GetPropertyPtr>(nativeLib.resolve("DNSServiceGetProperty")); |
| 127 | m_processResult = reinterpret_cast<ProcessResultPtr>(nativeLib.resolve("DNSServiceProcessResult")) ; |
| 128 | m_createConnection = reinterpret_cast<CreateConnectionPtr>(nativeLib.resolve("DNSServiceCreateConnection")); |
| 129 | m_refSockFD = reinterpret_cast<RefSockFDPtr>(nativeLib.resolve("DNSServiceRefSockFD")); |
| 130 | #else |
| 131 | // static linking |
| 132 | m_refDeallocate = reinterpret_cast<RefDeallocatePtr>(&DNSServiceRefDeallocate); |
| 133 | m_resolve = reinterpret_cast<ResolvePtr>(&DNSServiceResolve); |
| 134 | m_queryRecord = reinterpret_cast<QueryRecordPtr>(DNSServiceQueryRecord); |
| 135 | m_getAddrInfo = reinterpret_cast<GetAddrInfoPtr>(&DNSServiceGetAddrInfo); |
| 136 | m_reconfirmRecord = reinterpret_cast<ReconfirmRecordPtr>(&DNSServiceReconfirmRecord); |
| 137 | m_browse = reinterpret_cast<BrowsePtr>(&DNSServiceBrowse); |
| 138 | m_getProperty = reinterpret_cast<GetPropertyPtr>(&DNSServiceGetProperty); |
| 139 | m_processResult = reinterpret_cast<ProcessResultPtr>(&DNSServiceProcessResult) ; |
| 140 | m_createConnection = reinterpret_cast<CreateConnectionPtr>(&DNSServiceCreateConnection); |
| 141 | m_refSockFD = reinterpret_cast<RefSockFDPtr>(&DNSServiceRefSockFD); |
| 142 | #endif |
| 143 | if (DEBUG_ZEROCONF){ |
| 144 | if (m_refDeallocate == 0) qDebug() << QLatin1String("NativeZConfLib.m_refDeallocate == 0"); |
| 145 | if (m_resolve == 0) qDebug() << QLatin1String("NativeZConfLib.m_resolve == 0"); |
| 146 | if (m_queryRecord == 0) qDebug() << QLatin1String("NativeZConfLib.m_queryRecord == 0"); |
| 147 | if (m_getAddrInfo == 0) qDebug() << QLatin1String("NativeZConfLib.m_getAddrInfo == 0"); |
| 148 | if (m_reconfirmRecord == 0) qDebug() << QLatin1String("NativeZConfLib.m_reconfirmRecord == 0"); |
| 149 | if (m_browse == 0) qDebug() << QLatin1String("NativeZConfLib.m_browse == 0"); |
| 150 | if (m_getProperty == 0) qDebug() << QLatin1String("NativeZConfLib.m_getProperty == 0"); |
| 151 | if (m_processResult == 0) qDebug() << QLatin1String("NativeZConfLib.m_processResult == 0"); |
| 152 | if (m_createConnection == 0) qDebug() << QLatin1String("NativeZConfLib.m_createConnection == 0"); |
| 153 | if (m_refSockFD == 0) qDebug() << QLatin1String("NativeZConfLib.m_refSockFD == 0"); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ~NativeZConfLib() { |
| 158 | } |
| 159 | |
| 160 | QString name(){ |
| 161 | return QString::fromUtf8("NativeZeroConfLib@%1").arg(size_t(this),0,16); |
| 162 | } |
| 163 | |
| 164 | // bool tryStartDaemon(); |
| 165 | void refDeallocate(DNSServiceRef sdRef) { |
| 166 | if (m_refDeallocate == 0) return; |
| 167 | m_refDeallocate(sdRef); |
| 168 | } |
| 169 | |
| 170 | void browserDeallocate(BrowserRef *bRef) { |
| 171 | if (m_refDeallocate == 0) return; |
| 172 | if (bRef) { |
| 173 | m_refDeallocate(*reinterpret_cast<DNSServiceRef *>(bRef)); |
| 174 | *bRef=0; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void stopConnection(ConnectionRef cRef) |
| 179 | { |
| 180 | int sock = refSockFD(cRef); |
| 181 | if (sock>0) |
| 182 | shutdown(sock,SHUT_RDWR); |
| 183 | } |
| 184 | |
| 185 | void destroyConnection(ConnectionRef *sdRef) { |
| 186 | if (m_refDeallocate == 0) return; |
| 187 | if (sdRef) { |
| 188 | m_refDeallocate(*reinterpret_cast<DNSServiceRef *>(sdRef)); |
| 189 | *sdRef=0; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | DNSServiceErrorType resolve(ConnectionRef cRef, DNSServiceRef *sdRef, |
| 194 | uint32_t interfaceIndex, const char *name, |
| 195 | const char *regtype, const char *domain, |
| 196 | ServiceGatherer *gatherer) |
| 197 | { |
| 198 | if (m_resolve == 0) return kDNSServiceErr_Unsupported; |
| 199 | *sdRef = reinterpret_cast<DNSServiceRef>(cRef); |
| 200 | return m_resolve(sdRef, kDNSServiceFlagsShareConnection | kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsTimeout, |
| 201 | interfaceIndex, name, regtype, domain, &cServiceResolveReply, gatherer |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | DNSServiceErrorType queryRecord(ConnectionRef cRef, DNSServiceRef *sdRef, |
| 206 | uint32_t interfaceIndex, const char *fullname, |
| 207 | ServiceGatherer *gatherer) |
| 208 | { |
| 209 | if (m_queryRecord == 0) return kDNSServiceErr_Unsupported; |
| 210 | *sdRef = reinterpret_cast<DNSServiceRef>(cRef); |
| 211 | return m_queryRecord(sdRef, kDNSServiceFlagsShareConnection | kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsTimeout, |
| 212 | interfaceIndex, fullname, |
| 213 | kDNSServiceType_TXT, kDNSServiceClass_IN, &cTxtRecordReply, gatherer); |
| 214 | } |
| 215 | |
| 216 | DNSServiceErrorType getAddrInfo(ConnectionRef cRef, DNSServiceRef *sdRef, |
| 217 | uint32_t interfaceIndex, DNSServiceProtocol protocol, |
| 218 | const char *hostname, ServiceGatherer *gatherer) |
| 219 | { |
| 220 | enum { longTTL = 100 }; |
| 221 | if (m_getAddrInfo == 0) { |
| 222 | #ifdef Q_OS_UNIX |
| 223 | // try to use getaddrinfo (for example on linux with avahi) |
| 224 | struct addrinfo req, *ans; int err; |
| 225 | memset(&req,0,sizeof(req)); |
| 226 | req.ai_flags = 0; |
| 227 | req.ai_family = AF_UNSPEC; |
| 228 | req.ai_socktype = SOCK_STREAM; |
| 229 | req.ai_protocol = 0; |
| 230 | if ((err = getaddrinfo(hostname, 0, &req, &ans)) != 0) { |
| 231 | qDebug() << "getaddrinfo for " << hostname << " failed with " << gai_strerror(err); |
| 232 | return kDNSServiceErr_Unsupported; // use another error here??? |
| 233 | } |
| 234 | for (struct addrinfo *ansAtt = ans; ansAtt != 0; ansAtt = ansAtt->ai_next){ |
| 235 | gatherer->addrReply(kDNSServiceFlagsAdd, kDNSServiceErr_NoError, |
| 236 | hostname, ansAtt->ai_addr, longTTL); |
| 237 | } |
| 238 | freeaddrinfo(ans); |
| 239 | return kDNSServiceErr_NoError; |
| 240 | #else |
| 241 | return kDNSServiceErr_Unsupported; |
| 242 | #endif |
| 243 | } |
| 244 | *sdRef = reinterpret_cast<DNSServiceRef>(cRef); |
| 245 | return m_getAddrInfo(sdRef, kDNSServiceFlagsShareConnection | kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsTimeout, |
| 246 | interfaceIndex, protocol, hostname, &cAddrReply, gatherer); |
| 247 | } |
| 248 | |
| 249 | DNSServiceErrorType reconfirmRecord(ConnectionRef /*cRef*/, uint32_t /*interfaceIndex*/, |
| 250 | const char * /*name*/, const char * /*type*/, const char * /*domain*/, |
| 251 | const char * /*fullname*/) |
| 252 | { |
| 253 | if (m_reconfirmRecord == 0) return kDNSServiceErr_Unsupported; |
| 254 | // reload and force update with in the callback with |
| 255 | // m_reconfirmRecord(flags, interfaceIndex, fullname, rrtype, |
| 256 | // rrclass, rdlen, rdata); |
| 257 | return kDNSServiceErr_Unsupported; |
| 258 | } |
| 259 | |
| 260 | DNSServiceErrorType browse(ConnectionRef cRef, BrowserRef *bRef, |
| 261 | uint32_t interfaceIndex, const char *regtype, |
| 262 | const char *domain, ServiceBrowserPrivate *browser) |
| 263 | { |
| 264 | if (m_browse == 0) return kDNSServiceErr_Unsupported; |
| 265 | DNSServiceRef *sdRef = reinterpret_cast<DNSServiceRef *>(bRef); |
| 266 | *sdRef = reinterpret_cast<DNSServiceRef>(cRef); |
| 267 | return m_browse(sdRef, kDNSServiceFlagsShareConnection | kDNSServiceFlagsSuppressUnusable, |
| 268 | interfaceIndex, regtype, domain, &cBrowseReply, browser); |
| 269 | } |
| 270 | |
| 271 | DNSServiceErrorType getProperty(const char *property, // Requested property (i.e. kDNSServiceProperty_DaemonVersion) |
| 272 | void *result, // Pointer to place to store result |
| 273 | uint32_t *size // size of result location |
| 274 | ) |
| 275 | { |
| 276 | if (m_getProperty == 0) |
| 277 | return kDNSServiceErr_Unsupported; |
| 278 | return m_getProperty(property, result, size); |
| 279 | } |
| 280 | |
| 281 | ProcessStatus processResult(ConnectionRef sdRef) { |
| 282 | if (m_processResult == 0) |
| 283 | return ProcessedFailure; |
| 284 | if (m_processResult(reinterpret_cast<DNSServiceRef>(sdRef)) != kDNSServiceErr_NoError) |
| 285 | return ProcessedError; |
| 286 | return ProcessedOk; |
| 287 | } |
| 288 | |
| 289 | // alternative processResult implementation using select |
| 290 | DNSServiceErrorType processResultSelect(ConnectionRef cRef) |
| 291 | { |
| 292 | #if _DNS_SD_LIBDISPATCH |
| 293 | { |
| 294 | main_queue = dispatch_get_main_queue(); |
| 295 | if (mainRef) DNSServiceSetDispatchQueue(mainRef, main_queue); |
| 296 | dispatch_main(); |
| 297 | } |
| 298 | #else |
| 299 | { |
| 300 | if (m_processResult == 0) return kDNSServiceErr_Unsupported; |
| 301 | int dns_sd_fd = (cRef?refSockFD(cRef):-1); |
| 302 | int nfds = dns_sd_fd + 1; |
| 303 | fd_set readfds; |
| 304 | struct timeval tv; |
| 305 | int result; |
| 306 | while (true) { |
| 307 | dns_sd_fd = (cRef?refSockFD(cRef):-1); |
| 308 | if (dns_sd_fd < 0) |
| 309 | return false; |
| 310 | nfds = dns_sd_fd + 1; |
| 311 | FD_ZERO(&readfds); |
| 312 | FD_SET(dns_sd_fd , &readfds); |
| 313 | |
| 314 | tv.tv_sec = LONG_TIME; |
| 315 | tv.tv_usec = 0; |
| 316 | |
| 317 | result = select(nfds, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv); |
| 318 | if (result > 0) { |
| 319 | if (FD_ISSET(dns_sd_fd , &readfds)) { |
| 320 | m_processResult(reinterpret_cast<DNSServiceRef>(cRef)); |
| 321 | return true; |
| 322 | } |
| 323 | } else if (result == 0) { |
| 324 | // we are idle... could do something productive... :) |
| 325 | } else if (errno != EINTR) { |
| 326 | qDebug() << "select() returned " << result << " errno " << errno << strerror(errno); |
| 327 | return false; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | #endif |
| 332 | } |
| 333 | |
| 334 | DNSServiceErrorType createConnection(ConnectionRef *sdRef) { |
| 335 | if (m_createConnection == 0) return kDNSServiceErr_Unsupported; |
| 336 | return m_createConnection(reinterpret_cast<DNSServiceRef *>(sdRef)); |
| 337 | } |
| 338 | |
| 339 | int refSockFD(ConnectionRef sdRef) { |
| 340 | if (m_refSockFD == 0) return kDNSServiceErr_Unsupported; |
| 341 | return m_refSockFD(reinterpret_cast<DNSServiceRef>(sdRef)); |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | ZConfLib *ZConfLib::createNativeLib(const QString &libName, ZConfLib *fallback) { |
| 346 | return new NativeZConfLib(libName, fallback); |
| 347 | return fallback; |
| 348 | } |
| 349 | } // namespace Internal |
| 350 | } // namespace ZeroConf |
| 351 | |
| 352 | #else // no native lib |
| 353 | |
| 354 | namespace ZeroConf { |
| 355 | namespace Internal { |
| 356 | |
| 357 | ZConfLib *ZConfLib::createNativeLib(const QString &/*extraPaths*/, ZConfLib * fallback) { |
| 358 | return fallback; |
| 359 | } |
| 360 | |
| 361 | } // namespace Internal |
| 362 | } // namespace ZeroConf |
| 363 | #endif |
| 364 |