d2_cfg_mgr.cc 6.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <d2/d2_log.h>
#include <d2/d2_cfg_mgr.h>
17
#include <util/encode/hex.h>
18

19 20
#include <boost/foreach.hpp>

21 22 23 24 25
namespace isc {
namespace d2 {

// *********************** D2CfgContext  *************************

26 27
D2CfgContext::D2CfgContext()
    : forward_mgr_(new DdnsDomainListMgr("forward_mgr")),
28 29
      reverse_mgr_(new DdnsDomainListMgr("reverse_mgr")),
      keys_(new TSIGKeyInfoMap()) {
30 31
}

32 33 34 35 36 37 38 39 40 41
D2CfgContext::D2CfgContext(const D2CfgContext& rhs) : DCfgContextBase(rhs) {
    if (rhs.forward_mgr_) {
        forward_mgr_.reset(new DdnsDomainListMgr(rhs.forward_mgr_->getName()));
        forward_mgr_->setDomains(rhs.forward_mgr_->getDomains());
    }

    if (rhs.reverse_mgr_) {
        reverse_mgr_.reset(new DdnsDomainListMgr(rhs.reverse_mgr_->getName()));
        reverse_mgr_->setDomains(rhs.reverse_mgr_->getDomains());
    }
42

43
    keys_ = rhs.keys_;
44 45 46 47 48 49 50
}

D2CfgContext::~D2CfgContext() {
}

// *********************** D2CfgMgr  *************************

51 52 53 54
const char* D2CfgMgr::IPV4_REV_ZONE_SUFFIX = "in-addr.arpa.";

const char* D2CfgMgr::IPV6_REV_ZONE_SUFFIX = "ip6.arpa.";

55
D2CfgMgr::D2CfgMgr() : DCfgMgrBase(DCfgContextBasePtr(new D2CfgContext())) {
56 57 58 59 60 61 62 63
    // TSIG keys need to parse before the Domains, so we can catch Domains
    // that specify undefined keys. Create the necessary parsing order now.
    addToParseOrder("interface");
    addToParseOrder("ip_address");
    addToParseOrder("port");
    addToParseOrder("tsig_keys");
    addToParseOrder("forward_ddns");
    addToParseOrder("reverse_ddns");
64 65 66 67 68
}

D2CfgMgr::~D2CfgMgr() {
}

69 70
bool
D2CfgMgr::matchForward(const std::string& fqdn, DdnsDomainPtr& domain) {
71
    if (fqdn.empty()) {
72
        // This is a programmatic error and should not happen.
73
        isc_throw(D2CfgError, "matchForward passed an empty fqdn");
74 75 76
    }

    // Fetch the forward manager from the D2 context.
77
    DdnsDomainListMgrPtr mgr = getD2CfgContext()->getForwardMgr();
78 79 80 81 82 83

    // Call the manager's match method and return the result.
    return (mgr->matchDomain(fqdn, domain));
}

bool
84 85 86
D2CfgMgr::matchReverse(const std::string& ip_address, DdnsDomainPtr& domain) {
    // Note, reverseIpAddress will throw if the ip_address is invalid.
    std::string reverse_address = reverseIpAddress(ip_address);
87 88

    // Fetch the reverse manager from the D2 context.
89
    DdnsDomainListMgrPtr mgr = getD2CfgContext()->getReverseMgr();
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    return (mgr->matchDomain(reverse_address, domain));
}

std::string
D2CfgMgr::reverseIpAddress(const std::string& address) {
    try {
        // Convert string address into an IOAddress and invoke the
        // appropriate reverse method.
        isc::asiolink::IOAddress ioaddr(address);
        if (ioaddr.getFamily() == AF_INET) {
            return (reverseV4Address(ioaddr));
        }

        return (reverseV6Address(ioaddr));

    } catch (const isc::Exception& ex) {
        isc_throw(D2CfgError, "D2CfgMgr cannot reverse address :"
                              << address << " : " << ex.what());
    }
}

std::string
D2CfgMgr::reverseV4Address(const isc::asiolink::IOAddress& ioaddr) {
    if (ioaddr.getFamily() != AF_INET) {
        isc_throw(D2CfgError, "D2CfgMgr address is not IPv4 address :"
                              << ioaddr.toText());
    }

    // Get the address in byte vector form.
    std::vector<uint8_t> bytes = ioaddr.toBytes();

    // Walk backwards through vector outputting each octet and a dot.
    std::ostringstream stream;
    for (int i = 3; i >= 0; i--) {
        stream << (unsigned int)(bytes[i]) << ".";
    }

    // Tack on the suffix and we're done.
    stream << IPV4_REV_ZONE_SUFFIX;
    return(stream.str());
}

std::string
D2CfgMgr::reverseV6Address(const isc::asiolink::IOAddress& ioaddr) {
    if (ioaddr.getFamily() != AF_INET6) {
        isc_throw(D2CfgError, "D2Cfg address is not IPv6 address :"
                              << ioaddr.toText());
    }

    // Turn the address into a string of digits.
    std::vector<uint8_t> bytes = ioaddr.toBytes();
    std::string digits;
    digits = isc::util::encode::encodeHex(bytes);

    // Walk backwards through string outputting each digits and a dot.
    std::ostringstream stream;
    std::string::const_reverse_iterator rit;
    for (rit = digits.rbegin(); rit != digits.rend(); ++rit) {
        stream << (char)(*rit) << ".";
    }

    // Tack on the suffix and we're done.
    stream << IPV6_REV_ZONE_SUFFIX;
    return(stream.str());
155 156 157
}


158
isc::dhcp::ParserPtr
159 160 161 162 163
D2CfgMgr::createConfigParser(const std::string& config_id) {
    // Get D2 specific context.
    D2CfgContextPtr context = getD2CfgContext();

    // Create parser instance based on element_id.
164
    isc::dhcp::DhcpConfigParser* parser = NULL;
165 166
    if ((config_id == "interface")  ||
        (config_id == "ip_address")) {
167
        parser = new isc::dhcp::StringParser(config_id,
168
                                             context->getStringStorage());
169
    } else if (config_id == "port") {
170
        parser = new isc::dhcp::Uint32Parser(config_id,
171
                                             context->getUint32Storage());
172 173
    } else if (config_id ==  "forward_ddns") {
        parser = new DdnsDomainListMgrParser("forward_mgr",
174 175
                                             context->getForwardMgr(),
                                             context->getKeys());
176 177
    } else if (config_id ==  "reverse_ddns") {
        parser = new DdnsDomainListMgrParser("reverse_mgr",
178 179 180 181
                                             context->getReverseMgr(),
                                             context->getKeys());
    } else if (config_id ==  "tsig_keys") {
        parser = new TSIGKeyInfoListParser("tsig_key_list", context->getKeys());
182 183 184 185 186 187 188
    } else {
        isc_throw(NotImplemented,
                  "parser error: D2CfgMgr parameter not supported: "
                  << config_id);
    }

    return (isc::dhcp::ParserPtr(parser));
189 190 191 192 193
}

}; // end of isc::dhcp namespace
}; // end of isc namespace