libtins  4.0
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 
40 namespace Tins {
41 
45 class TINS_API IPv6Address {
46 public:
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 
70  IPv6Address();
71 
77  IPv6Address(const char* addr);
78 
84  IPv6Address(const std::string& addr);
85 
93  IPv6Address(const_iterator ptr);
94 
100  std::string to_string() const;
101 
105  iterator begin() {
106  return address_;
107  }
108 
112  const_iterator begin() const {
113  return address_;
114  }
115 
119  iterator end() {
120  return address_ + address_size;
121  }
122 
127  const_iterator end() const {
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 
238  size_t size() const {
239  return address_size;
240  }
241 
249  TINS_API friend std::ostream& operator<<(std::ostream& os, const IPv6Address& addr);
250 
254  IPv6Address operator&(const IPv6Address& rhs) const;
255 
259  IPv6Address operator|(const IPv6Address& rhs) const;
260 
264  IPv6Address operator~() const ;
265 
266 private:
267  void init(const char* addr);
268 
269  uint8_t address_[address_size];
270 };
271 
272 } // Tins
273 
274 #if TINS_IS_CXX11
275 namespace std {
276 
277 template<>
278 struct hash<Tins::IPv6Address> {
279  // Implementation taken from boost.functional
280  size_t operator()(const Tins::IPv6Address& addr) const {
281  std::size_t output = Tins::IPv6Address::address_size;
283  for (; iter != addr.end(); ++iter) {
284  output ^= *iter + 0x9e3779b9 + (output << 6) + (output >> 2);
285  }
286  return output;
287  }
288 };
289 
290 } // std
291 
292 #endif // TINS_IS_CXX11
293 
294 #endif // TINS_IPV6_ADDRESS
bool operator>(const IPv6Address &rhs) const
Compares this address for greater-than inequality.
Definition: ipv6_address.h:182
iterator end()
Definition: ipv6_address.h:119
Definition: hw_address.h:456
uint8_t * iterator
Definition: ipv6_address.h:52
bool operator==(const IPv6Address &rhs) const
Compares this address for equality.
Definition: ipv6_address.h:138
bool operator>=(const IPv6Address &rhs) const
Compares this address for greater-than equality.
Definition: ipv6_address.h:193
size_t size() const
Returns the size of an IPv6 Address.
Definition: ipv6_address.h:238
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
The Tins namespace.
Definition: address_range.h:38
OutputIterator copy(OutputIterator iter) const
Helper function which copies the address into an output iterator.
Definition: ipv6_address.h:213
iterator begin()
Definition: ipv6_address.h:105
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_iterator begin() const
Definition: ipv6_address.h:112
Definition: ipv6_address.h:45
const uint8_t * const_iterator
Definition: ipv6_address.h:57