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