A new object oriented network api that can be used to replace the one provided by the EthernetInterface library.

Dependents:   NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more

Object oriented network interface for the mbed platform

Currently implemented:

  • Address
  • Endpoint
  • UDP Socket
  • TCP Socket
  • Databuffer
  • Select API

It depends on the EthernetInterface for the lwip network stack.

Please do not hesitate to contact me with any remarks, improvements or questions.

The API is also available for unix at GitHub: LibNosa

Examples

Files at this revision

API Documentation at this revision

Comitter:
NegativeBlack
Date:
Wed Jul 18 11:22:37 2012 +0000
Parent:
2:e283a0062097
Child:
4:d854fa394f85
Commit message:
Implemented UDP and TCP socket. UDP is fully tested, TCP still needs to be tested.

Changed in this revision

buffer.cpp Show annotated file Show diff for this revision Revisions of this file
buffer.hpp Show annotated file Show diff for this revision Revisions of this file
ip/address.cpp Show annotated file Show diff for this revision Revisions of this file
ip/address.hpp Show annotated file Show diff for this revision Revisions of this file
ip/endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
ip/endpoint.hpp Show annotated file Show diff for this revision Revisions of this file
select.cpp Show annotated file Show diff for this revision Revisions of this file
select.hpp Show annotated file Show diff for this revision Revisions of this file
socket.cpp Show annotated file Show diff for this revision Revisions of this file
socket.hpp Show annotated file Show diff for this revision Revisions of this file
tcp/socket.cpp Show annotated file Show diff for this revision Revisions of this file
tcp/socket.hpp Show annotated file Show diff for this revision Revisions of this file
udp/socket.cpp Show annotated file Show diff for this revision Revisions of this file
udp/socket.hpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buffer.hpp	Wed Jul 18 11:22:37 2012 +0000
@@ -0,0 +1,58 @@
+/**
+ * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _NETWORK_BUFFER_HPP_
+#define _NETWORK_BUFFER_HPP_
+
+#include "
+
+namespace network {
+
+    class Buffer
+    {
+        protected:
+            unsigned char *_memory;
+            size_t _bytes_allocated;
+            size_t _bytes_used;
+            
+        public:
+            Buffer();
+            explicit Buffer(const size_t size);
+        
+            ~Buffer();
+            
+            int read(void *data, size_t max_length);
+            int write(const void *data, size_t length);
+            
+            void *getPointer(bool offset = false);
+            
+            size_t getUsed();
+            size_t getFree();
+            size_t getTotal();
+    };
+
+} // namespace network
+
+#endif // _NETWORK_UDP_SOCKET_HPP_
\ No newline at end of file
--- a/ip/address.cpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/ip/address.cpp	Wed Jul 18 11:22:37 2012 +0000
@@ -72,10 +72,10 @@
 {
     // Decode the ASCI string into integer values.
     int result = std::sscanf(address.c_str(), "%3u.%3u.%3u.%3u",
-        (int *)&this->_address[3],
-        (int *)&this->_address[2],
-        (int *)&this->_address[1],
-        (int *)&this->_address[0]);
+        &this->_address[0],
+        &this->_address[1],
+        &this->_address[2],
+        &this->_address[3]);
 
     // Check if all four fields got set.
     if (result != 4) {
@@ -92,12 +92,12 @@
     
     // Encode integer fields into the ASCI string.
     int result = std::sprintf(address, "%u.%u.%u.%u",
-        (unsigned int)this->_address[3],
-        (unsigned int)this->_address[2],
-        (unsigned int)this->_address[1],
-        (unsigned int)this->_address[0]);
+        (int)this->_address[0],
+        (int)this->_address[1],
+        (int)this->_address[2],
+        (int)this->_address[3]);
 
-    // Check if atleast 8 and at maximum 15 bytes got written.
+    // Check if atleast 8 and at maximum 15 bytes has been written.
     if (result < 8 || result > 16) {
         return std::string("0.0.0.0");
     }
@@ -119,22 +119,32 @@
 int
 Address::fromHostname(const std::string &hostname)
 {
+    // Resolve hostname by DNS
     struct hostent *address = ::gethostbyname(hostname.c_str());
     if (address == NULL) {
         return -1;
     }
     
-    std::memcpy(this->_address, address->h_addr_list[0], sizeof(this->_address));
+    // Check if the address has the correct size
+    if (address->h_length != sizeof(this->_address)) {
+        return -1;
+    }
+    
+    // Copy network address
+    this->_address[0] = address->h_addr_list[0][0];
+    this->_address[1] = address->h_addr_list[0][1];
+    this->_address[2] = address->h_addr_list[0][2];
+    this->_address[3] = address->h_addr_list[0][3];
+    
     // Todo: Free hostent structure? I have no idea...
-    
     return 0;
 }
 
 void
-Address::fromNative(const int address)
+Address::fromNative(int address)
 {
-    // Todo: Check byteorder
-    std::memcpy(&this->_address, &address, sizeof(address));
+    // Copy address
+    std::memcpy(this->_address, &address, sizeof(this->_address));
 }
 
 int
@@ -142,12 +152,21 @@
 {
     int address;
     
-    // Todo: Check byteorder
-    std::memcpy(&address, &this->_address, sizeof(address));
+    // Copy address
+    std::memcpy(&address, this->_address, sizeof(address));
     
     return address;
 }
 
+bool
+Address::isEmpty()
+{
+    return ((this->_address[0] == 0) &&
+            (this->_address[1] == 0) &&
+            (this->_address[2] == 0) &&
+            (this->_address[3] == 0));
+}
+
 Address &
 Address::operator=(const Address &other) {
     std::memcpy(&this->_address, &other._address, sizeof(this->_address));
--- a/ip/address.hpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/ip/address.hpp	Wed Jul 18 11:22:37 2012 +0000
@@ -39,7 +39,7 @@
     {
         public:
             static const int Any       = (int)0x00000000;
-            static const int Loopback  = (int)0x7f000001;
+            static const int Loopback  = (int)0x0100007f;
             static const int Broadcast = (int)0xffffffff;
     
         protected:
@@ -59,9 +59,11 @@
             int fromHostname(const char *hostname);
             int fromHostname(const std::string &hostname);
             
-            void fromNative(const int address);
+            void fromNative(int address);
             int toNative();
             
+            bool isEmpty();
+            
             Address &operator=(const Address &other);
             
             bool operator==(const Address &other);
--- a/ip/endpoint.cpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/ip/endpoint.cpp	Wed Jul 18 11:22:37 2012 +0000
@@ -103,3 +103,11 @@
     this->_address.fromNative(endpoint->sin_addr.s_addr);
     return 0;
 }
+
+Endpoint &
+Endpoint::operator=(const Endpoint &other) {
+    this->_address = other._address;
+    this->_port = other._port;
+    
+    return (*this);
+}
\ No newline at end of file
--- a/ip/endpoint.hpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/ip/endpoint.hpp	Wed Jul 18 11:22:37 2012 +0000
@@ -54,6 +54,8 @@
             
             int toNative(struct sockaddr_in *endpoint);
             int fromNative(struct sockaddr_in *endpoint);
+            
+            Endpoint &operator=(const Endpoint &other);
     };
 
 } // namespace ip
--- a/socket.cpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/socket.cpp	Wed Jul 18 11:22:37 2012 +0000
@@ -39,25 +39,59 @@
 }
 
 int
+Socket::open()
+{
+    return -1;
+}
+
+int
 Socket::close()
 {
     if (this->_status == Socket::Closed) {
         return -1;
     }
     
+    // Close the socket
     int result = ::close(this->_socket);
     this->_socket = -1;
     
+    // Update status and return
+    this->_status = Socket::Closed;
     return result;
 }
 
-const ip::Endpoint &
+int
+Socket::bind(int port)
+{
+    // Check socket status
+    if (this->_status != Socket::Open) {
+        return -1;
+    }
+    
+    // Update local endpoint and create native
+    struct sockaddr_in native_endpoint;
+    this->_local_endpoint.setAddress(ip::Address(ip::Address::Any));
+    this->_local_endpoint.setPort(port);
+    this->_local_endpoint.toNative(&native_endpoint);
+    
+    // Bind socket to endpoint
+    if (::bind(this->_socket,
+        (const struct sockaddr *)&native_endpoint,
+        sizeof(native_endpoint)) < 0)
+    {
+        return -1;
+    }
+
+    return 0;
+}
+
+ip::Endpoint &
 Socket::getRemoteEndpoint()
 {
     return this->_remote_endpoint;
 }
 
-const ip::Endpoint &
+ip::Endpoint &
 Socket::getLocalEndpoint()
 {
     return this->_local_endpoint;
--- a/socket.hpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/socket.hpp	Wed Jul 18 11:22:37 2012 +0000
@@ -26,6 +26,9 @@
 #ifndef _NETWORK_SOCKET_HPP_
 #define _NETWORK_SOCKET_HPP_
 
+#include <cstring>
+
+#include "mbed.h"
 #include "bsd_socket.h"
 #include "ip/endpoint.hpp"
 
@@ -56,12 +59,12 @@
             Socket();
             virtual ~Socket();
             
-            virtual int open() =0;
+            virtual int open();
             virtual int close();
-            virtual int bind(int port) =0;
+            virtual int bind(int port);
             
-            const ip::Endpoint &getRemoteEndpoint();
-            const ip::Endpoint &getLocalEndpoint();
+            ip::Endpoint &getRemoteEndpoint();
+            ip::Endpoint &getLocalEndpoint();
             
             int getHandle();
             enum Status getStatus();
--- a/tcp/socket.cpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/tcp/socket.cpp	Wed Jul 18 11:22:37 2012 +0000
@@ -0,0 +1,219 @@
+/**
+ * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "socket.hpp"
+using namespace network::tcp;
+
+int
+Socket::open()
+{
+    // Check socket status
+    if (this->_status != Socket::Closed) {
+        return -1;
+    }
+    
+    // Open socket
+    this->_socket = ::socket(AF_INET, SOCK_STREAM, 0);
+    if (this->_socket < 0) {
+        return -1;
+    }
+    
+    // Update status and return
+    this->_status = Socket::Open;
+    return 0;
+}
+
+int
+Socket::connect(ip::Address &address, int port)
+{
+    ip::Endpoint endpoint(address, port);
+    return this->connect(endpoint);
+}
+
+int
+Socket::connect(ip::Endpoint &endpoint)
+{
+    // Check socket status
+    if ((this->_status != Socket::Open) ||
+        (this->_status != Socket::Disconnected))
+    {
+        return -1;
+    }
+    
+    // Create native endpoint
+    struct sockaddr_in native_endpoint;
+    endpoint.toNative(&native_endpoint);
+    
+    // Attempt to connect with remote endpoint.
+    int result = ::connect(this->_socket,
+        (const struct sockaddr *)&native_endpoint, sizeof(native_endpoint));
+    
+    // Check result
+    if (result < 0) {
+        return -1;
+    }
+    
+    // Update remote endpoint information.
+    this->_remote_endpoint = endpoint;
+    
+    // Update status and return
+    this->_status = Socket::Connected;
+    return 0;
+}
+
+int
+Socket::shutdown()
+{
+    // Check socket status
+    if (this->_status != Socket::Connected) {
+        return -1;
+    }
+    
+    // Attempt to shutdown the connection.
+    int result = ::shutdown(this->_socket, SHUT_RDWR);
+    if (result < 0) {
+        return -1;
+    }
+    
+    // Update status and return
+    this->_status = Socket::Disconnected;
+    return 0;
+}
+
+int
+Socket::listen(int max_pending)
+{
+    // Check socket status
+    if (this->_status != Socket::Open) {
+        return -1;
+    }
+    
+    // Put socket into listening mode.
+    int result = ::listen(this->_socket, max_pending);
+    if (result < 0) {
+        return -1;
+    }
+    
+    // Update status and return
+    this->_status = Socket::Listening;
+    return 0;
+}
+
+int
+Socket::accept(Socket &client)
+{
+    // Check socket status
+    if (this->_status != Socket::Listening) {
+        return -1;
+    }
+    
+    // Check client socket status
+    if (client._status != Socket::Closed) {
+        return -1;
+    }
+    
+    // Create native endpoint
+    struct sockaddr_in native_endpoint;
+    int native_endpoint_size = sizeof(native_endpoint);
+    std::memset(&native_endpoint, 0, sizeof(native_endpoint));
+    
+    // Accept new (pending) connections.
+    int socket = ::accept(this->_socket,
+        (struct sockaddr*)&native_endpoint, (u32_t *)&native_endpoint_size);
+    
+    // Did we succeed?
+    if (socket < 0) {
+        return -1;
+    }
+    
+    // Check if we received the endpoint information correctly.
+    if (native_endpoint_size != sizeof(native_endpoint)) {
+        printf("Warning: invalid endpoint size received\n\r");
+    }
+    
+    // Populate client socket
+    client._socket = socket;
+    client._status = Socket::Connected;
+    client._remote_endpoint.fromNative(&native_endpoint);
+    
+    return 0;
+}
+
+int
+Socket::write(void *data, size_t size)
+{
+    // Check data buffer and size
+    if (data == NULL || size == 0) {
+        return -1;
+    }
+
+    // Check socket status
+    if (this->_status != Socket::Connected) {
+        return -1;
+    }
+    
+    // Update status
+    this->_status = Socket::Sending;
+    
+    // Try to send the specified amount of bytes.
+    int bytes_written = ::send(this->_socket, data, size, 0);
+    
+    // Update status
+    this->_status = (bytes_written == 0)
+        ? Socket::Disconnected
+        : Socket::Connected;
+    
+    // Return the result.
+    return bytes_written;
+}
+
+
+int
+Socket::read(void *data, size_t max_size)
+{
+    // Check data buffer and size
+    if (data == NULL || max_size == 0) {
+        return -1;
+    }
+
+    // Check socket status
+    if (this->_status != Socket::Connected) {
+        return -1;
+    }
+    
+    // Update status
+    this->_status = Socket::Receiving;
+    
+    // Try to read data from the socket.
+    int bytes_read = ::recv(this->_socket, data, max_size, 0);
+    
+    // Update status
+    this->_status = (bytes_read == 0)
+        ? Socket::Disconnected
+        : Socket::Connected;
+            
+    // Return bytes read
+    return bytes_read;
+}
--- a/tcp/socket.hpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/tcp/socket.hpp	Wed Jul 18 11:22:37 2012 +0000
@@ -28,26 +28,26 @@
 
 #include "../socket.hpp"
 #include "../ip/address.hpp"
+#include "../ip/endpoint.hpp"
 
 namespace network {
 namespace tcp {
 
     class Socket :
         public network::Socket
-    {
-        protected:
-            int _max_pending;
-        
-        public:    
-            ~Socket();
+    {        
+        public:            
+            int open();
             
-            int open();
-            int close();
-            int bind(int port);
-            int listen(int max_pending);
+            int connect(ip::Address &address, int port);
+            int connect(ip::Endpoint &endpoint);
+            int shutdown();
+            
+            int listen(int max_pending = 10);
+            int accept(Socket &client);
         
-            int send(void *data, size_t size);
-            int reveive(void *data, size_t max_size);
+            int write(void *data, size_t size);
+            int read(void *data, size_t max_size);
     };
 
 } // namespace tcp
--- a/udp/socket.cpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/udp/socket.cpp	Wed Jul 18 11:22:37 2012 +0000
@@ -46,31 +46,6 @@
 }
 
 int
-Socket::bind(int port)
-{
-    // Check socket status
-    if (this->_status != Socket::Open) {
-        return -1;
-    }
-    
-    // Update local endpoint and create native
-    struct sockaddr_in native_endpoint;
-    this->_local_endpoint.setAddress(ip::Address(ip::Address::Any));
-    this->_local_endpoint.setPort(port);
-    this->_local_endpoint.toNative(&native_endpoint);
-    
-    // Bind socket to endpoint
-    if (::bind(this->_socket,
-        (const struct sockaddr *)&native_endpoint,
-        sizeof(native_endpoint)) < 0)
-    {
-        return -1;
-    }
-
-    return 0;
-}
-
-int
 Socket::send(void *data, size_t size, ip::Address &address, int port)
 {
     ip::Endpoint endpoint(address, port);
@@ -109,7 +84,13 @@
 }
 
 int
-Socket::reveive(void *data, size_t max_size, ip::Endpoint &endpoint)
+Socket::receive(void *data, size_t max_size)
+{
+    return this->receive(data, max_size, this->_remote_endpoint);
+}
+
+int
+Socket::receive(void *data, size_t max_size, ip::Endpoint &endpoint)
 {
     // Check data buffer and size
     if (data == NULL || max_size == 0) {
@@ -122,8 +103,8 @@
     }
 
     // Create native endpoint
-    int native_endpoint_size;
     struct sockaddr_in native_endpoint;
+    int native_endpoint_size = sizeof(native_endpoint);
     std::memset(&native_endpoint, 0, sizeof(native_endpoint));
     
     // Update status
@@ -141,11 +122,15 @@
         return -1;
     }
     
-    // Copy native endpoint
-    if (native_endpoint_size == sizeof(native_endpoint)) {
-        endpoint.fromNative(&native_endpoint);
+    // Check if we received the endpoint information correctly
+    if (native_endpoint_size != sizeof(native_endpoint)) {
+        printf("Warning: invalid endpoint size received\n\r");
+        return bytes_read;
     }
     
+    // Copy remote endpoint information
+    endpoint.fromNative(&native_endpoint);
+    
     // Return bytes read
     return bytes_read;
 }
--- a/udp/socket.hpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/udp/socket.hpp	Wed Jul 18 11:22:37 2012 +0000
@@ -37,12 +37,12 @@
     {
         public:        
             int open();
-            int bind(int port);
         
             int send(void *data, size_t size, ip::Address &address, int port);
             int send(void *data, size_t size, ip::Endpoint &endpoint);
             
-            int reveive(void *data, size_t max_size, ip::Endpoint &endpoint);
+            int receive(void *data, size_t max_size);
+            int receive(void *data, size_t max_size, ip::Endpoint &endpoint);
     };
 
 } // namespace udp