Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IPV4Address.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * IPV4Address.h
17  * Represents a IPv4 Address
18  * Copyright (C) 2011-2014 Simon Newton
19  */
20 
34 #ifndef INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_
35 #define INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_
36 
37 #ifdef WIN32
38 #include <winsock2.h>
39 #else
40 #include <netinet/in.h>
41 #endif
42 
43 #include <stdint.h>
44 #include <string.h>
45 #include <sstream>
46 #include <string>
47 
48 namespace ola {
49 namespace network {
50 
61 class IPV4Address {
62  public:
66  enum { LENGTH = 4 };
67 
72  m_address.s_addr = 0;
73  }
74 
79  explicit IPV4Address(const struct in_addr &address)
80  : m_address(address) {
81  }
82 
87  explicit IPV4Address(unsigned int address) {
88  m_address.s_addr = address;
89  }
90 
95  IPV4Address(const IPV4Address &other)
96  : m_address(other.m_address) {
97  }
98 
104  if (this != &other) {
105  m_address = other.m_address;
106  }
107  return *this;
108  }
109 
115  bool operator==(const IPV4Address &other) const {
116  return m_address.s_addr == other.m_address.s_addr;
117  }
118 
124  bool operator!=(const IPV4Address &other) const {
125  return !(*this == other);
126  }
127 
133  bool operator<(const IPV4Address &other) const {
134  return m_address.s_addr < other.m_address.s_addr;
135  }
136 
142  bool operator>(const IPV4Address &other) const {
143  return m_address.s_addr > other.m_address.s_addr;
144  }
145 
150  const struct in_addr Address() const {
151  return m_address;
152  }
153 
158  uint32_t AsInt() const { return m_address.s_addr; }
159 
164  bool IsWildcard() const {
165  return m_address.s_addr == INADDR_ANY;
166  }
167 
174  void Get(uint8_t ptr[LENGTH]) {
175  memcpy(ptr,
176  reinterpret_cast<uint8_t*>(&m_address.s_addr),
177  LENGTH);
178  }
179 
184  std::string ToString() const;
185 
192  friend std::ostream& operator<<(std::ostream &out,
193  const IPV4Address &address) {
194  return out << address.ToString();
195  }
196 
203  static IPV4Address* FromString(const std::string &address);
204 
211  static bool FromString(const std::string &address, IPV4Address *target);
212 
219  static IPV4Address FromStringOrDie(const std::string &address);
220 
228  static bool ToCIDRMask(IPV4Address address, uint8_t *mask);
229 
235  return IPV4Address(INADDR_ANY);
236  }
237 
243  return IPV4Address(INADDR_NONE);
244  }
245 
250  static IPV4Address Loopback();
251 
252  private:
253  struct in_addr m_address;
254 };
258 } // namespace network
259 } // namespace ola
260 #endif // INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_