libdhcp++.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2011 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. #ifndef LIBDHCP_H_
  15. #define LIBDHCP_H_
  16. #include <iostream>
  17. #include <util/buffer.h>
  18. #include <dhcp/pkt6.h>
  19. namespace isc {
  20. namespace dhcp {
  21. class LibDHCP {
  22. public:
  23. /// Map of factory functions.
  24. typedef std::map<unsigned short, Option::Factory*> FactoryMap;
  25. /// @brief Factory function to create instance of option.
  26. ///
  27. /// Factory method creates instance of specified option. The option
  28. /// to be created has to have corresponding factory function
  29. /// registered with \ref LibDHCP::OptionFactoryRegister.
  30. ///
  31. /// @param u universe of the option (V4 or V6)
  32. /// @param type option-type
  33. /// @param buf option-buffer
  34. /// @throw isc::InvalidOperation if there is no factory function
  35. /// registered for specified option type.
  36. /// @return instance of option.
  37. static isc::dhcp::OptionPtr optionFactory(isc::dhcp::Option::Universe u,
  38. uint16_t type,
  39. const OptionBuffer& buf);
  40. /// Builds collection of options.
  41. ///
  42. /// Builds raw (on-wire) data for provided collection of options.
  43. ///
  44. /// @param buf output buffer (assembled options will be stored here)
  45. /// @param options collection of options to store to
  46. static void packOptions6(isc::util::OutputBuffer& buf,
  47. const isc::dhcp::Option::OptionCollection& options);
  48. /// @brief Stores options in a buffer.
  49. ///
  50. /// Stores all options defined in options containers in a on-wire
  51. /// format in output buffer specified by buf.
  52. ///
  53. /// May throw different exceptions if option assembly fails. There
  54. /// may be different reasons (option too large, option malformed,
  55. /// too many options etc.)
  56. ///
  57. /// @param buf output buffer (assembled options will be stored here)
  58. /// @param options collection of options to store to
  59. static void packOptions(isc::util::OutputBuffer& buf,
  60. const isc::dhcp::Option::OptionCollection& options);
  61. /// @brief Parses provided buffer as DHCPv4 options and creates Option objects.
  62. ///
  63. /// Parses provided buffer and stores created Option objects
  64. /// in options container.
  65. ///
  66. /// @param buf Buffer to be parsed.
  67. /// @param options Reference to option container. Options will be
  68. /// put here.
  69. static size_t unpackOptions4(const OptionBuffer& buf,
  70. isc::dhcp::Option::OptionCollection& options);
  71. /// @brief Parses provided buffer as DHCPv6 options and creates Option objects.
  72. ///
  73. /// Parses provided buffer and stores created Option objects
  74. /// in options container.
  75. ///
  76. /// @param buf Buffer to be parsed.
  77. /// @param options Reference to option container. Options will be
  78. /// put here.
  79. static size_t unpackOptions6(const OptionBuffer& buf,
  80. isc::dhcp::Option::OptionCollection& options);
  81. /// Registers factory method that produces options of specific option types.
  82. ///
  83. /// @exception BadValue if provided type is already registered, has too large
  84. /// value or invalid universe is specified
  85. ///
  86. /// @param u universe of the option (V4 or V6)
  87. /// @param type option-type
  88. /// @param factory function pointer
  89. static void OptionFactoryRegister(Option::Universe u,
  90. uint16_t type,
  91. Option::Factory * factory);
  92. protected:
  93. /// pointers to factories that produce DHCPv6 options
  94. static FactoryMap v4factories_;
  95. /// pointers to factories that produce DHCPv6 options
  96. static FactoryMap v6factories_;
  97. };
  98. }
  99. }
  100. #endif