packet_peer_udp.cpp 11 KB

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