Open Lighting Architecture  0.9.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #include <ola/win/CleanWinSock2.h>
38 #else
39 #include <sys/socket.h>
40 #endif
41 #include <sstream>
42 #include <string>
43 
44 namespace ola {
45 namespace network {
46 
58  public:
59  virtual ~SocketAddress() {}
60 
61  virtual uint16_t Family() const = 0;
62  virtual bool ToSockAddr(struct sockaddr *addr, unsigned int size) const = 0;
63  virtual std::string ToString() const = 0;
64 
65  friend std::ostream& operator<<(std::ostream &out,
66  const SocketAddress &address) {
67  return out << address.ToString();
68  }
69 };
70 
71 
78  public:
80  : SocketAddress(),
81  m_host(),
82  m_port(0) {
83  }
84 
85  IPV4SocketAddress(const IPV4Address &host, uint16_t port)
86  : SocketAddress(),
87  m_host(host),
88  m_port(port) {
89  }
91  : SocketAddress(),
92  m_host(other.m_host),
93  m_port(other.m_port) {
94  }
95 
96  ~IPV4SocketAddress() {}
97 
98  IPV4SocketAddress& operator=(const IPV4SocketAddress &other) {
99  if (this != &other) {
100  m_host = other.m_host;
101  m_port = other.m_port;
102  }
103  return *this;
104  }
105 
106  bool operator==(const IPV4SocketAddress &other) const {
107  return m_host == other.m_host && m_port == other.m_port;
108  }
109 
110  bool operator!=(const IPV4SocketAddress &other) const {
111  return !(*this == other);
112  }
113 
119  bool operator<(const IPV4SocketAddress &other) const {
120  if (m_host == other.m_host)
121  return m_port < other.m_port;
122  else
123  return m_host < other.m_host;
124  }
125 
131  bool operator>(const IPV4SocketAddress &other) const {
132  if (m_host == other.m_host)
133  return m_port > other.m_port;
134  else
135  return m_host > other.m_host;
136  }
137 
138  uint16_t Family() const { return AF_INET; }
139  const IPV4Address& Host() const { return m_host; }
140  void Host(const IPV4Address &host) { m_host = host; }
141  uint16_t Port() const { return m_port; }
142  void Port(uint16_t port) { m_port = port; }
143 
144  std::string ToString() const;
145 
146  static bool FromString(const std::string &str,
147  IPV4SocketAddress *socket_address);
148 
149  // useful for testing
150  static IPV4SocketAddress FromStringOrDie(const std::string &address);
151 
152  bool ToSockAddr(struct sockaddr *addr, unsigned int size) const;
153 
154  private:
155  IPV4Address m_host;
156  uint16_t m_port;
157 };
158 
159 
166  public:
167  explicit GenericSocketAddress(const struct sockaddr &addr)
168  : m_addr(addr) {
169  }
170 
172  memset(reinterpret_cast<uint8_t*>(&m_addr), 0, sizeof(m_addr));
173  }
174 
175  bool IsValid() const;
176 
177  uint16_t Family() const {
178  return m_addr.sa_family;
179  }
180 
181  GenericSocketAddress& operator=(const GenericSocketAddress &other) {
182  if (this != &other) {
183  memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
184  }
185  return *this;
186  }
187 
188  bool ToSockAddr(struct sockaddr *addr,
189  OLA_UNUSED unsigned int size) const {
190  *addr = m_addr;
191  return true;
192  }
193 
194  std::string ToString() const;
195 
196  // Return a IPV4SocketAddress object, only valid if Family() is AF_INET
197  IPV4SocketAddress V4Addr() const;
198  // Add V6 here as well
199 
200  private:
201  struct sockaddr m_addr;
202 };
206 } // namespace network
207 } // namespace ola
208 #endif // INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_