summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/LinkLocal')
-rw-r--r--Swiften/LinkLocal/AppleDNSSDService.cpp1
-rw-r--r--Swiften/LinkLocal/AvahiDNSSDService.cpp157
-rw-r--r--Swiften/LinkLocal/AvahiDNSSDService.h55
-rw-r--r--Swiften/LinkLocal/Makefile.inc4
4 files changed, 217 insertions, 0 deletions
diff --git a/Swiften/LinkLocal/AppleDNSSDService.cpp b/Swiften/LinkLocal/AppleDNSSDService.cpp
index 521c584..969cbb2 100644
--- a/Swiften/LinkLocal/AppleDNSSDService.cpp
+++ b/Swiften/LinkLocal/AppleDNSSDService.cpp
@@ -268,6 +268,7 @@ void AppleDNSSDService::handleServiceResolvedGlobal(DNSServiceRef sdRef, DNSServ
void AppleDNSSDService::handleServiceResolved(DNSServiceRef sdRef, DNSServiceFlags, uint32_t, DNSServiceErrorType errorCode, const char *, const char *hosttarget, uint16_t port, uint16_t txtLen, const unsigned char *txtRecord) {
if (errorCode != kDNSServiceErr_NoError) {
+ // TODO
std::cerr << "Resolve error " << hosttarget << std::endl;
return;
}
diff --git a/Swiften/LinkLocal/AvahiDNSSDService.cpp b/Swiften/LinkLocal/AvahiDNSSDService.cpp
new file mode 100644
index 0000000..62ea172
--- /dev/null
+++ b/Swiften/LinkLocal/AvahiDNSSDService.cpp
@@ -0,0 +1,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;
+ }
+}
+
+}
diff --git a/Swiften/LinkLocal/AvahiDNSSDService.h b/Swiften/LinkLocal/AvahiDNSSDService.h
new file mode 100644
index 0000000..8d31e41
--- /dev/null
+++ b/Swiften/LinkLocal/AvahiDNSSDService.h
@@ -0,0 +1,55 @@
+#pragma once
+
+#include <map>
+#include <boost/enable_shared_from_this.hpp>
+#include <avahi-client/client.h>
+#include <avahi-client/lookup.h>
+#include <avahi-common/thread-watch.h>
+#include <avahi-common/watch.h>
+#include <avahi-common/malloc.h>
+#include <avahi-common/error.h>
+
+
+#include "Swiften/EventLoop/EventOwner.h"
+#include "Swiften/LinkLocal/DNSSDService.h"
+
+namespace Swift {
+ class AvahiDNSSDService : public DNSSDService, public EventOwner, public boost::enable_shared_from_this<AvahiDNSSDService> {
+ public:
+ AvahiDNSSDService();
+ ~AvahiDNSSDService();
+
+ virtual void start();
+ virtual void stop();
+
+ virtual void registerService(const String& name, int port, const LinkLocalServiceInfo&);
+ virtual void updateService(const LinkLocalServiceInfo&);
+ virtual void unregisterService();
+
+ virtual void startResolvingService(const Service&);
+ virtual void stopResolvingService(const Service&);
+
+ virtual void resolveHostname(const String& hostname, int interfaceIndex = 0);
+
+ private:
+ static void handleServiceDiscoveredGlobal(AvahiServiceBrowser *b, AvahiIfIndex networkInterface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* userdata) {
+ static_cast<AvahiDNSSDService*>(userdata)->handleServiceDiscovered(b, networkInterface, protocol, event, name, type, domain, flags);
+ }
+ void handleServiceDiscovered(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AVAHI_GCC_UNUSED AvahiLookupResultFlags flags);
+
+ static void handleServiceResolvedGlobal(AvahiServiceResolver *r, AvahiIfIndex interfaceIndex, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *hostname, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void* userdata) {
+ static_cast<AvahiDNSSDService*>(userdata)->handleServiceResolved(r, interfaceIndex, protocol, event, name, type, domain, hostname, address, port, txt, flags);
+ }
+ void handleServiceResolved(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *hostname, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags);
+
+
+ private:
+ AvahiClient* client;
+ AvahiThreadedPoll* threadedPoll;
+ AvahiServiceBrowser* serviceBrowser;
+ typedef std::map<Service, AvahiServiceResolver*> ServiceResolverMap;
+ ServiceResolverMap serviceResolvers;
+ typedef std::map<String, HostAddress> HostnameAddressMap;
+ HostnameAddressMap hostnameAddresses;
+ };
+}
diff --git a/Swiften/LinkLocal/Makefile.inc b/Swiften/LinkLocal/Makefile.inc
index c24f2b8..788c000 100644
--- a/Swiften/LinkLocal/Makefile.inc
+++ b/Swiften/LinkLocal/Makefile.inc
@@ -10,5 +10,9 @@ ifeq ($(MACOSX),1)
SWIFTEN_SOURCES += \
Swiften/LinkLocal/AppleDNSSDService.cpp
endif
+ifeq ($(HAVE_AVAHI),yes)
+SWIFTEN_SOURCES += \
+ Swiften/LinkLocal/AvahiDNSSDService.cpp
+endif
include Swiften/LinkLocal/UnitTest/Makefile.inc