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:
Tue Jul 17 18:31:22 2012 +0000
Revision:
2:e283a0062097
Parent:
1:6956f6f96fef
Child:
3:d30db8752485
Implemented udp socket class, 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::udp;
NegativeBlack 0:00d5bc4b46e1 28
NegativeBlack 0:00d5bc4b46e1 29 int
NegativeBlack 0:00d5bc4b46e1 30 Socket::open()
NegativeBlack 0:00d5bc4b46e1 31 {
NegativeBlack 2:e283a0062097 32 // Check socket status
NegativeBlack 2:e283a0062097 33 if (this->_status != Socket::Closed) {
NegativeBlack 2:e283a0062097 34 return -1;
NegativeBlack 2:e283a0062097 35 }
NegativeBlack 2:e283a0062097 36
NegativeBlack 2:e283a0062097 37 // Open socket
NegativeBlack 2:e283a0062097 38 this->_socket = ::socket(AF_INET, SOCK_DGRAM, 0);
NegativeBlack 2:e283a0062097 39 if (this->_socket < 0) {
NegativeBlack 2:e283a0062097 40 return -1;
NegativeBlack 2:e283a0062097 41 }
NegativeBlack 2:e283a0062097 42
NegativeBlack 2:e283a0062097 43 // Update status and return
NegativeBlack 2:e283a0062097 44 this->_status = Socket::Open;
NegativeBlack 0:00d5bc4b46e1 45 return 0;
NegativeBlack 0:00d5bc4b46e1 46 }
NegativeBlack 0:00d5bc4b46e1 47
NegativeBlack 0:00d5bc4b46e1 48 int
NegativeBlack 0:00d5bc4b46e1 49 Socket::bind(int port)
NegativeBlack 0:00d5bc4b46e1 50 {
NegativeBlack 2:e283a0062097 51 // Check socket status
NegativeBlack 2:e283a0062097 52 if (this->_status != Socket::Open) {
NegativeBlack 2:e283a0062097 53 return -1;
NegativeBlack 2:e283a0062097 54 }
NegativeBlack 2:e283a0062097 55
NegativeBlack 2:e283a0062097 56 // Update local endpoint and create native
NegativeBlack 2:e283a0062097 57 struct sockaddr_in native_endpoint;
NegativeBlack 2:e283a0062097 58 this->_local_endpoint.setAddress(ip::Address(ip::Address::Any));
NegativeBlack 2:e283a0062097 59 this->_local_endpoint.setPort(port);
NegativeBlack 2:e283a0062097 60 this->_local_endpoint.toNative(&native_endpoint);
NegativeBlack 2:e283a0062097 61
NegativeBlack 2:e283a0062097 62 // Bind socket to endpoint
NegativeBlack 2:e283a0062097 63 if (::bind(this->_socket,
NegativeBlack 2:e283a0062097 64 (const struct sockaddr *)&native_endpoint,
NegativeBlack 2:e283a0062097 65 sizeof(native_endpoint)) < 0)
NegativeBlack 2:e283a0062097 66 {
NegativeBlack 2:e283a0062097 67 return -1;
NegativeBlack 2:e283a0062097 68 }
NegativeBlack 0:00d5bc4b46e1 69
NegativeBlack 0:00d5bc4b46e1 70 return 0;
NegativeBlack 0:00d5bc4b46e1 71 }
NegativeBlack 0:00d5bc4b46e1 72
NegativeBlack 0:00d5bc4b46e1 73 int
NegativeBlack 0:00d5bc4b46e1 74 Socket::send(void *data, size_t size, ip::Address &address, int port)
NegativeBlack 0:00d5bc4b46e1 75 {
NegativeBlack 2:e283a0062097 76 ip::Endpoint endpoint(address, port);
NegativeBlack 2:e283a0062097 77 return this->send(data, size, endpoint);
NegativeBlack 2:e283a0062097 78 }
NegativeBlack 2:e283a0062097 79
NegativeBlack 2:e283a0062097 80 int
NegativeBlack 2:e283a0062097 81 Socket::send(void *data, size_t size, ip::Endpoint &endpoint)
NegativeBlack 2:e283a0062097 82 {
NegativeBlack 2:e283a0062097 83 // Check data buffer and size
NegativeBlack 2:e283a0062097 84 if (data == NULL || size == 0) {
NegativeBlack 2:e283a0062097 85 return -1;
NegativeBlack 2:e283a0062097 86 }
NegativeBlack 2:e283a0062097 87
NegativeBlack 2:e283a0062097 88 // Check socket status
NegativeBlack 2:e283a0062097 89 if (this->_status != Socket::Open) {
NegativeBlack 2:e283a0062097 90 return -1;
NegativeBlack 2:e283a0062097 91 }
NegativeBlack 2:e283a0062097 92
NegativeBlack 2:e283a0062097 93 // Create native endpoint
NegativeBlack 2:e283a0062097 94 struct sockaddr_in native_endpoint;
NegativeBlack 2:e283a0062097 95 endpoint.toNative(&native_endpoint);
NegativeBlack 2:e283a0062097 96
NegativeBlack 2:e283a0062097 97 // Update status
NegativeBlack 2:e283a0062097 98 this->_status = Socket::Sending;
NegativeBlack 2:e283a0062097 99
NegativeBlack 2:e283a0062097 100 // Try to write the specified amount of bytes.
NegativeBlack 2:e283a0062097 101 int bytes_written = ::sendto(this->_socket, data, size, 0,
NegativeBlack 2:e283a0062097 102 (const struct sockaddr *)&native_endpoint, sizeof(native_endpoint));
NegativeBlack 2:e283a0062097 103
NegativeBlack 2:e283a0062097 104 // Update status
NegativeBlack 2:e283a0062097 105 this->_status = Socket::Open;
NegativeBlack 2:e283a0062097 106
NegativeBlack 2:e283a0062097 107 // Return the result.
NegativeBlack 2:e283a0062097 108 return bytes_written;
NegativeBlack 0:00d5bc4b46e1 109 }
NegativeBlack 0:00d5bc4b46e1 110
NegativeBlack 0:00d5bc4b46e1 111 int
NegativeBlack 0:00d5bc4b46e1 112 Socket::reveive(void *data, size_t max_size, ip::Endpoint &endpoint)
NegativeBlack 0:00d5bc4b46e1 113 {
NegativeBlack 2:e283a0062097 114 // Check data buffer and size
NegativeBlack 2:e283a0062097 115 if (data == NULL || max_size == 0) {
NegativeBlack 2:e283a0062097 116 return -1;
NegativeBlack 2:e283a0062097 117 }
NegativeBlack 2:e283a0062097 118
NegativeBlack 2:e283a0062097 119 // Check socket status
NegativeBlack 2:e283a0062097 120 if (this->_status != Socket::Open) {
NegativeBlack 2:e283a0062097 121 return -1;
NegativeBlack 2:e283a0062097 122 }
NegativeBlack 0:00d5bc4b46e1 123
NegativeBlack 2:e283a0062097 124 // Create native endpoint
NegativeBlack 2:e283a0062097 125 int native_endpoint_size;
NegativeBlack 2:e283a0062097 126 struct sockaddr_in native_endpoint;
NegativeBlack 2:e283a0062097 127 std::memset(&native_endpoint, 0, sizeof(native_endpoint));
NegativeBlack 2:e283a0062097 128
NegativeBlack 2:e283a0062097 129 // Update status
NegativeBlack 2:e283a0062097 130 this->_status = Socket::Receiving;
NegativeBlack 2:e283a0062097 131
NegativeBlack 2:e283a0062097 132 // Try to read a packet from the socket.
NegativeBlack 2:e283a0062097 133 int bytes_read = ::recvfrom(this->_socket, data, max_size, 0,
NegativeBlack 2:e283a0062097 134 (struct sockaddr*)&native_endpoint, (u32_t *)&native_endpoint_size);
NegativeBlack 2:e283a0062097 135
NegativeBlack 2:e283a0062097 136 // Update status and return
NegativeBlack 2:e283a0062097 137 this->_status = Socket::Open;
NegativeBlack 2:e283a0062097 138
NegativeBlack 2:e283a0062097 139 // Did we succeed?
NegativeBlack 2:e283a0062097 140 if (bytes_read < 0) {
NegativeBlack 2:e283a0062097 141 return -1;
NegativeBlack 2:e283a0062097 142 }
NegativeBlack 2:e283a0062097 143
NegativeBlack 2:e283a0062097 144 // Copy native endpoint
NegativeBlack 2:e283a0062097 145 if (native_endpoint_size == sizeof(native_endpoint)) {
NegativeBlack 2:e283a0062097 146 endpoint.fromNative(&native_endpoint);
NegativeBlack 2:e283a0062097 147 }
NegativeBlack 2:e283a0062097 148
NegativeBlack 2:e283a0062097 149 // Return bytes read
NegativeBlack 2:e283a0062097 150 return bytes_read;
NegativeBlack 0:00d5bc4b46e1 151 }