Free (GPLv2) TCP/IP stack developed by TASS Belgium

Dependents:   lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more

PicoTCP. Copyright (c) 2013 TASS Belgium NV.

Released under the GNU General Public License, version 2.

Different licensing models may exist, at the sole discretion of the Copyright holders.

Official homepage: http://www.picotcp.com

Bug tracker: https://github.com/tass-belgium/picotcp/issues

Development steps:

  • initial integration with mbed RTOS
  • generic mbed Ethernet driver
  • high performance NXP LPC1768 specific Ethernet driver
  • Multi-threading support for mbed RTOS
  • Berkeley sockets and integration with the New Socket API
  • Fork of the apps running on top of the New Socket API
  • Scheduling optimizations
  • Debugging/benchmarking/testing

Demo application (measuring TCP sender performance):

Import programlpc1768-picotcp-demo

A PicoTCP demo app testing the ethernet throughput on the lpc1768 mbed board.

Files at this revision

API Documentation at this revision

Comitter:
daniele
Date:
Sat Jun 08 13:48:10 2013 +0000
Parent:
19:c7debad9a20a
Child:
21:909873ea3e67
Commit message:
Refactoring of the socket interface

Changed in this revision

Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Endpoint.cpp	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,65 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
+#include "wrapper.h"
+#include "proxy_endpoint.h"
+
+extern "C"
+{
+#include "pico_ipv4.h"
+}
+#include <cstring>
+#include <cstdio>
+
+Endpoint::Endpoint()  {
+    reset_address();
+}
+Endpoint::~Endpoint() {}
+
+void Endpoint::reset_address(void) {
+    memset(&_remoteHost,0,sizeof(_remoteHost));
+}
+
+#include "stdio.h"
+
+int Endpoint::set_address(const char* host, const int port) {
+    _remoteHost.sin_port = short_be(port);
+    pico_string_to_ipv4(host,&_remoteHost.sin_addr.s_addr);
+    memcpy(_ipAddress,host, strlen(host)+1);
+    return 0;
+}
+
+char* Endpoint::get_address() {
+    return _ipAddress;
+}
+
+int   Endpoint::get_port() {
+    return short_be(_remoteHost.sin_port);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Endpoint.h	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,74 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifndef ENDPOINT_H
+#define ENDPOINT_H
+
+class UDPSocket;
+
+/**
+IP Endpoint (address, port)
+*/
+class Endpoint {
+    friend class UDPSocket;
+
+public:
+    /** IP Endpoint (address, port)
+     */
+    Endpoint(void);
+    
+    ~Endpoint(void);
+    
+    /** Reset the address of this endpoint
+     */
+    void reset_address(void);
+    
+    /** Set the address of this endpoint
+    \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS).
+    \param port The endpoint port
+    \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    int  set_address(const char* host, const int port);
+    
+    /** Get the IP address of this endpoint
+    \return The IP address of this endpoint.
+     */
+    char* get_address(void);
+    
+    /** Get the port of this endpoint
+    \return The port of this endpoint
+     */
+    int get_port(void);
+
+protected:
+    char _ipAddress[17];
+    struct sockaddr_in _remoteHost;
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Socket.cpp	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,99 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "Socket/Socket.h"
+#include "wrapper.h"
+#include "proxy_endpoint.h"
+#include <cstring>
+
+using std::memset;
+
+Socket::Socket() : _ep(NULL), _blocking(true)
+{
+    //set_blocking(true,1500); // ?
+}
+
+void Socket::set_blocking(bool blocking, unsigned int timeout) {
+    _blocking = blocking;
+    _timeout = timeout;
+}
+
+int Socket::init_socket(int type) {
+    if (_ep != NULL)
+    {
+        printf("Sock open already...\n");
+        return -1;
+    }
+    struct stack_endpoint *ep  = picotcp_socket(AF_INET, type, 0);
+    if (!ep)
+    {
+        printf("Error opening socket...\n");
+        return -1;
+    }
+    _ep = ep;
+    return 0;
+}
+
+int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
+    return picotcp_setsockopt(_ep, optname, (void *)optval);
+}
+
+int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
+    return picotcp_getsockopt(_ep, optname, optval);
+}
+
+int Socket::select(struct timeval *timeout, bool read, bool write) {
+    return picotcp_select(_ep, timeout, read, write);
+}
+
+int Socket::wait_readable(TimeInterval& timeout) {
+    return (select(&timeout._time, true, false) == 0 ? -1 : 0);
+}
+
+int Socket::wait_writable(TimeInterval& timeout) {
+    return (select(&timeout._time, false, true) == 0 ? -1 : 0);
+}
+
+int Socket::close() {
+    if (_ep < 0)
+        return -1;
+    
+    picotcp_close(_ep);
+    _ep = NULL;
+    
+    return 0;
+}
+
+Socket::~Socket() {
+    close(); //Don't want to leak
+}
+
+TimeInterval::TimeInterval(unsigned int ms) {
+    _time.tv_sec = ms / 1000;
+    _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Socket.h	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,124 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef SOCKET_H_
+#define SOCKET_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+    #include "pico_dns_client.h"
+#ifdef __cplusplus
+}
+#endif
+
+#include "wrapper.h"
+#include "proxy_endpoint.h"
+
+class TimeInterval;
+
+/** Socket file descriptor and select wrapper
+  */
+class Socket {
+public:
+    /** Socket
+     */
+    Socket();
+    
+    /** Set blocking or non-blocking mode of the socket and a timeout on
+        blocking socket operations
+    \param blocking  true for blocking mode, false for non-blocking mode.
+    \param timeout   timeout in ms [Default: (1500)ms].
+    */
+    void set_blocking(bool blocking, unsigned int timeout=1500);
+    
+    /** Set socket options
+    \param level     stack level (see: lwip/sockets.h)
+    \param optname   option ID
+    \param optval    option value
+    \param socklen_t length of the option value
+    \return 0 on success, -1 on failure
+    */
+    int set_option(int level, int optname, const void *optval, socklen_t optlen);
+    
+    /** Get socket options
+        \param level     stack level (see: lwip/sockets.h)
+        \param optname   option ID
+        \param optval    buffer pointer where to write the option value
+        \param socklen_t length of the option value
+        \return 0 on success, -1 on failure
+        */
+    int get_option(int level, int optname, void *optval, socklen_t *optlen);
+    
+    /** Close the socket file descriptor
+     */
+    int close();
+    
+    ~Socket();
+    
+protected:
+    struct stack_endpoint *_ep;
+    int init_socket(int type);
+    
+    int wait_readable(TimeInterval& timeout);
+    int wait_writable(TimeInterval& timeout);
+    
+    bool _blocking;
+    unsigned int _timeout;
+    
+private:
+    int select(struct timeval *timeout, bool read, bool write);
+};
+
+/** Time interval class used to specify timeouts
+ */
+class TimeInterval {
+    friend class Socket;
+
+public:
+    /** Time Interval
+     \param ms time interval expressed in milliseconds
+      */
+    TimeInterval(unsigned int ms);
+    
+private:
+    struct timeval _time;
+    struct stack_endpoint * _ep;
+};
+
+ 
+inline struct hostent *gethostbyname(const char *name) {
+  return picotcp_gethostbyname(name);
+   
+}
+/* DNS 
+inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) {
+  return picotcp_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
+}*/
+#endif /* SOCKET_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketConnection.h	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,92 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef TCPSOCKET_H
+#define TCPSOCKET_H
+
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
+
+/**
+TCP socket connection
+*/
+class TCPSocketConnection : public Socket, public Endpoint {
+    friend class TCPSocketServer;
+    
+public:
+    /** TCP socket connection
+    */
+    TCPSocketConnection();
+    
+    /** Connects this TCP socket to the server
+    \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS.
+    \param port The host's port to connect to.
+    \return 0 on success, -1 on failure.
+    */
+    int connect(const char* host, const int port);
+    
+    /** Check if the socket is connected
+    \return true if connected, false otherwise.
+    */
+    bool is_connected(void);
+    
+    /** Send data to the remote host.
+    \param data The buffer to send to the host.
+    \param length The length of the buffer to send.
+    \return the number of written bytes on success (>=0) or -1 on failure
+     */
+    int send(char* data, int length);
+    
+    /** Send all the data to the remote host.
+    \param data The buffer to send to the host.
+    \param length The length of the buffer to send.
+    \return the number of written bytes on success (>=0) or -1 on failure
+    */
+    int send_all(char* data, int length);
+    
+    /** Receive data from the remote host.
+    \param data The buffer in which to store the data received from the host.
+    \param length The maximum length of the buffer.
+    \return the number of received bytes on success (>=0) or -1 on failure
+     */
+    int receive(char* data, int length);
+    
+    /** Receive all the data from the remote host.
+    \param data The buffer in which to store the data received from the host.
+    \param length The maximum length of the buffer.
+    \return the number of received bytes on success (>=0) or -1 on failure
+    */
+    int receive_all(char* data, int length);
+
+private:
+    bool _is_connected;
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketServer.cpp	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,91 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "TCPSocketServer.h"
+#include "wrapper.h"
+
+#include <cstring>
+
+using std::memset;
+using std::memcpy;
+
+TCPSocketServer::TCPSocketServer() {
+        
+}
+
+int TCPSocketServer::bind(int port) {
+    if (init_socket(SOCK_STREAM) < 0)
+        return -1;
+    
+    struct sockaddr_in localHost;
+    memset(&localHost, 0, sizeof(localHost));
+    
+    localHost.sin_family = AF_INET;
+    localHost.sin_port = short_be(port);
+    localHost.sin_addr.s_addr = INADDR_ANY;
+    
+    if (picotcp_bind(_ep, (struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
+        close();
+        return -1;
+    }
+    
+    return 0;
+}
+
+int TCPSocketServer::listen(int max) {
+    if (_ep < 0)
+        return -1;
+    
+    if (picotcp_listen(_ep, max) < 0) {
+        close();
+        return -1;
+    }
+    
+    return 0;
+}
+
+int TCPSocketServer::accept(TCPSocketConnection& connection) {
+    if (_ep < 0)
+        return -1;
+    
+    if (!_blocking) {
+        printf("Not blocking...\n");
+        TimeInterval timeout(_timeout);
+        if (wait_readable(timeout) != 0)
+            return -1;
+    }
+    connection.reset_address();
+    socklen_t newSockRemoteHostLen = sizeof(connection._remoteHost);
+    connection._ep = picotcp_accept(_ep, (struct sockaddr*) &connection._remoteHost, &newSockRemoteHostLen);
+    if (connection._ep->retval < 0) {
+        return -1; //Accept failed
+    }
+    connection.set_blocking(true,1500);
+    connection._is_connected = true;
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketServer.h	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,64 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifndef TCPSOCKETSERVER_H
+#define TCPSOCKETSERVER_H
+
+#include "Socket/Socket.h"
+#include "wrapper.h"
+#include "TCPSocketConnection.h"
+
+/** TCP Server.
+  */
+class TCPSocketServer : public Socket {
+  public:
+    /** Instantiate a TCP Server.
+    */
+    TCPSocketServer();
+    
+    /** Bind a socket to a specific port.
+    \param port The port to listen for incoming connections on.
+    \return 0 on success, -1 on failure.
+    */
+    int bind(int port);
+    
+    /** Start listening for incoming connections.
+    \param backlog number of pending connections that can be queued up at any
+                   one time [Default: 1].
+    \return 0 on success, -1 on failure.
+    */
+    int listen(int backlog=1);
+    
+    /** Accept a new connection.
+    \param connection A TCPSocketConnection instance that will handle the incoming connection.
+    \return 0 on success, -1 on failure.
+    */
+    int accept(TCPSocketConnection& connection);
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/UDPSocket.cpp	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,106 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "Socket/UDPSocket.h"
+#include "wrapper.h"
+#include "proxy_endpoint.h"
+
+#include <cstring>
+
+using std::memset;
+
+UDPSocket::UDPSocket() {
+}
+
+int UDPSocket::init(void) {
+    return init_socket(SOCK_DGRAM);
+}
+
+// Server initialization
+int UDPSocket::bind(int port) {
+    if (init_socket(SOCK_DGRAM) < 0)
+        return -1;
+    
+    struct sockaddr_in localHost; 
+    std::memset(&localHost, 0, sizeof(localHost));
+    
+    localHost.sin_family = AF_INET;
+    localHost.sin_port = short_be(port);
+    localHost.sin_addr.s_addr = INADDR_ANY;
+    
+    if (picotcp_bind(_ep, (struct sockaddr *) &localHost, (socklen_t)sizeof(localHost)) < 0) {
+        close();
+        return -1;
+    }
+    
+    return 0;
+}
+
+int UDPSocket::join_multicast_group(EthernetInterface& eth, const char* address) {
+    
+    return picotcp_join_multicast(_ep,address,eth.getIPAddress());
+}
+
+int UDPSocket::set_broadcasting(void) {
+    int option = 1;
+    return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
+}
+
+// -1 if unsuccessful, else number of bytes written
+int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
+    if (_ep < 0)
+    {
+        printf("Error on socket descriptor \n");
+        return -1;
+    }
+    
+    /* This is not needed for our udp sock ?
+    if (_blocking) {
+        TimeInterval timeout(_timeout);
+        if (wait_writable(timeout) != 0)
+            return 0;
+    }*/
+    
+    return picotcp_sendto(_ep, packet, length, (struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
+}
+
+// -1 if unsuccessful, else number of bytes received
+int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
+    if (_ep < 0)
+        return -1;
+    
+    if (!_blocking) {
+        TimeInterval timeout(_timeout);
+        if (wait_readable(timeout) != 0)
+            return 0;
+    }
+    //remote.reset_address(); why is this needed ?
+    socklen_t remoteHostLen = sizeof(remote._remoteHost);
+    return picotcp_recvfrom(_ep, buffer, length, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/UDPSocket.h	Sat Jun 08 13:48:10 2013 +0000
@@ -0,0 +1,88 @@
+/* 
+ *
+ * PicoTCP Socket interface for mbed.
+ * Copyright (C) 2013 TASS Belgium NV
+ * 
+ * Released under GPL v2
+ *
+ * Other licensing models might apply at the sole discretion of the copyright holders.
+ *
+ *
+ * This software is based on the mbed.org EthernetInterface implementation:
+ * Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef UDPSOCKET_H
+#define UDPSOCKET_H
+
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
+#include "EthernetInterface.h"
+#include <cstdint>
+
+/**
+UDP Socket
+*/
+class UDPSocket : public Socket {
+
+public:
+    /** Instantiate an UDP Socket.
+    */
+    UDPSocket();
+    
+    /** Init the UDP Client Socket without binding it to any specific port
+    \return 0 on success, -1 on failure.
+    */
+    int init(void);
+    
+    /** Bind a UDP Server Socket to a specific port
+    \param port The port to listen for incoming connections on
+    \return 0 on success, -1 on failure.
+    */
+    int bind(int port);
+    
+    /** Join the multicast group at the given address
+    \param address  The address of the multicast group
+    \return 0 on success, -1 on failure.
+    */
+    int join_multicast_group(EthernetInterface& eth, const char* address);
+    
+    /** Set the socket in broadcasting mode
+    \return 0 on success, -1 on failure.
+    */
+    int set_broadcasting(void);
+    
+    /** Send a packet to a remote endpoint
+    \param remote   The remote endpoint
+    \param packet   The packet to be sent
+    \param length   The length of the packet to be sent
+    \return the number of written bytes on success (>=0) or -1 on failure
+    */
+    int sendTo(Endpoint &remote, char *packet, int length);
+    
+    /** Receive a packet from a remote endpoint
+    \param remote   The remote endpoint
+    \param buffer   The buffer for storing the incoming packet data. If a packet
+           is too long to fit in the supplied buffer, excess bytes are discarded
+    \param length   The length of the buffer
+    \return the number of received bytes on success (>=0) or -1 on failure
+    */
+    int receiveFrom(Endpoint &remote, char *buffer, int length);
+};
+
+#endif