mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* Copyright (C) 2012 mbed.org, MIT License
elessair 0:f269e3021894 2 *
elessair 0:f269e3021894 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
elessair 0:f269e3021894 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
elessair 0:f269e3021894 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
elessair 0:f269e3021894 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
elessair 0:f269e3021894 7 * furnished to do so, subject to the following conditions:
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * The above copyright notice and this permission notice shall be included in all copies or
elessair 0:f269e3021894 10 * substantial portions of the Software.
elessair 0:f269e3021894 11 *
elessair 0:f269e3021894 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
elessair 0:f269e3021894 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
elessair 0:f269e3021894 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
elessair 0:f269e3021894 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
elessair 0:f269e3021894 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
elessair 0:f269e3021894 17 */
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #include "Socket/UDPSocket.h"
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 #include <cstring>
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 using std::memset;
elessair 0:f269e3021894 24
elessair 0:f269e3021894 25 UDPSocket::UDPSocket() {
elessair 0:f269e3021894 26 }
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 int UDPSocket::init(void) {
elessair 0:f269e3021894 29 return init_socket(SOCK_DGRAM);
elessair 0:f269e3021894 30 }
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 // Server initialization
elessair 0:f269e3021894 33 int UDPSocket::bind(int port) {
elessair 0:f269e3021894 34 if (init_socket(SOCK_DGRAM) < 0)
elessair 0:f269e3021894 35 return -1;
elessair 0:f269e3021894 36
elessair 0:f269e3021894 37 struct sockaddr_in localHost;
elessair 0:f269e3021894 38 std::memset(&localHost, 0, sizeof(localHost));
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 localHost.sin_family = AF_INET;
elessair 0:f269e3021894 41 localHost.sin_port = htons(port);
elessair 0:f269e3021894 42 localHost.sin_addr.s_addr = INADDR_ANY;
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
elessair 0:f269e3021894 45 close();
elessair 0:f269e3021894 46 return -1;
elessair 0:f269e3021894 47 }
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 return 0;
elessair 0:f269e3021894 50 }
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52 int UDPSocket::join_multicast_group(const char* address) {
elessair 0:f269e3021894 53 struct ip_mreq mreq;
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 // Set up group address
elessair 0:f269e3021894 56 mreq.imr_multiaddr.s_addr = inet_addr(address);
elessair 0:f269e3021894 57 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 return set_option(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
elessair 0:f269e3021894 60 }
elessair 0:f269e3021894 61
elessair 0:f269e3021894 62 int UDPSocket::set_broadcasting(bool broadcast) {
elessair 0:f269e3021894 63 int option = (broadcast) ? (1) : (0);
elessair 0:f269e3021894 64 return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
elessair 0:f269e3021894 65 }
elessair 0:f269e3021894 66
elessair 0:f269e3021894 67 // -1 if unsuccessful, else number of bytes written
elessair 0:f269e3021894 68 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
elessair 0:f269e3021894 69 if (_sock_fd < 0)
elessair 0:f269e3021894 70 return -1;
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 if (!_blocking) {
elessair 0:f269e3021894 73 TimeInterval timeout(_timeout);
elessair 0:f269e3021894 74 if (wait_writable(timeout) != 0)
elessair 0:f269e3021894 75 return 0;
elessair 0:f269e3021894 76 }
elessair 0:f269e3021894 77
elessair 0:f269e3021894 78 return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
elessair 0:f269e3021894 79 }
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 // -1 if unsuccessful, else number of bytes received
elessair 0:f269e3021894 82 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
elessair 0:f269e3021894 83 if (_sock_fd < 0)
elessair 0:f269e3021894 84 return -1;
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 if (!_blocking) {
elessair 0:f269e3021894 87 TimeInterval timeout(_timeout);
elessair 0:f269e3021894 88 if (wait_readable(timeout) != 0)
elessair 0:f269e3021894 89 return 0;
elessair 0:f269e3021894 90 }
elessair 0:f269e3021894 91 remote.reset_address();
elessair 0:f269e3021894 92 socklen_t remoteHostLen = sizeof(remote._remoteHost);
elessair 0:f269e3021894 93 return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
elessair 0:f269e3021894 94 }