add getsock(), setsock()
Fork of WIZnetInterface by
Socket/UDPSocket.cpp@0:6f28332c466f, 2015-06-15 (annotated)
- Committer:
- Soohwan Kim
- Date:
- Mon Jun 15 11:18:37 2015 +0900
- Revision:
- 0:6f28332c466f
initial version
Who changed what in which revision?
User | Revision | Line number | New 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 "UDPSocket.h" |
Soohwan Kim |
0:6f28332c466f | 20 | |
Soohwan Kim |
0:6f28332c466f | 21 | static int udp_local_port; |
Soohwan Kim |
0:6f28332c466f | 22 | |
Soohwan Kim |
0:6f28332c466f | 23 | UDPSocket::UDPSocket() |
Soohwan Kim |
0:6f28332c466f | 24 | { |
Soohwan Kim |
0:6f28332c466f | 25 | } |
Soohwan Kim |
0:6f28332c466f | 26 | // After init function, bind() should be called. |
Soohwan Kim |
0:6f28332c466f | 27 | int UDPSocket::init(void) |
Soohwan Kim |
0:6f28332c466f | 28 | { |
Soohwan Kim |
0:6f28332c466f | 29 | if (_sock_fd < 0) { |
Soohwan Kim |
0:6f28332c466f | 30 | _sock_fd = eth->new_socket(); |
Soohwan Kim |
0:6f28332c466f | 31 | } |
Soohwan Kim |
0:6f28332c466f | 32 | if (eth->setProtocol(_sock_fd, WIZnet_Chip::UDP) == false) return -1; |
Soohwan Kim |
0:6f28332c466f | 33 | return 0; |
Soohwan Kim |
0:6f28332c466f | 34 | } |
Soohwan Kim |
0:6f28332c466f | 35 | |
Soohwan Kim |
0:6f28332c466f | 36 | // Server initialization |
Soohwan Kim |
0:6f28332c466f | 37 | int UDPSocket::bind(int port) |
Soohwan Kim |
0:6f28332c466f | 38 | { |
Soohwan Kim |
0:6f28332c466f | 39 | if (_sock_fd < 0) { |
Soohwan Kim |
0:6f28332c466f | 40 | _sock_fd = eth->new_socket(); |
Soohwan Kim |
0:6f28332c466f | 41 | if (_sock_fd < 0) { |
Soohwan Kim |
0:6f28332c466f | 42 | return -1; |
Soohwan Kim |
0:6f28332c466f | 43 | } |
Soohwan Kim |
0:6f28332c466f | 44 | } |
Soohwan Kim |
0:6f28332c466f | 45 | // set local port |
Soohwan Kim |
0:6f28332c466f | 46 | if (port != 0) { |
Soohwan Kim |
0:6f28332c466f | 47 | eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); |
Soohwan Kim |
0:6f28332c466f | 48 | } else { |
Soohwan Kim |
0:6f28332c466f | 49 | udp_local_port++; |
Soohwan Kim |
0:6f28332c466f | 50 | eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port); |
Soohwan Kim |
0:6f28332c466f | 51 | } |
Soohwan Kim |
0:6f28332c466f | 52 | // set udp protocol |
Soohwan Kim |
0:6f28332c466f | 53 | eth->setProtocol(_sock_fd, WIZnet_Chip::UDP); |
Soohwan Kim |
0:6f28332c466f | 54 | eth->scmd(_sock_fd, WIZnet_Chip::OPEN); |
Soohwan Kim |
0:6f28332c466f | 55 | return 0; |
Soohwan Kim |
0:6f28332c466f | 56 | } |
Soohwan Kim |
0:6f28332c466f | 57 | |
Soohwan Kim |
0:6f28332c466f | 58 | // -1 if unsuccessful, else number of bytes written |
Soohwan Kim |
0:6f28332c466f | 59 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
Soohwan Kim |
0:6f28332c466f | 60 | { |
Soohwan Kim |
0:6f28332c466f | 61 | int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1); |
Soohwan Kim |
0:6f28332c466f | 62 | if (size < 0) { |
Soohwan Kim |
0:6f28332c466f | 63 | return -1; |
Soohwan Kim |
0:6f28332c466f | 64 | } |
Soohwan Kim |
0:6f28332c466f | 65 | confEndpoint(remote); |
Soohwan Kim |
0:6f28332c466f | 66 | int ret = eth->send(_sock_fd, packet, length); |
Soohwan Kim |
0:6f28332c466f | 67 | return ret; |
Soohwan Kim |
0:6f28332c466f | 68 | } |
Soohwan Kim |
0:6f28332c466f | 69 | |
Soohwan Kim |
0:6f28332c466f | 70 | // -1 if unsuccessful, else number of bytes received |
Soohwan Kim |
0:6f28332c466f | 71 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
Soohwan Kim |
0:6f28332c466f | 72 | { |
Soohwan Kim |
0:6f28332c466f | 73 | uint8_t info[8]; |
Soohwan Kim |
0:6f28332c466f | 74 | int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info)); |
Soohwan Kim |
0:6f28332c466f | 75 | if (size < 0) { |
Soohwan Kim |
0:6f28332c466f | 76 | return -1; |
Soohwan Kim |
0:6f28332c466f | 77 | } |
Soohwan Kim |
0:6f28332c466f | 78 | eth->recv(_sock_fd, (char*)info, sizeof(info)); |
Soohwan Kim |
0:6f28332c466f | 79 | readEndpoint(remote, info); |
Soohwan Kim |
0:6f28332c466f | 80 | int udp_size = info[6]<<8|info[7]; |
Soohwan Kim |
0:6f28332c466f | 81 | //TEST_ASSERT(udp_size <= (size-sizeof(info))); |
Soohwan Kim |
0:6f28332c466f | 82 | if (udp_size > (size-sizeof(info))) { |
Soohwan Kim |
0:6f28332c466f | 83 | return -1; |
Soohwan Kim |
0:6f28332c466f | 84 | } |
Soohwan Kim |
0:6f28332c466f | 85 | |
Soohwan Kim |
0:6f28332c466f | 86 | /* Perform Length check here to prevent buffer overrun */ |
Soohwan Kim |
0:6f28332c466f | 87 | /* fixed by Sean Newton (https://developer.mbed.org/users/SeanNewton/) */ |
Soohwan Kim |
0:6f28332c466f | 88 | if (udp_size > length) { |
Soohwan Kim |
0:6f28332c466f | 89 | //printf("udp_size: %d\n",udp_size); |
Soohwan Kim |
0:6f28332c466f | 90 | return -1; |
Soohwan Kim |
0:6f28332c466f | 91 | } |
Soohwan Kim |
0:6f28332c466f | 92 | return eth->recv(_sock_fd, buffer, udp_size); |
Soohwan Kim |
0:6f28332c466f | 93 | } |
Soohwan Kim |
0:6f28332c466f | 94 | |
Soohwan Kim |
0:6f28332c466f | 95 | void UDPSocket::confEndpoint(Endpoint & ep) |
Soohwan Kim |
0:6f28332c466f | 96 | { |
Soohwan Kim |
0:6f28332c466f | 97 | char * host = ep.get_address(); |
Soohwan Kim |
0:6f28332c466f | 98 | // set remote host |
Soohwan Kim |
0:6f28332c466f | 99 | eth->sreg_ip(_sock_fd, Sn_DIPR, host); |
Soohwan Kim |
0:6f28332c466f | 100 | // set remote port |
Soohwan Kim |
0:6f28332c466f | 101 | eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port()); |
Soohwan Kim |
0:6f28332c466f | 102 | } |
Soohwan Kim |
0:6f28332c466f | 103 | |
Soohwan Kim |
0:6f28332c466f | 104 | void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[]) |
Soohwan Kim |
0:6f28332c466f | 105 | { |
Soohwan Kim |
0:6f28332c466f | 106 | char addr[17]; |
Soohwan Kim |
0:6f28332c466f | 107 | snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]); |
Soohwan Kim |
0:6f28332c466f | 108 | uint16_t port = info[4]<<8|info[5]; |
Soohwan Kim |
0:6f28332c466f | 109 | ep.set_address(addr, port); |
Soohwan Kim |
0:6f28332c466f | 110 | } |
Soohwan Kim |
0:6f28332c466f | 111 |