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

Committer:
NegativeBlack
Date:
Wed Jul 18 11:22:37 2012 +0000
Revision:
3:d30db8752485
Parent:
1:6956f6f96fef
Child:
8:cdee0f2b6ff0
Implemented UDP and TCP socket. UDP is fully tested, TCP still needs to be tested.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NegativeBlack 0:00d5bc4b46e1 1 /**
NegativeBlack 0:00d5bc4b46e1 2 * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
NegativeBlack 0:00d5bc4b46e1 3 * All rights reserved.
NegativeBlack 0:00d5bc4b46e1 4 *
NegativeBlack 0:00d5bc4b46e1 5 * Redistribution and use in source and binary forms, with or without
NegativeBlack 0:00d5bc4b46e1 6 * modification, are permitted provided that the following conditions are met:
NegativeBlack 0:00d5bc4b46e1 7 *
NegativeBlack 0:00d5bc4b46e1 8 * 1. Redistributions of source code must retain the above copyright notice, this
NegativeBlack 0:00d5bc4b46e1 9 * list of conditions and the following disclaimer.
NegativeBlack 0:00d5bc4b46e1 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
NegativeBlack 0:00d5bc4b46e1 11 * this list of conditions and the following disclaimer in the documentation
NegativeBlack 0:00d5bc4b46e1 12 * and/or other materials provided with the distribution.
NegativeBlack 0:00d5bc4b46e1 13 *
NegativeBlack 0:00d5bc4b46e1 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
NegativeBlack 0:00d5bc4b46e1 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
NegativeBlack 0:00d5bc4b46e1 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NegativeBlack 0:00d5bc4b46e1 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
NegativeBlack 0:00d5bc4b46e1 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
NegativeBlack 0:00d5bc4b46e1 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
NegativeBlack 0:00d5bc4b46e1 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
NegativeBlack 0:00d5bc4b46e1 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
NegativeBlack 0:00d5bc4b46e1 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
NegativeBlack 0:00d5bc4b46e1 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NegativeBlack 0:00d5bc4b46e1 24 */
NegativeBlack 0:00d5bc4b46e1 25
NegativeBlack 1:6956f6f96fef 26 #include "socket.hpp"
NegativeBlack 0:00d5bc4b46e1 27 using namespace network;
NegativeBlack 0:00d5bc4b46e1 28
NegativeBlack 0:00d5bc4b46e1 29 Socket::Socket():
NegativeBlack 0:00d5bc4b46e1 30 _status(Socket::Closed),
NegativeBlack 0:00d5bc4b46e1 31 _socket(-1)
NegativeBlack 0:00d5bc4b46e1 32 {}
NegativeBlack 0:00d5bc4b46e1 33
NegativeBlack 0:00d5bc4b46e1 34 Socket::~Socket()
NegativeBlack 0:00d5bc4b46e1 35 {
NegativeBlack 0:00d5bc4b46e1 36 if (this->_status != Socket::Closed) {
NegativeBlack 0:00d5bc4b46e1 37 this->close();
NegativeBlack 0:00d5bc4b46e1 38 }
NegativeBlack 0:00d5bc4b46e1 39 }
NegativeBlack 0:00d5bc4b46e1 40
NegativeBlack 0:00d5bc4b46e1 41 int
NegativeBlack 3:d30db8752485 42 Socket::open()
NegativeBlack 3:d30db8752485 43 {
NegativeBlack 3:d30db8752485 44 return -1;
NegativeBlack 3:d30db8752485 45 }
NegativeBlack 3:d30db8752485 46
NegativeBlack 3:d30db8752485 47 int
NegativeBlack 0:00d5bc4b46e1 48 Socket::close()
NegativeBlack 0:00d5bc4b46e1 49 {
NegativeBlack 0:00d5bc4b46e1 50 if (this->_status == Socket::Closed) {
NegativeBlack 0:00d5bc4b46e1 51 return -1;
NegativeBlack 0:00d5bc4b46e1 52 }
NegativeBlack 0:00d5bc4b46e1 53
NegativeBlack 3:d30db8752485 54 // Close the socket
NegativeBlack 0:00d5bc4b46e1 55 int result = ::close(this->_socket);
NegativeBlack 0:00d5bc4b46e1 56 this->_socket = -1;
NegativeBlack 0:00d5bc4b46e1 57
NegativeBlack 3:d30db8752485 58 // Update status and return
NegativeBlack 3:d30db8752485 59 this->_status = Socket::Closed;
NegativeBlack 0:00d5bc4b46e1 60 return result;
NegativeBlack 0:00d5bc4b46e1 61 }
NegativeBlack 0:00d5bc4b46e1 62
NegativeBlack 3:d30db8752485 63 int
NegativeBlack 3:d30db8752485 64 Socket::bind(int port)
NegativeBlack 3:d30db8752485 65 {
NegativeBlack 3:d30db8752485 66 // Check socket status
NegativeBlack 3:d30db8752485 67 if (this->_status != Socket::Open) {
NegativeBlack 3:d30db8752485 68 return -1;
NegativeBlack 3:d30db8752485 69 }
NegativeBlack 3:d30db8752485 70
NegativeBlack 3:d30db8752485 71 // Update local endpoint and create native
NegativeBlack 3:d30db8752485 72 struct sockaddr_in native_endpoint;
NegativeBlack 3:d30db8752485 73 this->_local_endpoint.setAddress(ip::Address(ip::Address::Any));
NegativeBlack 3:d30db8752485 74 this->_local_endpoint.setPort(port);
NegativeBlack 3:d30db8752485 75 this->_local_endpoint.toNative(&native_endpoint);
NegativeBlack 3:d30db8752485 76
NegativeBlack 3:d30db8752485 77 // Bind socket to endpoint
NegativeBlack 3:d30db8752485 78 if (::bind(this->_socket,
NegativeBlack 3:d30db8752485 79 (const struct sockaddr *)&native_endpoint,
NegativeBlack 3:d30db8752485 80 sizeof(native_endpoint)) < 0)
NegativeBlack 3:d30db8752485 81 {
NegativeBlack 3:d30db8752485 82 return -1;
NegativeBlack 3:d30db8752485 83 }
NegativeBlack 3:d30db8752485 84
NegativeBlack 3:d30db8752485 85 return 0;
NegativeBlack 3:d30db8752485 86 }
NegativeBlack 3:d30db8752485 87
NegativeBlack 3:d30db8752485 88 ip::Endpoint &
NegativeBlack 0:00d5bc4b46e1 89 Socket::getRemoteEndpoint()
NegativeBlack 0:00d5bc4b46e1 90 {
NegativeBlack 0:00d5bc4b46e1 91 return this->_remote_endpoint;
NegativeBlack 0:00d5bc4b46e1 92 }
NegativeBlack 0:00d5bc4b46e1 93
NegativeBlack 3:d30db8752485 94 ip::Endpoint &
NegativeBlack 0:00d5bc4b46e1 95 Socket::getLocalEndpoint()
NegativeBlack 0:00d5bc4b46e1 96 {
NegativeBlack 0:00d5bc4b46e1 97 return this->_local_endpoint;
NegativeBlack 0:00d5bc4b46e1 98 }
NegativeBlack 0:00d5bc4b46e1 99
NegativeBlack 0:00d5bc4b46e1 100 int
NegativeBlack 0:00d5bc4b46e1 101 Socket::getHandle()
NegativeBlack 0:00d5bc4b46e1 102 {
NegativeBlack 0:00d5bc4b46e1 103 return this->_socket;
NegativeBlack 0:00d5bc4b46e1 104 }
NegativeBlack 0:00d5bc4b46e1 105
NegativeBlack 0:00d5bc4b46e1 106 enum Socket::Status
NegativeBlack 0:00d5bc4b46e1 107 Socket::getStatus()
NegativeBlack 0:00d5bc4b46e1 108 {
NegativeBlack 0:00d5bc4b46e1 109 return this->_status;
NegativeBlack 0:00d5bc4b46e1 110 }