option4_addrlst.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 OPTION4_ADDRLST_H_
  15. #define OPTION4_ADDRLST_H_
  16. #include <string>
  17. #include <map>
  18. #include <vector>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/shared_array.hpp>
  21. #include <util/buffer.h>
  22. #include <dhcp/option.h>
  23. namespace isc {
  24. namespace dhcp {
  25. /// @brief DHCPv4 Option class for handling list of IPv4 addresses.
  26. ///
  27. /// This class handles a list of IPv4 addresses. An example of such option
  28. /// is dns-servers option. It can also be used to handle a single address.
  29. class Option4AddrLst : public isc::dhcp::Option {
  30. public:
  31. /// Defines a collection of IPv4 addresses.
  32. typedef std::vector<isc::asiolink::IOAddress> AddressContainer;
  33. /// @brief Constructor, creates an option with empty list of addresses.
  34. ///
  35. /// Creates empty option that can hold addresses. Addresses can be added
  36. /// with addAddress(), setAddress() or setAddresses().
  37. ///
  38. /// @param type option type
  39. Option4AddrLst(uint8_t type);
  40. /// @brief Constructor, creates an option with a list of addresses.
  41. ///
  42. /// Creates an option that contains specified list of IPv4 addresses.
  43. ///
  44. /// @param type option type
  45. /// @param addrs container with a list of addresses
  46. Option4AddrLst(uint8_t type, const AddressContainer& addrs);
  47. /// @brief Constructor, creates an option with a single address.
  48. ///
  49. /// Creates an option that contains a single address.
  50. ///
  51. /// @param type option type
  52. /// @param addr a single address that will be stored as 1-elem. address list
  53. Option4AddrLst(uint8_t type, const isc::asiolink::IOAddress& addr);
  54. /// @brief Constructor, used for received options.
  55. ///
  56. /// TODO: This can be templated to use different containers, not just
  57. /// vector. Prototype should look like this:
  58. /// template<typename InputIterator> Option(Universe u, uint16_t type,
  59. /// InputIterator first, InputIterator last);
  60. ///
  61. /// vector<int8_t> myData;
  62. /// Example usage: new Option(V4, 123, myData.begin()+1, myData.end()-1)
  63. /// This will create DHCPv4 option of type 123 that contains data from
  64. /// trimmed (first and last byte removed) myData vector.
  65. ///
  66. /// @param type option type (0-255 for V4 and 0-65535 for V6)
  67. /// @param first iterator to the first element that should be copied
  68. /// @param last iterator to the next element after the last one
  69. /// to be copied.
  70. Option4AddrLst(uint8_t type, OptionBufferConstIter first,
  71. OptionBufferConstIter last);
  72. /// @brief Writes option in a wire-format to a buffer.
  73. ///
  74. /// Method will throw if option storing fails for some reason.
  75. ///
  76. /// TODO Once old (DHCPv6) implementation is rewritten,
  77. /// unify pack4() and pack6() and rename them to just pack().
  78. ///
  79. /// @param buf output buffer (option will be stored there)
  80. virtual void pack4(isc::util::OutputBuffer& buf);
  81. /// Returns string representation of the option.
  82. ///
  83. /// @param indent number of spaces before printing text
  84. ///
  85. /// @return string with text representation.
  86. virtual std::string toText(int indent = 0);
  87. /// Returns length of the complete option (data length + DHCPv4/DHCPv6
  88. /// option header)
  89. ///
  90. /// @return length of the option
  91. virtual uint16_t len();
  92. /// @brief Returns vector with addresses.
  93. ///
  94. /// We return a copy of our list. Although this includes overhead,
  95. /// it also makes this list safe to use after this option object
  96. /// is no longer available. As options are expected to hold only
  97. /// a couple (1-3) addresses, the overhead is not that big.
  98. ///
  99. /// @return address container with addresses
  100. AddressContainer getAddresses() { return addrs_; };
  101. /// @brief Sets addresses list.
  102. ///
  103. /// Clears existing list of addresses and adds a single address to that
  104. /// list. This is very convenient method for options that are supposed to
  105. /// only a single option. See addAddress() if you want to add
  106. /// address to existing list or setAddresses() if you want to
  107. /// set the whole list at once.
  108. ///
  109. /// Passed address must be IPv4 address. Otherwire BadValue exception
  110. /// will be thrown.
  111. ///
  112. /// @param addrs address collection to be set
  113. void setAddresses(const AddressContainer& addrs);
  114. /// @brief Clears address list and sets a single address.
  115. ///
  116. /// Clears existing list of addresses and adds a single address to that
  117. /// list. This is very convenient method for options that are supposed to
  118. /// only a single option. See addAddress() if you want to add
  119. /// address to existing list or setAddresses() if you want to
  120. /// set the whole list at once.
  121. ///
  122. /// Passed address must be IPv4 address. Otherwire BadValue exception
  123. /// will be thrown.
  124. ///
  125. /// @param addr an address that is going to be set as 1-element address list
  126. void setAddress(const isc::asiolink::IOAddress& addr);
  127. /// @brief Adds address to existing list of addresses.
  128. ///
  129. /// Adds a single address to that list. See setAddress() if you want to
  130. /// define only a single address or setAddresses() if you want to
  131. /// set the whole list at once.
  132. ///
  133. /// Passed address must be IPv4 address. Otherwire BadValue exception
  134. /// will be thrown.
  135. ///
  136. /// @param addr an address thait is going to be added to existing list
  137. void addAddress(const isc::asiolink::IOAddress& addr);
  138. protected:
  139. /// contains list of addresses
  140. AddressContainer addrs_;
  141. };
  142. } // namespace isc::dhcp
  143. } // namespace isc
  144. #endif