summaryrefslogtreecommitdiffstats
path: root/dnssd/remoteservice.cpp
blob: 2f5bc59279e8a55b37b8d92247da9a3ea63a72fd (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
/* 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 <tqeventloop.h>
#include <tqapplication.h>
#include <kurl.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <netinet/in.h>
#ifdef HAVE_DNSSD
#include <avahi-client/client.h>
#include <avahi-common/strlst.h>
#ifdef AVAHI_API_0_6
#include <avahi-client/lookup.h>
#endif
#endif
#include "remoteservice.h"
#include "responder.h"
#include "sdevent.h"

namespace DNSSD
{
#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
    const char* name, const char* type, const char* domain, const char* hostname, const AvahiAddress* a,
    uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags, void* context);
#else
void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
    const char* name, const char* type, const char* domain, const char* hostname, const AvahiAddress* a,
    uint16_t port, AvahiStringList* txt, void* context);
#endif
#endif

class RemoteServicePrivate : public Responder
{
public:
	RemoteServicePrivate() :  m_resolved(false), m_running(false)
#ifdef HAVE_DNSSD
	, m_resolver(0)
#endif
	{}
	bool m_resolved;
	bool m_running;
#ifdef HAVE_DNSSD
	AvahiServiceResolver* m_resolver;
	void stop() {
	    m_running = false;
	    if (m_resolver) avahi_service_resolver_free(m_resolver);
	    m_resolver=0;
	}
#endif
};

RemoteService::RemoteService(const TQString& label)
{
	decode(label);
	d =  new RemoteServicePrivate();
}
RemoteService::RemoteService(const TQString& name,const TQString& type,const TQString& 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()
{
#ifdef HAVE_DNSSD
	if (d->m_resolver) avahi_service_resolver_free(d->m_resolver);
#endif
	delete d;
}

bool RemoteService::resolve()
{
	resolveAsync();
	while (d->m_running && !d->m_resolved) Responder::self().process();
#ifdef HAVE_DNSSD
	d->stop();
#endif
	return d->m_resolved;
}

void RemoteService::resolveAsync()
{
	if (d->m_running) return;
	d->m_resolved = false;
	// FIXME: first protocol should be set?
#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
	d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
	    m_serviceName.utf8(), m_type.ascii(), domainToDNS(m_domain), AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_ADDRESS,
	    resolve_callback, this);
#else
	d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
	    m_serviceName.utf8(), m_type.ascii(), m_domain.utf8(), AVAHI_PROTO_UNSPEC, resolve_callback, this);
#endif
	if (d->m_resolver) d->m_running=true;
	    else  emit resolved(false);
#endif
}

bool RemoteService::isResolved() const
{
	return d->m_resolved;
}

void RemoteService::customEvent(TQCustomEvent* event)
{
	if (event->type() == TQEvent::User+SD_ERROR) {
#ifdef HAVE_DNSSD
		d->stop();
#endif
		d->m_resolved=false;
		emit resolved(false);
	}
	if (event->type() == TQEvent::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*);
}

TQDataStream & operator<< (TQDataStream & s, const RemoteService & a)
{
	s << (static_cast<ServiceBase>(a));
	TQ_INT8 resolved = a.d->m_resolved ? 1:0;
	s << resolved;
	return s;
}

TQDataStream & operator>> (TQDataStream & s, RemoteService & a)
{
	// stop any possible resolve going on
#ifdef HAVE_DNSSD
	a.d->stop();
#endif
	TQ_INT8 resolved;
	operator>>(s,(static_cast<ServiceBase&>(a)));
	s >> resolved;
	a.d->m_resolved = (resolved == 1);
	return s;
}

#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
    const char*, const char*, const char*, const char* hostname, const AvahiAddress*,
    uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags, void* context)
#else
void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
    const char*, const char*, const char*, const char* hostname, const AvahiAddress*,
    uint16_t port, AvahiStringList* txt, void* context)
#endif
{
	TQObject *obj = reinterpret_cast<TQObject*>(context);
	if (e != AVAHI_RESOLVER_FOUND) {
		ErrorEvent err;
		TQApplication::sendEvent(obj, &err);
		return;
	}
	TQMap<TQString,TQString> map;
	while (txt) {
	    char *key, *value;
	    size_t size;
	    if (avahi_string_list_get_pair(txt,&key,&value,&size)) break;
	    map[TQString::fromUtf8(key)]=(value) ? TQString::fromUtf8(value) : TQString::null;
	    txt = txt->next;
	}
	ResolveEvent rev(DNSToDomain(hostname),port,map);
	TQApplication::sendEvent(obj, &rev);
}
#endif


}

#include "remoteservice.moc"