Open Lighting Architecture  Latest Git
SocketAddress.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  * SocketAddress.h
17  * Represents a sockaddr structure.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
29 #ifndef INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
30 #define INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
31 
33 #include <ola/base/Macro.h>
34 #include <stdint.h>
35 #ifdef _WIN32
36 #define VC_EXTRALEAN
37 #define WIN32_LEAN_AND_MEAN
38 #include <ola/win/CleanWinSock2.h>
39 #else
40 #include <sys/socket.h>
41 #endif // _WIN32
42 #include <sstream>
43 #include <string>
44 
45 namespace ola {
46 namespace network {
47 
59  public:
60  virtual ~SocketAddress() {}
61 
62  virtual uint16_t Family() const = 0;
63  virtual bool ToSockAddr(struct sockaddr *addr, unsigned int size) const = 0;
64  virtual std::string ToString() const = 0;
65 
66  friend std::ostream& operator<<(std::ostream &out,
67  const SocketAddress &address) {
68  return out << address.ToString();
69  }
70 };
71 
72 
79  public:
81  : SocketAddress(),
82  m_host(),
83  m_port(0) {
84  }
85 
86  IPV4SocketAddress(const IPV4Address &host, uint16_t port)
87  : SocketAddress(),
88  m_host(host),
89  m_port(port) {
90  }
92  : SocketAddress(),
93  m_host(other.m_host),
94  m_port(other.m_port) {
95  }
96 
97  ~IPV4SocketAddress() {}
98 
99  IPV4SocketAddress& operator=(const IPV4SocketAddress &other) {
100  if (this != &other) {
101  m_host = other.m_host;
102  m_port = other.m_port;
103  }
104  return *this;
105  }
106 
107  bool operator==(const IPV4SocketAddress &other) const {
108  return m_host == other.m_host && m_port == other.m_port;
109  }
110 
111  bool operator!=(const IPV4SocketAddress &other) const {
112  return !(*this == other);
113  }
114 
120  bool operator<(const IPV4SocketAddress &other) const {
121  if (m_host == other.m_host)
122  return m_port < other.m_port;
123  else
124  return m_host < other.m_host;
125  }
126 
132  bool operator>(const IPV4SocketAddress &other) const {
133  if (m_host == other.m_host)
134  return m_port > other.m_port;
135  else
136  return m_host > other.m_host;
137  }
138 
139  uint16_t Family() const { return AF_INET; }
140  const IPV4Address& Host() const { return m_host; }
141  void Host(const IPV4Address &host) { m_host = host; }
142  uint16_t Port() const { return m_port; }
143  void Port(uint16_t port) { m_port = port; }
144 
145  std::string ToString() const;
146 
147  static bool FromString(const std::string &str,
148  IPV4SocketAddress *socket_address);
149 
150  // useful for testing
151  static IPV4SocketAddress FromStringOrDie(const std::string &address);
152 
153  bool ToSockAddr(struct sockaddr *addr, unsigned int size) const;
154 
155  private:
156  IPV4Address m_host;
157  uint16_t m_port;
158 };
159 
160 
167  public:
168  explicit GenericSocketAddress(const struct sockaddr &addr)
169  : m_addr(addr) {
170  }
171 
173  memset(reinterpret_cast<uint8_t*>(&m_addr), 0, sizeof(m_addr));
174  }
175 
177  memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
178  }
179 
180  bool IsValid() const;
181 
182  uint16_t Family() const {
183  return m_addr.sa_family;
184  }
185 
186  GenericSocketAddress& operator=(const GenericSocketAddress &other) {
187  if (this != &other) {
188  memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
189  }
190  return *this;
191  }
192 
193  bool ToSockAddr(struct sockaddr *addr,
194  OLA_UNUSED unsigned int size) const {
195  *addr = m_addr;
196  return true;
197  }
198 
199  std::string ToString() const;
200 
201  // Return a IPV4SocketAddress object, only valid if Family() is AF_INET
202  IPV4SocketAddress V4Addr() const;
203  // Add V6 here as well
204 
205  private:
206  struct sockaddr m_addr;
207 };
211 } // namespace network
212 } // namespace ola
213 #endif // INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
bool operator>(const IPV4SocketAddress &other) const
Greater than operator.
Definition: SocketAddress.h:132
Represents a IPv4 Address.
Definition: IPV4Address.h:55
#define OLA_UNUSED
Mark unused arguments & types.
Definition: Macro.h:62
bool operator<(const IPV4SocketAddress &other) const
Less than operator for partial ordering.
Definition: SocketAddress.h:120
Represents an IPv4 Address.
Helper macros.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
The base SocketAddress.
Definition: SocketAddress.h:58
The base port class.
Definition: Port.h:49
An IPv4 SocketAddress.
Definition: SocketAddress.h:78
a Generic Socket Address
Definition: SocketAddress.h:166