WIZnetInterface using namespace

Dependents:   DualNetworkInterface-Basic

Fork of WIZnetInterface by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.cpp Source File

UDPSocket.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "UDPSocket.hpp"
00020 
00021 using namespace wiznet_space;
00022 
00023 static int udp_local_port;
00024 
00025 UDPSocket::UDPSocket()
00026 {
00027 }
00028 // After init function, bind() should be called.
00029 int UDPSocket::init(void)
00030 {
00031     if (_sock_fd < 0) {
00032         _sock_fd = eth->new_socket();
00033     }
00034     if (eth->setProtocol(_sock_fd, WIZnet_Chip::UDP) == false) return -1;
00035     return 0;
00036 }
00037 
00038 // Server initialization
00039 int UDPSocket::bind(int port)
00040 {
00041     if (_sock_fd < 0) {
00042         _sock_fd = eth->new_socket();
00043         if (_sock_fd < 0) {
00044             return -1;
00045         }
00046     }
00047     // set local port
00048     if (port != 0) {
00049         eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
00050     } else {
00051         udp_local_port++;
00052         eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port);
00053     }
00054     // set udp protocol
00055     eth->setProtocol(_sock_fd, WIZnet_Chip::UDP);
00056     eth->scmd(_sock_fd, WIZnet_Chip::OPEN);
00057     return 0;
00058 }
00059 
00060 // -1 if unsuccessful, else number of bytes written
00061 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
00062 {
00063     int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
00064     if (size < 0) {
00065         return -1;
00066     }
00067     confEndpoint(remote);
00068     int ret = eth->send(_sock_fd, packet, length);
00069     return ret;
00070 }
00071 
00072 // -1 if unsuccessful, else number of bytes received
00073 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
00074 {
00075     uint8_t info[8];
00076     int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
00077     if (size < 0) {
00078         return -1;
00079     }
00080     eth->recv(_sock_fd, (char*)info, sizeof(info));
00081     readEndpoint(remote, info);
00082     int udp_size = info[6]<<8|info[7];
00083     //TEST_ASSERT(udp_size <= (size-sizeof(info)));
00084     if (udp_size > (size-sizeof(info))) {
00085         return -1;
00086     }
00087 
00088     /* Perform Length check here to prevent buffer overrun */
00089     /* fixed by Sean Newton (https://developer.mbed.org/users/SeanNewton/) */
00090     if (udp_size > length) {
00091         //printf("udp_size: %d\n",udp_size);
00092         return -1;
00093     }
00094     return eth->recv(_sock_fd, buffer, udp_size);
00095 }
00096 
00097 void UDPSocket::confEndpoint(Endpoint & ep)
00098 {
00099     char * host = ep.get_address();
00100     // set remote host
00101     eth->sreg_ip(_sock_fd, Sn_DIPR, host);
00102     // set remote port
00103     eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
00104 }
00105 
00106 void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
00107 {
00108     char addr[17];
00109     snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
00110     uint16_t port = info[4]<<8|info[5];
00111     ep.set_address(addr, port);
00112 }
00113