tcp_server_posix.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*************************************************************************/
  2. /* tcp_server_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 "tcp_server_posix.h"
  31. #include "stream_peer_tcp_posix.h"
  32. #ifdef UNIX_ENABLED
  33. #include <poll.h>
  34. #include <errno.h>
  35. #include <netdb.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41. #ifndef NO_FCNTL
  42. #ifdef __HAIKU__
  43. #include <fcntl.h>
  44. #else
  45. #include <sys/fcntl.h>
  46. #endif
  47. #else
  48. #include <sys/ioctl.h>
  49. #endif
  50. #ifdef JAVASCRIPT_ENABLED
  51. #include <arpa/inet.h>
  52. #endif
  53. #include <assert.h>
  54. #include <netinet/in.h>
  55. #include <sys/socket.h>
  56. #include "drivers/unix/socket_helpers.h"
  57. TCP_Server *TCPServerPosix::_create() {
  58. return memnew(TCPServerPosix);
  59. };
  60. void TCPServerPosix::make_default() {
  61. TCP_Server::_create = TCPServerPosix::_create;
  62. };
  63. Error TCPServerPosix::listen(uint16_t p_port, const IP_Address &p_bind_address) {
  64. ERR_FAIL_COND_V(listen_sockfd != -1, ERR_ALREADY_IN_USE);
  65. ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
  66. int sockfd;
  67. #ifdef __OpenBSD__
  68. sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
  69. #else
  70. sock_type = IP::TYPE_ANY;
  71. #endif
  72. // If the bind address is valid use its type as the socket type
  73. if (p_bind_address.is_valid())
  74. sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  75. sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
  76. ERR_FAIL_COND_V(sockfd == -1, FAILED);
  77. #ifndef NO_FCNTL
  78. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  79. #else
  80. int bval = 1;
  81. ioctl(sockfd, FIONBIO, &bval);
  82. #endif
  83. int reuse = 1;
  84. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
  85. WARN_PRINT("REUSEADDR failed!")
  86. }
  87. struct sockaddr_storage addr;
  88. size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, p_bind_address);
  89. if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
  90. if (::listen(sockfd, 1) == -1) {
  91. close(sockfd);
  92. ERR_FAIL_V(FAILED);
  93. };
  94. } else {
  95. return ERR_ALREADY_IN_USE;
  96. };
  97. if (listen_sockfd != -1) {
  98. stop();
  99. };
  100. listen_sockfd = sockfd;
  101. return OK;
  102. };
  103. bool TCPServerPosix::is_connection_available() const {
  104. if (listen_sockfd == -1) {
  105. return false;
  106. };
  107. struct pollfd pfd;
  108. pfd.fd = listen_sockfd;
  109. pfd.events = POLLIN;
  110. pfd.revents = 0;
  111. int ret = poll(&pfd, 1, 0);
  112. ERR_FAIL_COND_V(ret < 0, FAILED);
  113. if (ret && (pfd.revents & POLLIN)) {
  114. return true;
  115. };
  116. return false;
  117. };
  118. Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
  119. if (!is_connection_available()) {
  120. return Ref<StreamPeerTCP>();
  121. };
  122. struct sockaddr_storage their_addr;
  123. socklen_t size = sizeof(their_addr);
  124. int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &size);
  125. ERR_FAIL_COND_V(fd == -1, Ref<StreamPeerTCP>());
  126. #ifndef NO_FCNTL
  127. fcntl(fd, F_SETFL, O_NONBLOCK);
  128. #else
  129. int bval = 1;
  130. ioctl(fd, FIONBIO, &bval);
  131. #endif
  132. Ref<StreamPeerTCPPosix> conn = memnew(StreamPeerTCPPosix);
  133. IP_Address ip;
  134. int port = 0;
  135. _set_ip_addr_port(ip, port, &their_addr);
  136. conn->set_socket(fd, ip, port, sock_type);
  137. return conn;
  138. };
  139. void TCPServerPosix::stop() {
  140. if (listen_sockfd != -1) {
  141. int ret = close(listen_sockfd);
  142. ERR_FAIL_COND(ret != 0);
  143. };
  144. listen_sockfd = -1;
  145. sock_type = IP::TYPE_NONE;
  146. };
  147. TCPServerPosix::TCPServerPosix() {
  148. listen_sockfd = -1;
  149. sock_type = IP::TYPE_NONE;
  150. };
  151. TCPServerPosix::~TCPServerPosix() {
  152. stop();
  153. };
  154. #endif