Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * Socket.h
17  * The UDP Socket interfaces
18  * Copyright (C) 2005 Simon Newton
19  *
20  * UDPSocket, allows sending and receiving UDP datagrams
21  */
22 
23 #ifndef INCLUDE_OLA_NETWORK_SOCKET_H_
24 #define INCLUDE_OLA_NETWORK_SOCKET_H_
25 
26 #include <stdint.h>
27 
28 #include <ola/Callback.h>
29 #include <ola/base/Macro.h>
30 #include <ola/io/Descriptor.h>
31 #include <ola/io/IOQueue.h>
34 #include <string>
35 
36 namespace ola {
37 namespace network {
38 
46  public:
48  ~UDPSocketInterface() {}
49 
54  virtual bool Init() = 0;
55 
61  virtual bool Bind(const IPV4SocketAddress &endpoint) = 0;
62 
68  virtual bool GetSocketAddress(IPV4SocketAddress *address) const = 0;
69 
74  virtual bool Close() = 0;
75 
76  virtual ola::io::DescriptorHandle ReadDescriptor() const = 0;
77  virtual ola::io::DescriptorHandle WriteDescriptor() const = 0;
78 
89  virtual ssize_t SendTo(const uint8_t *buffer,
90  unsigned int size,
91  const IPV4Address &ip,
92  unsigned short port) const = 0;
93 
101  virtual ssize_t SendTo(const uint8_t *buffer,
102  unsigned int size,
103  const IPV4SocketAddress &dest) const = 0;
104 
118  virtual ssize_t SendTo(ola::io::IOVecInterface *data,
119  const IPV4Address &ip,
120  unsigned short port) const = 0;
121 
132  virtual ssize_t SendTo(ola::io::IOVecInterface *data,
133  const IPV4SocketAddress &dest) const = 0;
134 
142  virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const = 0;
143 
154  virtual bool RecvFrom(
155  uint8_t *buffer,
156  ssize_t *data_read,
157  IPV4Address &source) const = 0; // NOLINT(runtime/references)
158 
170  virtual bool RecvFrom(
171  uint8_t *buffer,
172  ssize_t *data_read,
173  IPV4Address &source, // NOLINT(runtime/references)
174  uint16_t &port) const = 0; // NOLINT(runtime/references)
175 
184  virtual bool RecvFrom(uint8_t *buffer,
185  ssize_t *data_read,
186  IPV4SocketAddress *source) = 0;
187 
192  virtual bool EnableBroadcast() = 0;
193 
198  virtual bool SetMulticastInterface(const IPV4Address &iface) = 0;
199 
207  virtual bool JoinMulticast(const IPV4Address &iface,
208  const IPV4Address &group,
209  bool multicast_loop = false) = 0;
210 
217  virtual bool LeaveMulticast(const IPV4Address &iface,
218  const IPV4Address &group) = 0;
219 
225  virtual bool SetTos(uint8_t tos) = 0;
226 
227  private:
228  DISALLOW_COPY_AND_ASSIGN(UDPSocketInterface);
229 };
230 
231 
232 /*
233  * A UDPSocket (non connected)
234  */
236  public:
237  UDPSocket()
238  : UDPSocketInterface(),
239  m_handle(ola::io::INVALID_DESCRIPTOR),
240  m_bound_to_port(false) {}
241  ~UDPSocket() { Close(); }
242  bool Init();
243  bool Bind(const IPV4SocketAddress &endpoint);
244 
245  bool GetSocketAddress(IPV4SocketAddress *address) const;
246 
247  bool Close();
248  ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
249  ola::io::DescriptorHandle WriteDescriptor() const { return m_handle; }
250  ssize_t SendTo(const uint8_t *buffer,
251  unsigned int size,
252  const IPV4Address &ip,
253  unsigned short port) const;
254  ssize_t SendTo(const uint8_t *buffer,
255  unsigned int size,
256  const IPV4SocketAddress &dest) const;
257  ssize_t SendTo(ola::io::IOVecInterface *data,
258  const IPV4Address &ip,
259  unsigned short port) const;
260  ssize_t SendTo(ola::io::IOVecInterface *data,
261  const IPV4SocketAddress &dest) const;
262 
263  bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const;
264  bool RecvFrom(uint8_t *buffer,
265  ssize_t *data_read,
266  IPV4Address &source) const; // NOLINT(runtime/references)
267  bool RecvFrom(uint8_t *buffer,
268  ssize_t *data_read,
269  IPV4Address &source, // NOLINT(runtime/references)
270  uint16_t &port) const; // NOLINT(runtime/references)
271 
272  bool RecvFrom(uint8_t *buffer,
273  ssize_t *data_read,
274  IPV4SocketAddress *source);
275 
276  bool EnableBroadcast();
277  bool SetMulticastInterface(const IPV4Address &iface);
278  bool JoinMulticast(const IPV4Address &iface,
279  const IPV4Address &group,
280  bool multicast_loop = false);
281  bool LeaveMulticast(const IPV4Address &iface,
282  const IPV4Address &group);
283 
284  bool SetTos(uint8_t tos);
285 
286  private:
287  ola::io::DescriptorHandle m_handle;
288  bool m_bound_to_port;
289 
290  DISALLOW_COPY_AND_ASSIGN(UDPSocket);
291 };
292 } // namespace network
293 } // namespace ola
294 #endif // INCLUDE_OLA_NETWORK_SOCKET_H_
virtual bool LeaveMulticast(const IPV4Address &iface, const IPV4Address &group)=0
Leave a multicast group.
Represents Socket Addresses.
bool EnableBroadcast()
Enable broadcasting for this socket.
Definition: Socket.cpp:345
virtual bool GetSocketAddress(IPV4SocketAddress *address) const =0
Return the local address this socket is bound to.
virtual ssize_t SendTo(const uint8_t *buffer, unsigned int size, const IPV4Address &ip, unsigned short port) const =0
Send data on this UDPSocket.
Definition: Socket.h:235
bool JoinMulticast(const IPV4Address &iface, const IPV4Address &group, bool multicast_loop=false)
Join a multicast group.
Definition: Socket.cpp:387
The interface for UDPSockets.
Definition: Socket.h:45
bool Close()
Close the socket.
Definition: Socket.cpp:171
virtual bool SetMulticastInterface(const IPV4Address &iface)=0
Set the outgoing interface to be used for multicast transmission.
bool SetMulticastInterface(const IPV4Address &iface)
Set the outgoing interface to be used for multicast transmission.
Definition: Socket.cpp:367
virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const =0
Receive data.
virtual bool Init()=0
Initialize the socket.
virtual bool JoinMulticast(const IPV4Address &iface, const IPV4Address &group, bool multicast_loop=false)=0
Join a multicast group.
Represents a IPv4 Address.
Definition: IPV4Address.h:55
bool Bind(const IPV4SocketAddress &endpoint)
Bind this socket to an external address:port.
Definition: Socket.cpp:107
A file descriptor that supports both read & write.
Definition: Descriptor.h:200
ssize_t SendTo(const uint8_t *buffer, unsigned int size, const IPV4Address &ip, unsigned short port) const
Send data on this UDPSocket.
Definition: Socket.cpp:193
virtual ola::io::DescriptorHandle WriteDescriptor() const =0
Returns the write descriptor for this socket.
virtual bool Bind(const IPV4SocketAddress &endpoint)=0
Bind this socket to an external address:port.
virtual ola::io::DescriptorHandle ReadDescriptor() const =0
Returns the read descriptor for this socket.
virtual bool Close()=0
Close the socket.
Definition: IOVecInterface.h:53
Represents an IPv4 Address.
Helper macros.
bool SetTos(uint8_t tos)
Set the tos field for a socket.
Definition: Socket.cpp:450
bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const
Receive data.
Definition: Socket.cpp:282
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
ola::io::DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition: Socket.h:248
bool LeaveMulticast(const IPV4Address &iface, const IPV4Address &group)
Leave a multicast group.
Definition: Socket.cpp:427
virtual bool SetTos(uint8_t tos)=0
Set the tos field for a socket.
An IPv4 SocketAddress.
Definition: SocketAddress.h:77
virtual bool EnableBroadcast()=0
Enable broadcasting for this socket.
bool Init()
Initialize the socket.
Definition: Socket.cpp:84
bool GetSocketAddress(IPV4SocketAddress *address) const
Return the local address this socket is bound to.
Definition: Socket.cpp:158
ola::io::DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition: Socket.h:249