Open Lighting Architecture  0.9.1
 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * IPV4Address.h
17  * Represents a IPv4 Address
18  * Copyright (C) 2011 Simon Newton
19  */
20 
34 #ifndef INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_
35 #define INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_
36 
37 #include <stdint.h>
38 #include <string.h>
39 #include <sstream>
40 #include <string>
41 
42 namespace ola {
43 namespace network {
44 
55 class IPV4Address {
56  public:
60  enum { LENGTH = 4 };
61 
66  m_address = 0;
67  }
68 
73  explicit IPV4Address(uint32_t address) {
74  m_address = address;
75  }
76 
81  IPV4Address(const IPV4Address &other)
82  : m_address(other.m_address) {
83  }
84 
90  if (this != &other) {
91  m_address = other.m_address;
92  }
93  return *this;
94  }
95 
101  bool operator==(const IPV4Address &other) const {
102  return m_address == other.m_address;
103  }
104 
110  bool operator!=(const IPV4Address &other) const {
111  return !(*this == other);
112  }
113 
119  bool operator<(const IPV4Address &other) const {
120  return m_address < other.m_address;
121  }
122 
128  bool operator>(const IPV4Address &other) const {
129  return m_address > other.m_address;
130  }
131 
136  uint32_t AsInt() const { return m_address; }
137 
142  bool IsWildcard() const;
143 
150  void Get(uint8_t ptr[LENGTH]) {
151  memcpy(ptr, reinterpret_cast<uint8_t*>(&m_address), LENGTH);
152  }
153 
158  std::string ToString() const;
159 
166  friend std::ostream& operator<<(std::ostream &out,
167  const IPV4Address &address) {
168  return out << address.ToString();
169  }
170 
177  static IPV4Address* FromString(const std::string &address);
178 
185  static bool FromString(const std::string &address, IPV4Address *target);
186 
193  static IPV4Address FromStringOrDie(const std::string &address);
194 
202  static bool ToCIDRMask(IPV4Address address, uint8_t *mask);
203 
208  static IPV4Address WildCard();
209 
214  static IPV4Address Broadcast();
215 
220  static IPV4Address Loopback();
221 
222  private:
223  uint32_t m_address;
224 };
228 } // namespace network
229 } // namespace ola
230 #endif // INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_