Kea  1.5.0
io_address.h
Go to the documentation of this file.
1 // Copyright (C) 2010-2018 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 IO_ADDRESS_H
8 #define IO_ADDRESS_H 1
9 
10 // IMPORTANT NOTE: only very few ASIO headers files can be included in
11 // this file. In particular, asio.hpp should never be included here.
12 // See the description of the namespace below.
13 #include <unistd.h> // for some network system calls
14 #include <stdint.h> // for uint32_t
15 #include <boost/asio/ip/address.hpp>
16 
17 #include <functional>
18 #include <string>
19 #include <vector>
20 
21 #include <exceptions/exceptions.h>
22 
23 namespace isc {
24 namespace asiolink {
25 
27  static constexpr size_t V6ADDRESS_LEN = 16;
28 
30  static constexpr size_t V4ADDRESS_LEN = 4;
31 
34  static constexpr size_t V4ADDRESS_TEXT_MAX_LEN = 15u;
35 
39  static constexpr size_t V6ADDRESS_TEXT_MAX_LEN = 39u;
40 
45 class IOAddress {
46 public:
53 
54  IOAddress(const std::string& address_str);
65 
74  IOAddress(const boost::asio::ip::address& asio_address);
76 
84  IOAddress(uint32_t v4address);
85 
93  std::string toText() const;
94 
98  short getFamily() const;
99 
103  bool isV4() const {
104  return (asio_address_.is_v4());
105  }
106 
110  bool isV4Zero() const {
111  return (equals(IPV4_ZERO_ADDRESS()));
112  }
113 
118  bool isV4Bcast() const {
119  return (equals(IPV4_BCAST_ADDRESS()));
120  }
121 
125  bool isV6() const {
126  return (asio_address_.is_v6());
127  }
128 
132  bool isV6Zero() const {
133  return (equals(IPV6_ZERO_ADDRESS()));
134  }
135 
139  bool isV6LinkLocal() const;
140 
144  bool isV6Multicast() const;
145 
152  static IOAddress fromBytes(short family, const uint8_t* data);
153 
158  std::vector<uint8_t> toBytes() const;
159 
165  bool equals(const IOAddress& other) const {
166  return (asio_address_ == other.asio_address_);
167  }
168 
174  bool operator==(const IOAddress& other) const {
175  return equals(other);
176  }
177 
183  bool nequals(const IOAddress& other) const {
184  return (!equals(other));
185  }
186 
197  bool lessThan(const IOAddress& other) const {
198  if (this->getFamily() == other.getFamily()) {
199  if (this->getFamily() == AF_INET6) {
200  return (this->asio_address_.to_v6() < other.asio_address_.to_v6());
201  } else {
202  return (this->asio_address_.to_v4() < other.asio_address_.to_v4());
203  }
204  }
205  return (this->getFamily() < other.getFamily());
206  }
207 
213  bool smallerEqual(const IOAddress& other) const {
214  if (equals(other)) {
215  return (true);
216  }
217  return (lessThan(other));
218  }
219 
225  bool operator<(const IOAddress& other) const {
226  return (lessThan(other));
227  }
228 
234  bool operator<=(const IOAddress& other) const {
235  return (smallerEqual(other));
236  }
237 
243  bool operator!=(const IOAddress& other) const {
244  return (nequals(other));
245  }
246 
268  static IOAddress subtract(const IOAddress& a, const IOAddress& b);
269 
287  static IOAddress
288  increase(const IOAddress& addr);
289 
297  uint32_t toUint32() const;
298 
301 
302  static const IOAddress& IPV4_ZERO_ADDRESS() {
304  static IOAddress address(0);
305  return (address);
306  }
307 
309  static const IOAddress& IPV4_BCAST_ADDRESS() {
310  static IOAddress address(0xFFFFFFFF);
311  return (address);
312  }
313 
315  static const IOAddress& IPV6_ZERO_ADDRESS() {
316  static IOAddress address("::");
317  return (address);
318  }
319 
321 
322 private:
323  boost::asio::ip::address asio_address_;
324 };
325 
339 std::ostream&
340 operator<<(std::ostream& os, const IOAddress& address);
341 
342 } // namespace asiolink
343 } // namespace isc
344 #endif // IO_ADDRESS_H
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
exceptions.h