Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Socket.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  * Socket.h
17  * The Socket interfaces
18  * Copyright (C) 2005-2009 Simon Newton
19  *
20  * - UDPSocket, allows sending and receiving UDP datagrams
21  * - TCPSocket, this represents a TCP connection to a remote endpoint
22  *
23  * AcceptingSocket is the interface that defines sockets which can spawn new
24  * ConnectedDescriptors. TCPAcceptingSocket is the only subclass and provides
25  * the accept() functionality.
26  */
27 
28 #ifndef INCLUDE_OLA_NETWORK_SOCKET_H_
29 #define INCLUDE_OLA_NETWORK_SOCKET_H_
30 
31 #include <stdint.h>
32 
33 #ifdef WIN32
34 #include <winsock2.h>
35 #include <ws2tcpip.h>
36 #else
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #endif
40 
41 #include <ola/Callback.h>
42 #include <ola/io/Descriptor.h>
43 #include <ola/io/IOQueue.h>
44 #include <ola/network/IPV4Address.h>
45 #include <ola/network/SocketAddress.h>
46 #include <string>
47 
48 
49 namespace ola {
50 namespace network {
51 
52 
53 /*
54  * The UDPSocketInterface.
55  * This is done as an Interface so we can mock it out for testing.
56  */
58  public:
60  ~UDPSocketInterface() {}
61  virtual bool Init() = 0;
62  virtual bool Bind(const IPV4SocketAddress &endpoint) = 0;
63 
64  // Deprecated. Do not use in new code.
65  bool Bind(const IPV4Address &ip, unsigned short port) {
66  return Bind(IPV4SocketAddress(ip, port));
67  }
68 
69  virtual bool GetSocketAddress(IPV4SocketAddress *address) const = 0;
70 
71  virtual bool Close() = 0;
72  virtual int ReadDescriptor() const = 0;
73  virtual int WriteDescriptor() const = 0;
74 
75  virtual ssize_t SendTo(const uint8_t *buffer,
76  unsigned int size,
77  const IPV4Address &ip,
78  unsigned short port) const = 0;
79  virtual ssize_t SendTo(const uint8_t *buffer,
80  unsigned int size,
81  const IPV4SocketAddress &dest) const = 0;
82  virtual ssize_t SendTo(ola::io::IOVecInterface *data,
83  const IPV4Address &ip,
84  unsigned short port) const = 0;
85  virtual ssize_t SendTo(ola::io::IOVecInterface *data,
86  const IPV4SocketAddress &dest) const = 0;
87 
88  virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const = 0;
89  virtual bool RecvFrom(uint8_t *buffer,
90  ssize_t *data_read,
91  IPV4Address &source) const = 0;
92  virtual bool RecvFrom(uint8_t *buffer,
93  ssize_t *data_read,
94  IPV4Address &source,
95  uint16_t &port) const = 0;
96 
97  virtual bool EnableBroadcast() = 0;
98  virtual bool SetMulticastInterface(const IPV4Address &iface) = 0;
99  virtual bool JoinMulticast(const IPV4Address &iface,
100  const IPV4Address &group,
101  bool loop = false) = 0;
102  virtual bool LeaveMulticast(const IPV4Address &iface,
103  const IPV4Address &group) = 0;
104  virtual bool SetTos(uint8_t tos) = 0;
105 
106  private:
108  UDPSocketInterface& operator=(const UDPSocketInterface &other);
109 };
110 
111 
112 /*
113  * A UDPSocket (non connected)
114  */
116  public:
118  m_fd(ola::io::INVALID_DESCRIPTOR),
119  m_bound_to_port(false) {}
120  ~UDPSocket() { Close(); }
121  bool Init();
122  bool Bind(const IPV4SocketAddress &endpoint);
123 
124  bool GetSocketAddress(IPV4SocketAddress *address) const;
125 
126  bool Close();
127  int ReadDescriptor() const { return m_fd; }
128  int WriteDescriptor() const { return m_fd; }
129  ssize_t SendTo(const uint8_t *buffer,
130  unsigned int size,
131  const IPV4Address &ip,
132  unsigned short port) const;
133  ssize_t SendTo(const uint8_t *buffer,
134  unsigned int size,
135  const IPV4SocketAddress &dest) const {
136  return SendTo(buffer, size, dest.Host(), dest.Port());
137  }
138  ssize_t SendTo(ola::io::IOVecInterface *data,
139  const IPV4Address &ip,
140  unsigned short port) const;
141  ssize_t SendTo(ola::io::IOVecInterface *data,
142  const IPV4SocketAddress &dest) const {
143  return SendTo(data, dest.Host(), dest.Port());
144  }
145 
146  bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const;
147  bool RecvFrom(uint8_t *buffer,
148  ssize_t *data_read,
149  IPV4Address &source) const;
150  bool RecvFrom(uint8_t *buffer,
151  ssize_t *data_read,
152  IPV4Address &source,
153  uint16_t &port) const;
154 
155  bool EnableBroadcast();
156  bool SetMulticastInterface(const IPV4Address &iface);
157  bool JoinMulticast(const IPV4Address &iface,
158  const IPV4Address &group,
159  bool loop = false);
160  bool LeaveMulticast(const IPV4Address &iface,
161  const IPV4Address &group);
162 
163  bool SetTos(uint8_t tos);
164 
165  private:
166  int m_fd;
167  bool m_bound_to_port;
168  UDPSocket(const UDPSocket &other);
169  UDPSocket& operator=(const UDPSocket &other);
170  bool _RecvFrom(uint8_t *buffer,
171  ssize_t *data_read,
172  struct sockaddr_in *source,
173  socklen_t *src_size) const;
174 };
175 } // namespace network
176 } // namespace ola
177 #endif // INCLUDE_OLA_NETWORK_SOCKET_H_