libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
ip_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_IPADDRESS_H
31 #define TINS_IPADDRESS_H
32 
33 #include <string>
34 #include <iostream>
35 #include <stdint.h>
36 #include "cxxstd.h"
37 #include "macros.h"
38 
39 namespace Tins {
44 class TINS_API IPv4Address {
45 public:
49  static const size_t address_size = sizeof(uint32_t);
50 
54  static const IPv4Address broadcast;
55 
61  static IPv4Address from_prefix_length(uint32_t prefix_length);
62 
73  IPv4Address(const char* ip = 0);
74 
82  IPv4Address(const std::string& ip);
83 
92  explicit IPv4Address(uint32_t ip);
93 
97  operator uint32_t() const;
98 
104  std::string to_string() const;
105 
112  bool operator==(const IPv4Address& rhs) const {
113  return ip_addr_ == rhs.ip_addr_;
114  }
115 
123  bool operator!=(const IPv4Address& rhs) const {
124  return !(*this == rhs);
125  }
126 
133  bool operator<(const IPv4Address& rhs) const {
134  return ip_addr_ < rhs.ip_addr_;
135  }
136 
143  IPv4Address operator&(const IPv4Address& mask) const;
144 
156  bool is_private() const;
157 
164  bool is_loopback() const;
165 
172  bool is_multicast() const;
173 
177  bool is_unicast() const;
178 
182  bool is_broadcast() const;
183 
194  TINS_API friend std::ostream& operator<<(std::ostream& output, const IPv4Address& addr);
195 private:
196  uint32_t ip_to_int(const char* ip);
197 
198  uint32_t ip_addr_;
199 };
200 
201 } // Tins
202 
203 #if TINS_IS_CXX11
204 namespace std {
205 
206 template<>
207 struct hash<Tins::IPv4Address> {
208  size_t operator()(const Tins::IPv4Address& addr) const {
209  return std::hash<uint32_t>()(addr);
210  }
211 };
212 
213 } // std
214 
215 #endif // TINS_IS_CXX11
216 
217 #endif // TINS_IPADDRESS_H
bool operator!=(const IPv4Address &rhs) const
Compare this IPv4Address for inequality.
Definition: ip_address.h:123
static const IPv4Address broadcast
Definition: ip_address.h:54
bool operator==(const IPv4Address &rhs) const
Compare this IPv4Address for equality.
Definition: ip_address.h:112
IPv6Address operator&(const IPv6Address &lhs, const IPv6Address &rhs)
Definition: ipv6_address.cpp:135
Abstraction of an IPv4 address.
Definition: ip_address.h:44
bool operator<(const IPv4Address &rhs) const
Compare this IPv4Address for less-than inequality.
Definition: ip_address.h:133