packet_peer_udp.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #include "packet_peer_udp.h"
  31. #include "core/io/ip.h"
  32. #include "core/io/udp_server.h"
  33. void PacketPeerUDP::set_blocking_mode(bool p_enable) {
  34. blocking = p_enable;
  35. }
  36. void PacketPeerUDP::set_broadcast_enabled(bool p_enabled) {
  37. ERR_FAIL_COND(udp_server);
  38. broadcast = p_enabled;
  39. if (_sock.is_valid() && _sock->is_open()) {
  40. _sock->set_broadcasting_enabled(p_enabled);
  41. }
  42. }
  43. Error PacketPeerUDP::join_multicast_group(IPAddress p_multi_address, const String &p_if_name) {
  44. ERR_FAIL_COND_V(udp_server, ERR_LOCKED);
  45. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  46. ERR_FAIL_COND_V(!p_multi_address.is_valid(), ERR_INVALID_PARAMETER);
  47. if (!_sock->is_open()) {
  48. IP::Type ip_type = p_multi_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  49. Error err = _sock->open(NetSocket::TYPE_UDP, ip_type);
  50. ERR_FAIL_COND_V(err != OK, err);
  51. _sock->set_blocking_enabled(false);
  52. _sock->set_broadcasting_enabled(broadcast);
  53. }
  54. return _sock->join_multicast_group(p_multi_address, p_if_name);
  55. }
  56. Error PacketPeerUDP::leave_multicast_group(IPAddress p_multi_address, const String &p_if_name) {
  57. ERR_FAIL_COND_V(udp_server, ERR_LOCKED);
  58. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  59. ERR_FAIL_COND_V(!_sock->is_open(), ERR_UNCONFIGURED);
  60. return _sock->leave_multicast_group(p_multi_address, p_if_name);
  61. }
  62. String PacketPeerUDP::_get_packet_ip() const {
  63. return get_packet_address();
  64. }
  65. Error PacketPeerUDP::_set_dest_address(const String &p_address, int p_port) {
  66. IPAddress ip;
  67. if (p_address.is_valid_ip_address()) {
  68. ip = p_address;
  69. } else {
  70. ip = IP::get_singleton()->resolve_hostname(p_address);
  71. if (!ip.is_valid()) {
  72. return ERR_CANT_RESOLVE;
  73. }
  74. }
  75. set_dest_address(ip, p_port);
  76. return OK;
  77. }
  78. int PacketPeerUDP::get_available_packet_count() const {
  79. // TODO we should deprecate this, and expose poll instead!
  80. Error err = const_cast<PacketPeerUDP *>(this)->_poll();
  81. if (err != OK) {
  82. return -1;
  83. }
  84. return queue_count;
  85. }
  86. Error PacketPeerUDP::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  87. Error err = _poll();
  88. if (err != OK) {
  89. return err;
  90. }
  91. if (queue_count == 0) {
  92. return ERR_UNAVAILABLE;
  93. }
  94. uint32_t size = 0;
  95. uint8_t ipv6[16] = {};
  96. rb.read(ipv6, 16, true);
  97. packet_ip.set_ipv6(ipv6);
  98. rb.read((uint8_t *)&packet_port, 4, true);
  99. rb.read((uint8_t *)&size, 4, true);
  100. rb.read(packet_buffer, size, true);
  101. --queue_count;
  102. *r_buffer = packet_buffer;
  103. r_buffer_size = size;
  104. return OK;
  105. }
  106. Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  107. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  108. ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
  109. Error err;
  110. int sent = -1;
  111. if (!_sock->is_open()) {
  112. IP::Type ip_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  113. err = _sock->open(NetSocket::TYPE_UDP, ip_type);
  114. ERR_FAIL_COND_V(err != OK, err);
  115. _sock->set_blocking_enabled(false);
  116. _sock->set_broadcasting_enabled(broadcast);
  117. }
  118. do {
  119. if (connected && !udp_server) {
  120. err = _sock->send(p_buffer, p_buffer_size, sent);
  121. } else {
  122. err = _sock->sendto(p_buffer, p_buffer_size, sent, peer_addr, peer_port);
  123. }
  124. if (err != OK) {
  125. if (err != ERR_BUSY) {
  126. return FAILED;
  127. } else if (!blocking) {
  128. return ERR_BUSY;
  129. }
  130. // Keep trying to send full packet
  131. continue;
  132. }
  133. return OK;
  134. } while (sent != p_buffer_size);
  135. return OK;
  136. }
  137. int PacketPeerUDP::get_max_packet_size() const {
  138. return 512; // uhm maybe not
  139. }
  140. Error PacketPeerUDP::bind(int p_port, const IPAddress &p_bind_address, int p_recv_buffer_size) {
  141. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  142. ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE);
  143. ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
  144. 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).");
  145. Error err;
  146. IP::Type ip_type = IP::TYPE_ANY;
  147. if (p_bind_address.is_valid()) {
  148. ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  149. }
  150. err = _sock->open(NetSocket::TYPE_UDP, ip_type);
  151. if (err != OK) {
  152. return ERR_CANT_CREATE;
  153. }
  154. _sock->set_blocking_enabled(false);
  155. _sock->set_broadcasting_enabled(broadcast);
  156. err = _sock->bind(p_bind_address, p_port);
  157. if (err != OK) {
  158. _sock->close();
  159. return err;
  160. }
  161. rb.resize(nearest_shift(p_recv_buffer_size));
  162. return OK;
  163. }
  164. Error PacketPeerUDP::connect_shared_socket(Ref<NetSocket> p_sock, IPAddress p_ip, uint16_t p_port, UDPServer *p_server) {
  165. udp_server = p_server;
  166. connected = true;
  167. _sock = p_sock;
  168. peer_addr = p_ip;
  169. peer_port = p_port;
  170. packet_ip = peer_addr;
  171. packet_port = peer_port;
  172. return OK;
  173. }
  174. void PacketPeerUDP::disconnect_shared_socket() {
  175. udp_server = nullptr;
  176. _sock = Ref<NetSocket>(NetSocket::create());
  177. close();
  178. }
  179. Error PacketPeerUDP::connect_to_host(const IPAddress &p_host, int p_port) {
  180. ERR_FAIL_COND_V(udp_server, ERR_LOCKED);
  181. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  182. ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
  183. 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).");
  184. Error err;
  185. if (!_sock->is_open()) {
  186. IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  187. err = _sock->open(NetSocket::TYPE_UDP, ip_type);
  188. ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
  189. _sock->set_blocking_enabled(false);
  190. }
  191. err = _sock->connect_to_host(p_host, p_port);
  192. // I see no reason why we should get ERR_BUSY (wouldblock/eagain) here.
  193. // This is UDP, so connect is only used to tell the OS to which socket
  194. // it should deliver packets when multiple are bound on the same address/port.
  195. if (err != OK) {
  196. close();
  197. ERR_FAIL_V_MSG(FAILED, "Unable to connect");
  198. }
  199. connected = true;
  200. peer_addr = p_host;
  201. peer_port = p_port;
  202. // Flush any packet we might still have in queue.
  203. rb.clear();
  204. return OK;
  205. }
  206. bool PacketPeerUDP::is_socket_connected() const {
  207. return connected;
  208. }
  209. void PacketPeerUDP::close() {
  210. if (udp_server) {
  211. udp_server->remove_peer(peer_addr, peer_port);
  212. udp_server = nullptr;
  213. _sock = Ref<NetSocket>(NetSocket::create());
  214. } else if (_sock.is_valid()) {
  215. _sock->close();
  216. }
  217. rb.resize(16);
  218. queue_count = 0;
  219. connected = false;
  220. }
  221. Error PacketPeerUDP::wait() {
  222. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  223. return _sock->poll(NetSocket::POLL_TYPE_IN, -1);
  224. }
  225. Error PacketPeerUDP::_poll() {
  226. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  227. if (!_sock->is_open()) {
  228. return FAILED;
  229. }
  230. if (udp_server) {
  231. return OK; // Handled by UDPServer.
  232. }
  233. Error err;
  234. int read;
  235. IPAddress ip;
  236. uint16_t port;
  237. while (true) {
  238. if (connected) {
  239. err = _sock->recv(recv_buffer, sizeof(recv_buffer), read);
  240. ip = peer_addr;
  241. port = peer_port;
  242. } else {
  243. err = _sock->recvfrom(recv_buffer, sizeof(recv_buffer), read, ip, port);
  244. }
  245. if (err != OK) {
  246. if (err == ERR_BUSY) {
  247. break;
  248. }
  249. return FAILED;
  250. }
  251. err = store_packet(ip, port, recv_buffer, read);
  252. #ifdef TOOLS_ENABLED
  253. if (err != OK) {
  254. WARN_PRINT("Buffer full, dropping packets!");
  255. }
  256. #endif
  257. }
  258. return OK;
  259. }
  260. Error PacketPeerUDP::store_packet(IPAddress p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size) {
  261. if (rb.space_left() < p_buf_size + 24) {
  262. return ERR_OUT_OF_MEMORY;
  263. }
  264. rb.write(p_ip.get_ipv6(), 16);
  265. rb.write((uint8_t *)&p_port, 4);
  266. rb.write((uint8_t *)&p_buf_size, 4);
  267. rb.write(p_buf, p_buf_size);
  268. ++queue_count;
  269. return OK;
  270. }
  271. bool PacketPeerUDP::is_bound() const {
  272. return _sock.is_valid() && _sock->is_open();
  273. }
  274. IPAddress PacketPeerUDP::get_packet_address() const {
  275. return packet_ip;
  276. }
  277. int PacketPeerUDP::get_packet_port() const {
  278. return packet_port;
  279. }
  280. int PacketPeerUDP::get_local_port() const {
  281. uint16_t local_port;
  282. _sock->get_socket_address(nullptr, &local_port);
  283. return local_port;
  284. }
  285. void PacketPeerUDP::set_dest_address(const IPAddress &p_address, int p_port) {
  286. ERR_FAIL_COND_MSG(connected, "Destination address cannot be set for connected sockets");
  287. peer_addr = p_address;
  288. peer_port = p_port;
  289. }
  290. void PacketPeerUDP::_bind_methods() {
  291. ClassDB::bind_method(D_METHOD("bind", "port", "bind_address", "recv_buf_size"), &PacketPeerUDP::bind, DEFVAL("*"), DEFVAL(65536));
  292. ClassDB::bind_method(D_METHOD("close"), &PacketPeerUDP::close);
  293. ClassDB::bind_method(D_METHOD("wait"), &PacketPeerUDP::wait);
  294. ClassDB::bind_method(D_METHOD("is_bound"), &PacketPeerUDP::is_bound);
  295. ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &PacketPeerUDP::connect_to_host);
  296. ClassDB::bind_method(D_METHOD("is_socket_connected"), &PacketPeerUDP::is_socket_connected);
  297. ClassDB::bind_method(D_METHOD("get_packet_ip"), &PacketPeerUDP::_get_packet_ip);
  298. ClassDB::bind_method(D_METHOD("get_packet_port"), &PacketPeerUDP::get_packet_port);
  299. ClassDB::bind_method(D_METHOD("get_local_port"), &PacketPeerUDP::get_local_port);
  300. ClassDB::bind_method(D_METHOD("set_dest_address", "host", "port"), &PacketPeerUDP::_set_dest_address);
  301. ClassDB::bind_method(D_METHOD("set_broadcast_enabled", "enabled"), &PacketPeerUDP::set_broadcast_enabled);
  302. ClassDB::bind_method(D_METHOD("join_multicast_group", "multicast_address", "interface_name"), &PacketPeerUDP::join_multicast_group);
  303. ClassDB::bind_method(D_METHOD("leave_multicast_group", "multicast_address", "interface_name"), &PacketPeerUDP::leave_multicast_group);
  304. }
  305. PacketPeerUDP::PacketPeerUDP() :
  306. _sock(Ref<NetSocket>(NetSocket::create())) {
  307. rb.resize(16);
  308. }
  309. PacketPeerUDP::~PacketPeerUDP() {
  310. close();
  311. }