libtins 4.5
Loading...
Searching...
No Matches
ipv6_address.h
1/*
2 * Copyright (c) 2017, Matias Fontanini
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#ifndef TINS_IPV6_ADDRESS
31#define TINS_IPV6_ADDRESS
32
33#include <string>
34#include <iosfwd>
35#include <functional>
36#include <stdint.h>
37#include <tins/cxxstd.h>
38#include <tins/macros.h>
39
40namespace Tins {
41
45class TINS_API IPv6Address {
46public:
47 static const size_t address_size = 16;
48
52 typedef uint8_t* iterator;
53
57 typedef const uint8_t* const_iterator;
58
64 static IPv6Address from_prefix_length(uint32_t prefix_length);
65
71
77 IPv6Address(const char* addr);
78
84 IPv6Address(const std::string& addr);
85
94
100 std::string to_string() const;
101
106 return address_;
107 }
108
113 return address_;
114 }
115
120 return address_ + address_size;
121 }
122
128 return address_ + address_size;
129 }
130
138 bool operator==(const IPv6Address& rhs) const {
139 return std::equal(begin(), end(), rhs.address_);
140 }
141
149 bool operator!=(const IPv6Address& rhs) const {
150 return !(*this == rhs);
151 }
152
160 bool operator<(const IPv6Address& rhs) const {
161 return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
162 }
163
171 bool operator<=(const IPv6Address& rhs) const {
172 return !operator>(rhs);
173 }
174
182 bool operator>(const IPv6Address& rhs) const {
183 return std::lexicographical_compare(rhs.begin(), rhs.end(), begin(), end());
184 }
185
193 bool operator>=(const IPv6Address& rhs) const {
194 return !operator<(rhs);
195 }
196
212 template<typename OutputIterator>
213 OutputIterator copy(OutputIterator iter) const {
214 return std::copy(begin(), end(), iter);
215 }
216
223 bool is_loopback() const;
224
231 bool is_multicast() const;
232
239 bool is_local_unicast() const;
240
246 size_t size() const {
247 return address_size;
248 }
249
257 TINS_API friend std::ostream& operator<<(std::ostream& os, const IPv6Address& addr);
258
262 IPv6Address operator&(const IPv6Address& rhs) const;
263
267 IPv6Address operator|(const IPv6Address& rhs) const;
268
272 IPv6Address operator~() const ;
273
274private:
275 void init(const char* addr);
276
277 uint8_t address_[address_size];
278};
279
280} // Tins
281
282#if TINS_IS_CXX11
283namespace std {
284
285template<>
286struct hash<Tins::IPv6Address> {
287 // Implementation taken from boost.functional
288 size_t operator()(const Tins::IPv6Address& addr) const {
289 std::size_t output = Tins::IPv6Address::address_size;
291 for (; iter != addr.end(); ++iter) {
292 output ^= *iter + 0x9e3779b9 + (output << 6) + (output >> 2);
293 }
294 return output;
295 }
296};
297
298} // std
299
300#endif // TINS_IS_CXX11
301
302#endif // TINS_IPV6_ADDRESS
Definition ipv6_address.h:45
iterator end()
Definition ipv6_address.h:119
bool operator!=(const IPv6Address &rhs) const
Compares this address for inequality.
Definition ipv6_address.h:149
const_iterator end() const
Definition ipv6_address.h:127
iterator begin()
Definition ipv6_address.h:105
uint8_t * iterator
Definition ipv6_address.h:52
bool operator<=(const IPv6Address &rhs) const
Compares this address for less-than equality.
Definition ipv6_address.h:171
bool operator<(const IPv6Address &rhs) const
Compares this address for less-than inequality.
Definition ipv6_address.h:160
const uint8_t * const_iterator
Definition ipv6_address.h:57
bool operator>(const IPv6Address &rhs) const
Compares this address for greater-than inequality.
Definition ipv6_address.h:182
size_t size() const
Returns the size of an IPv6 Address.
Definition ipv6_address.h:246
const_iterator begin() const
Definition ipv6_address.h:112
bool operator>=(const IPv6Address &rhs) const
Compares this address for greater-than equality.
Definition ipv6_address.h:193
OutputIterator copy(OutputIterator iter) const
Helper function which copies the address into an output iterator.
Definition ipv6_address.h:213
bool operator==(const IPv6Address &rhs) const
Compares this address for equality.
Definition ipv6_address.h:138
TINS_API friend std::ostream & operator<<(std::ostream &os, const IPv6Address &addr)
Writes this address in hex-notation to a std::ostream.
The Tins namespace.
Definition address_range.h:38