| 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 | #include <qglobal.h> |
| 33 | #ifdef Q_OS_LINUX |
| 34 | #define EMBEDDED_LIB |
| 35 | #endif |
| 36 | |
| 37 | #ifdef Q_OS_WIN |
| 38 | # include <Winsock2.h> |
| 39 | #endif |
| 40 | #include "servicebrowser_p.h" |
| 41 | |
| 42 | #include <QtCore/QString> |
| 43 | #include <QtCore/QStringList> |
| 44 | #include <QtCore/QProcess> |
| 45 | #include <QtCore/QCoreApplication> |
| 46 | #include <QtCore/QDebug> |
| 47 | |
| 48 | #ifdef EMBEDDED_LIB |
| 49 | #include "embed/dnssd_ipc.c" |
| 50 | #include "embed/dnssd_clientlib.c" |
| 51 | #include "embed/dnssd_clientstub.c" |
| 52 | #ifdef Q_OS_WIN |
| 53 | #include "embed/DebugServices.c" |
| 54 | #endif |
| 55 | |
| 56 | namespace ZeroConf { |
| 57 | namespace Internal { |
| 58 | // represents a zero conf library exposing the dns-sd interface |
| 59 | class EmbeddedZConfLib : public ZConfLib |
| 60 | { |
| 61 | public: |
| 62 | QString daemonPath; |
| 63 | |
| 64 | EmbeddedZConfLib(const QString &daemonPath, ZConfLib *fallBack = 0) : ZConfLib(fallBack), daemonPath(daemonPath) |
| 65 | { |
| 66 | if (!daemonPath.isEmpty() && daemonPath.at(0) != '/' && daemonPath.at(0) != '.') |
| 67 | this->daemonPath = QCoreApplication::applicationDirPath() + QChar('/') + daemonPath; |
| 68 | } |
| 69 | |
| 70 | ~EmbeddedZConfLib() |
| 71 | { } |
| 72 | |
| 73 | QString name() |
| 74 | { |
| 75 | return QString::fromUtf8("EmbeddedZeroConfLib@%1").arg(size_t(this),0,16); |
| 76 | } |
| 77 | |
| 78 | bool tryStartDaemon() |
| 79 | { |
| 80 | if (!daemonPath.isEmpty()) { |
| 81 | if (QProcess::startDetached(daemonPath)) { |
| 82 | QThread::yieldCurrentThread(); |
| 83 | // sleep a bit? |
| 84 | if (DEBUG_ZEROCONF) |
| 85 | qDebug() << name() << " started " << daemonPath; |
| 86 | return true; |
| 87 | } else { |
| 88 | this->setError(true, tr("%1 failed starting embedded daemon at %2").arg(name()).arg(daemonPath)); |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | void refDeallocate(DNSServiceRef sdRef) |
| 95 | { |
| 96 | embeddedLib::DNSServiceRefDeallocate(sdRef); |
| 97 | } |
| 98 | |
| 99 | void browserDeallocate(BrowserRef *bRef) |
| 100 | { |
| 101 | if (bRef){ |
| 102 | embeddedLib::DNSServiceRefDeallocate(*reinterpret_cast<DNSServiceRef*>(bRef)); |
| 103 | *bRef=0; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void stopConnection(ConnectionRef cRef) |
| 108 | { |
| 109 | int sock = refSockFD(cRef); |
| 110 | if (sock>0) |
| 111 | shutdown(sock,SHUT_RDWR); |
| 112 | } |
| 113 | |
| 114 | void destroyConnection(ConnectionRef *sdRef) |
| 115 | { |
| 116 | if (sdRef) { |
| 117 | embeddedLib::DNSServiceRefDeallocate(*reinterpret_cast<DNSServiceRef*>(sdRef)); |
| 118 | *sdRef = 0; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | DNSServiceErrorType resolve(ConnectionRef cRef, |
| 123 | DNSServiceRef *sdRef, |
| 124 | uint32_t interfaceIndex, |
| 125 | const char *name, |
| 126 | const char *regtype, |
| 127 | const char *domain, |
| 128 | ServiceGatherer *gatherer) |
| 129 | { |
| 130 | *sdRef = reinterpret_cast<DNSServiceRef>(cRef); |
| 131 | return embeddedLib::DNSServiceResolve(sdRef, kDNSServiceFlagsShareConnection | kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsTimeout, |
| 132 | interfaceIndex, name, regtype, domain, &cServiceResolveReply, gatherer); |
| 133 | } |
| 134 | |
| 135 | DNSServiceErrorType queryRecord(ConnectionRef cRef, |
| 136 | DNSServiceRef *sdRef, |
| 137 | uint32_t interfaceIndex, |
| 138 | const char *fullname, |
| 139 | ServiceGatherer *gatherer) |
| 140 | { |
| 141 | *sdRef = reinterpret_cast<DNSServiceRef>(cRef); |
| 142 | return embeddedLib::DNSServiceQueryRecord(sdRef, kDNSServiceFlagsShareConnection | kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsTimeout, |
| 143 | interfaceIndex, fullname, |
| 144 | kDNSServiceType_TXT, kDNSServiceClass_IN, &cTxtRecordReply ,gatherer); |
| 145 | } |
| 146 | |
| 147 | DNSServiceErrorType getAddrInfo(ConnectionRef cRef, |
| 148 | DNSServiceRef *sdRef, |
| 149 | uint32_t interfaceIndex, |
| 150 | DNSServiceProtocol protocol, |
| 151 | const char *hostname, |
| 152 | ServiceGatherer *gatherer) |
| 153 | { |
| 154 | *sdRef = reinterpret_cast<DNSServiceRef>(cRef); |
| 155 | return embeddedLib::DNSServiceGetAddrInfo(sdRef, kDNSServiceFlagsShareConnection | kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsTimeout, |
| 156 | interfaceIndex, protocol, |
| 157 | hostname, &cAddrReply, gatherer); |
| 158 | } |
| 159 | |
| 160 | DNSServiceErrorType reconfirmRecord(ConnectionRef /*cRef*/, uint32_t /*interfaceIndex*/, |
| 161 | const char * /*name*/, const char * /*type*/, const char * /*domain*/, |
| 162 | const char * /*fullname*/) |
| 163 | { |
| 164 | // reload and force update with in the callback with |
| 165 | // embeddedLib::DNSServiceReconfirmRecord(flags, interfaceIndex, fullname, rrtype, |
| 166 | // rrclass, rdlen, rdata); |
| 167 | return kDNSServiceErr_Unsupported; |
| 168 | } |
| 169 | |
| 170 | DNSServiceErrorType browse(ConnectionRef cRef, |
| 171 | BrowserRef *bRef, |
| 172 | uint32_t interfaceIndex, |
| 173 | const char *regtype, |
| 174 | const char *domain, /* may be NULL */ |
| 175 | ServiceBrowserPrivate *browser) |
| 176 | { |
| 177 | DNSServiceRef *sdRef = reinterpret_cast<DNSServiceRef *>(bRef); |
| 178 | *sdRef = reinterpret_cast<DNSServiceRef>(cRef); |
| 179 | return embeddedLib::DNSServiceBrowse(sdRef, kDNSServiceFlagsShareConnection | kDNSServiceFlagsSuppressUnusable, |
| 180 | interfaceIndex, regtype, domain, &cBrowseReply, browser); |
| 181 | } |
| 182 | |
| 183 | DNSServiceErrorType getProperty(const char *property, /* Requested property (i.e. kDNSServiceProperty_DaemonVersion) */ |
| 184 | void *result, /* Pointer to place to store result */ |
| 185 | uint32_t *size /* size of result location */ |
| 186 | ) |
| 187 | { |
| 188 | return embeddedLib::DNSServiceGetProperty(property, result, size); |
| 189 | } |
| 190 | |
| 191 | ProcessStatus processResult(ConnectionRef sdRef) { |
| 192 | if (embeddedLib::DNSServiceProcessResult(reinterpret_cast<DNSServiceRef>(sdRef)) != kDNSServiceErr_NoError) |
| 193 | return ProcessedError; |
| 194 | return ProcessedOk; |
| 195 | } |
| 196 | |
| 197 | DNSServiceErrorType createConnection(ConnectionRef *sdRef) |
| 198 | { |
| 199 | return embeddedLib::DNSServiceCreateConnection(reinterpret_cast<DNSServiceRef*>(sdRef)); |
| 200 | } |
| 201 | |
| 202 | int refSockFD(ConnectionRef sdRef) |
| 203 | { |
| 204 | return embeddedLib::DNSServiceRefSockFD(reinterpret_cast<DNSServiceRef>(sdRef)); |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | ZConfLib *ZConfLib::createEmbeddedLib(const QString &daemonPath, ZConfLib *fallback) |
| 209 | { |
| 210 | return new EmbeddedZConfLib(daemonPath, fallback); |
| 211 | } |
| 212 | } // namespace Internal |
| 213 | } // namespace ZeroConf |
| 214 | |
| 215 | #else // no embedded lib |
| 216 | |
| 217 | namespace ZeroConf { |
| 218 | namespace Internal { |
| 219 | |
| 220 | ZConfLib *ZConfLib::createEmbeddedLib(const QString &, ZConfLib * fallback) |
| 221 | { |
| 222 | return fallback; |
| 223 | } |
| 224 | |
| 225 | } // namespace Internal |
| 226 | } // namespace ZeroConf |
| 227 | #endif |