libtins  4.0
ip_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_IPADDRESS_H
31 #define TINS_IPADDRESS_H
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 {
45 class TINS_API IPv4Address {
46 public:
50  static const size_t address_size = sizeof(uint32_t);
51 
55  static const IPv4Address broadcast;
56 
62  static IPv4Address from_prefix_length(uint32_t prefix_length);
63 
74  IPv4Address(const char* ip = 0);
75 
83  IPv4Address(const std::string& ip);
84 
93  explicit IPv4Address(uint32_t ip);
94 
98  operator uint32_t() const;
99 
105  std::string to_string() const;
106 
113  bool operator==(const IPv4Address& rhs) const {
114  return ip_addr_ == rhs.ip_addr_;
115  }
116 
124  bool operator!=(const IPv4Address& rhs) const {
125  return !(*this == rhs);
126  }
127 
134  bool operator<(const IPv4Address& rhs) const {
135  return ip_addr_ < rhs.ip_addr_;
136  }
137 
145  bool operator<=(const IPv4Address& rhs) const {
146  return !operator>(rhs);
147  }
148 
155  bool operator>(const IPv4Address& rhs) const {
156  return ip_addr_ > rhs.ip_addr_;
157  }
158 
166  bool operator>=(const IPv4Address& rhs) const {
167  return !operator<(rhs);
168  }
169 
176  IPv4Address operator&(const IPv4Address& mask) const;
177 
184  IPv4Address operator|(const IPv4Address& mask) const;
185 
189  IPv4Address operator~() const;
190 
202  bool is_private() const;
203 
210  bool is_loopback() const;
211 
218  bool is_multicast() const;
219 
223  bool is_unicast() const;
224 
228  bool is_broadcast() const;
229 
235  size_t size() const {
236  return address_size;
237  }
238 
249  TINS_API friend std::ostream& operator<<(std::ostream& output, const IPv4Address& addr);
250 private:
251  uint32_t ip_to_int(const char* ip);
252 
253  uint32_t ip_addr_;
254 };
255 
256 } // Tins
257 
258 #if TINS_IS_CXX11
259 namespace std {
260 
261 template<>
262 struct hash<Tins::IPv4Address> {
263  size_t operator()(const Tins::IPv4Address& addr) const
264  {
265  return std::hash<std::uint32_t>()(addr);
266  }
267 };
268 
269 } // std
270 
271 #endif // TINS_IS_CXX11
272 
273 #endif // TINS_IPADDRESS_H
bool operator!=(const IPv4Address &rhs) const
Compare this IPv4Address for inequality.
Definition: ip_address.h:124
bool operator>(const IPv4Address &rhs) const
Compare this IPv4Address for greater-than inequality.
Definition: ip_address.h:155
static const IPv4Address broadcast
Definition: ip_address.h:55
Definition: hw_address.h:456
bool operator<=(const IPv4Address &rhs) const
Compares this address for less-than equality.
Definition: ip_address.h:145
The Tins namespace.
Definition: address_range.h:38
bool operator==(const IPv4Address &rhs) const
Compare this IPv4Address for equality.
Definition: ip_address.h:113
Abstraction of an IPv4 address.
Definition: ip_address.h:45
size_t size() const
Returns the size of an IPv4 Address.
Definition: ip_address.h:235
bool operator<(const IPv4Address &rhs) const
Compare this IPv4Address for less-than inequality.
Definition: ip_address.h:134
bool operator>=(const IPv4Address &rhs) const
Compares this address for greater-than equality.
Definition: ip_address.h:166