summaryrefslogtreecommitdiffstats
path: root/plugins/zeroconf/avahiservice.cpp
blob: 714d92a353803921f785e886c3e542fb3b31bef0 (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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/***************************************************************************
 *   Copyright (C) 2006 by Lesly Weyts and Kevin Andre                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/
#include <util/log.h>
#include <torrent/peerid.h>
#include <avahi-common/watch.h>
#include <avahi-qt3/tqt-watch.h>
#include "localbrowser.h"
#include "avahiservice.h"
		
using namespace bt;
		
namespace kt
{
	
	void group_callback(AvahiEntryGroup* g, AvahiEntryGroupState state, void* userdata)
	{
		AvahiService* service = reinterpret_cast<AvahiService*>(userdata);
	
		if (g == service->group) 
		{
			switch (state) 
			{
			case AVAHI_ENTRY_GROUP_ESTABLISHED:
				break;
			case AVAHI_ENTRY_GROUP_COLLISION:
				Out(SYS_ZCO|LOG_DEBUG) << "ZC: Entry group collision." << endl;
				break;
			case AVAHI_ENTRY_GROUP_FAILURE:
				Out(SYS_ZCO|LOG_DEBUG) <<  "ZC: Entry group failure." << endl;
				break;
			case AVAHI_ENTRY_GROUP_UNCOMMITED:
				Out(SYS_ZCO|LOG_DEBUG) <<  "ZC: Entry group uncommited." << endl;
				break;
			case AVAHI_ENTRY_GROUP_REGISTERING:
				;
			}
		}
	}
	
	void publish_service(AvahiService* service, AvahiClient *c)
	{
		assert(c);
	
		if (!service->group) 
		{
			if (!(service->group = avahi_entry_group_new(c, group_callback, service))) 
			{
				Out(SYS_ZCO|LOG_DEBUG) << "ZC: avahi_entry_group_new failed." << endl;
				return;
			}
		}
	
		const char* name    = avahi_strdup(TQString("%1__%2%3").tqarg(service->id).tqarg((rand() % 26) + 65).tqarg((rand() % 26) + 65).ascii());
		const char* type    = avahi_strdup("_bittorrent._tcp");
		const char* subtype = avahi_strdup(TQString("_" + service->infoHash + "._sub._bittorrent._tcp").ascii());
	
		if (avahi_entry_group_add_service(
				service->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
				(AvahiPublishFlags)0, name, type, NULL, NULL, service->port, NULL)) 
		{
			if (avahi_client_errno(c) != -8) 
				Out(SYS_ZCO|LOG_DEBUG) << TQString("ZC: Failed to add the service (%i).").tqarg(avahi_client_errno(c)) << endl;
			else
				publish_service(service, c);
			return;
		}
	
		if (avahi_entry_group_add_service_subtype(
				service->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
				(AvahiPublishFlags)0, name, type, NULL, subtype)) 
		{
			Out(SYS_ZCO|LOG_DEBUG) << TQString("ZC: Failed to add the service subtype (%i).").tqarg( avahi_client_errno(c)) << endl;
			return;
		}
	
		if (avahi_entry_group_commit(service->group)) 
		{
			Out(SYS_ZCO|LOG_DEBUG) << "ZC: Failed to commit the entry group." << endl;
			return;
		}
	}
	
	void publisher_callback(AvahiClient* c, AvahiClientState state, void* userdata)
	{
		if (!c)
			return;
	
		AvahiService* service = reinterpret_cast<AvahiService*>(userdata);
	
		switch (state) {
		case AVAHI_CLIENT_S_RUNNING:
			{
				if (!service->group)
					publish_service(service, c);
				break;
			}
		case AVAHI_CLIENT_FAILURE:
			{
				Out(SYS_ZCO|LOG_DEBUG) << "Failure when publishing." << endl;
				break;
			}
		case AVAHI_CLIENT_S_COLLISION:
		case AVAHI_CLIENT_S_REGISTERING:
			{
				if (service->group)
					avahi_entry_group_reset(service->group);
				break;
			}
		case AVAHI_CLIENT_CONNECTING:
			;
		}
	}
	
	void listener_callback(AvahiClient* c, AvahiClientState state, void* userdata)
	{
		assert(c);
	
		AvahiService* service = reinterpret_cast<AvahiService*>(userdata);
		
		if (state == AVAHI_CLIENT_FAILURE) 
		{
			Out(SYS_ZCO|LOG_DEBUG) <<  "ZC: Server connection failure." << endl;
		}
	}
	
	void resolve_callback(
		AvahiServiceResolver* r,
		AVAHI_GCC_UNUSED AvahiIfIndex interface,
		AVAHI_GCC_UNUSED AvahiProtocol protocol,
		AvahiResolverEvent event,
		const char* name,
		AVAHI_GCC_UNUSED const char* type,
		AVAHI_GCC_UNUSED const char* domain,
		AVAHI_GCC_UNUSED const char* host_name,
		const AvahiAddress* address,
		uint16_t port,
		AVAHI_GCC_UNUSED AvahiStringList* txt,
		AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
		void* userdata)
	{
		assert(r);
	
		switch (event) {
		case AVAHI_RESOLVER_FAILURE:
			{
				Out(SYS_ZCO|LOG_DEBUG) <<  "ZC: Resolver failed." << endl;
				break;
			}
		case AVAHI_RESOLVER_FOUND:
			{
				AvahiService* service = reinterpret_cast<AvahiService*>(userdata);
				
				TQString realname = TQString(name);
				realname.truncate(realname.length() - 5);
	
				if (service->id != TQString(realname)) 
				{
					char a[AVAHI_ADDRESS_STR_MAX];
					avahi_address_snprint(a, sizeof(a), address);
					const char* ip = a;
					LocalBrowser::insert(bt::PeerID(realname.ascii()));
	
					Out(SYS_ZCO|LOG_NOTICE) << "ZC: found local peer " << ip << ":" << port << endl;
					service->addPeer(ip,port,true);
					service->emitPeersReady(); 
				}
			}
		}
		avahi_service_resolver_free(r);
	}
	
	void browser_callback(
		AvahiServiceBrowser* b,
		AvahiIfIndex interface,
		AvahiProtocol protocol,
		AvahiBrowserEvent event,
		const char* name,
		const char* type,
		const char* domain,
		AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
		void* userdata)
	{
		assert(b);
	
		AvahiService* service = reinterpret_cast<AvahiService*>(userdata);
	
		switch (event) {
		case AVAHI_BROWSER_FAILURE:
			{
				Out(SYS_ZCO|LOG_DEBUG) << "ZC: Browser failure." << endl;
				break;
			}
		case AVAHI_BROWSER_NEW:
			{
				if (!(avahi_service_resolver_new(service->listener, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, (AvahiLookupFlags)0, resolve_callback, userdata)))
					Out(SYS_ZCO|LOG_DEBUG) << "ZC: Failed to resolve the service." << endl;
				break;
			}
		case AVAHI_BROWSER_REMOVE:
			{
				TQString realname = TQString(name);
				realname.truncate(realname.length() - 5);
	
				LocalBrowser::remove(bt::PeerID(realname.ascii()));
	
				Out(SYS_ZCO|LOG_DEBUG) << "ZC: Browser removed." << endl;
				break;
			}
		case AVAHI_BROWSER_ALL_FOR_NOW:
		case AVAHI_BROWSER_CACHE_EXHAUSTED:
			;
		}
	}
	
	AvahiService::AvahiService(const bt::PeerID& id,bt::Uint16 port, const bt::SHA1Hash & infoHash)
		: group(0), publisher_poll(0), listener_poll(0),publisher(0), listener(0), browser(0)
	{ 
		started = false; 
	
		this->id = id.toString();
		this->port = port;
		this->infoHash = infoHash.toString();
	}
	
	AvahiService::~AvahiService() 
	{
		stop(0);
	}
	
	void AvahiService::stop(bt::WaitJob*)
	{
		if (started) 
		{
			started = false;
	
			publisher_poll = 0;
			if (publisher)
			{
				avahi_client_free(publisher);
				publisher = 0;
			}
	
			listener_poll = 0;
			if (listener)
			{
				avahi_client_free(listener);
				listener = 0;
			}
		}
	}
	
	void AvahiService::start()
	{
		started = startPublishing() && startBrowsing();
	}
	
	bool AvahiService::startPublishing()
	{
		group = NULL;
		publisher_poll = NULL;
		publisher = NULL;
	
		if (!(publisher_poll = avahi_qt_poll_get())) 
		{
			Out(SYS_ZCO|LOG_DEBUG) << "ZC: Failed to create a poll for publishing." << endl;
			stop(); 
			return false;
		}
	
		publisher = avahi_client_new(publisher_poll, AVAHI_CLIENT_NO_FAIL, publisher_callback, this, NULL);
	
		if (!(publisher)) 
		{
			Out(SYS_ZCO|LOG_DEBUG) << "ZC: Failed to create a client for publishing." << endl;
			stop(); 
			return false;
		}
	
		return true;
	}
	
	bool AvahiService::startBrowsing()
	{
		listener_poll = NULL;
		listener = NULL;
		browser = NULL;
		
		if (!(listener_poll = avahi_qt_poll_get())) 
		{
			Out(SYS_ZCO|LOG_DEBUG) << "ZC: Failed to create a poll for browsing." << endl;
			stop(); 
			return false;
		}
		
		listener = avahi_client_new(listener_poll,AVAHI_CLIENT_NO_FAIL, listener_callback, this, NULL);
		
		if (!listener) 
		{
			Out(SYS_ZCO|LOG_DEBUG) << "ZC: Failed to create a client for browsing." << endl;
			stop(); 
			return false;
		}
		
		if (!(browser = avahi_service_browser_new(listener, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, avahi_strdup(TQString("_" + infoHash + "._sub._bittorrent._tcp").ascii()), NULL, (AvahiLookupFlags)0, browser_callback, this))) 
		{
			Out(SYS_ZCO|LOG_DEBUG) << "ZC: Failed to create a service browser." << endl;
			stop(); 
			return false;
		}
	
		return true;
	}
	
	void AvahiService::emitPeersReady()
	{
		peersReady(this);
	}
	
	void AvahiService::aboutToBeDestroyed()
	{
		serviceDestroyed(this);
	}
}

#include "avahiservice.moc"