Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IPV4Address.h
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 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_
22 #define INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_
23 
24 #ifdef WIN32
25 #include <winsock2.h>
26 #else
27 #include <netinet/in.h>
28 #endif
29 
30 #include <string.h>
31 #include <sstream>
32 #include <string>
33 
34 namespace ola {
35 namespace network {
36 
37 using std::ostream;
38 
39 /*
40  * Represents a IPv4 Address.
41  * All methods use network byte order unless otherwise mentioned.
42  */
43 class IPV4Address {
44  public:
45  enum { LENGTH = 4 };
46 
47  IPV4Address() {
48  m_address.s_addr = 0;
49  }
50 
51  explicit IPV4Address(const struct in_addr &address)
52  : m_address(address) {
53  }
54 
55  explicit IPV4Address(unsigned int address) {
56  m_address.s_addr = address;
57  }
58 
59  IPV4Address(const IPV4Address &other)
60  : m_address(other.m_address) {
61  }
62 
63  IPV4Address& operator=(const IPV4Address &other) {
64  if (this != &other) {
65  m_address = other.m_address;
66  }
67  return *this;
68  }
69 
70  bool operator==(const IPV4Address &other) const {
71  return m_address.s_addr == other.m_address.s_addr;
72  }
73 
74  bool operator!=(const IPV4Address &other) const {
75  return !(*this == other);
76  }
77 
78  // Order addresses. Note that this won't order how humans expect
79  // because s_addr is in network byte order.
80  bool operator<(const IPV4Address &other) const {
81  return m_address.s_addr < other.m_address.s_addr;
82  }
83 
84  bool operator>(const IPV4Address &other) const {
85  return m_address.s_addr > other.m_address.s_addr;
86  }
87 
88  const struct in_addr Address() const {
89  return m_address;
90  }
91 
92  uint32_t AsInt() const { return m_address.s_addr; }
93 
94  bool IsWildcard() const {
95  return m_address.s_addr == INADDR_ANY;
96  }
97 
98  // copy the address in network byte order to a location. The location
99  // should be at least LENGTH bytes.
100  void Get(uint8_t ptr[LENGTH]) {
101  memcpy(ptr,
102  reinterpret_cast<uint8_t*>(&m_address.s_addr),
103  LENGTH);
104  }
105 
106  std::string ToString() const;
107 
108  friend ostream& operator<< (ostream &out, const IPV4Address &address) {
109  return out << address.ToString();
110  }
111 
112  static IPV4Address* FromString(const std::string &address);
113  static bool FromString(const std::string &address, IPV4Address *target);
114  // useful for testing
115  static IPV4Address FromStringOrDie(const std::string &address);
116 
117  static IPV4Address WildCard() {
118  return IPV4Address(INADDR_ANY);
119  }
120 
121  static IPV4Address Broadcast() {
122  return IPV4Address(INADDR_NONE);
123  }
124 
125  static IPV4Address Loopback();
126 
127  private:
128  struct in_addr m_address;
129 };
130 } // namespace network
131 } // namespace ola
132 #endif // INCLUDE_OLA_NETWORK_IPV4ADDRESS_H_