libdhcp++.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 options Reference to option container. Options will be
  99. /// put here.
  100. static size_t unpackOptions4(const OptionBuffer& buf,
  101. isc::dhcp::OptionCollection& options);
  102. /// @brief Parses provided buffer as DHCPv6 options and creates Option objects.
  103. ///
  104. /// Parses provided buffer and stores created Option objects in options
  105. /// container. The last two parameters are optional and are used in
  106. /// relay parsing. If they are specified, relay-msg option is not created,
  107. /// but rather those two parameters are specified to point out where
  108. /// the relay-msg option resides and what is its length. This is perfromance
  109. /// optimization that avoids unnecessary copying of potentially large
  110. /// relay-msg option. It is not used for anything, except in the next
  111. /// iteration its content will be treated as buffer to be parsed.
  112. ///
  113. /// @param buf Buffer to be parsed.
  114. /// @param options Reference to option container. Options will be
  115. /// put here.
  116. /// @param relay_msg_offset reference to a size_t structure. If specified,
  117. /// offset to beginning of relay_msg option will be stored in it.
  118. /// @param relay_msg_len reference to a size_t structure. If specified,
  119. /// length of the relay_msg option will be stored in it.
  120. /// @return offset to the first byte after last parsed option
  121. static size_t unpackOptions6(const OptionBuffer& buf,
  122. isc::dhcp::OptionCollection& options,
  123. size_t* relay_msg_offset = 0,
  124. size_t* relay_msg_len = 0);
  125. /// Registers factory method that produces options of specific option types.
  126. ///
  127. /// @throw isc::BadValue if provided the type is already registered, has
  128. /// too large a value or an invalid universe is specified.
  129. ///
  130. /// @param u universe of the option (V4 or V6)
  131. /// @param type option-type
  132. /// @param factory function pointer
  133. static void OptionFactoryRegister(Option::Universe u,
  134. uint16_t type,
  135. Option::Factory * factory);
  136. private:
  137. /// Initialize standard DHCPv4 option definitions.
  138. ///
  139. /// The method creates option definitions for all DHCPv4 options.
  140. /// Currently this function is not implemented.
  141. ///
  142. /// @throw std::bad alloc if system went out of memory.
  143. /// @throw MalformedOptionDefinition if any of the definitions
  144. /// are incorrect. This is programming error.
  145. static void initStdOptionDefs4();
  146. /// Initialize standard DHCPv6 option definitions.
  147. ///
  148. /// The method creates option definitions for all DHCPv6 options.
  149. ///
  150. /// @throw std::bad_alloc if system went out of memory.
  151. /// @throw MalformedOptionDefinition if any of the definitions
  152. /// is incorrect. This is a programming error.
  153. static void initStdOptionDefs6();
  154. /// pointers to factories that produce DHCPv6 options
  155. static FactoryMap v4factories_;
  156. /// pointers to factories that produce DHCPv6 options
  157. static FactoryMap v6factories_;
  158. /// Container with DHCPv4 option definitions.
  159. static OptionDefContainer v4option_defs_;
  160. /// Container with DHCPv6 option definitions.
  161. static OptionDefContainer v6option_defs_;
  162. };
  163. }
  164. }
  165. #endif // LIBDHCP_H