summaryrefslogtreecommitdiffstats
path: root/dnssd/query.cpp
blob: 3306dc256825f48bf8b8eda31195a4eb9c8cfc7b (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
/* This file is part of the KDE project
 *
 * Copyright (C) 2004 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 "query.h"
#include "responder.h"
#include "remoteservice.h"
#include "sdevent.h"
#include <tqdatetime.h>
#include <tqapplication.h>
#include <tqtimer.h>

#ifdef HAVE_DNSSD
#include <avahi-client/client.h>
#ifdef AVAHI_API_0_6
#include <avahi-client/lookup.h>
#endif
#endif

#define TIMEOUT_LAN 200

namespace DNSSD
{
#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6

void services_callback(AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* name,
    const char* regtype, const char* domain, AvahiLookupResultFlags, void* context);
void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* regtype,
    const char* replyDomain, AvahiLookupResultFlags, void* context);
#else
void services_callback(AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* name,
    const char* regtype, const char* domain, void* context);
void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* regtype,
    const char* replyDomain, void* context);
void domains_callback(AvahiDomainBrowser*,  AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain,
     void* context);
#endif
#endif

enum BrowserType { Types, Services };

class QueryPrivate
{
public:
	QueryPrivate(const TQString& type, const TQString& domain) : m_finished(false), m_browser(0),
	m_running(false), m_domain(domain), m_type(type) {}

	bool m_finished;
	BrowserType m_browserType;
	void* m_browser;
	bool m_running;
	TQString m_domain;
	TQTimer timeout;
	TQString m_type;
};

Query::Query(const TQString& type, const TQString& domain)
{
	d = new QueryPrivate(type,domain);
	connect(&d->timeout,TQT_SIGNAL(timeout()),this,TQT_SLOT(timeout()));
}


Query::~Query()
{
#ifdef HAVE_DNSSD
	if (d->m_browser) {
	    switch (d->m_browserType) {
		case Services: avahi_service_browser_free((AvahiServiceBrowser*)d->m_browser); break;
		case Types: avahi_service_type_browser_free((AvahiServiceTypeBrowser*)d->m_browser); break;
	    }
	}
#endif
	delete d;
}

bool Query::isRunning() const
{
	return d->m_running;
}

bool Query::isFinished() const
{
	return d->m_finished;
}

const TQString& Query::domain() const
{
	return d->m_domain;
}

void Query::startQuery()
{
	if (d->m_running) return;
	d->m_finished = false;
	if (d->m_type=="_services._dns-sd._udp") {
	    d->m_browserType = Types;
#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
	    d->m_browser = avahi_service_type_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
		domainToDNS(d->m_domain), (AvahiLookupFlags)0, types_callback, this);
#else
	    d->m_browser = avahi_service_type_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
		d->m_domain.utf8(), types_callback, this);
#endif
#endif
	} else {
	    d->m_browserType = Services;
#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
	    d->m_browser = avahi_service_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
	    d->m_type.ascii(),domainToDNS(d->m_domain),  (AvahiLookupFlags)0, services_callback,this);
#else
	    d->m_browser = avahi_service_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
	    d->m_type.ascii(),d->m_domain.utf8(),services_callback,this);
#endif
#endif
	}
	if (d->m_browser) {
		d->m_running=true;
		d->timeout.start(TIMEOUT_LAN,true);
	} else emit finished();
}
void Query::virtual_hook(int, void*)
{
}

void Query::customEvent(TQCustomEvent* event)
{
	if (event->type()==TQEvent::User+SD_ADDREMOVE) {
		d->timeout.start(TIMEOUT_LAN,true);
		d->m_finished=false;
		AddRemoveEvent *aev = static_cast<AddRemoveEvent*>(event);
		// m_type has useless trailing dot
		RemoteService*  svr = new RemoteService(aev->m_name,
		    	aev->m_type,aev->m_domain);
		if (aev->m_op==AddRemoveEvent::Add) emit serviceAdded(svr);
			else emit serviceRemoved(svr);
	}
}

void Query::timeout()
{
	d->m_finished=true;
	emit finished();
}

#ifdef HAVE_DNSSD
#ifdef AVAHI_API_0_6
void services_callback (AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
    const char* serviceName, const char* regtype, const char* replyDomain, AvahiLookupResultFlags, void* context)
#else
void services_callback (AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
    const char* serviceName, const char* regtype, const char* replyDomain, void* context)
#endif
{
	TQObject *obj = reinterpret_cast<TQObject*>(context);
	AddRemoveEvent* arev = new AddRemoveEvent((event==AVAHI_BROWSER_NEW) ? AddRemoveEvent::Add :
			AddRemoveEvent::Remove, TQString::fromUtf8(serviceName), regtype,
			DNSToDomain(replyDomain));
		TQApplication::postEvent(obj, arev);
}

#ifdef AVAHI_API_0_6
void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* regtype,
    const char* replyDomain, AvahiLookupResultFlags, void* context)
#else
void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* regtype,
    const char* replyDomain, void* context)
#endif
{
	TQObject *obj = reinterpret_cast<TQObject*>(context);
	AddRemoveEvent* arev = new AddRemoveEvent((event==AVAHI_BROWSER_NEW) ? AddRemoveEvent::Add :
			AddRemoveEvent::Remove, TQString::null, regtype,
			DNSToDomain(replyDomain));
		TQApplication::postEvent(obj, arev);
}
#endif

}
#include "query.moc"