libdhcp++.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (C) 2011-2012 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 <dhcp/option_definition.h>
  17. #include <dhcp/pkt6.h>
  18. #include <util/buffer.h>
  19. #include <iostream>
  20. namespace isc {
  21. namespace dhcp {
  22. class LibDHCP {
  23. public:
  24. /// Map of factory functions.
  25. typedef std::map<unsigned short, Option::Factory*> FactoryMap;
  26. /// @brief Return collection of option definitions.
  27. ///
  28. /// @param u universe of the options (V4 or V6).
  29. /// @return collection of option definitions.
  30. static const OptionDefContainer& getOptionDefs(Option::Universe u);
  31. /// @brief Factory function to create instance of option.
  32. ///
  33. /// Factory method creates instance of specified option. The option
  34. /// to be created has to have corresponding factory function
  35. /// registered with \ref LibDHCP::OptionFactoryRegister.
  36. ///
  37. /// @param u universe of the option (V4 or V6)
  38. /// @param type option-type
  39. /// @param buf option-buffer
  40. /// @throw isc::InvalidOperation if there is no factory function
  41. /// registered for specified option type.
  42. /// @return instance of option.
  43. static isc::dhcp::OptionPtr optionFactory(isc::dhcp::Option::Universe u,
  44. uint16_t type,
  45. const OptionBuffer& buf);
  46. /// Builds collection of options.
  47. ///
  48. /// Builds raw (on-wire) data for provided collection of options.
  49. ///
  50. /// @param buf output buffer (assembled options will be stored here)
  51. /// @param options collection of options to store to
  52. static void packOptions6(isc::util::OutputBuffer& buf,
  53. const isc::dhcp::Option::OptionCollection& options);
  54. /// @brief Stores options in a buffer.
  55. ///
  56. /// Stores all options defined in options containers in a on-wire
  57. /// format in output buffer specified by buf.
  58. ///
  59. /// May throw different exceptions if option assembly fails. There
  60. /// may be different reasons (option too large, option malformed,
  61. /// too many options etc.)
  62. ///
  63. /// @param buf output buffer (assembled options will be stored here)
  64. /// @param options collection of options to store to
  65. static void packOptions(isc::util::OutputBuffer& buf,
  66. const isc::dhcp::Option::OptionCollection& options);
  67. /// @brief Parses provided buffer as DHCPv4 options and creates Option objects.
  68. ///
  69. /// Parses provided buffer and stores created Option objects
  70. /// in options container.
  71. ///
  72. /// @param buf Buffer to be parsed.
  73. /// @param options Reference to option container. Options will be
  74. /// put here.
  75. static size_t unpackOptions4(const OptionBuffer& buf,
  76. isc::dhcp::Option::OptionCollection& options);
  77. /// @brief Parses provided buffer as DHCPv6 options and creates Option objects.
  78. ///
  79. /// Parses provided buffer and stores created Option objects
  80. /// in options container.
  81. ///
  82. /// @param buf Buffer to be parsed.
  83. /// @param options Reference to option container. Options will be
  84. /// put here.
  85. static size_t unpackOptions6(const OptionBuffer& buf,
  86. isc::dhcp::Option::OptionCollection& options);
  87. /// Registers factory method that produces options of specific option types.
  88. ///
  89. /// @exception BadValue if provided type is already registered, has too large
  90. /// value or invalid universe is specified
  91. ///
  92. /// @param u universe of the option (V4 or V6)
  93. /// @param type option-type
  94. /// @param factory function pointer
  95. static void OptionFactoryRegister(Option::Universe u,
  96. uint16_t type,
  97. Option::Factory * factory);
  98. /// Initialize standard DHCP options (V4 or V6).
  99. ///
  100. /// The method creates option definitions for all options
  101. /// (DHCPv4 or DHCPv6 depending on universe specified).
  102. /// Currently DHCPv4 option definitions initialization is not
  103. /// implemented thus this function will throw isc::NotImplemented
  104. /// if V4 universe is specified.
  105. ///
  106. /// @param u universe
  107. /// @throw isc::Unexpected if internal error occured during option
  108. /// definitions creation.
  109. /// @throw std::bad_alloc if system went out of memory.
  110. /// @throw isc::NotImplemented when V4 universe specified.
  111. static void initStdOptionDefs(Option::Universe u);
  112. private:
  113. /// Initialize standard DHCPv4 option definitions.
  114. ///
  115. /// The method creates option definitions for all DHCPv4 options.
  116. /// Currently this function is not implemented.
  117. ///
  118. /// @todo implemend this function.
  119. ///
  120. /// @throw isc::NotImplemeneted
  121. static void initStdOptionDefs4();
  122. /// Initialize standard DHCPv6 option definitions.
  123. ///
  124. /// The method creates option definitions for all DHCPv6 options.
  125. ///
  126. /// @throw isc::Unexpected if internal error occured during option
  127. /// definitions creation.
  128. /// @throw std::bad_alloc if system went out of memory.
  129. static void initStdOptionDefs6();
  130. /// pointers to factories that produce DHCPv6 options
  131. static FactoryMap v4factories_;
  132. /// pointers to factories that produce DHCPv6 options
  133. static FactoryMap v6factories_;
  134. /// Container with DHCPv4 option definitions.
  135. static OptionDefContainer v4option_defs_;
  136. /// Container with DHCPv6 option definitions.
  137. static OptionDefContainer v6option_defs_;
  138. };
  139. }
  140. }
  141. #endif