diff options
Diffstat (limited to 'Swiften/Network/UnboundDomainNameResolver.cpp')
-rwxr-xr-x | Swiften/Network/UnboundDomainNameResolver.cpp | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/Swiften/Network/UnboundDomainNameResolver.cpp b/Swiften/Network/UnboundDomainNameResolver.cpp index d986385..bc280eb 100755 --- a/Swiften/Network/UnboundDomainNameResolver.cpp +++ b/Swiften/Network/UnboundDomainNameResolver.cpp @@ -1,51 +1,52 @@ /* * Copyright (c) 2013 Tobias Markmann * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include "UnboundDomainNameResolver.h" #include <vector> #include <boost/bind.hpp> #include <boost/smart_ptr/make_shared.hpp> #include <boost/enable_shared_from_this.hpp> #include <Swiften/Base/Log.h> #include <Swiften/EventLoop/EventLoop.h> +#include <Swiften/IDN/IDNConverter.h> #include <Swiften/Network/DomainNameAddressQuery.h> #include <Swiften/Network/DomainNameResolveError.h> #include <Swiften/Network/DomainNameServiceQuery.h> #include <Swiften/Network/HostAddress.h> #include <Swiften/Network/TimerFactory.h> #include <arpa/inet.h> #include <unbound.h> #include <ldns/ldns.h> #include <unistd.h> namespace Swift { class UnboundQuery { public: UnboundQuery(UnboundDomainNameResolver* resolver, ub_ctx* context) : resolver(resolver), ubContext(context) {} virtual ~UnboundQuery() {} virtual void handleResult(int err, ub_result* result) = 0; protected: UnboundDomainNameResolver* resolver; ub_ctx* ubContext; }; struct UnboundWrapperHelper { UnboundWrapperHelper(UnboundDomainNameResolver* resolver, boost::shared_ptr<UnboundQuery> query) : resolver(resolver), query(query) {} UnboundDomainNameResolver* resolver; boost::shared_ptr<UnboundQuery> query; }; class UnboundDomainNameServiceQuery : public DomainNameServiceQuery, public UnboundQuery, public boost::enable_shared_from_this<UnboundDomainNameServiceQuery> { public: UnboundDomainNameServiceQuery(UnboundDomainNameResolver* resolver, ub_ctx* context, std::string name) : UnboundQuery(resolver, context), name(name) { } virtual ~UnboundDomainNameServiceQuery() { } @@ -141,101 +142,106 @@ class UnboundDomainNameAddressQuery : public DomainNameAddressQuery, public Unbo boost::optional<DomainNameResolveError> error; SWIFT_LOG(debug) << "Result for: " << name << std::endl; if(err != 0) { SWIFT_LOG(debug) << "resolve error: " << ub_strerror(err) << std::endl; error = DomainNameResolveError(); } else { if(result->havedata) { for(int i=0; result->data[i]; i++) { char address[100]; const char* addressStr = 0; if ((addressStr = inet_ntop(AF_INET, result->data[i], address, 100))) { SWIFT_LOG(debug) << "IPv4 address: " << addressStr << std::endl; addresses.push_back(HostAddress(std::string(addressStr))); } else if ((addressStr = inet_ntop(AF_INET6, result->data[i], address, 100))) { SWIFT_LOG(debug) << "IPv6 address: " << addressStr << std::endl; addresses.push_back(HostAddress(std::string(addressStr))); } else { SWIFT_LOG(debug) << "inet_ntop() failed" << std::endl; error = DomainNameResolveError(); } } } else { error = DomainNameResolveError(); } } ub_resolve_free(result); onResult(addresses, error); } private: std::string name; }; -UnboundDomainNameResolver::UnboundDomainNameResolver(boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop) : ioService(ioService), ubDescriptior(*ioService), eventLoop(eventLoop) { +UnboundDomainNameResolver::UnboundDomainNameResolver(IDNConverter* idnConverter, boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop) : idnConverter(idnConverter), ioService(ioService), ubDescriptior(*ioService), eventLoop(eventLoop) { ubContext = ub_ctx_create(); if(!ubContext) { SWIFT_LOG(debug) << "could not create unbound context" << std::endl; } eventOwner = boost::make_shared<EventOwner>(); ub_ctx_async(ubContext, true); int ret; /* read /etc/resolv.conf for DNS proxy settings (from DHCP) */ if( (ret=ub_ctx_resolvconf(ubContext, const_cast<char*>("/etc/resolv.conf"))) != 0) { - SWIFT_LOG(debug) << "error reading resolv.conf: " << ub_strerror(ret) << ". errno says: " << strerror(errno) << std::endl; + SWIFT_LOG(error) << "error reading resolv.conf: " << ub_strerror(ret) << ". errno says: " << strerror(errno) << std::endl; } /* read /etc/hosts for locally supplied host addresses */ if( (ret=ub_ctx_hosts(ubContext, const_cast<char*>("/etc/hosts"))) != 0) { - SWIFT_LOG(debug) << "error reading hosts: " << ub_strerror(ret) << ". errno says: " << strerror(errno) << std::endl; + SWIFT_LOG(error) << "error reading hosts: " << ub_strerror(ret) << ". errno says: " << strerror(errno) << std::endl; } ubDescriptior.assign(ub_fd(ubContext)); ubDescriptior.async_read_some(boost::asio::null_buffers(), boost::bind(&UnboundDomainNameResolver::handleUBSocketReadable, this, boost::asio::placeholders::error)); } UnboundDomainNameResolver::~UnboundDomainNameResolver() { eventLoop->removeEventsFromOwner(eventOwner); if (ubContext) { ub_ctx_delete(ubContext); } } void UnboundDomainNameResolver::unbound_callback(boost::shared_ptr<UnboundQuery> query, int err, ub_result* result) { query->handleResult(err, result); } void UnboundDomainNameResolver::unbound_callback_wrapper(void* data, int err, ub_result* result) { UnboundWrapperHelper* helper = static_cast<UnboundWrapperHelper*>(data); UnboundDomainNameResolver* resolver = helper->resolver; resolver->unbound_callback(helper->query, err, result); delete helper; } void UnboundDomainNameResolver::handleUBSocketReadable(boost::system::error_code) { eventLoop->postEvent(boost::bind(&UnboundDomainNameResolver::processData, this), eventOwner); ubDescriptior.async_read_some(boost::asio::null_buffers(), boost::bind(&UnboundDomainNameResolver::handleUBSocketReadable, this, boost::asio::placeholders::error)); } void UnboundDomainNameResolver::processData() { if (ub_poll(ubContext)) { int ret = ub_process(ubContext); if(ret != 0) { SWIFT_LOG(debug) << "resolve error: " << ub_strerror(ret) << std::endl; } } } -boost::shared_ptr<DomainNameServiceQuery> UnboundDomainNameResolver::createServiceQuery(const std::string& name) { - return boost::make_shared<UnboundDomainNameServiceQuery>(this, ubContext, name); +boost::shared_ptr<DomainNameServiceQuery> UnboundDomainNameResolver::createServiceQuery(const std::string& serviceLookupPrefix, const std::string& domain) { + boost::optional<std::string> encodedDomain = idnConverter->getIDNAEncoded(domain); + std::string result; + if (encodedDomain) { + result = serviceLookupPrefix + *encodedDomain; + } + return boost::make_shared<UnboundDomainNameServiceQuery>(this, ubContext, result); } boost::shared_ptr<DomainNameAddressQuery> UnboundDomainNameResolver::createAddressQuery(const std::string& name) { - return boost::make_shared<UnboundDomainNameAddressQuery>(this, ubContext, name); + return boost::make_shared<UnboundDomainNameAddressQuery>(this, ubContext, idnConverter->getIDNAEncoded(name).get_value_or("")); } } |