victor qiu / Mbed OS SimpleNTP

Fork of SimpleNTP by Akinori Hashimoto

Committer:
victorqiu2
Date:
Tue Aug 01 01:22:01 2017 +0000
Revision:
2:68ae27667d82
A

Who changed what in which revision?

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