libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 
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 
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 
179  template<typename OutputIterator>
180  OutputIterator copy(OutputIterator iter) const {
181  return std::copy(begin(), end(), iter);
182  }
183 
190  bool is_loopback() const;
191 
198  bool is_multicast() const;
199 
205  size_t size() const {
206  return address_size;
207  }
208 
216  TINS_API friend std::ostream& operator<<(std::ostream& os, const IPv6Address& addr);
217 
221  TINS_API friend IPv6Address operator&(const IPv6Address& lhs, const IPv6Address& rhs);
222 
223 private:
224  void init(const char* addr);
225 
226  uint8_t address_[address_size];
227 };
228 
229 } // Tins
230 
231 #if TINS_IS_CXX11
232 namespace std {
233 
234 template<>
235 struct hash<Tins::IPv6Address> {
236  // Implementation taken from boost.functional
237  size_t operator()(const Tins::IPv6Address& addr) const {
238  std::size_t output = Tins::IPv6Address::address_size;
240  for (; iter != addr.end(); ++iter) {
241  output ^= *iter + 0x9e3779b9 + (output << 6) + (output >> 2);
242  }
243  return output;
244  }
245 };
246 
247 } // std
248 
249 #endif // TINS_IS_CXX11
250 
251 #endif // TINS_IPV6_ADDRESS
iterator end()
Definition: ipv6_address.h:119
uint8_t * iterator
Definition: ipv6_address.h:52
bool operator==(const IPv6Address &rhs) const
Compares this address for equality.
Definition: ipv6_address.h:138
size_t size() const
Returns the size of an IPv6 Address.
Definition: ipv6_address.h:205
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
OutputIterator copy(OutputIterator iter) const
Helper function which copies the address into an output iterator.
Definition: ipv6_address.h:180
IPv6Address operator&(const IPv6Address &lhs, const IPv6Address &rhs)
Definition: ipv6_address.cpp:145
iterator begin()
Definition: ipv6_address.h:105
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