MQTT client test with W5200 ethernet shield

Dependents:   IBMIoTClientEthernetExample_W5200

Fork of W5500Interface by W5500-Ethernet-Interface Makers

Committer:
Bongjun
Date:
Mon Oct 06 01:37:10 2014 +0000
Revision:
8:7609a4bd55fd
Parent:
5:8aefaef88f79
Perform Length check here to prevent buffer overrun fixed by Sean Newton (https://developer.mbed.org/users/SeanNewton/)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bongjun 0:e11e8793c3ce 1 /* Copyright (C) 2012 mbed.org, MIT License
Bongjun 0:e11e8793c3ce 2 *
Bongjun 0:e11e8793c3ce 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Bongjun 0:e11e8793c3ce 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Bongjun 0:e11e8793c3ce 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Bongjun 0:e11e8793c3ce 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Bongjun 0:e11e8793c3ce 7 * furnished to do so, subject to the following conditions:
Bongjun 0:e11e8793c3ce 8 *
Bongjun 0:e11e8793c3ce 9 * The above copyright notice and this permission notice shall be included in all copies or
Bongjun 0:e11e8793c3ce 10 * substantial portions of the Software.
Bongjun 0:e11e8793c3ce 11 *
Bongjun 0:e11e8793c3ce 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Bongjun 0:e11e8793c3ce 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Bongjun 0:e11e8793c3ce 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Bongjun 0:e11e8793c3ce 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Bongjun 0:e11e8793c3ce 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Bongjun 0:e11e8793c3ce 17 */
Bongjun 0:e11e8793c3ce 18
Bongjun 0:e11e8793c3ce 19 #include "UDPSocket.h"
Bongjun 0:e11e8793c3ce 20
Bongjun 0:e11e8793c3ce 21 static int udp_local_port;
Bongjun 0:e11e8793c3ce 22
Bongjun 0:e11e8793c3ce 23 UDPSocket::UDPSocket()
Bongjun 0:e11e8793c3ce 24 {
Bongjun 0:e11e8793c3ce 25 }
Bongjun 0:e11e8793c3ce 26 // After init function, bind() should be called.
Bongjun 0:e11e8793c3ce 27 int UDPSocket::init(void)
Bongjun 0:e11e8793c3ce 28 {
Bongjun 0:e11e8793c3ce 29 if (_sock_fd < 0) {
Bongjun 0:e11e8793c3ce 30 _sock_fd = eth->new_socket();
Bongjun 0:e11e8793c3ce 31 }
kaizen 5:8aefaef88f79 32 if (eth->setProtocol(_sock_fd, WIZnet_Chip::UDP) == false) return -1;
Bongjun 0:e11e8793c3ce 33 return 0;
Bongjun 0:e11e8793c3ce 34 }
Bongjun 0:e11e8793c3ce 35
Bongjun 0:e11e8793c3ce 36 // Server initialization
Bongjun 0:e11e8793c3ce 37 int UDPSocket::bind(int port)
Bongjun 0:e11e8793c3ce 38 {
Bongjun 0:e11e8793c3ce 39 if (_sock_fd < 0) {
Bongjun 0:e11e8793c3ce 40 _sock_fd = eth->new_socket();
Bongjun 0:e11e8793c3ce 41 if (_sock_fd < 0) {
Bongjun 0:e11e8793c3ce 42 return -1;
Bongjun 0:e11e8793c3ce 43 }
Bongjun 0:e11e8793c3ce 44 }
Bongjun 0:e11e8793c3ce 45 // set local port
Bongjun 0:e11e8793c3ce 46 if (port != 0) {
Bongjun 0:e11e8793c3ce 47 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
Bongjun 0:e11e8793c3ce 48 } else {
Bongjun 0:e11e8793c3ce 49 udp_local_port++;
Bongjun 0:e11e8793c3ce 50 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port);
Bongjun 0:e11e8793c3ce 51 }
Bongjun 0:e11e8793c3ce 52 // set udp protocol
kaizen 5:8aefaef88f79 53 eth->setProtocol(_sock_fd, WIZnet_Chip::UDP);
kaizen 5:8aefaef88f79 54 eth->scmd(_sock_fd, WIZnet_Chip::OPEN);
Bongjun 0:e11e8793c3ce 55 return 0;
Bongjun 0:e11e8793c3ce 56 }
Bongjun 0:e11e8793c3ce 57
Bongjun 0:e11e8793c3ce 58 // -1 if unsuccessful, else number of bytes written
Bongjun 0:e11e8793c3ce 59 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
Bongjun 0:e11e8793c3ce 60 {
Bongjun 0:e11e8793c3ce 61 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
Bongjun 0:e11e8793c3ce 62 if (size < 0) {
Bongjun 0:e11e8793c3ce 63 return -1;
Bongjun 0:e11e8793c3ce 64 }
Bongjun 0:e11e8793c3ce 65 confEndpoint(remote);
Bongjun 0:e11e8793c3ce 66 int ret = eth->send(_sock_fd, packet, length);
Bongjun 0:e11e8793c3ce 67 return ret;
Bongjun 0:e11e8793c3ce 68 }
Bongjun 0:e11e8793c3ce 69
Bongjun 0:e11e8793c3ce 70 // -1 if unsuccessful, else number of bytes received
Bongjun 0:e11e8793c3ce 71 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
Bongjun 0:e11e8793c3ce 72 {
Bongjun 0:e11e8793c3ce 73 uint8_t info[8];
Bongjun 0:e11e8793c3ce 74 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
Bongjun 0:e11e8793c3ce 75 if (size < 0) {
Bongjun 0:e11e8793c3ce 76 return -1;
Bongjun 0:e11e8793c3ce 77 }
Bongjun 0:e11e8793c3ce 78 eth->recv(_sock_fd, (char*)info, sizeof(info));
Bongjun 0:e11e8793c3ce 79 readEndpoint(remote, info);
Bongjun 0:e11e8793c3ce 80 int udp_size = info[6]<<8|info[7];
Bongjun 0:e11e8793c3ce 81 //TEST_ASSERT(udp_size <= (size-sizeof(info)));
Bongjun 0:e11e8793c3ce 82 if (udp_size > (size-sizeof(info))) {
Bongjun 0:e11e8793c3ce 83 return -1;
Bongjun 0:e11e8793c3ce 84 }
Bongjun 8:7609a4bd55fd 85 /* Perform Length check here to prevent buffer overrun */
Bongjun 8:7609a4bd55fd 86 /* fixed by Sean Newton (https://developer.mbed.org/users/SeanNewton/) */
Bongjun 8:7609a4bd55fd 87 if (udp_size > length) {
Bongjun 8:7609a4bd55fd 88 //printf("udp_size: %d\n",udp_size);
Bongjun 8:7609a4bd55fd 89 return -1;
Bongjun 8:7609a4bd55fd 90 }
Bongjun 0:e11e8793c3ce 91 return eth->recv(_sock_fd, buffer, udp_size);
Bongjun 0:e11e8793c3ce 92 }
Bongjun 0:e11e8793c3ce 93
Bongjun 0:e11e8793c3ce 94 void UDPSocket::confEndpoint(Endpoint & ep)
Bongjun 0:e11e8793c3ce 95 {
Bongjun 0:e11e8793c3ce 96 char * host = ep.get_address();
Bongjun 0:e11e8793c3ce 97 // set remote host
Bongjun 0:e11e8793c3ce 98 eth->sreg_ip(_sock_fd, Sn_DIPR, host);
Bongjun 0:e11e8793c3ce 99 // set remote port
Bongjun 0:e11e8793c3ce 100 eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
Bongjun 0:e11e8793c3ce 101 }
Bongjun 0:e11e8793c3ce 102
Bongjun 0:e11e8793c3ce 103 void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
Bongjun 0:e11e8793c3ce 104 {
Bongjun 0:e11e8793c3ce 105 char addr[17];
Bongjun 0:e11e8793c3ce 106 snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
Bongjun 0:e11e8793c3ce 107 uint16_t port = info[4]<<8|info[5];
Bongjun 0:e11e8793c3ce 108 ep.set_address(addr, port);
Bongjun 0:e11e8793c3ce 109 }