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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/* This file is part of the KDE project
*
* Copyright (C) 2004, 2005 Jakub Stachowski <qbast@go2.pl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <qeventloop.h>
#include <qapplication.h>
#include <kurl.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <netinet/in.h>
#include "remoteservice.h"
#include "responder.h"
#include "sdevent.h"
#include <kdebug.h>
namespace DNSSD
{
#ifdef HAVE_DNSSD
void resolve_callback ( DNSServiceRef,
DNSServiceFlags,
uint32_t,
DNSServiceErrorType errorCode,
const char*,
const char *hosttarget,
uint16_t port,
uint16_t txtLen,
const unsigned char *txtRecord,
void *context
);
#endif
class RemoteServicePrivate : public Responder
{
public:
RemoteServicePrivate() : Responder(), m_resolved(false)
{};
bool m_resolved;
};
RemoteService::RemoteService(const QString& label)
{
decode(label);
d = new RemoteServicePrivate();
}
RemoteService::RemoteService(const QString& name,const QString& type,const QString& domain)
: ServiceBase(name, type, domain)
{
d = new RemoteServicePrivate();
}
RemoteService::RemoteService(const KURL& url)
{
d = new RemoteServicePrivate();
if (!url.isValid()) return;
if (url.protocol()!="invitation") return;
if (!url.hasPath()) return;
m_hostName = url.host();
m_port = url.port();
m_type = url.path().section('/',1,1);
m_serviceName = url.path().section('/',2);
m_textData = url.queryItems();
d->m_resolved=true;
}
RemoteService::~RemoteService()
{
delete d;
}
bool RemoteService::resolve()
{
resolveAsync();
while (d->isRunning() && !d->m_resolved) d->process();
d->stop();
return d->m_resolved;
}
void RemoteService::resolveAsync()
{
if (d->isRunning()) return;
d->m_resolved = false;
kdDebug() << this << ":Starting resolve of : " << m_serviceName << " " << m_type << " " << m_domain << "\n";
#ifdef HAVE_DNSSD
DNSServiceRef ref;
if (DNSServiceResolve(&ref,0,0,m_serviceName.utf8(), m_type.ascii(),
domainToDNS(m_domain),(DNSServiceResolveReply)resolve_callback,reinterpret_cast<void*>(this))
== kDNSServiceErr_NoError) d->setRef(ref);
#endif
if (!d->isRunning()) emit resolved(false);
}
bool RemoteService::isResolved() const
{
return d->m_resolved;
}
void RemoteService::customEvent(QCustomEvent* event)
{
if (event->type() == QEvent::User+SD_ERROR) {
d->stop();
d->m_resolved=false;
emit resolved(false);
}
if (event->type() == QEvent::User+SD_RESOLVE) {
ResolveEvent* rev = static_cast<ResolveEvent*>(event);
m_hostName = rev->m_hostname;
m_port = rev->m_port;
m_textData = rev->m_txtdata;
d->m_resolved = true;
emit resolved(true);
}
}
void RemoteService::virtual_hook(int, void*)
{
// BASE::virtual_hook(int, void*);
}
QDataStream & operator<< (QDataStream & s, const RemoteService & a)
{
s << (static_cast<ServiceBase>(a));
Q_INT8 resolved = a.d->m_resolved ? 1:0;
s << resolved;
return s;
}
QDataStream & operator>> (QDataStream & s, RemoteService & a)
{
// stop any possible resolve going on
a.d->stop();
Q_INT8 resolved;
operator>>(s,(static_cast<ServiceBase&>(a)));
s >> resolved;
a.d->m_resolved = (resolved == 1);
return s;
}
#ifdef HAVE_DNSSD
void resolve_callback ( DNSServiceRef,
DNSServiceFlags,
uint32_t,
DNSServiceErrorType errorCode,
const char*,
const char *hosttarget,
uint16_t port,
uint16_t txtLen,
const unsigned char *txtRecord,
void *context
)
{
QObject *obj = reinterpret_cast<QObject*>(context);
if (errorCode != kDNSServiceErr_NoError) {
ErrorEvent err;
QApplication::sendEvent(obj, &err);
return;
}
char key[256];
int index=0;
unsigned char valueLen;
kdDebug() << "Resolve callback\n";
QMap<QString,QString> map;
const void *voidValue = 0;
while (TXTRecordGetItemAtIndex(txtLen,txtRecord,index++,256,key,&valueLen,
&voidValue) == kDNSServiceErr_NoError)
{
if (voidValue) map[QString::fromUtf8(key)]=QString::fromUtf8((const char*)voidValue,valueLen);
else map[QString::fromUtf8(key)]=QString::null;
}
ResolveEvent rev(DNSToDomain(hosttarget),ntohs(port),map);
QApplication::sendEvent(obj, &rev);
}
#endif
}
#include "remoteservice.moc"
|