option4_addrlst.h 6.5 KB

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