summaryrefslogtreecommitdiffstats
path: root/dnssd/publicservice.cpp
blob: ad1ebdbb6ae13cbb22660b903902b2ac637c759c (plain)
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
/* 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 "publicservice.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <netinet/in.h>
#include <sys/socket.h>
#include <qapplication.h>
#include <network/ksocketaddress.h>
#include <kurl.h>
#include <unistd.h>
#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>
#include "sdevent.h"
#include "responder.h"
#include "servicebrowser.h"
#include "settings.h"

namespace DNSSD
{
static unsigned long publicIP();

void publish_callback (AvahiEntryGroup*, AvahiEntryGroupState s,  void *context);

class PublicServicePrivate 
{
public:
	PublicServicePrivate() : m_published(false), m_running(false), m_collision(false), m_group(false)
	{}
	bool m_published;
	bool m_running;
	bool m_collision;
	AvahiEntryGroup* m_group;
	void commit()
	{
	    if (!m_collision) avahi_entry_group_commit(m_group);
	}    
	
};

PublicService::PublicService(const QString& name, const QString& type, unsigned int port,
			      const QString& domain)
  		: QObject(), ServiceBase(name, type, QString::null, domain, port)
{
	d = new PublicServicePrivate;
	if (Responder::self().client()) { 
		d->m_group = avahi_entry_group_new(Responder::self().client(), publish_callback,this);
		connect(&Responder::self(),SIGNAL(stateChanged(AvahiClientState)),this,SLOT(clientState(AvahiClientState)));
	}
	if (domain.isNull())
		if (Configuration::publishType()==Configuration::EnumPublishType::LAN) m_domain="local.";
		else m_domain=Configuration::publishDomain();
}


PublicService::~PublicService()
{
	if (d->m_group) avahi_entry_group_free(d->m_group);
	delete d;
}

void PublicService::tryApply()
{
    if (fillEntryGroup()) d->commit();
    else {
	stop();
	emit published(false);
    }
}

void PublicService::setServiceName(const QString& serviceName)
{
	m_serviceName = serviceName;
	if (d->m_running) {
	    avahi_entry_group_reset(d->m_group);
	    tryApply();
	} 
}

void PublicService::setDomain(const QString& domain)
{
	m_domain = domain;
	if (d->m_running) {
	    avahi_entry_group_reset(d->m_group);
	    tryApply();
	} 
}


void PublicService::setType(const QString& type)
{
	m_type = type;
	if (d->m_running) {
	    avahi_entry_group_reset(d->m_group);
	    tryApply();
	} 
}

void PublicService::setPort(unsigned short port)
{
	m_port = port;
	if (d->m_running) {
	    avahi_entry_group_reset(d->m_group);
	    tryApply();
    	} 
}

void PublicService::setTextData(const QMap<QString,QString>& textData)
{
	m_textData = textData;
	if (d->m_running) {
	    avahi_entry_group_reset(d->m_group);
	    tryApply();
	} 
}

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()
{
    if (d->m_group) avahi_entry_group_reset(d->m_group);
    d->m_published = false;
}
bool PublicService::fillEntryGroup()
{
    AvahiStringList *s=0;
    QMap<QString,QString>::ConstIterator itEnd = m_textData.end();
    for (QMap<QString,QString>::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;
}

void PublicService::clientState(AvahiClientState s)
{
    if (!d->m_running) return;
    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();
	    }
    }
}				    

void PublicService::publishAsync()
{
	if (d->m_running) stop();
	
	if (!d->m_group) {
	    emit published(false);
	    return;
	}
	AvahiClientState s=Responder::self().state();
	d->m_running=true; 
	d->m_collision=true; // make it look like server is getting out of collision to force registering
	clientState(s);
}

void publish_callback (AvahiEntryGroup*, AvahiEntryGroupState s,  void *context)
{
	QObject *obj = reinterpret_cast<QObject*>(context);
	if (s!=AVAHI_ENTRY_GROUP_ESTABLISHED && s!=AVAHI_ENTRY_GROUP_COLLISION) return;
	PublishEvent* pev=new PublishEvent(s==AVAHI_ENTRY_GROUP_ESTABLISHED);
	QApplication::postEvent(obj, pev);
}

const KURL PublicService::toInvitation(const QString& 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));
	QString query;
	QMap<QString,QString>::ConstIterator itEnd = m_textData.end();
	for (QMap<QString,QString>::ConstIterator it = m_textData.begin(); it!=itEnd ; ++it)
		url.addQueryItem(it.key(),it.data());;
	return url;
}

void PublicService::customEvent(QCustomEvent* event)
{
	if (event->type()==QEvent::User+SD_PUBLISH) {
		if (!static_cast<PublishEvent*>(event)->m_ok) {
		    setServiceName(QString::fromUtf8(avahi_alternative_service_name(m_serviceName.utf8())));
		    return;
		}
		d->m_published=true;
		emit published(true);
	}
}

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"