Open Lighting Architecture  0.9.1
 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 
113  bool operator<(const IPV4SocketAddress &other) const {
114  if (m_host == other.m_host)
115  return m_port < other.m_port;
116  else
117  return m_host < other.m_host;
118  }
119 
120  uint16_t Family() const { return AF_INET; }
121  const IPV4Address& Host() const { return m_host; }
122  void Host(const IPV4Address &host) { m_host = host; }
123  uint16_t Port() const { return m_port; }
124  void Port(uint16_t port) { m_port = port; }
125 
126  std::string ToString() const;
127 
128  static bool FromString(const std::string &str,
129  IPV4SocketAddress *socket_address);
130 
131  // useful for testing
132  static IPV4SocketAddress FromStringOrDie(const std::string &address);
133 
134  bool ToSockAddr(struct sockaddr *addr, unsigned int size) const;
135 
136  private:
137  IPV4Address m_host;
138  uint16_t m_port;
139 };
140 
141 
148  public:
149  explicit GenericSocketAddress(const struct sockaddr &addr)
150  : m_addr(addr) {
151  }
152 
154  memset(reinterpret_cast<uint8_t*>(&m_addr), 0, sizeof(m_addr));
155  }
156 
157  bool IsValid() const;
158 
159  uint16_t Family() const {
160  return m_addr.sa_family;
161  }
162 
163  GenericSocketAddress& operator=(const GenericSocketAddress &other) {
164  if (this != &other) {
165  memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
166  }
167  return *this;
168  }
169 
170  bool ToSockAddr(struct sockaddr *addr, unsigned int size) const {
171  *addr = m_addr;
172  return true;
173  (void) size;
174  }
175 
176  std::string ToString() const;
177 
178  // Return a IPV4SocketAddress object, only valid if Family() is AF_INET
179  IPV4SocketAddress V4Addr() const;
180  // Add V6 here as well
181 
182  private:
183  struct sockaddr m_addr;
184 };
188 } // namespace network
189 } // namespace ola
190 #endif // INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_