cfg_duid.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2015 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/duid_factory.h>
  15. #include <dhcpsrv/cfg_duid.h>
  16. #include <util/encode/hex.h>
  17. #include <util/strutil.h>
  18. #include <iostream>
  19. using namespace isc;
  20. using namespace isc::util::encode;
  21. using namespace isc::util::str;
  22. namespace isc {
  23. namespace dhcp {
  24. CfgDUID::CfgDUID()
  25. : type_(DUID::DUID_LLT), identifier_(), htype_(0), time_(0),
  26. enterprise_id_(0), persist_(true) {
  27. }
  28. void
  29. CfgDUID::setIdentifier(const std::string& identifier_as_hex) {
  30. // Remove whitespaces.
  31. const std::string identifier = trim(identifier_as_hex);
  32. // Temporary result of parsing.
  33. std::vector<uint8_t> binary;
  34. if (!identifier.empty()) {
  35. try {
  36. // Decode identifier specified as a string of hexadecimal digits.
  37. decodeHex(identifier, binary);
  38. // All went ok, so let's replace identifier with a new value.
  39. identifier_.swap(binary);
  40. } catch (const std::exception& ex) {
  41. isc_throw(BadValue, "identifier specified in the DUID"
  42. " configuration '" << identifier
  43. << "' is not a valid string of hexadecimal digits");
  44. }
  45. } else {
  46. // If specified identifier is empty, clear our internal identifier.
  47. identifier_.clear();
  48. }
  49. }
  50. DuidPtr
  51. CfgDUID::create(const std::string& duid_file_path) const {
  52. // Use DUID factory to create a DUID instance.
  53. DUIDFactory factory(persist() ? duid_file_path : "");
  54. switch (getType()) {
  55. case DUID::DUID_LLT:
  56. factory.createLLT(getHType(), getTime(), getIdentifier());
  57. break;
  58. case DUID::DUID_EN:
  59. factory.createEN(getEnterpriseId(), getIdentifier());
  60. break;
  61. case DUID::DUID_LL:
  62. factory.createLL(getHType(), getIdentifier());
  63. break;
  64. default:
  65. // This should actually never happen.
  66. isc_throw(Unexpected, "invalid DUID type used " << getType()
  67. << " to create a new DUID");
  68. }
  69. // Return generated DUID.
  70. return (factory.get());
  71. }
  72. }
  73. }