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