1 // Copyright (C) 2011-2017 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef OPTION4_ADDRLST_H
8 #define OPTION4_ADDRLST_H
9 
10 #include <asiolink/io_address.h>
11 #include <dhcp/option.h>
12 #include <util/buffer.h>
13 
14 #include <boost/shared_array.hpp>
15 #include <boost/shared_ptr.hpp>
16 
17 #include <map>
18 #include <string>
19 #include <vector>
20 
21 namespace isc {
22 namespace dhcp {
23 
24 /// Forward declaration to Option4AddrLst class.
25 class Option4AddrLst;
26 
27 /// A pointer to the Option4AddrLst object.
28 typedef boost::shared_ptr<Option4AddrLst> Option4AddrLstPtr;
29 
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 
37     /// Defines a collection of IPv4 addresses.
38     typedef std::vector<isc::asiolink::IOAddress> AddressContainer;
39 
40     /// @brief Constructor, creates an option with empty list of addresses.
41     ///
42     /// Creates empty option that can hold addresses. Addresses can be added
43     /// with addAddress(), setAddress() or setAddresses().
44     ///
45     /// @param type option type
46     Option4AddrLst(uint8_t type);
47 
48     /// @brief Constructor, creates an option with a list of addresses.
49     ///
50     /// Creates an option that contains specified list of IPv4 addresses.
51     ///
52     /// @param type option type
53     /// @param addrs container with a list of addresses
54     Option4AddrLst(uint8_t type, const AddressContainer& addrs);
55 
56     /// @brief Constructor, creates an option with a single address.
57     ///
58     /// Creates an option that contains a single address.
59     ///
60     /// @param type option type
61     /// @param addr a single address that will be stored as 1-elem. address list
62     Option4AddrLst(uint8_t type, const isc::asiolink::IOAddress& addr);
63 
64     /// @brief Constructor, used for received options.
65     ///
66     /// TODO: This can be templated to use different containers, not just
67     /// vector. Prototype should look like this:
68     /// template<typename InputIterator> Option(Universe u, uint16_t type,
69     /// InputIterator first, InputIterator last);
70     ///
71     /// vector<int8_t> myData;
72     /// Example usage: new Option(V4, 123, myData.begin()+1, myData.end()-1)
73     /// This will create DHCPv4 option of type 123 that contains data from
74     /// trimmed (first and last byte removed) myData vector.
75     ///
76     /// @param type option type (0-255 for V4 and 0-65535 for V6)
77     /// @param first iterator to the first element that should be copied
78     /// @param last iterator to the next element after the last one
79     ///        to be copied.
80     Option4AddrLst(uint8_t type, OptionBufferConstIter first,
81                    OptionBufferConstIter last);
82 
83     /// @brief Copies this option and returns a pointer to the copy.
84     virtual OptionPtr clone() const;
85 
86     /// @brief Writes option in a wire-format to a buffer.
87     ///
88     /// Method will throw if option storing fails for some reason.
89     ///
90     /// @param buf output buffer (option will be stored there)
91     virtual void pack(isc::util::OutputBuffer& buf) const;
92 
93     /// Returns string representation of the option.
94     ///
95     /// @param indent number of spaces before printing text
96     ///
97     /// @return string with text representation.
98     virtual std::string toText(int indent = 0) const;
99 
100     /// Returns length of the complete option (data length + DHCPv4/DHCPv6
101     /// option header)
102     ///
103     /// @return length of the option
104     virtual uint16_t len() const;
105 
106     /// @brief Returns vector with addresses.
107     ///
108     /// We return a copy of our list. Although this includes overhead,
109     /// it also makes this list safe to use after this option object
110     /// is no longer available. As options are expected to hold only
111     /// a few (1-3) addresses, the overhead is not that big.
112     ///
113     /// @return address container with addresses
getAddresses()114     AddressContainer getAddresses() const { return addrs_; };
115 
116     /// @brief Sets addresses list.
117     ///
118     /// Clears existing list of addresses and adds a single address to that
119     /// list. This is very convenient method for options that are supposed to
120     /// only a single option. See addAddress() if you want to add
121     /// address to existing list or setAddresses() if you want to
122     /// set the whole list at once.
123     ///
124     /// Passed address must be IPv4 address. Otherwise BadValue exception
125     /// will be thrown.
126     ///
127     /// @param addrs address collection to be set
128     void setAddresses(const AddressContainer& addrs);
129 
130     /// @brief Clears address list and sets a single address.
131     ///
132     /// Clears existing list of addresses and adds a single address to that
133     /// list. This is very convenient method for options that are supposed to
134     /// only a single option. See addAddress() if you want to add
135     /// address to existing list or setAddresses() if you want to
136     /// set the whole list at once.
137     ///
138     /// Passed address must be IPv4 address. Otherwise BadValue exception
139     /// will be thrown.
140     ///
141     /// @param addr an address that is going to be set as 1-element address list
142     void setAddress(const isc::asiolink::IOAddress& addr);
143 
144     /// @brief Adds address to existing list of addresses.
145     ///
146     /// Adds a single address to that list. See setAddress() if you want to
147     /// define only a single address or setAddresses() if you want to
148     /// set the whole list at once.
149     ///
150     /// Passed address must be IPv4 address. Otherwise BadValue exception
151     /// will be thrown.
152     ///
153     /// @param addr an address that is going to be added to existing list
154     void addAddress(const isc::asiolink::IOAddress& addr);
155 
156 protected:
157     /// contains list of addresses
158     AddressContainer addrs_;
159 };
160 
161 } // namespace isc::dhcp
162 } // namespace isc
163 
164 #endif // OPTION4_ADDRLST_H
165