Deprecated fork of old network stack source from github. Please use official library instead: https://mbed.org/users/mbed_official/code/EthernetInterface/

Committer:
AdamGreen
Date:
Sat Oct 26 08:51:36 2013 +0000
Revision:
1:eadc868c2acf
Parent:
0:3b00827bb0b7
Fix TCP checksum bug and stranded large TCP segments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AdamGreen 0:3b00827bb0b7 1 /* Copyright (C) 2012 mbed.org, MIT License
AdamGreen 0:3b00827bb0b7 2 *
AdamGreen 0:3b00827bb0b7 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
AdamGreen 0:3b00827bb0b7 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
AdamGreen 0:3b00827bb0b7 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
AdamGreen 0:3b00827bb0b7 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
AdamGreen 0:3b00827bb0b7 7 * furnished to do so, subject to the following conditions:
AdamGreen 0:3b00827bb0b7 8 *
AdamGreen 0:3b00827bb0b7 9 * The above copyright notice and this permission notice shall be included in all copies or
AdamGreen 0:3b00827bb0b7 10 * substantial portions of the Software.
AdamGreen 0:3b00827bb0b7 11 *
AdamGreen 0:3b00827bb0b7 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
AdamGreen 0:3b00827bb0b7 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
AdamGreen 0:3b00827bb0b7 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
AdamGreen 0:3b00827bb0b7 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AdamGreen 0:3b00827bb0b7 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AdamGreen 0:3b00827bb0b7 17 */
AdamGreen 0:3b00827bb0b7 18 #include "TCPSocketServer.h"
AdamGreen 0:3b00827bb0b7 19
AdamGreen 0:3b00827bb0b7 20 #include <cstring>
AdamGreen 0:3b00827bb0b7 21
AdamGreen 0:3b00827bb0b7 22 using std::memset;
AdamGreen 0:3b00827bb0b7 23 using std::memcpy;
AdamGreen 0:3b00827bb0b7 24
AdamGreen 0:3b00827bb0b7 25 TCPSocketServer::TCPSocketServer() {
AdamGreen 0:3b00827bb0b7 26
AdamGreen 0:3b00827bb0b7 27 }
AdamGreen 0:3b00827bb0b7 28
AdamGreen 0:3b00827bb0b7 29 int TCPSocketServer::bind(int port) {
AdamGreen 0:3b00827bb0b7 30 if (init_socket(SOCK_STREAM) < 0)
AdamGreen 0:3b00827bb0b7 31 return -1;
AdamGreen 0:3b00827bb0b7 32
AdamGreen 0:3b00827bb0b7 33 struct sockaddr_in localHost;
AdamGreen 0:3b00827bb0b7 34 memset(&localHost, 0, sizeof(localHost));
AdamGreen 0:3b00827bb0b7 35
AdamGreen 0:3b00827bb0b7 36 localHost.sin_family = AF_INET;
AdamGreen 0:3b00827bb0b7 37 localHost.sin_port = htons(port);
AdamGreen 0:3b00827bb0b7 38 localHost.sin_addr.s_addr = INADDR_ANY;
AdamGreen 0:3b00827bb0b7 39
AdamGreen 0:3b00827bb0b7 40 if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
AdamGreen 0:3b00827bb0b7 41 close();
AdamGreen 0:3b00827bb0b7 42 return -1;
AdamGreen 0:3b00827bb0b7 43 }
AdamGreen 0:3b00827bb0b7 44
AdamGreen 0:3b00827bb0b7 45 return 0;
AdamGreen 0:3b00827bb0b7 46 }
AdamGreen 0:3b00827bb0b7 47
AdamGreen 0:3b00827bb0b7 48 int TCPSocketServer::listen(int max) {
AdamGreen 0:3b00827bb0b7 49 if (_sock_fd < 0)
AdamGreen 0:3b00827bb0b7 50 return -1;
AdamGreen 0:3b00827bb0b7 51
AdamGreen 0:3b00827bb0b7 52 if (lwip_listen(_sock_fd, max) < 0) {
AdamGreen 0:3b00827bb0b7 53 close();
AdamGreen 0:3b00827bb0b7 54 return -1;
AdamGreen 0:3b00827bb0b7 55 }
AdamGreen 0:3b00827bb0b7 56
AdamGreen 0:3b00827bb0b7 57 return 0;
AdamGreen 0:3b00827bb0b7 58 }
AdamGreen 0:3b00827bb0b7 59
AdamGreen 0:3b00827bb0b7 60 int TCPSocketServer::accept(TCPSocketConnection& connection) {
AdamGreen 0:3b00827bb0b7 61 if (_sock_fd < 0)
AdamGreen 0:3b00827bb0b7 62 return -1;
AdamGreen 0:3b00827bb0b7 63
AdamGreen 0:3b00827bb0b7 64 if (!_blocking) {
AdamGreen 0:3b00827bb0b7 65 TimeInterval timeout(_timeout);
AdamGreen 0:3b00827bb0b7 66 if (wait_readable(timeout) != 0)
AdamGreen 0:3b00827bb0b7 67 return -1;
AdamGreen 0:3b00827bb0b7 68 }
AdamGreen 0:3b00827bb0b7 69 connection.reset_address();
AdamGreen 0:3b00827bb0b7 70 socklen_t newSockRemoteHostLen = sizeof(connection._remoteHost);
AdamGreen 0:3b00827bb0b7 71 int fd = lwip_accept(_sock_fd, (struct sockaddr*) &connection._remoteHost, &newSockRemoteHostLen);
AdamGreen 0:3b00827bb0b7 72 if (fd < 0)
AdamGreen 0:3b00827bb0b7 73 return -1; //Accept failed
AdamGreen 0:3b00827bb0b7 74 connection._sock_fd = fd;
AdamGreen 0:3b00827bb0b7 75 connection._is_connected = true;
AdamGreen 0:3b00827bb0b7 76
AdamGreen 0:3b00827bb0b7 77 return 0;
AdamGreen 0:3b00827bb0b7 78 }