Modified wrapper for UDP socket allowing broadcast packets using the new network stack.

Files at this revision

API Documentation at this revision

Comitter:
innovodesign
Date:
Fri Jan 04 18:09:53 2013 +0000
Commit message:
Modified UDP socket wrapper for new network stack, allowing broadcast packets.

Changed in this revision

udpBroadcastSocket.cpp Show annotated file Show diff for this revision Revisions of this file
udpBroadcastSocket.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/udpBroadcastSocket.cpp	Fri Jan 04 18:09:53 2013 +0000
@@ -0,0 +1,43 @@
+/*
+ * UDPBroadcastSocket accept socket option flags parameter on bind or init
+ * usage 
+ *    mySocket.init(SO_BROADCAST); (not tested)
+ *   or mySocket.bind(port,SO_BROADCAST); {tested, ok)
+
+ * UDPBroadcastSocket accept socket option flags
+ * These option flags per-socket from lwip\core\include\lwip\sockets.h
+ */
+//#define  SO_DEBUG       0x0001 /* Unimplemented: turn on debugging info recording */
+//#define  SO_ACCEPTCONN  0x0002 /* socket has had listen() */
+//#define  SO_REUSEADDR   0x0004 /* Allow local address reuse */
+//#define  SO_KEEPALIVE   0x0008 /* keep connections alive */
+//#define  SO_DONTROUTE   0x0010 /* Unimplemented: just use interface addresses */
+//#define  SO_BROADCAST   0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
+//#define  SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
+//#define  SO_LINGER      0x0080 /* linger on close if data present */
+//#define  SO_OOBINLINE   0x0100 /* Unimplemented: leave received OOB data in line */
+//#define  SO_REUSEPORT   0x0200 /* Unimplemented: allow local address & port reuse */
+
+#include "udpBroadcastSocket.h"
+#include "Socket/UDPSocket.h"
+
+#include <cstring>
+
+using std::memset;
+
+int UDPBroadcastSocket::init(int optionFlags) {
+    int option = 1;
+    if (init_socket(SOCK_DGRAM)<0)
+        return -1;
+    else
+        return lwip_setsockopt(_sock_fd,SOL_SOCKET,optionFlags,(char*)&option,sizeof(option));
+}
+
+int UDPBroadcastSocket::bind(int port,int optionFlags)
+{
+    int option = 1;
+    if (UDPSocket::bind(port)<0)
+        return -1;
+    else
+        return lwip_setsockopt(_sock_fd,SOL_SOCKET,optionFlags,(char*)&option,sizeof(option));
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/udpBroadcastSocket.h	Fri Jan 04 18:09:53 2013 +0000
@@ -0,0 +1,20 @@
+#ifndef UDPBROADCASTSOCKET_H
+#define UDPBROADCASTSOCKET_H
+
+#include "Socket/UDPSocket.h"
+#include <cstdint>
+
+/**
+UDP Broadcast Socket
+    // Allows one-to-many communication over ethernet
+    // Override an UDP Socket with additional flag
+    // when initing or binding a port use init(SO_BROADCAST) or bind(port,SO_BROADCAST) respectively.
+    // Note multicasting many be more suitable for some applications, but isn't support by this socket.
+*/
+class UDPBroadcastSocket : public UDPSocket {
+
+public:
+    int init(int optionFlags); 
+    int bind(int port,int optionFlags);
+};
+#endif
\ No newline at end of file