Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SocketAddress.h
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * SocketAddress.h
17  * Represents a sockaddr structure.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
22 #define INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
23 
24 #include <ola/network/IPV4Address.h>
25 #include <stdint.h>
26 #include <sys/socket.h>
27 #include <sstream>
28 #include <string>
29 
30 namespace ola {
31 namespace network {
32 
33 using std::string;
34 
40  public:
41  virtual ~SocketAddress() {}
42 
43  virtual uint16_t Family() const = 0;
44  virtual bool ToSockAddr(struct sockaddr *addr, unsigned int size) const = 0;
45  virtual string ToString() const = 0;
46 
47  friend ostream& operator<< (ostream &out, const SocketAddress &address) {
48  return out << address.ToString();
49  }
50 };
51 
52 
57  public:
59  : SocketAddress(),
60  m_host(),
61  m_port(0) {
62  }
63 
64  IPV4SocketAddress(const IPV4Address &host, uint16_t port)
65  : SocketAddress(),
66  m_host(host),
67  m_port(port) {
68  }
70  : SocketAddress(),
71  m_host(other.m_host),
72  m_port(other.m_port) {
73  }
74 
75  ~IPV4SocketAddress() {}
76 
77  IPV4SocketAddress& operator=(const IPV4SocketAddress &other) {
78  if (this != &other) {
79  m_host = other.m_host;
80  m_port = other.m_port;
81  }
82  return *this;
83  }
84 
85  bool operator==(const IPV4SocketAddress &other) const {
86  return m_host == other.m_host && m_port == other.m_port;
87  }
88 
89  bool operator!=(const IPV4SocketAddress &other) const {
90  return !(*this == other);
91  }
92 
93  bool operator<(const IPV4SocketAddress &other) const {
94  if (m_host == other.m_host)
95  return m_port < other.m_port;
96  else
97  return m_host < other.m_host;
98  }
99 
100  uint16_t Family() const { return AF_INET; }
101  const IPV4Address& Host() const { return m_host; }
102  void Host(const IPV4Address &host) { m_host = host; }
103  uint16_t Port() const { return m_port; }
104  void Port(uint16_t port) { m_port = port; }
105 
106  string ToString() const {
107  std::ostringstream str;
108  str << Host() << ":" << Port();
109  return str.str();
110  }
111 
112  static bool FromString(const string &str,
113  IPV4SocketAddress *socket_address);
114 
115  // useful for testing
116  static IPV4SocketAddress FromStringOrDie(const std::string &address);
117 
118  bool ToSockAddr(struct sockaddr *addr, unsigned int size) const;
119 
120  private:
121  IPV4Address m_host;
122  uint16_t m_port;
123 };
124 
125 
130  public:
131  explicit GenericSocketAddress(const struct sockaddr &addr)
132  : m_addr(addr) {
133  }
134 
136  memset(reinterpret_cast<uint8_t*>(&m_addr), 0, sizeof(m_addr));
137  }
138 
139  bool IsValid() const {
140  return Family() != AF_UNSPEC;
141  }
142 
143  uint16_t Family() const {
144  return m_addr.sa_family;
145  }
146 
147  GenericSocketAddress& operator=(const GenericSocketAddress &other) {
148  if (this != &other) {
149  memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
150  }
151  return *this;
152  }
153 
154  bool ToSockAddr(struct sockaddr *addr, unsigned int size) const {
155  *addr = m_addr;
156  return true;
157  (void) size;
158  }
159 
160  string ToString() const;
161 
162  // Return a IPV4SocketAddress object, only valid if Family() is AF_INET
163  IPV4SocketAddress V4Addr() const;
164  // Add V6 here as well
165 
166  private:
167  struct sockaddr m_addr;
168 };
169 } // namespace network
170 } // namespace ola
171 #endif // INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_