/* * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include #include #include #include #include #include using namespace Swift; namespace { struct ServiceQuery : public DomainNameServiceQuery, public std::enable_shared_from_this { ServiceQuery(const std::string& service, Swift::StaticDomainNameResolver* resolver, EventLoop* eventLoop, std::shared_ptr owner) : eventLoop(eventLoop), service(service), resolver(resolver), owner(owner) {} virtual void run() { if (!resolver->getIsResponsive()) { return; } std::vector results; for(const auto& i : resolver->getServices()) { if (i.first == service) { results.push_back(i.second); } } eventLoop->postEvent(boost::bind(&ServiceQuery::emitOnResult, shared_from_this(), results), owner); } void emitOnResult(std::vector results) { onResult(results); } EventLoop* eventLoop; std::string service; StaticDomainNameResolver* resolver; std::shared_ptr owner; }; struct AddressQuery : public DomainNameAddressQuery, public std::enable_shared_from_this { AddressQuery(const std::string& host, StaticDomainNameResolver* resolver, EventLoop* eventLoop, std::shared_ptr owner) : eventLoop(eventLoop), host(host), resolver(resolver), owner(owner) {} virtual void run() { if (!resolver->getIsResponsive()) { return; } StaticDomainNameResolver::AddressesMap::const_iterator i = resolver->getAddresses().find(host); if (i != resolver->getAddresses().end()) { eventLoop->postEvent( boost::bind(&AddressQuery::emitOnResult, shared_from_this(), i->second, boost::optional())); } else { eventLoop->postEvent(boost::bind(&AddressQuery::emitOnResult, shared_from_this(), std::vector(), boost::optional(DomainNameResolveError())), owner); } } void emitOnResult(std::vector results, boost::optional error) { onResult(results, error); } EventLoop* eventLoop; std::string host; StaticDomainNameResolver* resolver; std::shared_ptr owner; }; } class StaticDomainNameResolverEventOwner : public EventOwner { public: ~StaticDomainNameResolverEventOwner() { } }; namespace Swift { StaticDomainNameResolver::StaticDomainNameResolver(EventLoop* eventLoop) : eventLoop(eventLoop), isResponsive(true), owner(new StaticDomainNameResolverEventOwner()) { } StaticDomainNameResolver::~StaticDomainNameResolver() { eventLoop->removeEventsFromOwner(owner); } void StaticDomainNameResolver::addAddress(const std::string& domain, const HostAddress& address) { addresses[domain].push_back(address); } void StaticDomainNameResolver::addService(const std::string& service, const DomainNameServiceQuery::Result& result) { services.push_back(std::make_pair(service, result)); } void StaticDomainNameResolver::addXMPPClientService(const std::string& domain, const HostAddressPort& address) { static int hostid = 0; std::string hostname(std::string("host-") + boost::lexical_cast(hostid)); hostid++; addService("_xmpp-client._tcp." + domain, ServiceQuery::Result(hostname, address.getPort(), 0, 0)); addAddress(hostname, address.getAddress()); } void StaticDomainNameResolver::addXMPPClientService(const std::string& domain, const std::string& hostname, int port) { addService("_xmpp-client._tcp." + domain, ServiceQuery::Result(hostname, port, 0, 0)); } std::shared_ptr StaticDomainNameResolver::createServiceQuery(const std::string& serviceLookupPrefix, const std::string& domain) { return std::make_shared(serviceLookupPrefix + domain, this, eventLoop, owner); } std::shared_ptr StaticDomainNameResolver::createAddressQuery(const std::string& name) { return std::make_shared(name, this, eventLoop, owner); } }