libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 
144  IPv4Address operator&(const IPv4Address& mask) const;
145 
157  bool is_private() const;
158 
165  bool is_loopback() const;
166 
173  bool is_multicast() const;
174 
178  bool is_unicast() const;
179 
183  bool is_broadcast() const;
184 
190  size_t size() const {
191  return address_size;
192  }
193 
204  TINS_API friend std::ostream& operator<<(std::ostream& output, const IPv4Address& addr);
205 private:
206  uint32_t ip_to_int(const char* ip);
207 
208  uint32_t ip_addr_;
209 };
210 
211 } // Tins
212 
213 #if TINS_IS_CXX11
214 namespace std {
215 
216 template<>
217 struct hash<Tins::IPv4Address> {
218  size_t operator()(const Tins::IPv4Address& addr) const
219  {
220  return std::hash<std::uint32_t>()(addr);
221  }
222 };
223 
224 } // std
225 
226 #endif // TINS_IS_CXX11
227 
228 #endif // TINS_IPADDRESS_H
bool operator!=(const IPv4Address &rhs) const
Compare this IPv4Address for inequality.
Definition: ip_address.h:124
static const IPv4Address broadcast
Definition: ip_address.h:55
bool operator==(const IPv4Address &rhs) const
Compare this IPv4Address for equality.
Definition: ip_address.h:113
IPv6Address operator&(const IPv6Address &lhs, const IPv6Address &rhs)
Definition: ipv6_address.cpp:145
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:190
bool operator<(const IPv4Address &rhs) const
Compare this IPv4Address for less-than inequality.
Definition: ip_address.h:134