libdhcp++.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright (C) 2011-2014 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. #include <string>
  21. namespace isc {
  22. namespace dhcp {
  23. class LibDHCP {
  24. public:
  25. /// Map of factory functions.
  26. typedef std::map<unsigned short, Option::Factory*> FactoryMap;
  27. /// @brief Return collection of option definitions.
  28. ///
  29. /// Method returns the collection of DHCP standard DHCP
  30. /// option definitions.
  31. /// @todo DHCPv4 option definitions are not implemented. For now
  32. /// this function will throw isc::NotImplemented in case of attempt
  33. /// to get option definitions for V4 universe.
  34. ///
  35. /// @param u universe of the options (V4 or V6).
  36. ///
  37. /// @return collection of option definitions.
  38. static const OptionDefContainer& getOptionDefs(const Option::Universe u);
  39. /// @brief Return the first option definition matching a
  40. /// particular option code.
  41. ///
  42. /// @param u universe (V4 or V6)
  43. /// @param code option code.
  44. ///
  45. /// @return reference to an option definition being requested
  46. /// or NULL pointer if option definition has not been found.
  47. static OptionDefinitionPtr getOptionDef(const Option::Universe u,
  48. const uint16_t code);
  49. /// @brief Return the definition of option having a specified name.
  50. ///
  51. /// @param u universe (v4 or V6)
  52. /// @param name Option name.
  53. ///
  54. /// @return Pointer to the option definition or NULL pointer if option
  55. /// definition has not been found.
  56. static OptionDefinitionPtr getOptionDef(const Option::Universe u,
  57. const std::string& name);
  58. /// @brief Returns vendor option definition for a given vendor-id and code
  59. ///
  60. /// @param u universe (V4 or V6)
  61. /// @param vendor_id enterprise-id for a given vendor
  62. /// @param code option code
  63. /// @return reference to an option definition being requested
  64. /// or NULL pointer if option definition has not been found.
  65. static OptionDefinitionPtr getVendorOptionDef(const Option::Universe u,
  66. const uint32_t vendor_id,
  67. const uint16_t code);
  68. /// @brief Returns vendor option definition for a given vendor-id and
  69. /// option name.
  70. ///
  71. /// @param u Universe (V4 or V6)
  72. /// @param vendor_id Enterprise-id for a given vendor
  73. /// @param name Option name.
  74. ///
  75. /// @return A pointer to an option definition or NULL pointer if
  76. /// no option definition found.
  77. static OptionDefinitionPtr getVendorOptionDef(const Option::Universe u,
  78. const uint32_t vendor_id,
  79. const std::string& name);
  80. /// @brief Check if the specified option is a standard option.
  81. ///
  82. /// @param u universe (V4 or V6)
  83. /// @param code option code.
  84. ///
  85. /// @return true if the specified option is a standard option.
  86. /// @todo We already create option definitions for the subset if
  87. /// standard options. We are aiming that this function checks
  88. /// the presence of the standard option definition and if it finds
  89. /// it, then the true value is returned. However, at this point
  90. /// this is not doable because some of the definitions (for less
  91. /// important options) are not created yet.
  92. static bool isStandardOption(const Option::Universe u,
  93. const uint16_t code);
  94. /// @brief Factory function to create instance of option.
  95. ///
  96. /// Factory method creates instance of specified option. The option
  97. /// to be created has to have corresponding factory function
  98. /// registered with \ref LibDHCP::OptionFactoryRegister.
  99. ///
  100. /// @param u universe of the option (V4 or V6)
  101. /// @param type option-type
  102. /// @param buf option-buffer
  103. ///
  104. /// @return instance of option.
  105. ///
  106. /// @throw isc::InvalidOperation if there is no factory function registered
  107. /// for the specified option type.
  108. static isc::dhcp::OptionPtr optionFactory(isc::dhcp::Option::Universe u,
  109. uint16_t type,
  110. const OptionBuffer& buf);
  111. /// @brief Stores options in a buffer.
  112. ///
  113. /// Stores all options defined in options containers in a on-wire
  114. /// format in output buffer specified by buf.
  115. ///
  116. /// May throw different exceptions if option assembly fails. There
  117. /// may be different reasons (option too large, option malformed,
  118. /// too many options etc.)
  119. ///
  120. /// @param buf output buffer (assembled options will be stored here)
  121. /// @param options collection of options to store to
  122. static void packOptions(isc::util::OutputBuffer& buf,
  123. const isc::dhcp::OptionCollection& options);
  124. /// @brief Parses provided buffer as DHCPv4 options and creates Option objects.
  125. ///
  126. /// Parses provided buffer and stores created Option objects
  127. /// in options container.
  128. ///
  129. /// @param buf Buffer to be parsed.
  130. /// @param option_space A name of the option space which holds definitions
  131. /// of to be used to parse options in the packets.
  132. /// @param options Reference to option container. Options will be
  133. /// put here.
  134. /// @return offset to the first byte after the last successfully parsed option
  135. static size_t unpackOptions4(const OptionBuffer& buf,
  136. const std::string& option_space,
  137. isc::dhcp::OptionCollection& options);
  138. /// @brief Parses provided buffer as DHCPv6 options and creates Option objects.
  139. ///
  140. /// Parses provided buffer and stores created Option objects in options
  141. /// container. The last two parameters are optional and are used in
  142. /// relay parsing. If they are specified, relay-msg option is not created,
  143. /// but rather those two parameters are specified to point out where
  144. /// the relay-msg option resides and what is its length. This is perfromance
  145. /// optimization that avoids unnecessary copying of potentially large
  146. /// relay-msg option. It is not used for anything, except in the next
  147. /// iteration its content will be treated as buffer to be parsed.
  148. ///
  149. /// @param buf Buffer to be parsed.
  150. /// @param option_space A name of the option space which holds definitions
  151. /// of to be used to parse options in the packets.
  152. /// @param options Reference to option container. Options will be
  153. /// put here.
  154. /// @param relay_msg_offset reference to a size_t structure. If specified,
  155. /// offset to beginning of relay_msg option will be stored in it.
  156. /// @param relay_msg_len reference to a size_t structure. If specified,
  157. /// length of the relay_msg option will be stored in it.
  158. /// @return offset to the first byte after the last successfully parsed option
  159. static size_t unpackOptions6(const OptionBuffer& buf,
  160. const std::string& option_space,
  161. isc::dhcp::OptionCollection& options,
  162. size_t* relay_msg_offset = 0,
  163. size_t* relay_msg_len = 0);
  164. /// Registers factory method that produces options of specific option types.
  165. ///
  166. /// @throw isc::BadValue if provided the type is already registered, has
  167. /// too large a value or an invalid universe is specified.
  168. ///
  169. /// @param u universe of the option (V4 or V6)
  170. /// @param type option-type
  171. /// @param factory function pointer
  172. static void OptionFactoryRegister(Option::Universe u,
  173. uint16_t type,
  174. Option::Factory * factory);
  175. /// @brief Returns v4 option definitions for a given vendor
  176. ///
  177. /// @param vendor_id enterprise-id of a given vendor
  178. /// @return a container for a given vendor (or NULL if not option
  179. /// definitions are defined)
  180. static const OptionDefContainer*
  181. getVendorOption4Defs(const uint32_t vendor_id);
  182. /// @brief Returns v6 option definitions for a given vendor
  183. ///
  184. /// @param vendor_id enterprise-id of a given vendor
  185. /// @return a container for a given vendor (or NULL if not option
  186. /// definitions are defined)
  187. static const OptionDefContainer*
  188. getVendorOption6Defs(const uint32_t vendor_id);
  189. /// @brief Parses provided buffer as DHCPv6 vendor options and creates
  190. /// Option objects.
  191. ///
  192. /// Parses provided buffer and stores created Option objects
  193. /// in options container.
  194. ///
  195. /// @param vendor_id enterprise-id of the vendor
  196. /// @param buf Buffer to be parsed.
  197. /// @param options Reference to option container. Options will be
  198. /// put here.
  199. /// @return offset to the first byte after the last successfully parsed option
  200. static size_t unpackVendorOptions6(const uint32_t vendor_id,
  201. const OptionBuffer& buf,
  202. isc::dhcp::OptionCollection& options);
  203. /// @brief Parses provided buffer as DHCPv4 vendor options and creates
  204. /// Option objects.
  205. ///
  206. /// Parses provided buffer and stores created Option objects
  207. /// in options container.
  208. ///
  209. /// @param vendor_id enterprise-id of the vendor
  210. /// @param buf Buffer to be parsed.
  211. /// @param options Reference to option container. Options will be
  212. /// put here.
  213. /// @return offset to the first byte after the last successfully parsed option
  214. static size_t unpackVendorOptions4(const uint32_t vendor_id, const OptionBuffer& buf,
  215. isc::dhcp::OptionCollection& options);
  216. private:
  217. /// Initialize standard DHCPv4 option definitions.
  218. ///
  219. /// The method creates option definitions for all DHCPv4 options.
  220. /// Currently this function is not implemented.
  221. ///
  222. /// @throw std::bad alloc if system went out of memory.
  223. /// @throw MalformedOptionDefinition if any of the definitions
  224. /// are incorrect. This is programming error.
  225. static void initStdOptionDefs4();
  226. /// Initialize standard DHCPv6 option definitions.
  227. ///
  228. /// The method creates option definitions for all DHCPv6 options.
  229. ///
  230. /// @throw std::bad_alloc if system went out of memory.
  231. /// @throw MalformedOptionDefinition if any of the definitions
  232. /// is incorrect. This is a programming error.
  233. static void initStdOptionDefs6();
  234. static void initVendorOptsDocsis4();
  235. static void initVendorOptsDocsis6();
  236. /// pointers to factories that produce DHCPv6 options
  237. static FactoryMap v4factories_;
  238. /// pointers to factories that produce DHCPv6 options
  239. static FactoryMap v6factories_;
  240. /// Container with DHCPv4 option definitions.
  241. static OptionDefContainer v4option_defs_;
  242. /// Container with DHCPv6 option definitions.
  243. static OptionDefContainer v6option_defs_;
  244. /// Container for v4 vendor option definitions
  245. static VendorOptionDefContainers vendor4_defs_;
  246. /// Container for v6 vendor option definitions
  247. static VendorOptionDefContainers vendor6_defs_;
  248. };
  249. }
  250. }
  251. #endif // LIBDHCP_H