Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
176  bool IsValid() const;
177 
178  uint16_t Family() const {
179  return m_addr.sa_family;
180  }
181 
182  GenericSocketAddress& operator=(const GenericSocketAddress &other) {
183  if (this != &other) {
184  memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
185  }
186  return *this;
187  }
188 
189  bool ToSockAddr(struct sockaddr *addr,
190  OLA_UNUSED unsigned int size) const {
191  *addr = m_addr;
192  return true;
193  }
194 
195  std::string ToString() const;
196 
197  // Return a IPV4SocketAddress object, only valid if Family() is AF_INET
198  IPV4SocketAddress V4Addr() const;
199  // Add V6 here as well
200 
201  private:
202  struct sockaddr m_addr;
203 };
207 } // namespace network
208 } // namespace ola
209 #endif // INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
IPV4SocketAddress V4Addr() const
Definition: SocketAddress.cpp:121
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.
bool ToSockAddr(struct sockaddr *addr, unsigned int size) const
Definition: SocketAddress.cpp:55
static bool FromString(const std::string &str, IPV4SocketAddress *socket_address)
Definition: SocketAddress.cpp:74
bool operator>(const IPV4SocketAddress &other) const
Greater than operator.
Definition: SocketAddress.h:132
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
The base SocketAddress.
Definition: SocketAddress.h:58
An IPv4 SocketAddress.
Definition: SocketAddress.h:78
a Generic Socket Address
Definition: SocketAddress.h:166