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
|
#include "Swiften/LinkLocal/AvahiDNSSDService.h"
#include <boost/bind.hpp>
#include "Swiften/EventLoop/MainEventLoop.h"
#include "Swiften/LinkLocal/LinkLocalServiceInfo.h"
#include "Swiften/Network/HostAddress.h"
namespace Swift {
AvahiDNSSDService::AvahiDNSSDService() : client(NULL), threadedPoll(0), serviceBrowser(0) {
}
AvahiDNSSDService::~AvahiDNSSDService() {
}
void AvahiDNSSDService::start() {
threadedPoll = avahi_threaded_poll_new();
int error;
client = avahi_client_new(
avahi_threaded_poll_get(threadedPoll),
static_cast<AvahiClientFlags>(0), NULL, this, &error); // TODO
if (!client) {
// TODO
std::cerr << "Error 1" << std::endl;
return;
}
serviceBrowser = avahi_service_browser_new(
client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
"_presence._tcp", NULL, static_cast<AvahiLookupFlags>(0),
handleServiceDiscoveredGlobal, this);
if (!serviceBrowser) {
// TODO
std::cerr << "Error 2" << std::endl;
return;
}
avahi_threaded_poll_start(threadedPoll);
}
void AvahiDNSSDService::stop() {
avahi_threaded_poll_stop(threadedPoll);
avahi_service_browser_free(serviceBrowser);
avahi_client_free(client);
avahi_threaded_poll_free(threadedPoll);
}
void AvahiDNSSDService::registerService(const String& name, int port, const LinkLocalServiceInfo& info) {
avahi_threaded_poll_lock(threadedPoll);
avahi_threaded_poll_unlock(threadedPoll);
}
void AvahiDNSSDService::updateService(const LinkLocalServiceInfo& info) {
avahi_threaded_poll_lock(threadedPoll);
avahi_threaded_poll_unlock(threadedPoll);
}
void AvahiDNSSDService::unregisterService() {
avahi_threaded_poll_lock(threadedPoll);
avahi_threaded_poll_unlock(threadedPoll);
}
void AvahiDNSSDService::startResolvingService(const Service& service) {
avahi_threaded_poll_lock(threadedPoll);
AvahiServiceResolver* resolver = avahi_service_resolver_new(
client,
service.networkInterface,
AVAHI_PROTO_INET,
service.name.getUTF8Data(),
service.type.getUTF8Data(),
service.domain.getUTF8Data(),
AVAHI_PROTO_UNSPEC,
static_cast<AvahiLookupFlags>(0),
&AvahiDNSSDService::handleServiceResolvedGlobal,
this);
assert(serviceResolvers.find(service) == serviceResolvers.end());
serviceResolvers[service] = resolver;
avahi_threaded_poll_unlock(threadedPoll);
}
void AvahiDNSSDService::stopResolvingService(const Service& service) {
avahi_threaded_poll_lock(threadedPoll);
ServiceResolverMap::iterator i = serviceResolvers.find(service);
assert(i != serviceResolvers.end());
avahi_service_resolver_free(i->second);
serviceResolvers.erase(i);
avahi_threaded_poll_unlock(threadedPoll);
}
// TODO: Take interfaceIndex into account
void AvahiDNSSDService::resolveHostname(const String& hostname, int) {
HostnameAddressMap::const_iterator i = hostnameAddresses.find(hostname);
if (i == hostnameAddresses.end()) {
MainEventLoop::postEvent(boost::bind(boost::ref(onHostnameResolved), hostname, boost::optional<HostAddress>()), shared_from_this());
}
else {
MainEventLoop::postEvent(boost::bind(boost::ref(onHostnameResolved), hostname, boost::optional<HostAddress>(i->second)), shared_from_this());
}
}
void AvahiDNSSDService::handleServiceDiscovered(AvahiServiceBrowser *, AvahiIfIndex interfaceIndex, AvahiProtocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags) {
switch(event) {
case AVAHI_BROWSER_FAILURE:
std::cerr << "Browse error" << std::endl;
// TODO
return;
case AVAHI_BROWSER_NEW: {
std::cerr << "Service added: " << name << " " << type << " " << domain << std::endl;
Service service(name, type, domain, interfaceIndex);
MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this());
}
break;
case AVAHI_BROWSER_REMOVE: {
std::cerr << "Service removed: " << name << " " << type << " " << domain << std::endl;
Service service(name, type, domain, interfaceIndex);
MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRemoved), service), shared_from_this());
}
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
break;
}
}
void AvahiDNSSDService::handleServiceResolved(AvahiServiceResolver *, AvahiIfIndex interfaceIndex, AvahiProtocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *hostname, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags) {
switch(event) {
case AVAHI_RESOLVER_FAILURE:
//TODO
std::cerr << "Resolve error" << std::endl;
break;
case AVAHI_RESOLVER_FOUND:
ByteArray data;
for(AvahiStringList* i = txt; i; i = avahi_string_list_get_next(i)) {
char size = i->size;
data += ByteArray(&size, 1);
data += ByteArray(reinterpret_cast<char*>(avahi_string_list_get_text(i)), avahi_string_list_get_size(i));
}
assert(address->proto == AVAHI_PROTO_INET);
HostAddress hostAddress(reinterpret_cast<const unsigned char*>(&address->data.ipv4.address), 4);
hostnameAddresses[String(hostname)] = hostAddress;
MainEventLoop::postEvent(boost::bind(boost::ref(onServiceResolved),
Service(name, type, domain, interfaceIndex),
ResolveResult(hostname, port,
LinkLocalServiceInfo::createFromTXTRecord(data))),
shared_from_this());
break;
}
}
}
|