Roy van Dam / NetworkAPI

Dependents:   NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more

Committer:
NegativeBlack
Date:
Thu Sep 27 09:31:40 2012 +0000
Revision:
8:cdee0f2b6ff0
Parent:
6:847a0b218e22
Fixed binding to the new Ethernet LWIP api

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 8:cdee0f2b6ff0 38 this->_socket = lwip_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 6:847a0b218e22 49 Socket::send(Buffer &buffer, ip::Address &address, int port)
NegativeBlack 6:847a0b218e22 50 {
NegativeBlack 6:847a0b218e22 51 ip::Endpoint endpoint(address, port);
NegativeBlack 8:cdee0f2b6ff0 52 return this->send(buffer.data(), buffer.length(), endpoint);
NegativeBlack 6:847a0b218e22 53 }
NegativeBlack 6:847a0b218e22 54
NegativeBlack 6:847a0b218e22 55 int
NegativeBlack 6:847a0b218e22 56 Socket::send(Buffer &buffer, ip::Endpoint &endpoint)
NegativeBlack 6:847a0b218e22 57 {
NegativeBlack 8:cdee0f2b6ff0 58 return this->send(buffer.data(), buffer.length(), endpoint);
NegativeBlack 6:847a0b218e22 59 }
NegativeBlack 6:847a0b218e22 60
NegativeBlack 6:847a0b218e22 61 int
NegativeBlack 0:00d5bc4b46e1 62 Socket::send(void *data, size_t size, ip::Address &address, int port)
NegativeBlack 0:00d5bc4b46e1 63 {
NegativeBlack 2:e283a0062097 64 ip::Endpoint endpoint(address, port);
NegativeBlack 2:e283a0062097 65 return this->send(data, size, endpoint);
NegativeBlack 2:e283a0062097 66 }
NegativeBlack 2:e283a0062097 67
NegativeBlack 2:e283a0062097 68 int
NegativeBlack 2:e283a0062097 69 Socket::send(void *data, size_t size, ip::Endpoint &endpoint)
NegativeBlack 2:e283a0062097 70 {
NegativeBlack 2:e283a0062097 71 // Check data buffer and size
NegativeBlack 2:e283a0062097 72 if (data == NULL || size == 0) {
NegativeBlack 2:e283a0062097 73 return -1;
NegativeBlack 2:e283a0062097 74 }
NegativeBlack 2:e283a0062097 75
NegativeBlack 2:e283a0062097 76 // Check socket status
NegativeBlack 2:e283a0062097 77 if (this->_status != Socket::Open) {
NegativeBlack 2:e283a0062097 78 return -1;
NegativeBlack 2:e283a0062097 79 }
NegativeBlack 2:e283a0062097 80
NegativeBlack 2:e283a0062097 81 // Create native endpoint
NegativeBlack 2:e283a0062097 82 struct sockaddr_in native_endpoint;
NegativeBlack 2:e283a0062097 83 endpoint.toNative(&native_endpoint);
NegativeBlack 2:e283a0062097 84
NegativeBlack 2:e283a0062097 85 // Update status
NegativeBlack 2:e283a0062097 86 this->_status = Socket::Sending;
NegativeBlack 2:e283a0062097 87
NegativeBlack 2:e283a0062097 88 // Try to write the specified amount of bytes.
NegativeBlack 8:cdee0f2b6ff0 89 int bytes_written = lwip_sendto(this->_socket, data, size, 0,
NegativeBlack 2:e283a0062097 90 (const struct sockaddr *)&native_endpoint, sizeof(native_endpoint));
NegativeBlack 2:e283a0062097 91
NegativeBlack 2:e283a0062097 92 // Update status
NegativeBlack 2:e283a0062097 93 this->_status = Socket::Open;
NegativeBlack 2:e283a0062097 94
NegativeBlack 2:e283a0062097 95 // Return the result.
NegativeBlack 2:e283a0062097 96 return bytes_written;
NegativeBlack 0:00d5bc4b46e1 97 }
NegativeBlack 0:00d5bc4b46e1 98
NegativeBlack 0:00d5bc4b46e1 99 int
NegativeBlack 6:847a0b218e22 100 Socket::receive(Buffer &buffer)
NegativeBlack 6:847a0b218e22 101 {
NegativeBlack 8:cdee0f2b6ff0 102 int result = this->receive(buffer.data(), buffer.size());
NegativeBlack 6:847a0b218e22 103 if (result >= 0) {
NegativeBlack 8:cdee0f2b6ff0 104 buffer.length(result);
NegativeBlack 6:847a0b218e22 105 }
NegativeBlack 6:847a0b218e22 106
NegativeBlack 6:847a0b218e22 107 return result;
NegativeBlack 6:847a0b218e22 108 }
NegativeBlack 6:847a0b218e22 109
NegativeBlack 6:847a0b218e22 110 int
NegativeBlack 3:d30db8752485 111 Socket::receive(void *data, size_t max_size)
NegativeBlack 3:d30db8752485 112 {
NegativeBlack 3:d30db8752485 113 return this->receive(data, max_size, this->_remote_endpoint);
NegativeBlack 3:d30db8752485 114 }
NegativeBlack 3:d30db8752485 115
NegativeBlack 3:d30db8752485 116 int
NegativeBlack 3:d30db8752485 117 Socket::receive(void *data, size_t max_size, ip::Endpoint &endpoint)
NegativeBlack 0:00d5bc4b46e1 118 {
NegativeBlack 2:e283a0062097 119 // Check data buffer and size
NegativeBlack 2:e283a0062097 120 if (data == NULL || max_size == 0) {
NegativeBlack 2:e283a0062097 121 return -1;
NegativeBlack 2:e283a0062097 122 }
NegativeBlack 2:e283a0062097 123
NegativeBlack 2:e283a0062097 124 // Check socket status
NegativeBlack 2:e283a0062097 125 if (this->_status != Socket::Open) {
NegativeBlack 2:e283a0062097 126 return -1;
NegativeBlack 2:e283a0062097 127 }
NegativeBlack 0:00d5bc4b46e1 128
NegativeBlack 2:e283a0062097 129 // Create native endpoint
NegativeBlack 2:e283a0062097 130 struct sockaddr_in native_endpoint;
NegativeBlack 3:d30db8752485 131 int native_endpoint_size = sizeof(native_endpoint);
NegativeBlack 2:e283a0062097 132 std::memset(&native_endpoint, 0, sizeof(native_endpoint));
NegativeBlack 2:e283a0062097 133
NegativeBlack 2:e283a0062097 134 // Update status
NegativeBlack 2:e283a0062097 135 this->_status = Socket::Receiving;
NegativeBlack 2:e283a0062097 136
NegativeBlack 2:e283a0062097 137 // Try to read a packet from the socket.
NegativeBlack 8:cdee0f2b6ff0 138 int bytes_read = lwip_recvfrom(this->_socket, data, max_size, 0,
NegativeBlack 2:e283a0062097 139 (struct sockaddr*)&native_endpoint, (u32_t *)&native_endpoint_size);
NegativeBlack 2:e283a0062097 140
NegativeBlack 2:e283a0062097 141 // Update status and return
NegativeBlack 2:e283a0062097 142 this->_status = Socket::Open;
NegativeBlack 2:e283a0062097 143
NegativeBlack 2:e283a0062097 144 // Did we succeed?
NegativeBlack 2:e283a0062097 145 if (bytes_read < 0) {
NegativeBlack 2:e283a0062097 146 return -1;
NegativeBlack 2:e283a0062097 147 }
NegativeBlack 2:e283a0062097 148
NegativeBlack 3:d30db8752485 149 // Check if we received the endpoint information correctly
NegativeBlack 3:d30db8752485 150 if (native_endpoint_size != sizeof(native_endpoint)) {
NegativeBlack 3:d30db8752485 151 printf("Warning: invalid endpoint size received\n\r");
NegativeBlack 3:d30db8752485 152 return bytes_read;
NegativeBlack 2:e283a0062097 153 }
NegativeBlack 2:e283a0062097 154
NegativeBlack 3:d30db8752485 155 // Copy remote endpoint information
NegativeBlack 3:d30db8752485 156 endpoint.fromNative(&native_endpoint);
NegativeBlack 3:d30db8752485 157
NegativeBlack 2:e283a0062097 158 // Return bytes read
NegativeBlack 2:e283a0062097 159 return bytes_read;
NegativeBlack 0:00d5bc4b46e1 160 }