diff options
Diffstat (limited to 'Swiften/LinkLocal/DNSSD/Bonjour')
m--------- | Swiften | 0 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h | 55 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp | 129 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h | 50 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.cpp | 31 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h | 32 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h | 55 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h | 63 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h | 58 |
9 files changed, 0 insertions, 473 deletions
diff --git a/Swiften b/Swiften new file mode 160000 +Subproject 8213ba16d0043d2461f4b031c881d61dda5a38c diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h deleted file mode 100644 index 2dec2fb..0000000 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h" -#include "Swiften/LinkLocal/DNSSD/DNSSDBrowseQuery.h" -#include "Swiften/EventLoop/MainEventLoop.h" - -namespace Swift { - class BonjourQuerier; - - class BonjourBrowseQuery : public DNSSDBrowseQuery, public BonjourQuery { - public: - BonjourBrowseQuery(boost::shared_ptr<BonjourQuerier> q) : BonjourQuery(q) { - DNSServiceErrorType result = DNSServiceBrowse( - &sdRef, 0, 0, "_presence._tcp", 0, - &BonjourBrowseQuery::handleServiceDiscoveredStatic, this); - if (result != kDNSServiceErr_NoError) { - sdRef = NULL; - } - } - - void startBrowsing() { - if (!sdRef) { - MainEventLoop::postEvent(boost::bind(boost::ref(onError)), shared_from_this()); - } - else { - run(); - } - } - - void stopBrowsing() { - finish(); - } - - private: - static void handleServiceDiscoveredStatic(DNSServiceRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *name, const char *type, const char *domain, void *context) { - static_cast<BonjourBrowseQuery*>(context)->handleServiceDiscovered(flags, interfaceIndex, errorCode, name, type, domain); - } - - void handleServiceDiscovered(DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *name, const char *type, const char *domain) { - if (errorCode != kDNSServiceErr_NoError) { - MainEventLoop::postEvent(boost::bind(boost::ref(onError)), shared_from_this()); - } - else { - //std::cout << "Discovered service: name:" << name << " domain:" << domain << " type: " << type << std::endl; - DNSSDServiceID service(name, domain, type, interfaceIndex); - if (flags & kDNSServiceFlagsAdd) { - MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this()); - } - else { - MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRemoved), service), shared_from_this()); - } - } - } - }; -} diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp deleted file mode 100644 index 9c9e64e..0000000 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h" - -#include <unistd.h> -#include <sys/socket.h> -#include <fcntl.h> - -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h" -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h" -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h" -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h" -#include "Swiften/Base/foreach.h" - -namespace Swift { - -BonjourQuerier::BonjourQuerier() : stopRequested(false), thread(0) { - int fds[2]; - int result = pipe(fds); - assert(result == 0); - interruptSelectReadSocket = fds[0]; - fcntl(interruptSelectReadSocket, F_SETFL, fcntl(interruptSelectReadSocket, F_GETFL)|O_NONBLOCK); - interruptSelectWriteSocket = fds[1]; -} - -BonjourQuerier::~BonjourQuerier() { - assert(!thread); -} - -boost::shared_ptr<DNSSDBrowseQuery> BonjourQuerier::createBrowseQuery() { - return boost::shared_ptr<DNSSDBrowseQuery>(new BonjourBrowseQuery(shared_from_this())); -} - -boost::shared_ptr<DNSSDRegisterQuery> BonjourQuerier::createRegisterQuery(const String& name, int port, const ByteArray& info) { - return boost::shared_ptr<DNSSDRegisterQuery>(new BonjourRegisterQuery(name, port, info, shared_from_this())); -} - -boost::shared_ptr<DNSSDResolveServiceQuery> BonjourQuerier::createResolveServiceQuery(const DNSSDServiceID& service) { - return boost::shared_ptr<DNSSDResolveServiceQuery>(new BonjourResolveServiceQuery(service, shared_from_this())); -} - -boost::shared_ptr<DNSSDResolveHostnameQuery> BonjourQuerier::createResolveHostnameQuery(const String& hostname, int interfaceIndex) { - return boost::shared_ptr<DNSSDResolveHostnameQuery>(new BonjourResolveHostnameQuery(hostname, interfaceIndex, shared_from_this())); -} - -void BonjourQuerier::addRunningQuery(boost::shared_ptr<BonjourQuery> query) { - { - boost::lock_guard<boost::mutex> lock(runningQueriesMutex); - runningQueries.push_back(query); - } - runningQueriesAvailableEvent.notify_one(); - interruptSelect(); -} - -void BonjourQuerier::removeRunningQuery(boost::shared_ptr<BonjourQuery> query) { - { - boost::lock_guard<boost::mutex> lock(runningQueriesMutex); - runningQueries.erase(std::remove( - runningQueries.begin(), runningQueries.end(), query), runningQueries.end()); - } -} - -void BonjourQuerier::interruptSelect() { - char c = 0; - write(interruptSelectWriteSocket, &c, 1); -} - -void BonjourQuerier::start() { - assert(!thread); - thread = new boost::thread(boost::bind(&BonjourQuerier::run, shared_from_this())); -} - -void BonjourQuerier::stop() { - if (thread) { - stopRequested = true; - assert(runningQueries.empty()); - runningQueriesAvailableEvent.notify_one(); - interruptSelect(); - thread->join(); - delete thread; - thread = NULL; - stopRequested = false; - } -} - -void BonjourQuerier::run() { - while (!stopRequested) { - fd_set fdSet; - int maxSocket; - { - boost::unique_lock<boost::mutex> lock(runningQueriesMutex); - if (runningQueries.empty()) { - runningQueriesAvailableEvent.wait(lock); - if (runningQueries.empty()) { - continue; - } - } - - // Run all running queries - FD_ZERO(&fdSet); - maxSocket = interruptSelectReadSocket; - FD_SET(interruptSelectReadSocket, &fdSet); - - foreach(const boost::shared_ptr<BonjourQuery>& query, runningQueries) { - int socketID = query->getSocketID(); - maxSocket = std::max(maxSocket, socketID); - FD_SET(socketID, &fdSet); - } - } - - if (select(maxSocket+1, &fdSet, NULL, NULL, 0) <= 0) { - continue; - } - - if (FD_ISSET(interruptSelectReadSocket, &fdSet)) { - char dummy; - while (read(interruptSelectReadSocket, &dummy, 1) > 0) {} - } - - { - boost::lock_guard<boost::mutex> lock(runningQueriesMutex); - foreach(boost::shared_ptr<BonjourQuery> query, runningQueries) { - if (FD_ISSET(query->getSocketID(), &fdSet)) { - query->processResult(); - } - } - } - } -} - -} diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h deleted file mode 100644 index d12f94f..0000000 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include <boost/shared_ptr.hpp> -#include <boost/enable_shared_from_this.hpp> -#include <list> -#include <boost/thread.hpp> -#include <boost/thread/mutex.hpp> - -#include "Swiften/LinkLocal/DNSSD/DNSSDQuerier.h" -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h" - -namespace Swift { - class ByteArray; - - class BonjourQuerier : - public DNSSDQuerier, - public boost::enable_shared_from_this<BonjourQuerier> { - public: - BonjourQuerier(); - ~BonjourQuerier(); - - boost::shared_ptr<DNSSDBrowseQuery> createBrowseQuery(); - boost::shared_ptr<DNSSDRegisterQuery> createRegisterQuery( - const String& name, int port, const ByteArray& info); - boost::shared_ptr<DNSSDResolveServiceQuery> createResolveServiceQuery( - const DNSSDServiceID&); - boost::shared_ptr<DNSSDResolveHostnameQuery> createResolveHostnameQuery( - const String& hostname, int interfaceIndex); - - void start(); - void stop(); - - private: - friend class BonjourQuery; - - void addRunningQuery(boost::shared_ptr<BonjourQuery>); - void removeRunningQuery(boost::shared_ptr<BonjourQuery>); - void interruptSelect(); - void run(); - - private: - bool stopRequested; - boost::thread* thread; - boost::mutex runningQueriesMutex; - std::list< boost::shared_ptr<BonjourQuery> > runningQueries; - int interruptSelectReadSocket; - int interruptSelectWriteSocket; - boost::condition_variable runningQueriesAvailableEvent; - }; -} diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.cpp b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.cpp deleted file mode 100644 index c1c481b..0000000 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h" -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h" - -namespace Swift { - -BonjourQuery::BonjourQuery(boost::shared_ptr<BonjourQuerier> q) : querier(q), sdRef(0) { -} - -BonjourQuery::~BonjourQuery() { - DNSServiceRefDeallocate(sdRef); -} - -void BonjourQuery::processResult() { - boost::lock_guard<boost::mutex> lock(sdRefMutex); - DNSServiceProcessResult(sdRef); -} - -int BonjourQuery::getSocketID() const { - boost::lock_guard<boost::mutex> lock(sdRefMutex); - return DNSServiceRefSockFD(sdRef); -} - -void BonjourQuery::run() { - querier->addRunningQuery(shared_from_this()); -} - -void BonjourQuery::finish() { - querier->removeRunningQuery(shared_from_this()); -} - -} diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h deleted file mode 100644 index bdb91a4..0000000 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include <dns_sd.h> -#include <boost/shared_ptr.hpp> -#include <boost/enable_shared_from_this.hpp> -#include <boost/thread/mutex.hpp> - -#include "Swiften/EventLoop/EventOwner.h" - -namespace Swift { - class BonjourQuerier; - - class BonjourQuery : - public EventOwner, - public boost::enable_shared_from_this<BonjourQuery> { - public: - BonjourQuery(boost::shared_ptr<BonjourQuerier>); - virtual ~BonjourQuery(); - - void processResult(); - int getSocketID() const; - - protected: - void run(); - void finish(); - - protected: - boost::shared_ptr<BonjourQuerier> querier; - mutable boost::mutex sdRefMutex; - DNSServiceRef sdRef; - }; -} diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h deleted file mode 100644 index ddc2788..0000000 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h" -#include "Swiften/LinkLocal/DNSSD/DNSSDRegisterQuery.h" -#include "Swiften/Base/ByteArray.h" -#include "Swiften/EventLoop/MainEventLoop.h" - -namespace Swift { - class BonjourQuerier; - - class BonjourRegisterQuery : public DNSSDRegisterQuery, public BonjourQuery { - public: - BonjourRegisterQuery(const String& name, int port, const ByteArray& txtRecord, boost::shared_ptr<BonjourQuerier> querier) : BonjourQuery(querier) { - DNSServiceErrorType result = DNSServiceRegister( - &sdRef, 0, 0, name.getUTF8Data(), "_presence._tcp", NULL, NULL, port, - txtRecord.getSize(), txtRecord.getData(), - &BonjourRegisterQuery::handleServiceRegisteredStatic, this); - if (result != kDNSServiceErr_NoError) { - sdRef = NULL; - } - } - - void registerService() { - if (sdRef) { - run(); - } - else { - MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this()); - } - } - - void unregisterService() { - finish(); - } - - void updateServiceInfo(const ByteArray& txtRecord) { - boost::lock_guard<boost::mutex> lock(sdRefMutex); - DNSServiceUpdateRecord(sdRef, NULL, NULL, txtRecord.getSize(), txtRecord.getData(), 0); - } - - private: - static void handleServiceRegisteredStatic(DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain, void *context) { - static_cast<BonjourRegisterQuery*>(context)->handleServiceRegistered(errorCode, name, regtype, domain); - } - - void handleServiceRegistered(DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain) { - if (errorCode != kDNSServiceErr_NoError) { - MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this()); - } - else { - MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>(DNSSDServiceID(name, domain, regtype, 0))), shared_from_this()); - } - } - }; -} diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h deleted file mode 100644 index 7b5f19a..0000000 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once - -#include "Swiften/Base/String.h" -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h" -#include "Swiften/LinkLocal/DNSSD/DNSSDResolveHostnameQuery.h" -#include "Swiften/EventLoop/MainEventLoop.h" -#include "Swiften/Network/HostAddress.h" - -#include <netinet/in.h> - -namespace Swift { - class BonjourQuerier; - - class BonjourResolveHostnameQuery : public DNSSDResolveHostnameQuery, public BonjourQuery { - public: - BonjourResolveHostnameQuery(const String& hostname, int interfaceIndex, boost::shared_ptr<BonjourQuerier> querier) : BonjourQuery(querier) { - DNSServiceErrorType result = DNSServiceGetAddrInfo( - &sdRef, 0, interfaceIndex, kDNSServiceProtocol_IPv4, - hostname.getUTF8Data(), - &BonjourResolveHostnameQuery::handleHostnameResolvedStatic, this); - if (result != kDNSServiceErr_NoError) { - sdRef = NULL; - } - } - - //void DNSSDResolveHostnameQuery::run() { - void run() { - if (sdRef) { - BonjourQuery::run(); - } - else { - MainEventLoop::postEvent(boost::bind(boost::ref(onHostnameResolved), boost::optional<HostAddress>()), shared_from_this()); - } - } - - void finish() { - BonjourQuery::finish(); - } - - private: - static void handleHostnameResolvedStatic(DNSServiceRef, DNSServiceFlags, uint32_t, DNSServiceErrorType errorCode, const char*, const struct sockaddr *address, uint32_t, void *context) { - static_cast<BonjourResolveHostnameQuery*>(context)->handleHostnameResolved(errorCode, address); - } - - void handleHostnameResolved(DNSServiceErrorType errorCode, const struct sockaddr *rawAddress) { - if (errorCode) { - MainEventLoop::postEvent( - boost::bind(boost::ref(onHostnameResolved), - boost::optional<HostAddress>()), - shared_from_this()); - } - else { - assert(rawAddress->sa_family == AF_INET); - const sockaddr_in* sa = reinterpret_cast<const sockaddr_in*>(rawAddress); - uint32_t address = ntohl(sa->sin_addr.s_addr); - MainEventLoop::postEvent(boost::bind( - boost::ref(onHostnameResolved), - HostAddress(reinterpret_cast<unsigned char*>(&address), 4)), - shared_from_this()); - } - } - }; -} diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h deleted file mode 100644 index 1c38179..0000000 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h" -#include "Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h" -#include "Swiften/LinkLocal/LinkLocalServiceInfo.h" -#include "Swiften/Base/ByteArray.h" -#include "Swiften/EventLoop/MainEventLoop.h" - -namespace Swift { - class BonjourQuerier; - - class BonjourResolveServiceQuery : public DNSSDResolveServiceQuery, public BonjourQuery { - public: - BonjourResolveServiceQuery(const DNSSDServiceID& service, boost::shared_ptr<BonjourQuerier> querier) : BonjourQuery(querier) { - DNSServiceErrorType result = DNSServiceResolve( - &sdRef, 0, service.getNetworkInterfaceID(), - service.getName().getUTF8Data(), service.getType().getUTF8Data(), - service.getDomain().getUTF8Data(), - &BonjourResolveServiceQuery::handleServiceResolvedStatic, this); - if (result != kDNSServiceErr_NoError) { - sdRef = NULL; - } - } - - void start() { - if (sdRef) { - run(); - } - else { - MainEventLoop::postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this()); - } - } - - void stop() { - finish(); - } - - private: - static void handleServiceResolvedStatic(DNSServiceRef, DNSServiceFlags, uint32_t, DNSServiceErrorType errorCode, const char *fullname, const char *hosttarget, uint16_t port, uint16_t txtLen, const unsigned char *txtRecord, void *context) { - static_cast<BonjourResolveServiceQuery*>(context)->handleServiceResolved(errorCode, fullname, hosttarget, port, txtLen, txtRecord); - } - - void handleServiceResolved(DNSServiceErrorType errorCode, const char* fullName, const char* host, uint16_t port, uint16_t txtLen, const unsigned char *txtRecord) { - if (errorCode != kDNSServiceErr_NoError) { - MainEventLoop::postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this()); - } - else { - //std::cout << "Service resolved: name:" << fullName << " host:" << host << " port:" << port << std::endl; - MainEventLoop::postEvent( - boost::bind( - boost::ref(onServiceResolved), - Result(String(fullName), String(host), port, - ByteArray(reinterpret_cast<const char*>(txtRecord), txtLen))), - shared_from_this()); - } - } - }; -} |