option_space.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2012, 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dhcp/option_space.h>
  15. #include <boost/algorithm/string/classification.hpp>
  16. #include <boost/algorithm/string/predicate.hpp>
  17. namespace isc {
  18. namespace dhcp {
  19. OptionSpace::OptionSpace(const std::string& name, const bool vendor_space)
  20. : name_(name), vendor_space_(vendor_space) {
  21. // Check that provided option space name is valid.
  22. if (!validateName(name_)) {
  23. isc_throw(InvalidOptionSpace, "Invalid option space name "
  24. << name_);
  25. }
  26. }
  27. bool
  28. OptionSpace::validateName(const std::string& name) {
  29. using namespace boost::algorithm;
  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. return (false);
  44. }
  45. OptionSpace6::OptionSpace6(const std::string& name)
  46. : OptionSpace(name),
  47. enterprise_number_(0) {
  48. }
  49. OptionSpace6::OptionSpace6(const std::string& name,
  50. const uint32_t enterprise_number)
  51. : OptionSpace(name, true),
  52. enterprise_number_(enterprise_number) {
  53. }
  54. void
  55. OptionSpace6::setVendorSpace(const uint32_t enterprise_number) {
  56. enterprise_number_ = enterprise_number;
  57. OptionSpace::setVendorSpace();
  58. }
  59. } // end of isc::dhcp namespace
  60. } // end of isc namespace