summaryrefslogtreecommitdiffstats
blob: b8f4c7ac42c271382c4ce17a1fb92fbb131ed703 (plain)
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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Network/StaticDomainNameResolver.h>

#include <string>

#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>

#include <Swiften/EventLoop/EventOwner.h>
#include <Swiften/Network/DomainNameResolveError.h>

using namespace Swift;

namespace {
    struct ServiceQuery : public DomainNameServiceQuery, public std::enable_shared_from_this<ServiceQuery> {
        ServiceQuery(const std::string& service, Swift::StaticDomainNameResolver* resolver, EventLoop* eventLoop, std::shared_ptr<EventOwner> owner) : eventLoop(eventLoop), service(service), resolver(resolver), owner(owner) {}

        virtual void run() {
            if (!resolver->getIsResponsive()) {
                return;
            }
            std::vector<DomainNameServiceQuery::Result> 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<DomainNameServiceQuery::Result> results) {
            onResult(results);
        }

        EventLoop* eventLoop;
        std::string service;
        StaticDomainNameResolver* resolver;
        std::shared_ptr<EventOwner> owner;
    };

    struct AddressQuery : public DomainNameAddressQuery, public std::enable_shared_from_this<AddressQuery> {
        AddressQuery(const std::string& host, StaticDomainNameResolver* resolver, EventLoop* eventLoop, std::shared_ptr<EventOwner> owner) : eventLoop(eventLoop), host(host), resolver(resolver), owner(owner) {}

        virtual void run() {
            if (!resolver->getIsResponsive()) {
                return;
            }
            if (auto address = HostAddress::fromString(host)) {
                // IP Literals should resolve to themselves
                resolver->addAddress(host, *address);
            }
            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<DomainNameResolveError>()));
            }
            else {
                eventLoop->postEvent(boost::bind(&AddressQuery::emitOnResult, shared_from_this(), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), owner);
            }
        }

        void emitOnResult(std::vector<HostAddress> results, boost::optional<DomainNameResolveError> error) {
            onResult(results, error);
        }

        EventLoop* eventLoop;
        std::string host;
        StaticDomainNameResolver* resolver;
        std::shared_ptr<EventOwner> 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<std::string>(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<DomainNameServiceQuery> StaticDomainNameResolver::createServiceQuery(const std::string& serviceLookupPrefix, const std::string& domain) {
    return std::make_shared<ServiceQuery>(serviceLookupPrefix + domain, this, eventLoop, owner);
}

std::shared_ptr<DomainNameAddressQuery> StaticDomainNameResolver::createAddressQuery(const std::string& name) {
    return std::make_shared<AddressQuery>(name, this, eventLoop, owner);
}

}