Kea  2.1.6-git
io_address.h
Go to the documentation of this file.
1 // Copyright (C) 2010-2020 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 
190  bool operator<(const IOAddress& other) const {
191  return (asio_address_ < other.asio_address_);
192  }
193 
197  bool operator<=(const IOAddress& other) const {
198  return (asio_address_ <= other.asio_address_);
199  }
200 
206  bool operator!=(const IOAddress& other) const {
207  return (nequals(other));
208  }
209 
231  static IOAddress subtract(const IOAddress& a, const IOAddress& b);
232 
250  static IOAddress
251  increase(const IOAddress& addr);
252 
260  uint32_t toUint32() const;
261 
264 
265  static const IOAddress& IPV4_ZERO_ADDRESS() {
267  static IOAddress address(0);
268  return (address);
269  }
270 
272  static const IOAddress& IPV4_BCAST_ADDRESS() {
273  static IOAddress address(0xFFFFFFFF);
274  return (address);
275  }
276 
278  static const IOAddress& IPV6_ZERO_ADDRESS() {
279  static IOAddress address("::");
280  return (address);
281  }
282 
284 
285 private:
286  boost::asio::ip::address asio_address_;
287 };
288 
302 std::ostream&
303 operator<<(std::ostream& os, const IOAddress& address);
304 
314 size_t hash_value(const IOAddress& address);
315 
316 } // namespace asiolink
317 } // namespace isc
318 #endif // IO_ADDRESS_H
Defines the logger used by the top-level component of kea-lfc.