2
0

packet_peer_udp_posix.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 -1;
  56. return queue_count;
  57. }
  58. Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  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. sock_blocking = true;
  135. rb.resize(16);
  136. queue_count = 0;
  137. }
  138. Error PacketPeerUDPPosix::wait() {
  139. return _poll(true);
  140. }
  141. Error PacketPeerUDPPosix::_poll(bool p_block) {
  142. if (sockfd == -1) {
  143. return FAILED;
  144. }
  145. _set_sock_blocking(p_block);
  146. struct sockaddr_storage from = { 0 };
  147. socklen_t len = sizeof(struct sockaddr_storage);
  148. int ret;
  149. while ((ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) {
  150. uint32_t port = 0;
  151. if (from.ss_family == AF_INET) {
  152. uint8_t type = (uint8_t)IP::TYPE_IPV4;
  153. rb.write(&type, 1);
  154. struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
  155. rb.write((uint8_t *)&sin_from->sin_addr, 4);
  156. port = ntohs(sin_from->sin_port);
  157. } else if (from.ss_family == AF_INET6) {
  158. uint8_t type = (uint8_t)IP::TYPE_IPV6;
  159. rb.write(&type, 1);
  160. struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
  161. rb.write((uint8_t *)&s6_from->sin6_addr, 16);
  162. port = ntohs(s6_from->sin6_port);
  163. } else {
  164. // WARN_PRINT("Ignoring packet with unknown address family");
  165. uint8_t type = (uint8_t)IP::TYPE_NONE;
  166. rb.write(&type, 1);
  167. };
  168. rb.write((uint8_t *)&port, 4);
  169. rb.write((uint8_t *)&ret, 4);
  170. rb.write(recv_buffer, ret);
  171. len = sizeof(struct sockaddr_storage);
  172. ++queue_count;
  173. if (p_block)
  174. break;
  175. };
  176. // TODO: Should ECONNRESET be handled here?
  177. if (ret == 0 || (ret == -1 && errno != EAGAIN)) {
  178. close();
  179. return FAILED;
  180. };
  181. return OK;
  182. }
  183. bool PacketPeerUDPPosix::is_listening() const {
  184. return sockfd != -1;
  185. }
  186. IP_Address PacketPeerUDPPosix::get_packet_address() const {
  187. return packet_ip;
  188. }
  189. int PacketPeerUDPPosix::get_packet_port() const {
  190. return packet_port;
  191. }
  192. int PacketPeerUDPPosix::_get_socket() {
  193. ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1);
  194. if (sockfd != -1)
  195. return sockfd;
  196. sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
  197. if (sockfd != -1)
  198. _set_sock_blocking(false);
  199. return sockfd;
  200. }
  201. void PacketPeerUDPPosix::_set_sock_blocking(bool p_blocking) {
  202. if (sock_blocking == p_blocking)
  203. return;
  204. sock_blocking = p_blocking;
  205. #ifndef NO_FCNTL
  206. int opts = fcntl(sockfd, F_GETFL);
  207. int ret = 0;
  208. if (sock_blocking)
  209. ret = fcntl(sockfd, F_SETFL, opts & ~O_NONBLOCK);
  210. else
  211. ret = fcntl(sockfd, F_SETFL, opts | O_NONBLOCK);
  212. if (ret == -1)
  213. perror("setting non-block mode");
  214. #else
  215. int bval = sock_blocking ? 0 : 1;
  216. if (ioctl(sockfd, FIONBIO, &bval) == -1)
  217. perror("setting non-block mode");
  218. #endif
  219. }
  220. void PacketPeerUDPPosix::set_dest_address(const IP_Address &p_address, int p_port) {
  221. peer_addr = p_address;
  222. peer_port = p_port;
  223. }
  224. PacketPeerUDP *PacketPeerUDPPosix::_create() {
  225. return memnew(PacketPeerUDPPosix);
  226. };
  227. void PacketPeerUDPPosix::make_default() {
  228. PacketPeerUDP::_create = PacketPeerUDPPosix::_create;
  229. };
  230. PacketPeerUDPPosix::PacketPeerUDPPosix() {
  231. blocking = true;
  232. sock_blocking = true;
  233. sockfd = -1;
  234. packet_port = 0;
  235. queue_count = 0;
  236. peer_port = 0;
  237. sock_type = IP::TYPE_NONE;
  238. rb.resize(16);
  239. }
  240. PacketPeerUDPPosix::~PacketPeerUDPPosix() {
  241. close();
  242. }
  243. #endif