| 1 | /**************************************************************************/ |
| 2 | /* packet_peer_udp.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "packet_peer_udp.h" |
| 32 | |
| 33 | #include "core/io/ip.h" |
| 34 | #include "core/io/udp_server.h" |
| 35 | |
| 36 | void PacketPeerUDP::set_blocking_mode(bool p_enable) { |
| 37 | blocking = p_enable; |
| 38 | } |
| 39 | |
| 40 | void PacketPeerUDP::set_broadcast_enabled(bool p_enabled) { |
| 41 | ERR_FAIL_COND(udp_server); |
| 42 | broadcast = p_enabled; |
| 43 | if (_sock.is_valid() && _sock->is_open()) { |
| 44 | _sock->set_broadcasting_enabled(p_enabled); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | Error PacketPeerUDP::join_multicast_group(IPAddress p_multi_address, const String &p_if_name) { |
| 49 | ERR_FAIL_COND_V(udp_server, ERR_LOCKED); |
| 50 | ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); |
| 51 | ERR_FAIL_COND_V(!p_multi_address.is_valid(), ERR_INVALID_PARAMETER); |
| 52 | |
| 53 | if (!_sock->is_open()) { |
| 54 | IP::Type ip_type = p_multi_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; |
| 55 | Error err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_UDP, ip_type); |
| 56 | ERR_FAIL_COND_V(err != OK, err); |
| 57 | _sock->set_blocking_enabled(false); |
| 58 | _sock->set_broadcasting_enabled(broadcast); |
| 59 | } |
| 60 | return _sock->join_multicast_group(p_multi_address, p_if_name); |
| 61 | } |
| 62 | |
| 63 | Error PacketPeerUDP::leave_multicast_group(IPAddress p_multi_address, const String &p_if_name) { |
| 64 | ERR_FAIL_COND_V(udp_server, ERR_LOCKED); |
| 65 | ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); |
| 66 | ERR_FAIL_COND_V(!_sock->is_open(), ERR_UNCONFIGURED); |
| 67 | return _sock->leave_multicast_group(p_multi_address, p_if_name); |
| 68 | } |
| 69 | |
| 70 | String PacketPeerUDP::_get_packet_ip() const { |
| 71 | return String(get_packet_address()); |
| 72 | } |
| 73 | |
| 74 | Error PacketPeerUDP::_set_dest_address(const String &p_address, int p_port) { |
| 75 | IPAddress ip; |
| 76 | if (p_address.is_valid_ip_address()) { |
| 77 | ip = p_address; |
| 78 | } else { |
| 79 | ip = IP::get_singleton()->resolve_hostname(p_address); |
| 80 | if (!ip.is_valid()) { |
| 81 | return ERR_CANT_RESOLVE; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | set_dest_address(ip, p_port); |
| 86 | return OK; |
| 87 | } |
| 88 | |
| 89 | int PacketPeerUDP::get_available_packet_count() const { |
| 90 | // TODO we should deprecate this, and expose poll instead! |
| 91 | Error err = const_cast<PacketPeerUDP *>(this)->_poll(); |
| 92 | if (err != OK) { |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | return queue_count; |
| 97 | } |
| 98 | |
| 99 | Error PacketPeerUDP::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { |
| 100 | Error err = _poll(); |
| 101 | if (err != OK) { |
| 102 | return err; |
| 103 | } |
| 104 | if (queue_count == 0) { |
| 105 | return ERR_UNAVAILABLE; |
| 106 | } |
| 107 | |
| 108 | /* Bogus GCC warning here: |
| 109 | * In member function 'int RingBuffer<T>::read(T*, int, bool) [with T = unsigned char]', |
| 110 | * inlined from 'virtual Error PacketPeerUDP::get_packet(const uint8_t**, int&)' at core/io/packet_peer_udp.cpp:112:9, |
| 111 | * inlined from 'virtual Error PacketPeerUDP::get_packet(const uint8_t**, int&)' at core/io/packet_peer_udp.cpp:99:7: |
| 112 | * Error: ./core/ring_buffer.h:68:46: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=] |
| 113 | * 68 | p_buf[dst++] = read[pos + i]; |
| 114 | * | ~~~~~~~~~~~~~^~~~~~~ |
| 115 | */ |
| 116 | GODOT_GCC_WARNING_PUSH |
| 117 | GODOT_GCC_PRAGMA(GCC diagnostic warning "-Wstringop-overflow=0") // Can't "ignore" this for some reason. |
| 118 | |
| 119 | uint32_t size = 0; |
| 120 | uint8_t ipv6[16] = {}; |
| 121 | rb.read(ipv6, 16, true); |
| 122 | packet_ip.set_ipv6(ipv6); |
| 123 | rb.read((uint8_t *)&packet_port, 4, true); |
| 124 | rb.read((uint8_t *)&size, 4, true); |
| 125 | rb.read(packet_buffer, size, true); |
| 126 | --queue_count; |
| 127 | *r_buffer = packet_buffer; |
| 128 | r_buffer_size = size; |
| 129 | |
| 130 | GODOT_GCC_WARNING_POP |
| 131 | |
| 132 | return OK; |
| 133 | } |
| 134 | |
| 135 | Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) { |
| 136 | ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); |
| 137 | ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED); |
| 138 | |
| 139 | Error err; |
| 140 | int sent = -1; |
| 141 | |
| 142 | if (!_sock->is_open()) { |
| 143 | IP::Type ip_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; |
| 144 | err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_UDP, ip_type); |
| 145 | ERR_FAIL_COND_V(err != OK, err); |
| 146 | _sock->set_blocking_enabled(false); |
| 147 | _sock->set_broadcasting_enabled(broadcast); |
| 148 | } |
| 149 | |
| 150 | do { |
| 151 | if (connected && !udp_server) { |
| 152 | err = _sock->send(p_buffer, p_buffer_size, sent); |
| 153 | } else { |
| 154 | err = _sock->sendto(p_buffer, p_buffer_size, sent, peer_addr, peer_port); |
| 155 | } |
| 156 | if (err != OK) { |
| 157 | if (err != ERR_BUSY) { |
| 158 | return FAILED; |
| 159 | } else if (!blocking) { |
| 160 | return ERR_BUSY; |
| 161 | } |
| 162 | // Keep trying to send full packet |
| 163 | continue; |
| 164 | } |
| 165 | return OK; |
| 166 | |
| 167 | } while (sent != p_buffer_size); |
| 168 | |
| 169 | return OK; |
| 170 | } |
| 171 | |
| 172 | int PacketPeerUDP::get_max_packet_size() const { |
| 173 | return 512; // uhm maybe not |
| 174 | } |
| 175 | |
| 176 | Error PacketPeerUDP::bind(int p_port, const IPAddress &p_bind_address, int p_recv_buffer_size) { |
| 177 | ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); |
| 178 | ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); |
| 179 | ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); |
| 180 | ERR_FAIL_COND_V_MSG(p_port < 0 || p_port > 65535, ERR_INVALID_PARAMETER, "The local port number must be between 0 and 65535 (inclusive)."); |
| 181 | |
| 182 | Error err; |
| 183 | IP::Type ip_type = IP::TYPE_ANY; |
| 184 | |
| 185 | if (p_bind_address.is_valid()) { |
| 186 | ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; |
| 187 | } |
| 188 | |
| 189 | err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_UDP, ip_type); |
| 190 | |
| 191 | if (err != OK) { |
| 192 | return ERR_CANT_CREATE; |
| 193 | } |
| 194 | |
| 195 | _sock->set_blocking_enabled(false); |
| 196 | _sock->set_broadcasting_enabled(broadcast); |
| 197 | NetSocket::Address addr(p_bind_address, p_port); |
| 198 | err = _sock->bind(addr); |
| 199 | |
| 200 | if (err != OK) { |
| 201 | _sock->close(); |
| 202 | return err; |
| 203 | } |
| 204 | rb.resize(nearest_shift((uint32_t)p_recv_buffer_size)); |
| 205 | return OK; |
| 206 | } |
| 207 | |
| 208 | Error PacketPeerUDP::connect_shared_socket(Ref<NetSocket> p_sock, IPAddress p_ip, uint16_t p_port, UDPServer *p_server) { |
| 209 | udp_server = p_server; |
| 210 | connected = true; |
| 211 | _sock = p_sock; |
| 212 | peer_addr = p_ip; |
| 213 | peer_port = p_port; |
| 214 | packet_ip = peer_addr; |
| 215 | packet_port = peer_port; |
| 216 | return OK; |
| 217 | } |
| 218 | |
| 219 | void PacketPeerUDP::disconnect_shared_socket() { |
| 220 | udp_server = nullptr; |
| 221 | _sock = Ref<NetSocket>(NetSocket::create()); |
| 222 | close(); |
| 223 | } |
| 224 | |
| 225 | Error PacketPeerUDP::connect_to_host(const IPAddress &p_host, int p_port) { |
| 226 | ERR_FAIL_COND_V(udp_server, ERR_LOCKED); |
| 227 | ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); |
| 228 | ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); |
| 229 | ERR_FAIL_COND_V_MSG(p_port < 1 || p_port > 65535, ERR_INVALID_PARAMETER, "The remote port number must be between 1 and 65535 (inclusive)."); |
| 230 | |
| 231 | Error err; |
| 232 | |
| 233 | if (!_sock->is_open()) { |
| 234 | IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; |
| 235 | err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_UDP, ip_type); |
| 236 | ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN); |
| 237 | _sock->set_blocking_enabled(false); |
| 238 | } |
| 239 | |
| 240 | NetSocket::Address addr(p_host, p_port); |
| 241 | err = _sock->connect_to_host(addr); |
| 242 | |
| 243 | // I see no reason why we should get ERR_BUSY (wouldblock/eagain) here. |
| 244 | // This is UDP, so connect is only used to tell the OS to which socket |
| 245 | // it should deliver packets when multiple are bound on the same address/port. |
| 246 | if (err != OK) { |
| 247 | close(); |
| 248 | ERR_FAIL_V_MSG(FAILED, "Unable to connect"); |
| 249 | } |
| 250 | |
| 251 | connected = true; |
| 252 | |
| 253 | peer_addr = p_host; |
| 254 | peer_port = p_port; |
| 255 | |
| 256 | // Flush any packet we might still have in queue. |
| 257 | rb.clear(); |
| 258 | return OK; |
| 259 | } |
| 260 | |
| 261 | bool PacketPeerUDP::is_socket_connected() const { |
| 262 | return connected; |
| 263 | } |
| 264 | |
| 265 | void PacketPeerUDP::close() { |
| 266 | if (udp_server) { |
| 267 | udp_server->remove_peer(peer_addr, peer_port); |
| 268 | udp_server = nullptr; |
| 269 | _sock = Ref<NetSocket>(NetSocket::create()); |
| 270 | } else if (_sock.is_valid()) { |
| 271 | _sock->close(); |
| 272 | } |
| 273 | rb.resize(16); |
| 274 | queue_count = 0; |
| 275 | connected = false; |
| 276 | } |
| 277 | |
| 278 | Error PacketPeerUDP::wait() { |
| 279 | ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); |
| 280 | return _sock->poll(NetSocket::POLL_TYPE_IN, -1); |
| 281 | } |
| 282 | |
| 283 | Error PacketPeerUDP::_poll() { |
| 284 | ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); |
| 285 | |
| 286 | if (!_sock->is_open()) { |
| 287 | return FAILED; |
| 288 | } |
| 289 | if (udp_server) { |
| 290 | return OK; // Handled by UDPServer. |
| 291 | } |
| 292 | |
| 293 | Error err; |
| 294 | int read; |
| 295 | IPAddress ip; |
| 296 | uint16_t port; |
| 297 | |
| 298 | while (true) { |
| 299 | if (connected) { |
| 300 | err = _sock->recv(recv_buffer, sizeof(recv_buffer), read); |
| 301 | ip = peer_addr; |
| 302 | port = peer_port; |
| 303 | } else { |
| 304 | err = _sock->recvfrom(recv_buffer, sizeof(recv_buffer), read, ip, port); |
| 305 | } |
| 306 | |
| 307 | if (err != OK) { |
| 308 | if (err == ERR_BUSY) { |
| 309 | break; |
| 310 | } |
| 311 | return FAILED; |
| 312 | } |
| 313 | |
| 314 | err = store_packet(ip, port, recv_buffer, read); |
| 315 | #ifdef TOOLS_ENABLED |
| 316 | if (err != OK) { |
| 317 | WARN_PRINT("Buffer full, dropping packets!"); |
| 318 | } |
| 319 | #endif |
| 320 | } |
| 321 | |
| 322 | return OK; |
| 323 | } |
| 324 | |
| 325 | Error PacketPeerUDP::store_packet(IPAddress p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size) { |
| 326 | if (rb.space_left() < p_buf_size + 24) { |
| 327 | return ERR_OUT_OF_MEMORY; |
| 328 | } |
| 329 | rb.write(p_ip.get_ipv6(), 16); |
| 330 | rb.write((uint8_t *)&p_port, 4); |
| 331 | rb.write((uint8_t *)&p_buf_size, 4); |
| 332 | rb.write(p_buf, p_buf_size); |
| 333 | ++queue_count; |
| 334 | return OK; |
| 335 | } |
| 336 | |
| 337 | bool PacketPeerUDP::is_bound() const { |
| 338 | return _sock.is_valid() && _sock->is_open(); |
| 339 | } |
| 340 | |
| 341 | IPAddress PacketPeerUDP::get_packet_address() const { |
| 342 | return packet_ip; |
| 343 | } |
| 344 | |
| 345 | int PacketPeerUDP::get_packet_port() const { |
| 346 | return packet_port; |
| 347 | } |
| 348 | |
| 349 | int PacketPeerUDP::get_local_port() const { |
| 350 | NetSocket::Address addr; |
| 351 | _sock->get_socket_address(&addr); |
| 352 | return addr.port(); |
| 353 | } |
| 354 | |
| 355 | void PacketPeerUDP::set_dest_address(const IPAddress &p_address, int p_port) { |
| 356 | ERR_FAIL_COND_MSG(connected, "Destination address cannot be set for connected sockets"); |
| 357 | peer_addr = p_address; |
| 358 | peer_port = p_port; |
| 359 | } |
| 360 | |
| 361 | void PacketPeerUDP::_bind_methods() { |
| 362 | ClassDB::bind_method(D_METHOD("bind", "port", "bind_address", "recv_buf_size"), &PacketPeerUDP::bind, DEFVAL("*"), DEFVAL(65536)); |
| 363 | ClassDB::bind_method(D_METHOD("close"), &PacketPeerUDP::close); |
| 364 | ClassDB::bind_method(D_METHOD("wait"), &PacketPeerUDP::wait); |
| 365 | ClassDB::bind_method(D_METHOD("is_bound"), &PacketPeerUDP::is_bound); |
| 366 | ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &PacketPeerUDP::connect_to_host); |
| 367 | ClassDB::bind_method(D_METHOD("is_socket_connected"), &PacketPeerUDP::is_socket_connected); |
| 368 | ClassDB::bind_method(D_METHOD("get_packet_ip"), &PacketPeerUDP::_get_packet_ip); |
| 369 | ClassDB::bind_method(D_METHOD("get_packet_port"), &PacketPeerUDP::get_packet_port); |
| 370 | ClassDB::bind_method(D_METHOD("get_local_port"), &PacketPeerUDP::get_local_port); |
| 371 | ClassDB::bind_method(D_METHOD("set_dest_address", "host", "port"), &PacketPeerUDP::_set_dest_address); |
| 372 | ClassDB::bind_method(D_METHOD("set_broadcast_enabled", "enabled"), &PacketPeerUDP::set_broadcast_enabled); |
| 373 | ClassDB::bind_method(D_METHOD("join_multicast_group", "multicast_address", "interface_name"), &PacketPeerUDP::join_multicast_group); |
| 374 | ClassDB::bind_method(D_METHOD("leave_multicast_group", "multicast_address", "interface_name"), &PacketPeerUDP::leave_multicast_group); |
| 375 | } |
| 376 | |
| 377 | PacketPeerUDP::PacketPeerUDP() : |
| 378 | _sock(Ref<NetSocket>(NetSocket::create())) { |
| 379 | rb.resize(16); |
| 380 | } |
| 381 | |
| 382 | PacketPeerUDP::~PacketPeerUDP() { |
| 383 | close(); |
| 384 | } |