libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
ipv6_address.h
1 /*
2  * Copyright (c) 2016, 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 <stdexcept>
35 #include <stdint.h>
36 #include "cxxstd.h"
37 #include "macros.h"
38 
39 namespace Tins {
40 
44 class TINS_API IPv6Address {
45 public:
46  static const size_t address_size = 16;
47 
51  typedef uint8_t* iterator;
52 
56  typedef const uint8_t* const_iterator;
57 
63  static IPv6Address from_prefix_length(uint32_t prefix_length);
64 
69  IPv6Address();
70 
76  IPv6Address(const char* addr);
77 
83  IPv6Address(const std::string& addr);
84 
93 
99  std::string to_string() const;
100 
105  return address_;
106  }
107 
112  return address_;
113  }
114 
119  return address_ + address_size;
120  }
121 
126  const_iterator end() const {
127  return address_ + address_size;
128  }
129 
137  bool operator==(const IPv6Address& rhs) const {
138  return std::equal(begin(), end(), rhs.address_);
139  }
140 
148  bool operator!=(const IPv6Address& rhs) const {
149  return !(*this == rhs);
150  }
151 
159  bool operator<(const IPv6Address& rhs) const {
160  return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
161  }
162 
178  template<typename OutputIterator>
179  OutputIterator copy(OutputIterator iter) const {
180  return std::copy(begin(), end(), iter);
181  }
182 
189  bool is_loopback() const;
190 
197  bool is_multicast() const;
198 
206  friend std::ostream& operator<<(std::ostream& os, const IPv6Address& addr) {
207  return os << addr.to_string();
208  }
209 
210 
214  TINS_API friend IPv6Address operator&(const IPv6Address& lhs, const IPv6Address& rhs);
215 
216 private:
217  void init(const char* addr);
218 
219  uint8_t address_[address_size];
220 };
221 
222 } // Tins
223 
224 #if TINS_IS_CXX11
225 namespace std {
226 
227 template<>
228 struct hash<Tins::IPv6Address> {
229  size_t operator()(const Tins::IPv6Address& addr) const {
230  return std::hash<std::string>()(addr.to_string());
231  }
232 };
233 
234 } // std
235 
236 #endif // TINS_IS_CXX11
237 
238 #endif // TINS_IPV6_ADDRESS
friend std::ostream & operator<<(std::ostream &os, const IPv6Address &addr)
Writes this address in hex-notation to a std::ostream.
Definition: ipv6_address.h:206
iterator end()
Definition: ipv6_address.h:118
std::string to_string() const
Retrieve the string representation of this address.
Definition: ipv6_address.cpp:105
uint8_t * iterator
Definition: ipv6_address.h:51
bool operator==(const IPv6Address &rhs) const
Compares this address for equality.
Definition: ipv6_address.h:137
bool operator!=(const IPv6Address &rhs) const
Compares this address for inequality.
Definition: ipv6_address.h:148
const_iterator end() const
Definition: ipv6_address.h:126
OutputIterator copy(OutputIterator iter) const
Helper function which copies the address into an output iterator.
Definition: ipv6_address.h:179
IPv6Address operator&(const IPv6Address &lhs, const IPv6Address &rhs)
Definition: ipv6_address.cpp:135
iterator begin()
Definition: ipv6_address.h:104
bool operator<(const IPv6Address &rhs) const
Compares this address for less-than inequality.
Definition: ipv6_address.h:159
const_iterator begin() const
Definition: ipv6_address.h:111
Definition: ipv6_address.h:44
const uint8_t * const_iterator
Definition: ipv6_address.h:56