packet_peer_udp_posix.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*************************************************************************/
  2. /* packet_peer_udp_posix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_posix.h"
  31. #ifdef UNIX_ENABLED
  32. #include <errno.h>
  33. #include <netdb.h>
  34. #include <sys/socket.h>
  35. #include <sys/types.h>
  36. #include <unistd.h>
  37. #include <netinet/in.h>
  38. #include <stdio.h>
  39. #ifndef NO_FCNTL
  40. #ifdef __HAIKU__
  41. #include <fcntl.h>
  42. #else
  43. #include <sys/fcntl.h>
  44. #endif
  45. #else
  46. #include <sys/ioctl.h>
  47. #endif
  48. #ifdef JAVASCRIPT_ENABLED
  49. #include <arpa/inet.h>
  50. #endif
  51. #include "drivers/unix/socket_helpers.h"
  52. int PacketPeerUDPPosix::get_available_packet_count() const {
  53. Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false);
  54. if (err != OK)
  55. return 0;
  56. return queue_count;
  57. }
  58. Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_size) const {
  59. Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false);
  60. if (err != OK)
  61. return err;
  62. if (queue_count == 0)
  63. return ERR_UNAVAILABLE;
  64. uint32_t size = 0;
  65. uint8_t type = IP::TYPE_NONE;
  66. rb.read(&type, 1, true);
  67. if (type == IP::TYPE_IPV4) {
  68. uint8_t ip[4];
  69. rb.read(ip, 4, true);
  70. packet_ip.set_ipv4(ip);
  71. } else {
  72. uint8_t ipv6[16];
  73. rb.read(ipv6, 16, true);
  74. packet_ip.set_ipv6(ipv6);
  75. };
  76. rb.read((uint8_t *)&packet_port, 4, true);
  77. rb.read((uint8_t *)&size, 4, true);
  78. rb.read(packet_buffer, size, true);
  79. --queue_count;
  80. *r_buffer = packet_buffer;
  81. r_buffer_size = size;
  82. return OK;
  83. }
  84. Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  85. ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
  86. if (sock_type == IP::TYPE_NONE)
  87. sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  88. int sock = _get_socket();
  89. ERR_FAIL_COND_V(sock == -1, FAILED);
  90. struct sockaddr_storage addr;
  91. size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
  92. errno = 0;
  93. int err;
  94. _set_sock_blocking(blocking);
  95. while ((err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) {
  96. if (errno != EAGAIN) {
  97. return FAILED;
  98. } else if (!blocking) {
  99. return ERR_UNAVAILABLE;
  100. }
  101. }
  102. return OK;
  103. }
  104. int PacketPeerUDPPosix::get_max_packet_size() const {
  105. return 512; // uhm maybe not
  106. }
  107. Error PacketPeerUDPPosix::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) {
  108. ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE);
  109. ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
  110. #ifdef __OpenBSD__
  111. sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
  112. #else
  113. sock_type = IP::TYPE_ANY;
  114. #endif
  115. if (p_bind_address.is_valid())
  116. sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  117. int sock = _get_socket();
  118. if (sock == -1)
  119. return ERR_CANT_CREATE;
  120. sockaddr_storage addr = { 0 };
  121. size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
  122. if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
  123. close();
  124. return ERR_UNAVAILABLE;
  125. }
  126. rb.resize(nearest_shift(p_recv_buffer_size));
  127. return OK;
  128. }
  129. void PacketPeerUDPPosix::close() {
  130. if (sockfd != -1)
  131. ::close(sockfd);
  132. sockfd = -1;
  133. sock_type = IP::TYPE_NONE;
  134. rb.resize(16);
  135. queue_count = 0;
  136. }
  137. Error PacketPeerUDPPosix::wait() {
  138. return _poll(true);
  139. }
  140. Error PacketPeerUDPPosix::_poll(bool p_block) {
  141. if (sockfd == -1) {
  142. return FAILED;
  143. }
  144. _set_sock_blocking(p_block);
  145. struct sockaddr_storage from = { 0 };
  146. socklen_t len = sizeof(struct sockaddr_storage);
  147. int ret;
  148. while ((ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) {
  149. uint32_t port = 0;
  150. if (from.ss_family == AF_INET) {
  151. uint8_t type = (uint8_t)IP::TYPE_IPV4;
  152. rb.write(&type, 1);
  153. struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
  154. rb.write((uint8_t *)&sin_from->sin_addr, 4);
  155. port = ntohs(sin_from->sin_port);
  156. } else if (from.ss_family == AF_INET6) {
  157. uint8_t type = (uint8_t)IP::TYPE_IPV6;
  158. rb.write(&type, 1);
  159. struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
  160. rb.write((uint8_t *)&s6_from->sin6_addr, 16);
  161. port = ntohs(s6_from->sin6_port);
  162. } else {
  163. // WARN_PRINT("Ignoring packet with unknown address family");
  164. uint8_t type = (uint8_t)IP::TYPE_NONE;
  165. rb.write(&type, 1);
  166. };
  167. rb.write((uint8_t *)&port, 4);
  168. rb.write((uint8_t *)&ret, 4);
  169. rb.write(recv_buffer, ret);
  170. len = sizeof(struct sockaddr_storage);
  171. ++queue_count;
  172. if (p_block)
  173. break;
  174. };
  175. // TODO: Should ECONNRESET be handled here?
  176. if (ret == 0 || (ret == -1 && errno != EAGAIN)) {
  177. close();
  178. return FAILED;
  179. };
  180. return OK;
  181. }
  182. bool PacketPeerUDPPosix::is_listening() const {
  183. return sockfd != -1;
  184. }
  185. IP_Address PacketPeerUDPPosix::get_packet_address() const {
  186. return packet_ip;
  187. }
  188. int PacketPeerUDPPosix::get_packet_port() const {
  189. return packet_port;
  190. }
  191. int PacketPeerUDPPosix::_get_socket() {
  192. ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1);
  193. if (sockfd != -1)
  194. return sockfd;
  195. sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
  196. if (sockfd != -1)
  197. _set_sock_blocking(false);
  198. return sockfd;
  199. }
  200. void PacketPeerUDPPosix::_set_sock_blocking(bool p_blocking) {
  201. if (sock_blocking == p_blocking)
  202. return;
  203. sock_blocking = p_blocking;
  204. #ifndef NO_FCNTL
  205. int opts = fcntl(sockfd, F_GETFL);
  206. int ret = 0;
  207. if (sock_blocking)
  208. ret = fcntl(sockfd, F_SETFL, opts & ~O_NONBLOCK);
  209. else
  210. ret = fcntl(sockfd, F_SETFL, opts | O_NONBLOCK);
  211. if (ret == -1)
  212. perror("setting non-block mode");
  213. #else
  214. int bval = sock_blocking ? 0 : 1;
  215. if (ioctl(sockfd, FIONBIO, &bval) == -1)
  216. perror("setting non-block mode");
  217. #endif
  218. }
  219. void PacketPeerUDPPosix::set_dest_address(const IP_Address &p_address, int p_port) {
  220. peer_addr = p_address;
  221. peer_port = p_port;
  222. }
  223. PacketPeerUDP *PacketPeerUDPPosix::_create() {
  224. return memnew(PacketPeerUDPPosix);
  225. };
  226. void PacketPeerUDPPosix::make_default() {
  227. PacketPeerUDP::_create = PacketPeerUDPPosix::_create;
  228. };
  229. PacketPeerUDPPosix::PacketPeerUDPPosix() {
  230. blocking = true;
  231. sock_blocking = true;
  232. sockfd = -1;
  233. packet_port = 0;
  234. queue_count = 0;
  235. peer_port = 0;
  236. sock_type = IP::TYPE_NONE;
  237. rb.resize(16);
  238. }
  239. PacketPeerUDPPosix::~PacketPeerUDPPosix() {
  240. close();
  241. }
  242. #endif