option_space.cc 2.49 KB
Newer Older
1
// Copyright (C) 2012, 2013 Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// 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 <dhcpsrv/option_space.h>
16
17
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
18
19
20
21
22
23

namespace isc {
namespace dhcp {

OptionSpace::OptionSpace(const std::string& name, const bool vendor_space)
    : name_(name), vendor_space_(vendor_space) {
24
    //  Check that provided option space name is valid.
25
26
27
28
29
30
31
32
    if (!validateName(name_)) {
        isc_throw(InvalidOptionSpace, "Invalid option space name "
                  << name_);
    }
}

bool
OptionSpace::validateName(const std::string& name) {
33
34
35
36

    using namespace boost::algorithm;

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

49
50
    }
    return (false);
51
52
}

53
54
55
56
57
58
OptionSpace6::OptionSpace6(const std::string& name)
    : OptionSpace(name),
      enterprise_number_(0) {
}

OptionSpace6::OptionSpace6(const std::string& name,
59
                           const uint32_t enterprise_number)
60
    : OptionSpace(name, true),
61
      enterprise_number_(enterprise_number) {
62
63
64
}

void
65
66
OptionSpace6::setVendorSpace(const uint32_t enterprise_number) {
    enterprise_number_ = enterprise_number;
67
68
69
    OptionSpace::setVendorSpace();
}

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