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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/* This file is part of the KDE project
*
* Copyright (C) 2004, 2005 Jakub Stachowski <[email protected]>
*
* 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 "publicservice.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <netinet/in.h>
#include <sys/socket.h>
#include <tqapplication.h>
#include <network/tdesocketaddress.h>
#include <kurl.h>
#include <unistd.h>
#ifdef HAVE_DNSSD
#include <avahi-client/client.h>
#ifdef AVAHI_API_0_6
#include <avahi-client/publish.h>
#endif
#include <avahi-common/alternative.h>
#include <avahi-common/strlst.h>
#endif
#include "sdevent.h"
#include "responder.h"
#include "servicebrowser.h"
#include "settings.h"
namespace DNSSD
{
static unsigned long publicIP();
#ifdef HAVE_DNSSD
void publish_callback (AvahiEntryGroup*, AvahiEntryGroupState s, void *context);
#endif
class PublicServicePrivate
{
public:
PublicServicePrivate() : m_published(false), m_running(false), m_collision(false)
#ifdef HAVE_DNSSD
, m_group(false)
#endif
{}
bool m_published;
bool m_running;
bool m_collision;
#ifdef HAVE_DNSSD
AvahiEntryGroup* m_group;
#endif
void commit()
{
#ifdef HAVE_DNSSD
if (!m_collision) avahi_entry_group_commit(m_group);
#endif
}
};
PublicService::PublicService(const TQString& name, const TQString& type, unsigned int port,
const TQString& domain)
: TQObject(), ServiceBase(name, type, TQString::null, domain, port)
{
d = new PublicServicePrivate;
#ifdef HAVE_DNSSD
if (Responder::self().client()) {
d->m_group = avahi_entry_group_new(Responder::self().client(), publish_callback,this);
connect(&Responder::self(),TQT_SIGNAL(stateChanged(AvahiClientState)),this,TQT_SLOT(clientState(AvahiClientState)));
}
#endif
if (domain.isNull())
if (Configuration::publishType()==Configuration::EnumPublishType::LAN) m_domain="local.";
else m_domain=Configuration::publishDomain();
}
PublicService::~PublicService()
{
#ifdef HAVE_DNSSD
if (d->m_group) avahi_entry_group_free(d->m_group);
#endif
delete d;
}
void PublicService::tryApply()
{
if (fillEntryGroup()) d->commit();
else {
stop();
emit published(false);
}
}
void PublicService::setServiceName(const TQString& serviceName)
{
m_serviceName = serviceName;
#ifdef HAVE_DNSSD
if (d->m_running) {
avahi_entry_group_reset(d->m_group);
tryApply();
}
#endif
}
void PublicService::setDomain(const TQString& domain)
{
m_domain = domain;
#ifdef HAVE_DNSSD
if (d->m_running) {
avahi_entry_group_reset(d->m_group);
tryApply();
}
#endif
}
void PublicService::setType(const TQString& type)
{
m_type = type;
#ifdef HAVE_DNSSD
if (d->m_running) {
avahi_entry_group_reset(d->m_group);
tryApply();
}
#endif
}
void PublicService::setPort(unsigned short port)
{
m_port = port;
#ifdef HAVE_DNSSD
if (d->m_running) {
avahi_entry_group_reset(d->m_group);
tryApply();
}
#endif
}
void PublicService::setTextData(const TQMap<TQString,TQString>& textData)
{
m_textData = textData;
#ifdef HAVE_DNSSD
if (d->m_running) {
avahi_entry_group_reset(d->m_group);
tryApply();
}
#endif
}
bool PublicService::isPublished() const
{
return d->m_published;
}
bool PublicService::publish()
{
publishAsync();
while (d->m_running && !d->m_published) Responder::self().process();
return d->m_published;
}
void PublicService::stop()
{
#ifdef HAVE_DNSSD
if (d->m_group) avahi_entry_group_reset(d->m_group);
#endif
d->m_published = false;
}
bool PublicService::fillEntryGroup()
{
#ifdef HAVE_DNSSD
AvahiStringList *s=0;
TQMap<TQString,TQString>::ConstIterator itEnd = m_textData.end();
for (TQMap<TQString,TQString>::ConstIterator it = m_textData.begin(); it!=itEnd ; ++it)
s = avahi_string_list_add_pair(s, it.key().utf8(),it.data().utf8());
#ifdef AVAHI_API_0_6
bool res = (!avahi_entry_group_add_service_strlst(d->m_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0,
m_serviceName.isNull() ? avahi_client_get_host_name(Responder::self().client()) : m_serviceName.utf8().data(),
m_type.ascii(),domainToDNS(m_domain),m_hostName.utf8(),m_port,s));
#else
bool res = (!avahi_entry_group_add_service_strlst(d->m_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
m_serviceName.isNull() ? avahi_client_get_host_name(Responder::self().client()) : m_serviceName.utf8().data(),
m_type.ascii(),m_domain.utf8(),m_hostName.utf8(),m_port,s));
#endif
avahi_string_list_free(s);
return res;
#else
return FALSE;
#endif
}
void PublicService::clientState(AvahiClientState s)
{
if (!d->m_running) return;
#ifdef HAVE_DNSSD
switch (s) {
#ifdef AVAHI_API_0_6
case AVAHI_CLIENT_FAILURE:
#else
case AVAHI_CLIENT_S_INVALID:
case AVAHI_CLIENT_DISCONNECTED:
#endif
stop();
emit published(false);
break;
case AVAHI_CLIENT_S_REGISTERING:
case AVAHI_CLIENT_S_COLLISION:
avahi_entry_group_reset(d->m_group);
d->m_collision=true;
break;
case AVAHI_CLIENT_S_RUNNING:
if (d->m_collision) {
d->m_collision=false;
tryApply();
}
}
#endif
}
void PublicService::publishAsync()
{
if (d->m_running) stop();
#ifdef HAVE_DNSSD
if (!d->m_group) {
emit published(false);
return;
}
AvahiClientState s=Responder::self().state();
#endif
d->m_running=true;
d->m_collision=true; // make it look like server is getting out of collision to force registering
#ifdef HAVE_DNSSD
clientState(s);
#endif
}
#ifdef HAVE_DNSSD
void publish_callback (AvahiEntryGroup*, AvahiEntryGroupState s, void *context)
{
TQObject *obj = reinterpret_cast<TQObject*>(context);
if (s!=AVAHI_ENTRY_GROUP_ESTABLISHED && s!=AVAHI_ENTRY_GROUP_COLLISION) return;
PublishEvent* pev=new PublishEvent(s==AVAHI_ENTRY_GROUP_ESTABLISHED);
TQApplication::postEvent(obj, pev);
}
#endif
const KURL PublicService::toInvitation(const TQString& host)
{
KURL url;
url.setProtocol("invitation");
if (host.isEmpty()) { // select best address
unsigned long s_address = publicIP();
if (!s_address) return KURL();
KNetwork::KIpAddress addr(s_address);
url.setHost(addr.toString());
} else url.setHost(host);
//FIXME: if there is no public interface, select any non-loopback
url.setPort(m_port);
url.setPath("/"+m_type+"/"+KURL::encode_string(m_serviceName));
TQString query;
TQMap<TQString,TQString>::ConstIterator itEnd = m_textData.end();
for (TQMap<TQString,TQString>::ConstIterator it = m_textData.begin(); it!=itEnd ; ++it)
url.addQueryItem(it.key(),it.data());;
return url;
}
void PublicService::customEvent(TQCustomEvent* event)
{
#ifdef HAVE_DNSSD
if (event->type()==TQEvent::User+SD_PUBLISH) {
if (!static_cast<PublishEvent*>(event)->m_ok) {
setServiceName(TQString::fromUtf8(avahi_alternative_service_name(m_serviceName.utf8())));
return;
}
d->m_published=true;
emit published(true);
}
#endif
}
void PublicService::virtual_hook(int, void*)
{
}
static unsigned long publicIP()
{
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
int sock = socket(AF_INET,SOCK_DGRAM,0);
if (sock == -1) return 0;
addr.sin_family = AF_INET;
addr.sin_port = 1; // Not important, any port and public address will do
addr.sin_addr.s_addr = 0x11111111;
if ((connect(sock,(const struct sockaddr*)&addr,sizeof(addr))) == -1) { close(sock); return 0; }
if ((getsockname(sock,(struct sockaddr*)&addr, &len)) == -1) { close(sock); return 0; }
::close(sock);
return addr.sin_addr.s_addr;
}
}
#include "publicservice.moc"
|