Open Lighting Architecture  0.9.4
 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 
117  bool operator<(const IPV4Address &other) const;
118 
122  bool operator>(const IPV4Address &other) const;
123 
128  uint32_t AsInt() const { return m_address; }
129 
134  bool IsWildcard() const;
135 
142  void Get(uint8_t ptr[LENGTH]) {
143  memcpy(ptr, reinterpret_cast<uint8_t*>(&m_address), LENGTH);
144  }
145 
150  std::string ToString() const;
151 
158  friend std::ostream& operator<<(std::ostream &out,
159  const IPV4Address &address) {
160  return out << address.ToString();
161  }
162 
169  static IPV4Address* FromString(const std::string &address);
170 
177  static bool FromString(const std::string &address, IPV4Address *target);
178 
185  static IPV4Address FromStringOrDie(const std::string &address);
186 
194  static bool ToCIDRMask(IPV4Address address, uint8_t *mask);
195 
200  static IPV4Address WildCard();
201 
206  static IPV4Address Broadcast();
207 
212  static IPV4Address Loopback();
213 
214  private:
215  uint32_t m_address;
216 };
220 } // namespace network
221 } // namespace ola
222 #endif // INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_