libdhcp.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /// Builds collection of options.
  24. ///
  25. /// Builds raw (on-wire) data for provided collection of options.
  26. ///
  27. /// @param buf shared pointer to buffer. Data will be stored there.
  28. /// @param buf_len buffer length. Used for buffer overflow protection.
  29. /// @param offset Offset from beginning of the buffer, where store options
  30. /// @param options collection of options to store to
  31. ///
  32. /// @return offset to the first unused byte in buffer (next one after last
  33. /// used byte)
  34. ///
  35. static unsigned int
  36. packOptions6(boost::shared_array<uint8_t> buf, unsigned int buf_len,
  37. unsigned int offset,
  38. const isc::dhcp::Option::Option6Collection& options);
  39. /// @brief Stores options in a buffer.
  40. ///
  41. /// Stores all options defined in options containers in a on-wire
  42. /// format in output buffer specified by buf.
  43. ///
  44. /// May throw different exceptions if option assembly fails. There
  45. /// may be different reasons (option too large, option malformed,
  46. /// too many options etc.)
  47. ///
  48. /// @param buf
  49. /// @param options
  50. static void
  51. packOptions(isc::util::OutputBuffer& buf,
  52. const isc::dhcp::Option::Option6Collection& options);
  53. static void
  54. unpackOptions4(const std::vector<uint8_t>& buf,
  55. isc::dhcp::Option::Option6Collection& options);
  56. ///
  57. /// Parses provided buffer and creates Option objects.
  58. ///
  59. /// Parses provided buf array and stores created Option objects
  60. /// in options container.
  61. ///
  62. /// @param buf Buffer to be parsed.
  63. /// @param offset Specifies offset for the first option.
  64. /// @param options Reference to option container. Options will be
  65. /// put here.
  66. ///
  67. /// @return offset to first byte after last parsed option
  68. ///
  69. static unsigned int
  70. unpackOptions6(const boost::shared_array<uint8_t> buf, unsigned int buf_len,
  71. unsigned int offset, unsigned int parse_len,
  72. isc::dhcp::Option::Option6Collection& options_);
  73. ///
  74. /// Registers factory method that produces options of specific option types.
  75. ///
  76. /// @param u universe of the option (V4 or V6)
  77. /// @param opt_type option-type
  78. /// @param factory function pointer
  79. ///
  80. /// @return true, if registration was successful, false otherwise
  81. ///
  82. static bool
  83. OptionFactoryRegister(Option::Universe u,
  84. unsigned short type,
  85. Option::Factory * factory);
  86. protected:
  87. // pointers to factories that produce DHCPv6 options
  88. static std::map<unsigned short, Option::Factory*> v6factories_;
  89. };
  90. }
  91. }
  92. #endif