Open Lighting Architecture  0.9.0
 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., 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 
29 #ifndef INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
30 #define INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
31 
33 #include <stdint.h>
34 #include <sys/socket.h>
35 #include <sstream>
36 #include <string>
37 
38 namespace ola {
39 namespace network {
40 
52  public:
53  virtual ~SocketAddress() {}
54 
55  virtual uint16_t Family() const = 0;
56  virtual bool ToSockAddr(struct sockaddr *addr, unsigned int size) const = 0;
57  virtual std::string ToString() const = 0;
58 
59  friend std::ostream& operator<<(std::ostream &out,
60  const SocketAddress &address) {
61  return out << address.ToString();
62  }
63 };
64 
65 
72  public:
74  : SocketAddress(),
75  m_host(),
76  m_port(0) {
77  }
78 
79  IPV4SocketAddress(const IPV4Address &host, uint16_t port)
80  : SocketAddress(),
81  m_host(host),
82  m_port(port) {
83  }
85  : SocketAddress(),
86  m_host(other.m_host),
87  m_port(other.m_port) {
88  }
89 
90  ~IPV4SocketAddress() {}
91 
92  IPV4SocketAddress& operator=(const IPV4SocketAddress &other) {
93  if (this != &other) {
94  m_host = other.m_host;
95  m_port = other.m_port;
96  }
97  return *this;
98  }
99 
100  bool operator==(const IPV4SocketAddress &other) const {
101  return m_host == other.m_host && m_port == other.m_port;
102  }
103 
104  bool operator!=(const IPV4SocketAddress &other) const {
105  return !(*this == other);
106  }
107 
108  bool operator<(const IPV4SocketAddress &other) const {
109  if (m_host == other.m_host)
110  return m_port < other.m_port;
111  else
112  return m_host < other.m_host;
113  }
114 
115  uint16_t Family() const { return AF_INET; }
116  const IPV4Address& Host() const { return m_host; }
117  void Host(const IPV4Address &host) { m_host = host; }
118  uint16_t Port() const { return m_port; }
119  void Port(uint16_t port) { m_port = port; }
120 
121  std::string ToString() const;
122 
123  static bool FromString(const std::string &str,
124  IPV4SocketAddress *socket_address);
125 
126  // useful for testing
127  static IPV4SocketAddress FromStringOrDie(const std::string &address);
128 
129  bool ToSockAddr(struct sockaddr *addr, unsigned int size) const;
130 
131  private:
132  IPV4Address m_host;
133  uint16_t m_port;
134 };
135 
136 
143  public:
144  explicit GenericSocketAddress(const struct sockaddr &addr)
145  : m_addr(addr) {
146  }
147 
149  memset(reinterpret_cast<uint8_t*>(&m_addr), 0, sizeof(m_addr));
150  }
151 
152  bool IsValid() const;
153 
154  uint16_t Family() const {
155  return m_addr.sa_family;
156  }
157 
158  GenericSocketAddress& operator=(const GenericSocketAddress &other) {
159  if (this != &other) {
160  memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
161  }
162  return *this;
163  }
164 
165  bool ToSockAddr(struct sockaddr *addr, unsigned int size) const {
166  *addr = m_addr;
167  return true;
168  (void) size;
169  }
170 
171  std::string ToString() const;
172 
173  // Return a IPV4SocketAddress object, only valid if Family() is AF_INET
174  IPV4SocketAddress V4Addr() const;
175  // Add V6 here as well
176 
177  private:
178  struct sockaddr m_addr;
179 };
183 } // namespace network
184 } // namespace ola
185 #endif // INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_