1 // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <dhcp/option_space.h>
10 #include <boost/algorithm/string/classification.hpp>
11 #include <boost/algorithm/string/predicate.hpp>
12 
13 namespace isc {
14 namespace dhcp {
15 
OptionSpace(const std::string & name,const bool vendor_space)16 OptionSpace::OptionSpace(const std::string& name, const bool vendor_space)
17     : name_(name), vendor_space_(vendor_space) {
18     //  Check that provided option space name is valid.
19     if (!validateName(name_)) {
20         isc_throw(InvalidOptionSpace, "Invalid option space name "
21                   << name_);
22     }
23 }
24 
25 bool
validateName(const std::string & name)26 OptionSpace::validateName(const std::string& name) {
27 
28     using namespace boost::algorithm;
29 
30     // Allowed characters are: lower or upper case letters, digits,
31     // underscores and hyphens. Empty option spaces are not allowed.
32     if (all(name, boost::is_from_range('a', 'z') ||
33             boost::is_from_range('A', 'Z') ||
34             boost::is_digit() ||
35             boost::is_any_of(std::string("-_"))) &&
36         !name.empty() &&
37         // Hyphens and underscores are not allowed at the beginning
38         // and at the end of the option space name.
39         !all(find_head(name, 1), boost::is_any_of(std::string("-_"))) &&
40         !all(find_tail(name, 1), boost::is_any_of(std::string("-_")))) {
41         return (true);
42 
43     }
44     return (false);
45 }
46 
OptionSpace6(const std::string & name)47 OptionSpace6::OptionSpace6(const std::string& name)
48     : OptionSpace(name),
49       enterprise_number_(0) {
50 }
51 
OptionSpace6(const std::string & name,const uint32_t enterprise_number)52 OptionSpace6::OptionSpace6(const std::string& name,
53                            const uint32_t enterprise_number)
54     : OptionSpace(name, true),
55       enterprise_number_(enterprise_number) {
56 }
57 
58 void
setVendorSpace(const uint32_t enterprise_number)59 OptionSpace6::setVendorSpace(const uint32_t enterprise_number) {
60     enterprise_number_ = enterprise_number;
61     OptionSpace::setVendorSpace();
62 }
63 
64 } // end of isc::dhcp namespace
65 } // end of isc namespace
66