Debian Codegraph @

interfaces/zeroconf/bonjourserviceresolver.cpp

raw symbols SCIP
1 /*
2 Copyright (c) 2007, Trenton Schulz
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 1. Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <QtCore/QSocketNotifier>
30 #include <QtNetwork/QHostInfo>
31
32 #include "bonjourrecord.h"
33 #include "bonjourserviceresolver.h"
34
35 BonjourServiceResolver::BonjourServiceResolver(QObject *parent)
36 : QObject(parent), dnssref(0), bonjourSocket(0), bonjourPort(-1)
37 {
38 }
39
40 BonjourServiceResolver::~BonjourServiceResolver()
41 {
42 cleanupResolve();
43 }
44
45 void BonjourServiceResolver::cleanupResolve()
46 {
47 if (dnssref) {
48 DNSServiceRefDeallocate(dnssref);
49 dnssref = 0;
50 delete bonjourSocket;
51 bonjourPort = -1;
52 }
53 }
54
55 void BonjourServiceResolver::resolveBonjourRecord(const BonjourRecord &record)
56 {
57 if (dnssref) {
58 qWarning("resolve in process, aborting");
59 return;
60 }
61 DNSServiceErrorType err = DNSServiceResolve(&dnssref, 0, 0,
62 record.serviceName.toUtf8().constData(),
63 record.registeredType.toUtf8().constData(),
64 record.replyDomain.toUtf8().constData(),
65 (DNSServiceResolveReply)bonjourResolveReply, this);
66 if (err != kDNSServiceErr_NoError) {
67 emit error(err);
68 } else {
69 int sockfd = DNSServiceRefSockFD(dnssref);
70 if (sockfd == -1) {
71 emit error(kDNSServiceErr_Invalid);
72 } else {
73 bonjourSocket = new QSocketNotifier(sockfd, QSocketNotifier::Read, this);
74 connect(bonjourSocket, SIGNAL(activated(int)), this, SLOT(bonjourSocketReadyRead()));
75 }
76 }
77 }
78
79 void BonjourServiceResolver::bonjourSocketReadyRead()
80 {
81 DNSServiceErrorType err = DNSServiceProcessResult(dnssref);
82 if (err != kDNSServiceErr_NoError)
83 emit error(err);
84 }
85
86
87 void BonjourServiceResolver::bonjourResolveReply(DNSServiceRef, DNSServiceFlags ,
88 quint32 , DNSServiceErrorType errorCode,
89 const char *, const char *hosttarget, quint16 port,
90 quint16 , const char *, void *context)
91 {
92 BonjourServiceResolver *serviceResolver = static_cast<BonjourServiceResolver *>(context);
93 if (errorCode != kDNSServiceErr_NoError) {
94 emit serviceResolver->error(errorCode);
95 return;
96 }
97 #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
98 {
99 port = 0 | ((port & 0x00ff) << 8) | ((port & 0xff00) >> 8);
100 }
101 #endif
102 serviceResolver->bonjourPort = port;
103 QHostInfo::lookupHost(QString::fromUtf8(hosttarget),
104 serviceResolver, SLOT(finishConnect(const QHostInfo &)));
105 }
106
107 void BonjourServiceResolver::finishConnect(const QHostInfo &hostInfo)
108 {
109 emit bonjourRecordResolved(hostInfo, bonjourPort);
110 QMetaObject::invokeMethod(this, "cleanupResolve", Qt::QueuedConnection);
111 }