summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcp/option_space.cc
blob: 3be318bfe753949538ac8d536a3d0309a3ff1503 (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
// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
//
// 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/.

#include <config.h>

#include <dhcp/option_space.h>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>

namespace isc {
namespace dhcp {

OptionSpace::OptionSpace(const std::string& name, const bool vendor_space)
    : name_(name), vendor_space_(vendor_space) {
    //  Check that provided option space name is valid.
    if (!validateName(name_)) {
        isc_throw(InvalidOptionSpace, "Invalid option space name "
                  << name_);
    }
}

bool
OptionSpace::validateName(const std::string& name) {

    using namespace boost::algorithm;

    // Allowed characters are: lower or upper case letters, digits,
    // underscores and hyphens. Empty option spaces are not allowed.
    if (all(name, boost::is_from_range('a', 'z') ||
            boost::is_from_range('A', 'Z') ||
            boost::is_digit() ||
            boost::is_any_of(std::string("-_"))) &&
        !name.empty() &&
        // Hyphens and underscores are not allowed at the beginning
        // and at the end of the option space name.
        !all(find_head(name, 1), boost::is_any_of(std::string("-_"))) &&
        !all(find_tail(name, 1), boost::is_any_of(std::string("-_")))) {
        return (true);

    }
    return (false);
}

OptionSpace6::OptionSpace6(const std::string& name)
    : OptionSpace(name),
      enterprise_number_(0) {
}

OptionSpace6::OptionSpace6(const std::string& name,
                           const uint32_t enterprise_number)
    : OptionSpace(name, true),
      enterprise_number_(enterprise_number) {
}

void
OptionSpace6::setVendorSpace(const uint32_t enterprise_number) {
    enterprise_number_ = enterprise_number;
    OptionSpace::setVendorSpace();
}

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