libdhcp++.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (C) 2011-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. #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. /// Method returns the collection of DHCP standard DHCP
  29. /// option definitions.
  30. /// @todo DHCPv4 option definitions are not implemented. For now
  31. /// this function will throw isc::NotImplemented in case of attempt
  32. /// to get option definitions for V4 universe.
  33. ///
  34. /// @param u universe of the options (V4 or V6).
  35. ///
  36. /// @return collection of option definitions.
  37. static const OptionDefContainer& getOptionDefs(const Option::Universe u);
  38. /// @brief Return the first option definition matching a
  39. /// particular option code.
  40. ///
  41. /// @param u universe (V4 or V6)
  42. /// @param code option code.
  43. ///
  44. /// @return reference to an option definition being requested
  45. /// or NULL pointer if option definition has not been found.
  46. static OptionDefinitionPtr getOptionDef(const Option::Universe u,
  47. const uint16_t code);
  48. /// @brief Check if the specified option is a standard option.
  49. ///
  50. /// @param u universe (V4 or V6)
  51. /// @param code option code.
  52. ///
  53. /// @return true if the specified option is a standard option.
  54. /// @todo We already create option definitions for the subset if
  55. /// standard options. We are aiming that this function checks
  56. /// the presence of the standard option definition and if it finds
  57. /// it, then the true value is returned. However, at this point
  58. /// this is not doable because some of the definitions (for less
  59. /// important options) are not created yet.
  60. static bool isStandardOption(const Option::Universe u,
  61. const uint16_t code);
  62. /// @brief Factory function to create instance of option.
  63. ///
  64. /// Factory method creates instance of specified option. The option
  65. /// to be created has to have corresponding factory function
  66. /// registered with \ref LibDHCP::OptionFactoryRegister.
  67. ///
  68. /// @param u universe of the option (V4 or V6)
  69. /// @param type option-type
  70. /// @param buf option-buffer
  71. ///
  72. /// @return instance of option.
  73. ///
  74. /// @throw isc::InvalidOperation if there is no factory function registered
  75. /// for the specified option type.
  76. static isc::dhcp::OptionPtr optionFactory(isc::dhcp::Option::Universe u,
  77. uint16_t type,
  78. const OptionBuffer& buf);
  79. /// @brief Stores options in a buffer.
  80. ///
  81. /// Stores all options defined in options containers in a on-wire
  82. /// format in output buffer specified by buf.
  83. ///
  84. /// May throw different exceptions if option assembly fails. There
  85. /// may be different reasons (option too large, option malformed,
  86. /// too many options etc.)
  87. ///
  88. /// @param buf output buffer (assembled options will be stored here)
  89. /// @param options collection of options to store to
  90. static void packOptions(isc::util::OutputBuffer& buf,
  91. const isc::dhcp::OptionCollection& options);
  92. /// @brief Parses provided buffer as DHCPv4 options and creates Option objects.
  93. ///
  94. /// Parses provided buffer and stores created Option objects
  95. /// in options container.
  96. ///
  97. /// @param buf Buffer to be parsed.
  98. /// @param option_space A name of the option space which holds definitions
  99. /// of to be used to parse options in the packets.
  100. /// @param options Reference to option container. Options will be
  101. /// put here.
  102. static size_t unpackOptions4(const OptionBuffer& buf,
  103. const std::string& option_space,
  104. isc::dhcp::OptionCollection& options);
  105. /// @brief Parses provided buffer as DHCPv6 options and creates Option objects.
  106. ///
  107. /// Parses provided buffer and stores created Option objects in options
  108. /// container. The last two parameters are optional and are used in
  109. /// relay parsing. If they are specified, relay-msg option is not created,
  110. /// but rather those two parameters are specified to point out where
  111. /// the relay-msg option resides and what is its length. This is perfromance
  112. /// optimization that avoids unnecessary copying of potentially large
  113. /// relay-msg option. It is not used for anything, except in the next
  114. /// iteration its content will be treated as buffer to be parsed.
  115. ///
  116. /// @param buf Buffer to be parsed.
  117. /// @param option_space A name of the option space which holds definitions
  118. /// of to be used to parse options in the packets.
  119. /// @param options Reference to option container. Options will be
  120. /// put here.
  121. /// @param relay_msg_offset reference to a size_t structure. If specified,
  122. /// offset to beginning of relay_msg option will be stored in it.
  123. /// @param relay_msg_len reference to a size_t structure. If specified,
  124. /// length of the relay_msg option will be stored in it.
  125. /// @return offset to the first byte after last parsed option
  126. static size_t unpackOptions6(const OptionBuffer& buf,
  127. const std::string& option_space,
  128. isc::dhcp::OptionCollection& options,
  129. size_t* relay_msg_offset = 0,
  130. size_t* relay_msg_len = 0);
  131. /// Registers factory method that produces options of specific option types.
  132. ///
  133. /// @throw isc::BadValue if provided the type is already registered, has
  134. /// too large a value or an invalid universe is specified.
  135. ///
  136. /// @param u universe of the option (V4 or V6)
  137. /// @param type option-type
  138. /// @param factory function pointer
  139. static void OptionFactoryRegister(Option::Universe u,
  140. uint16_t type,
  141. Option::Factory * factory);
  142. private:
  143. /// Initialize standard DHCPv4 option definitions.
  144. ///
  145. /// The method creates option definitions for all DHCPv4 options.
  146. /// Currently this function is not implemented.
  147. ///
  148. /// @throw std::bad alloc if system went out of memory.
  149. /// @throw MalformedOptionDefinition if any of the definitions
  150. /// are incorrect. This is programming error.
  151. static void initStdOptionDefs4();
  152. /// Initialize standard DHCPv6 option definitions.
  153. ///
  154. /// The method creates option definitions for all DHCPv6 options.
  155. ///
  156. /// @throw std::bad_alloc if system went out of memory.
  157. /// @throw MalformedOptionDefinition if any of the definitions
  158. /// is incorrect. This is a programming error.
  159. static void initStdOptionDefs6();
  160. /// pointers to factories that produce DHCPv6 options
  161. static FactoryMap v4factories_;
  162. /// pointers to factories that produce DHCPv6 options
  163. static FactoryMap v6factories_;
  164. /// Container with DHCPv4 option definitions.
  165. static OptionDefContainer v4option_defs_;
  166. /// Container with DHCPv6 option definitions.
  167. static OptionDefContainer v6option_defs_;
  168. };
  169. }
  170. }
  171. #endif // LIBDHCP_H