wip
Dependents: EthernetInterface_vz
Fork of Socket by
UDPSocket.cpp@1:8080965f5d76, 2012-06-26 (annotated)
- Committer:
- donatien
- Date:
- Tue Jun 26 15:32:12 2012 +0000
- Revision:
- 1:8080965f5d76
- Parent:
- 0:1f77255a22f5
- Child:
- 2:b227d242f3c7
Changed data type from uint8_t* to char*
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 | |
donatien | 0:1f77255a22f5 | 19 | #include "UDPSocket.h" |
donatien | 0:1f77255a22f5 | 20 | #include "bsd_socket.h" |
donatien | 0:1f77255a22f5 | 21 | |
donatien | 0:1f77255a22f5 | 22 | #include <cstring> |
donatien | 0:1f77255a22f5 | 23 | |
donatien | 0:1f77255a22f5 | 24 | using std::memset; |
donatien | 0:1f77255a22f5 | 25 | |
donatien | 0:1f77255a22f5 | 26 | UDPSocket::UDPSocket() : m_sock(-1) |
donatien | 0:1f77255a22f5 | 27 | { |
donatien | 0:1f77255a22f5 | 28 | } |
donatien | 0:1f77255a22f5 | 29 | |
donatien | 0:1f77255a22f5 | 30 | UDPSocket::~UDPSocket() |
donatien | 0:1f77255a22f5 | 31 | { |
donatien | 0:1f77255a22f5 | 32 | close(); //Don't want to leak |
donatien | 0:1f77255a22f5 | 33 | } |
donatien | 0:1f77255a22f5 | 34 | |
donatien | 0:1f77255a22f5 | 35 | int UDPSocket::bind(int port) |
donatien | 0:1f77255a22f5 | 36 | { |
donatien | 0:1f77255a22f5 | 37 | int ret = init(); |
donatien | 0:1f77255a22f5 | 38 | if( ret < 0 ) |
donatien | 0:1f77255a22f5 | 39 | { |
donatien | 0:1f77255a22f5 | 40 | return -1; |
donatien | 0:1f77255a22f5 | 41 | } |
donatien | 0:1f77255a22f5 | 42 | |
donatien | 0:1f77255a22f5 | 43 | struct sockaddr_in localHost; |
donatien | 0:1f77255a22f5 | 44 | std::memset(&localHost, 0, sizeof(localHost)); |
donatien | 0:1f77255a22f5 | 45 | |
donatien | 0:1f77255a22f5 | 46 | localHost.sin_family = AF_INET; |
donatien | 0:1f77255a22f5 | 47 | localHost.sin_port = htons(port); |
donatien | 0:1f77255a22f5 | 48 | localHost.sin_addr.s_addr = INADDR_ANY; |
donatien | 0:1f77255a22f5 | 49 | |
donatien | 0:1f77255a22f5 | 50 | ret = ::bind(m_sock, (const struct sockaddr *)&localHost, sizeof(localHost)); |
donatien | 0:1f77255a22f5 | 51 | if (ret < 0) |
donatien | 0:1f77255a22f5 | 52 | { |
donatien | 0:1f77255a22f5 | 53 | close(); |
donatien | 0:1f77255a22f5 | 54 | return -1; |
donatien | 0:1f77255a22f5 | 55 | } |
donatien | 0:1f77255a22f5 | 56 | |
donatien | 0:1f77255a22f5 | 57 | return 0; |
donatien | 0:1f77255a22f5 | 58 | } |
donatien | 0:1f77255a22f5 | 59 | |
donatien | 0:1f77255a22f5 | 60 | // -1 if unsuccessful, else number of bytes written |
donatien | 1:8080965f5d76 | 61 | int UDPSocket::sendTo(char* data, int length, char* host, int port, int timeout) |
donatien | 0:1f77255a22f5 | 62 | { |
donatien | 0:1f77255a22f5 | 63 | if( m_sock < 0 ) |
donatien | 0:1f77255a22f5 | 64 | { |
donatien | 0:1f77255a22f5 | 65 | return -1; |
donatien | 0:1f77255a22f5 | 66 | } |
donatien | 0:1f77255a22f5 | 67 | |
donatien | 0:1f77255a22f5 | 68 | struct sockaddr_in remoteHost; |
donatien | 0:1f77255a22f5 | 69 | |
donatien | 0:1f77255a22f5 | 70 | //Populate m_remoteHost |
donatien | 0:1f77255a22f5 | 71 | std::memset(&remoteHost, 0, sizeof(struct sockaddr_in)); |
donatien | 0:1f77255a22f5 | 72 | |
donatien | 0:1f77255a22f5 | 73 | //Resolve DNS address or populate hard-coded IP address |
donatien | 0:1f77255a22f5 | 74 | struct hostent *server = ::gethostbyname(host); |
donatien | 0:1f77255a22f5 | 75 | if(server == NULL) |
donatien | 0:1f77255a22f5 | 76 | { |
donatien | 0:1f77255a22f5 | 77 | return -1; //Could not resolve address |
donatien | 0:1f77255a22f5 | 78 | } |
donatien | 0:1f77255a22f5 | 79 | std::memcpy((char*)&remoteHost.sin_addr.s_addr, (char*)server->h_addr_list[0], server->h_length); |
donatien | 0:1f77255a22f5 | 80 | |
donatien | 0:1f77255a22f5 | 81 | remoteHost.sin_family = AF_INET; |
donatien | 0:1f77255a22f5 | 82 | remoteHost.sin_port = htons(port); |
donatien | 0:1f77255a22f5 | 83 | |
donatien | 0:1f77255a22f5 | 84 | size_t writtenLen = 0; |
donatien | 0:1f77255a22f5 | 85 | struct timeval t_val; //t_val will be decremented on each call to select() |
donatien | 0:1f77255a22f5 | 86 | t_val.tv_sec = timeout / 1000; |
donatien | 0:1f77255a22f5 | 87 | t_val.tv_usec = (timeout - (t_val.tv_sec * 1000)) * 1000; |
donatien | 0:1f77255a22f5 | 88 | while(writtenLen < length) |
donatien | 0:1f77255a22f5 | 89 | { |
donatien | 0:1f77255a22f5 | 90 | //Wait for socket to be writeable |
donatien | 0:1f77255a22f5 | 91 | //Creating FS set |
donatien | 0:1f77255a22f5 | 92 | fd_set socksSet; |
donatien | 0:1f77255a22f5 | 93 | FD_ZERO(&socksSet); |
donatien | 0:1f77255a22f5 | 94 | FD_SET(m_sock, &socksSet); |
donatien | 0:1f77255a22f5 | 95 | |
donatien | 0:1f77255a22f5 | 96 | int ret = ::select(FD_SETSIZE, NULL, &socksSet, NULL, &t_val); |
donatien | 0:1f77255a22f5 | 97 | if(ret <= 0 || !FD_ISSET(m_sock, &socksSet)) |
donatien | 0:1f77255a22f5 | 98 | { |
donatien | 0:1f77255a22f5 | 99 | return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ? |
donatien | 0:1f77255a22f5 | 100 | } |
donatien | 0:1f77255a22f5 | 101 | |
donatien | 0:1f77255a22f5 | 102 | ret = ::sendto(m_sock, data + writtenLen, length - writtenLen, 0, (const struct sockaddr *)&remoteHost, sizeof(remoteHost)); |
donatien | 0:1f77255a22f5 | 103 | if( ret > 0) |
donatien | 0:1f77255a22f5 | 104 | { |
donatien | 0:1f77255a22f5 | 105 | writtenLen += ret; |
donatien | 0:1f77255a22f5 | 106 | continue; |
donatien | 0:1f77255a22f5 | 107 | } |
donatien | 0:1f77255a22f5 | 108 | else if( ret == 0 ) |
donatien | 0:1f77255a22f5 | 109 | { |
donatien | 0:1f77255a22f5 | 110 | return writtenLen; //Connection was closed by server -- FIXME how do we signal that the connection was closed ? |
donatien | 0:1f77255a22f5 | 111 | } |
donatien | 0:1f77255a22f5 | 112 | else |
donatien | 0:1f77255a22f5 | 113 | { |
donatien | 0:1f77255a22f5 | 114 | return -1; //Connnection error |
donatien | 0:1f77255a22f5 | 115 | } |
donatien | 0:1f77255a22f5 | 116 | } |
donatien | 0:1f77255a22f5 | 117 | |
donatien | 0:1f77255a22f5 | 118 | return writtenLen; |
donatien | 0:1f77255a22f5 | 119 | } |
donatien | 0:1f77255a22f5 | 120 | |
donatien | 0:1f77255a22f5 | 121 | // -1 if unsuccessful, else number of bytes received |
donatien | 1:8080965f5d76 | 122 | int UDPSocket::receiveFrom(char* data, int length, char** host, int* port, int timeout) |
donatien | 0:1f77255a22f5 | 123 | { |
donatien | 0:1f77255a22f5 | 124 | if( m_sock < 0 ) |
donatien | 0:1f77255a22f5 | 125 | { |
donatien | 0:1f77255a22f5 | 126 | return -1; |
donatien | 0:1f77255a22f5 | 127 | } |
donatien | 0:1f77255a22f5 | 128 | |
donatien | 0:1f77255a22f5 | 129 | struct sockaddr_in remoteHost; |
donatien | 0:1f77255a22f5 | 130 | |
donatien | 0:1f77255a22f5 | 131 | //Populate m_remoteHost |
donatien | 0:1f77255a22f5 | 132 | std::memset(&remoteHost, 0, sizeof(struct sockaddr_in)); |
donatien | 0:1f77255a22f5 | 133 | |
donatien | 0:1f77255a22f5 | 134 | socklen_t remoteHostLen = sizeof(remoteHost); |
donatien | 0:1f77255a22f5 | 135 | |
donatien | 0:1f77255a22f5 | 136 | size_t readLen = 0; |
donatien | 0:1f77255a22f5 | 137 | struct timeval t_val; //t_val will be decremented on each call to select() |
donatien | 0:1f77255a22f5 | 138 | t_val.tv_sec = timeout / 1000; |
donatien | 0:1f77255a22f5 | 139 | t_val.tv_usec = (timeout - (t_val.tv_sec * 1000)) * 1000; |
donatien | 0:1f77255a22f5 | 140 | |
donatien | 0:1f77255a22f5 | 141 | //No loop here as we don't want to mix packets from different sources |
donatien | 0:1f77255a22f5 | 142 | do |
donatien | 0:1f77255a22f5 | 143 | { |
donatien | 0:1f77255a22f5 | 144 | //Wait for socket to be readable |
donatien | 0:1f77255a22f5 | 145 | //Creating FS set |
donatien | 0:1f77255a22f5 | 146 | fd_set socksSet; |
donatien | 0:1f77255a22f5 | 147 | FD_ZERO(&socksSet); |
donatien | 0:1f77255a22f5 | 148 | FD_SET(m_sock, &socksSet); |
donatien | 0:1f77255a22f5 | 149 | int ret = ::select(FD_SETSIZE, &socksSet, NULL, NULL, &t_val); |
donatien | 0:1f77255a22f5 | 150 | if(ret <= 0 || !FD_ISSET(m_sock, &socksSet)) |
donatien | 0:1f77255a22f5 | 151 | { |
donatien | 0:1f77255a22f5 | 152 | break; //Timeout -- FIXME should we return -1 or writtenLength ? |
donatien | 0:1f77255a22f5 | 153 | } |
donatien | 0:1f77255a22f5 | 154 | |
donatien | 0:1f77255a22f5 | 155 | ret = ::recvfrom(m_sock, data + readLen, length - readLen, 0, (struct sockaddr*)&remoteHost, &remoteHostLen); |
donatien | 0:1f77255a22f5 | 156 | if( ret > 0) |
donatien | 0:1f77255a22f5 | 157 | { |
donatien | 0:1f77255a22f5 | 158 | readLen += ret; |
donatien | 0:1f77255a22f5 | 159 | } |
donatien | 0:1f77255a22f5 | 160 | else if( ret == 0 ) |
donatien | 0:1f77255a22f5 | 161 | { |
donatien | 0:1f77255a22f5 | 162 | //Connection was closed by server -- FIXME how do we signal that the connection was closed ? |
donatien | 0:1f77255a22f5 | 163 | //Continue and populate address |
donatien | 0:1f77255a22f5 | 164 | } |
donatien | 0:1f77255a22f5 | 165 | else |
donatien | 0:1f77255a22f5 | 166 | { |
donatien | 0:1f77255a22f5 | 167 | return -1; //Connection error |
donatien | 0:1f77255a22f5 | 168 | } |
donatien | 0:1f77255a22f5 | 169 | } while(0); |
donatien | 0:1f77255a22f5 | 170 | |
donatien | 0:1f77255a22f5 | 171 | static char hostBuf[16]; |
donatien | 0:1f77255a22f5 | 172 | inet_ntoa_r(remoteHost.sin_addr, hostBuf, sizeof(hostBuf)); |
donatien | 0:1f77255a22f5 | 173 | |
donatien | 0:1f77255a22f5 | 174 | *host = hostBuf; |
donatien | 0:1f77255a22f5 | 175 | *port = ntohs(remoteHost.sin_port); |
donatien | 0:1f77255a22f5 | 176 | |
donatien | 0:1f77255a22f5 | 177 | return readLen; |
donatien | 0:1f77255a22f5 | 178 | } |
donatien | 0:1f77255a22f5 | 179 | |
donatien | 0:1f77255a22f5 | 180 | int UDPSocket::close() |
donatien | 0:1f77255a22f5 | 181 | { |
donatien | 0:1f77255a22f5 | 182 | if( m_sock < 0 ) |
donatien | 0:1f77255a22f5 | 183 | { |
donatien | 0:1f77255a22f5 | 184 | return -1; |
donatien | 0:1f77255a22f5 | 185 | } |
donatien | 0:1f77255a22f5 | 186 | |
donatien | 0:1f77255a22f5 | 187 | ::close(m_sock); |
donatien | 0:1f77255a22f5 | 188 | m_sock = -1; |
donatien | 0:1f77255a22f5 | 189 | |
donatien | 0:1f77255a22f5 | 190 | return 0; |
donatien | 0:1f77255a22f5 | 191 | } |
donatien | 0:1f77255a22f5 | 192 | |
donatien | 0:1f77255a22f5 | 193 | int UDPSocket::init() |
donatien | 0:1f77255a22f5 | 194 | { |
donatien | 0:1f77255a22f5 | 195 | if( m_sock != -1 ) |
donatien | 0:1f77255a22f5 | 196 | { |
donatien | 0:1f77255a22f5 | 197 | return -1; |
donatien | 0:1f77255a22f5 | 198 | } |
donatien | 0:1f77255a22f5 | 199 | m_sock = ::socket(AF_INET, SOCK_DGRAM, 0); //UDP socket |
donatien | 0:1f77255a22f5 | 200 | if (m_sock < 0) |
donatien | 0:1f77255a22f5 | 201 | { |
donatien | 0:1f77255a22f5 | 202 | return -1; //Could not create socket (Out of memory / available descriptors) |
donatien | 0:1f77255a22f5 | 203 | } |
donatien | 0:1f77255a22f5 | 204 | return 0; |
donatien | 0:1f77255a22f5 | 205 | } |