Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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_
static IPV4Address * FromString(const std::string &address)
Convert a string to an IPV4Address.
Definition: IPV4Address.cpp:114
void Get(uint8_t ptr[LENGTH])
Copy the IPV4Address to a memory location.
Definition: IPV4Address.h:142
IPV4Address(const IPV4Address &other)
Copy constructor.
Definition: IPV4Address.h:81
static IPV4Address Broadcast()
Returns the broadcast address INADDR_NONE (255.255.255.255).
Definition: IPV4Address.cpp:161
std::string ToString() const
Convert the IPV4Address to a string.
Definition: IPV4Address.cpp:98
IPV4Address & operator=(const IPV4Address &other)
Assignment operator.
Definition: IPV4Address.h:89
bool operator<(const IPV4Address &other) const
Less than operator for partial ordering.
Definition: IPV4Address.cpp:58
IPV4Address()
Create a new IPv4 Address set to INADDR_ANY (0.0.0.0).
Definition: IPV4Address.h:65
bool IsWildcard() const
Checks if this address is the wildcard address (0.0.0.0).
Definition: IPV4Address.cpp:94
static IPV4Address WildCard()
Returns the wildcard address INADDR_ANY (0.0.0.0).
Definition: IPV4Address.cpp:157
bool operator==(const IPV4Address &other) const
Equals operator.
Definition: IPV4Address.h:101
Represents a IPv4 Address.
Definition: IPV4Address.h:55
uint32_t AsInt() const
Return the IPV4Address as an int in network-byte order.
Definition: IPV4Address.h:128
static IPV4Address FromStringOrDie(const std::string &address)
Convert a string to an IPV4Address or abort.
Definition: IPV4Address.cpp:132
IPV4Address(uint32_t address)
Create a new IPv4 Address from an uint32.
Definition: IPV4Address.h:73
friend std::ostream & operator<<(std::ostream &out, const IPV4Address &address)
Write the string representation of this IPV4Address to an ostream.
Definition: IPV4Address.h:158
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
bool operator!=(const IPV4Address &other) const
Not equals operator.
Definition: IPV4Address.h:110
static IPV4Address Loopback()
Returns the loopback address (127.0.0.1).
Definition: IPV4Address.cpp:165
bool operator>(const IPV4Address &other) const
Greater than operator.
Definition: IPV4Address.cpp:63
static bool ToCIDRMask(IPV4Address address, uint8_t *mask)
Convert a subnet mask to its CIDR format value.
Definition: IPV4Address.cpp:138