Kea 3.2.0-git
io_address.h
Go to the documentation of this file.
1// Copyright (C) 2010-2025 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
16#ifdef __GNUC__
17#pragma GCC diagnostic push
18#pragma GCC diagnostic ignored "-Woverloaded-virtual"
19#endif
20#include <boost/asio/ip/address.hpp>
21#ifdef __GNUC__
22#pragma GCC diagnostic pop
23#endif
24
25#include <functional>
26#include <string>
27#include <vector>
28
30
31namespace isc {
32namespace asiolink {
33
35 static constexpr size_t V6ADDRESS_LEN = 16;
36
38 static constexpr size_t V4ADDRESS_LEN = 4;
39
42 static constexpr size_t V4ADDRESS_TEXT_MAX_LEN = 15u;
43
47 static constexpr size_t V6ADDRESS_TEXT_MAX_LEN = 39u;
48
53class IOAddress {
54public:
55
58 struct Hash {
63 size_t operator()(const IOAddress &io_address) const;
64 };
65
72
73
83 IOAddress(const std::string& address_str);
84
93 IOAddress(const boost::asio::ip::address& asio_address);
95
103 IOAddress(uint32_t v4address);
104
112 std::string toText() const;
113
117 short getFamily() const;
118
122 bool isV4() const {
123 return (asio_address_.is_v4());
124 }
125
129 bool isV4Zero() const {
130 return (equals(IPV4_ZERO_ADDRESS()));
131 }
132
137 bool isV4Bcast() const {
138 return (equals(IPV4_BCAST_ADDRESS()));
139 }
140
144 bool isV6() const {
145 return (asio_address_.is_v6());
146 }
147
151 bool isV6Zero() const {
152 return (equals(IPV6_ZERO_ADDRESS()));
153 }
154
158 bool isV6LinkLocal() const;
159
163 bool isV6Multicast() const;
164
171 static IOAddress fromBytes(short family, const uint8_t* data);
172
177 std::vector<uint8_t> toBytes() const;
178
184 bool equals(const IOAddress& other) const {
185 return (asio_address_ == other.asio_address_);
186 }
187
193 bool operator==(const IOAddress& other) const {
194 return equals(other);
195 }
196
202 bool nequals(const IOAddress& other) const {
203 return (!equals(other));
204 }
205
209 bool operator<(const IOAddress& other) const {
210 return (asio_address_ < other.asio_address_);
211 }
212
216 bool operator<=(const IOAddress& other) const {
217 return (asio_address_ <= other.asio_address_);
218 }
219
225 bool operator!=(const IOAddress& other) const {
226 return (nequals(other));
227 }
228
250 static IOAddress subtract(const IOAddress& a, const IOAddress& b);
251
269 static IOAddress
270 increase(const IOAddress& addr);
271
279 uint32_t toUint32() const;
280
283
284
285 static const IOAddress& IPV4_ZERO_ADDRESS() {
286 static IOAddress address(0);
287 return (address);
288 }
289
292 static IOAddress address(0xFFFFFFFF);
293 return (address);
294 }
295
297 static const IOAddress& IPV6_ZERO_ADDRESS() {
298 static IOAddress address("::");
299 return (address);
300 }
301
303
304private:
305 boost::asio::ip::address asio_address_;
306};
307
321std::ostream&
322operator<<(std::ostream& os, const IOAddress& address);
323
333size_t hash_value(const IOAddress& address);
334
335} // namespace asiolink
336} // namespace isc
337
338#endif // IO_ADDRESS_H
Defines the logger used by the top-level component of kea-lfc.