update main WIZnetInterface library for supporting honeypot server

Fork of WIZnetInterface by Shlomi Ruder

Update WIZnetInterface for supporting Honeypot server

/media/uploads/proxytype/honeypot.png

Update sockets handling.

//add new type to enum status
 SOCK_TIMEOUT     = 0x23,
Committer:
proxytype
Date:
Fri Sep 01 23:36:01 2017 +0000
Revision:
30:58f7cd21cf37
Parent:
0:6f28332c466f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Soohwan Kim 0:6f28332c466f 1 /* Copyright (C) 2012 mbed.org, MIT License
Soohwan Kim 0:6f28332c466f 2 *
Soohwan Kim 0:6f28332c466f 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Soohwan Kim 0:6f28332c466f 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Soohwan Kim 0:6f28332c466f 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Soohwan Kim 0:6f28332c466f 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Soohwan Kim 0:6f28332c466f 7 * furnished to do so, subject to the following conditions:
Soohwan Kim 0:6f28332c466f 8 *
Soohwan Kim 0:6f28332c466f 9 * The above copyright notice and this permission notice shall be included in all copies or
Soohwan Kim 0:6f28332c466f 10 * substantial portions of the Software.
Soohwan Kim 0:6f28332c466f 11 *
Soohwan Kim 0:6f28332c466f 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Soohwan Kim 0:6f28332c466f 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Soohwan Kim 0:6f28332c466f 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Soohwan Kim 0:6f28332c466f 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Soohwan Kim 0:6f28332c466f 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Soohwan Kim 0:6f28332c466f 17 */
Soohwan Kim 0:6f28332c466f 18
Soohwan Kim 0:6f28332c466f 19 #include "TCPSocketServer.h"
Soohwan Kim 0:6f28332c466f 20
Soohwan Kim 0:6f28332c466f 21 TCPSocketServer::TCPSocketServer() {}
Soohwan Kim 0:6f28332c466f 22
Soohwan Kim 0:6f28332c466f 23 // Server initialization
Soohwan Kim 0:6f28332c466f 24 int TCPSocketServer::bind(int port)
Soohwan Kim 0:6f28332c466f 25 {
proxytype 30:58f7cd21cf37 26 // set the listen_port for next connection.
proxytype 30:58f7cd21cf37 27 listen_port = port;
Soohwan Kim 0:6f28332c466f 28 if (_sock_fd < 0) {
Soohwan Kim 0:6f28332c466f 29 _sock_fd = eth->new_socket();
Soohwan Kim 0:6f28332c466f 30 if (_sock_fd < 0) {
Soohwan Kim 0:6f28332c466f 31 return -1;
Soohwan Kim 0:6f28332c466f 32 }
Soohwan Kim 0:6f28332c466f 33 }
Soohwan Kim 0:6f28332c466f 34 // set TCP protocol
Soohwan Kim 0:6f28332c466f 35 eth->setProtocol(_sock_fd, WIZnet_Chip::TCP);
Soohwan Kim 0:6f28332c466f 36 // set local port
Soohwan Kim 0:6f28332c466f 37 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
Soohwan Kim 0:6f28332c466f 38 // connect the network
Soohwan Kim 0:6f28332c466f 39 eth->scmd(_sock_fd, WIZnet_Chip::OPEN);
proxytype 30:58f7cd21cf37 40
proxytype 30:58f7cd21cf37 41
Soohwan Kim 0:6f28332c466f 42 return 0;
Soohwan Kim 0:6f28332c466f 43 }
Soohwan Kim 0:6f28332c466f 44
Soohwan Kim 0:6f28332c466f 45 int TCPSocketServer::listen(int backlog)
Soohwan Kim 0:6f28332c466f 46 {
Soohwan Kim 0:6f28332c466f 47 if (_sock_fd < 0) {
Soohwan Kim 0:6f28332c466f 48 return -1;
Soohwan Kim 0:6f28332c466f 49 }
Soohwan Kim 0:6f28332c466f 50 if (backlog != 1) {
Soohwan Kim 0:6f28332c466f 51 return -1;
Soohwan Kim 0:6f28332c466f 52 }
Soohwan Kim 0:6f28332c466f 53 eth->scmd(_sock_fd, WIZnet_Chip::LISTEN);
Soohwan Kim 0:6f28332c466f 54 return 0;
Soohwan Kim 0:6f28332c466f 55 }
Soohwan Kim 0:6f28332c466f 56
Soohwan Kim 0:6f28332c466f 57
Soohwan Kim 0:6f28332c466f 58 int TCPSocketServer::accept(TCPSocketConnection& connection)
Soohwan Kim 0:6f28332c466f 59 {
Soohwan Kim 0:6f28332c466f 60 if (_sock_fd < 0) {
proxytype 30:58f7cd21cf37 61 printf("socket closed");
Soohwan Kim 0:6f28332c466f 62 return -1;
Soohwan Kim 0:6f28332c466f 63 }
Soohwan Kim 0:6f28332c466f 64 Timer t;
Soohwan Kim 0:6f28332c466f 65 t.reset();
Soohwan Kim 0:6f28332c466f 66 t.start();
Soohwan Kim 0:6f28332c466f 67 while(1) {
Soohwan Kim 0:6f28332c466f 68 if (t.read_ms() > _timeout && _blocking == false) {
proxytype 30:58f7cd21cf37 69 //nothing in the queue keep pooling
proxytype 30:58f7cd21cf37 70 return WIZnet_Chip::SOCK_TIMEOUT;
proxytype 30:58f7cd21cf37 71 }
proxytype 30:58f7cd21cf37 72
proxytype 30:58f7cd21cf37 73 //should rebinding again.
proxytype 30:58f7cd21cf37 74 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == WIZnet_Chip::SOCK_CLOSED) {
proxytype 30:58f7cd21cf37 75 return WIZnet_Chip::SOCK_CLOSED;
Soohwan Kim 0:6f28332c466f 76 }
proxytype 30:58f7cd21cf37 77
proxytype 30:58f7cd21cf37 78 //should rebinding again.
proxytype 30:58f7cd21cf37 79 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == WIZnet_Chip::SOCK_CLOSE_WAIT) {
proxytype 30:58f7cd21cf37 80 break;
proxytype 30:58f7cd21cf37 81 }
proxytype 30:58f7cd21cf37 82
proxytype 30:58f7cd21cf37 83 //connection establish, start processing
Soohwan Kim 0:6f28332c466f 84 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == WIZnet_Chip::SOCK_ESTABLISHED) {
Soohwan Kim 0:6f28332c466f 85 break;
Soohwan Kim 0:6f28332c466f 86 }
proxytype 30:58f7cd21cf37 87
Soohwan Kim 0:6f28332c466f 88 }
Soohwan Kim 0:6f28332c466f 89 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
Soohwan Kim 0:6f28332c466f 90 char host[16];
Soohwan Kim 0:6f28332c466f 91 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Soohwan Kim 0:6f28332c466f 92 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
Soohwan Kim 0:6f28332c466f 93
Soohwan Kim 0:6f28332c466f 94 // change this server socket to connection socket.
Soohwan Kim 0:6f28332c466f 95 connection._sock_fd = _sock_fd;
Soohwan Kim 0:6f28332c466f 96 connection._is_connected = true;
Soohwan Kim 0:6f28332c466f 97 connection.set_address(host, port);
Soohwan Kim 0:6f28332c466f 98
Soohwan Kim 0:6f28332c466f 99 // and then, for the next connection, server socket should be assigned new one.
Soohwan Kim 0:6f28332c466f 100 _sock_fd = -1; // want to assign new available _sock_fd.
Soohwan Kim 0:6f28332c466f 101 if(bind(listen_port) < 0) {
Soohwan Kim 0:6f28332c466f 102 // modified by Patrick Pollet
proxytype 30:58f7cd21cf37 103 printf("No more socket for listening, bind error");
Soohwan Kim 0:6f28332c466f 104 return -1;
Soohwan Kim 0:6f28332c466f 105 } else {
Soohwan Kim 0:6f28332c466f 106 //return -1;
Soohwan Kim 0:6f28332c466f 107 if(listen(1) < 0) {
Soohwan Kim 0:6f28332c466f 108 // modified by Patrick Pollet
proxytype 30:58f7cd21cf37 109 printf("No more socket for listening, listen error");
Soohwan Kim 0:6f28332c466f 110 return -1;
Soohwan Kim 0:6f28332c466f 111 }
Soohwan Kim 0:6f28332c466f 112 }
Soohwan Kim 0:6f28332c466f 113
proxytype 30:58f7cd21cf37 114 return WIZnet_Chip::SOCK_ESTABLISHED;
Soohwan Kim 0:6f28332c466f 115 }
proxytype 30:58f7cd21cf37 116
proxytype 30:58f7cd21cf37 117 int TCPSocketServer::getPort()
proxytype 30:58f7cd21cf37 118 {
proxytype 30:58f7cd21cf37 119 return listen_port;
proxytype 30:58f7cd21cf37 120 }