host_reservation_parser.cc 10.2 KB
Newer Older
1
// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6

7
#include <config.h>
8
#include <asiolink/io_address.h>
9
#include <dhcpsrv/cfgmgr.h>
10
#include <dhcpsrv/parsers/dhcp_parsers.h>
11
#include <dhcpsrv/parsers/host_reservation_parser.h>
12
#include <boost/foreach.hpp>
13
#include <boost/lexical_cast.hpp>
14
#include <sys/socket.h>
15 16 17
#include <string>

using namespace isc::asiolink;
18
using namespace isc::data;
19

20 21 22 23 24 25 26 27 28
namespace {

/// @brief Returns set of the supported parameters for DHCPv4.
///
/// This function returns the set of supported parameters for
/// host reservation in DHCPv4.
const std::set<std::string>& getSupportedParams4() {
    static std::set<std::string> params_set;
    if (params_set.empty()) {
29
        const char* params[] = {
30 31
            "duid", "hw-address", "hostname", "ip-address",
            "option-data", NULL
32
        };
33
        for (int i = 0; params[i] != NULL; ++i) {
34 35 36 37 38 39 40 41 42 43 44 45 46
            params_set.insert(std::string(params[i]));
        }
    }
    return (params_set);
}

/// @brief Returns set of the supported parameters for DHCPv4.
///
/// This function returns the set of supported parameters for
/// host reservation in DHCPv6.
const std::set<std::string>& getSupportedParams6() {
    static std::set<std::string> params_set;
    if (params_set.empty()) {
47
        const char* params[] = {
48 49
            "duid", "hw-address", "hostname", "ip-addresses", "prefixes",
            "option-data", NULL
50
        };
51
        for (int i = 0; params[i] != NULL; ++i) {
52 53 54 55 56 57 58 59
            params_set.insert(std::string(params[i]));
        }
    }
    return (params_set);
}

}

60 61 62
namespace isc {
namespace dhcp {

63 64
HostReservationParser::HostReservationParser(const SubnetID& subnet_id)
    : DhcpConfigParser(), subnet_id_(subnet_id) {
65 66 67 68 69 70 71 72
}

void
HostReservationParser::build(isc::data::ConstElementPtr reservation_data) {
    std::string identifier;
    std::string identifier_name;
    std::string hostname;

73 74
    // Gather those parameters that are common for both IPv4 and IPv6
    // reservations.
75 76
    BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) {
        try {
77 78 79 80 81 82
            // Check if we support this parameter.
            if (!isSupportedParameter(element.first)) {
                isc_throw(DhcpConfigError, "unsupported configuration"
                          " parameter '" << element.first << "'");
            }

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            if (element.first == "hw-address" || element.first == "duid") {
                if (!identifier_name.empty()) {
                    isc_throw(DhcpConfigError, "the 'hw-address' and 'duid'"
                              " parameters are mutually exclusive");
                }
                identifier = element.second->stringValue();
                identifier_name = element.first;

            } else if (element.first == "hostname") {
                hostname = element.second->stringValue();

            }
        } catch (const std::exception& ex) {
            // Append line number where the error occurred.
            isc_throw(DhcpConfigError, ex.what() << " ("
                      << element.second->getPosition() << ")");
        }
    }

102 103 104
    try {
        // hw-address or duid is a must.
        if (identifier_name.empty()) {
105
            isc_throw(DhcpConfigError, "'hw-address' or 'duid' is a required"
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
                      " parameter for host reservation");
        }

        // Create a host object from the basic parameters we already parsed.
        host_.reset(new Host(identifier, identifier_name, SubnetID(0),
                             SubnetID(0), IOAddress("0.0.0.0"), hostname));

    } catch (const std::exception& ex) {
        // Append line number where the error occurred.
        isc_throw(DhcpConfigError, ex.what() << " ("
                  << reservation_data->getPosition() << ")");
    }
}

void
HostReservationParser::addHost(isc::data::ConstElementPtr reservation_data) {
    try {
        CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host_);

    } catch (const std::exception& ex) {
        // Append line number to the exception string.
        isc_throw(DhcpConfigError, ex.what() << " ("
                  << reservation_data->getPosition() << ")");
    }
}

HostReservationParser4::HostReservationParser4(const SubnetID& subnet_id)
    : HostReservationParser(subnet_id) {
}

void
HostReservationParser4::build(isc::data::ConstElementPtr reservation_data) {
    HostReservationParser::build(reservation_data);

    host_->setIPv4SubnetID(subnet_id_);

    BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) {
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        // For 'option-data' element we will use another parser which
        // already returns errors with position appended, so don't
        // surround it with try-catch.
        if (element.first == "option-data") {
            CfgOptionPtr cfg_option = host_->getCfgOption4();
            OptionDataListParser parser(element.first, cfg_option, AF_INET);
            parser.build(element.second);

       // Everything else should be surrounded with try-catch to append
       // position.
        } else {
            try {
                if (element.first == "ip-address") {
                    host_->setIPv4Reservation(IOAddress(element.second->
                                                        stringValue()));
                }
            } catch (const std::exception& ex) {
                // Append line number where the error occurred.
                isc_throw(DhcpConfigError, ex.what() << " ("
                          << reservation_data->getPosition() << ")");
163 164 165 166 167 168 169
            }
        }
    }

    addHost(reservation_data);
}

170 171 172 173 174
bool
HostReservationParser4::isSupportedParameter(const std::string& param_name) const {
    return (getSupportedParams4().count(param_name) > 0);
}

175 176 177 178 179 180 181 182 183 184 185
HostReservationParser6::HostReservationParser6(const SubnetID& subnet_id)
    : HostReservationParser(subnet_id) {
}

void
HostReservationParser6::build(isc::data::ConstElementPtr reservation_data) {
    HostReservationParser::build(reservation_data);

    host_->setIPv6SubnetID(subnet_id_);

    BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) {
186 187 188 189 190 191 192 193 194 195
        // Parse option values. Note that the configuration option parser
        // returns errors with position information appended, so there is no
        // need to surround it with try-clause (and rethrow with position
        // appended).
        if (element.first == "option-data") {
            CfgOptionPtr cfg_option = host_->getCfgOption6();
            OptionDataListParser parser(element.first, cfg_option, AF_INET6);
            parser.build(element.second);

        } else if (element.first == "ip-addresses" || element.first == "prefixes") {
196 197 198 199 200
            BOOST_FOREACH(ConstElementPtr prefix_element,
                          element.second->listValue()) {
                try {
                    // For the IPv6 address the prefix length is 128 and the
                    // value specified in the list is a reserved address.
201
                    IPv6Resrv::Type resrv_type = IPv6Resrv::TYPE_NA;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
                    std::string prefix = prefix_element->stringValue();
                    uint8_t prefix_len = 128;

                    // If we're dealing with prefixes, instead of addresses,
                    // we will have to extract the prefix length from the value
                    // specified in the following format: 2001:db8:2000::/64.
                    if (element.first == "prefixes") {
                        // The slash is mandatory for prefixes. If there is no
                        // slash, return an error.
                        size_t len_pos  = prefix.find('/');
                        if (len_pos == std::string::npos) {
                            isc_throw(DhcpConfigError, "prefix reservation"
                                      " requires prefix length be specified"
                                      " in '" << prefix << "'");

                        // If there is nothing after the slash, we should also
                        // report an error.
                        } else if (len_pos >= prefix.length() - 1) {
                            isc_throw(DhcpConfigError, "prefix '" <<  prefix
                                      << "' requires length after '/'");

                        }

                        // Convert the prefix length from the string to the
                        // number. Note, that we don't use the uint8_t type
                        // as the lexical cast would expect a chracter, e.g.
                        // 'a', instead of prefix length, e.g. '64'.
                        try {
                            prefix_len = boost::lexical_cast<
                                unsigned int>(prefix.substr(len_pos + 1));

                        } catch (const boost::bad_lexical_cast&) {
                            isc_throw(DhcpConfigError, "prefix length value '"
                                      << prefix.substr(len_pos + 1)
                                      << "' is invalid");
                        }

                        // Remove the  slash character and the prefix length
                        // from the parsed value.
                        prefix.erase(len_pos);
242 243 244

                        // Finally, set the reservation type.
                        resrv_type = IPv6Resrv::TYPE_PD;
245 246 247
                    }

                    // Create a reservation for an address or prefix.
248 249
                    host_->addReservation(IPv6Resrv(resrv_type,
                                                    IOAddress(prefix),
250 251 252 253 254 255 256 257 258 259 260 261 262
                                                    prefix_len));

                } catch (const std::exception& ex) {
                    // Append line number where the error occurred.
                    isc_throw(DhcpConfigError, ex.what() << " ("
                              << prefix_element->getPosition() << ")");
                }
            }
        }
    }

    // This may fail, but the addHost function will handle this on its own.
    addHost(reservation_data);
263 264
}

265 266 267 268 269
bool
HostReservationParser6::isSupportedParameter(const std::string& param_name) const {
    return (getSupportedParams6().count(param_name) > 0);
}

270 271
} // end of namespace isc::dhcp
} // end of namespace isc