tcp_server_posix.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*************************************************************************/
  2. /* tcp_server_posix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "tcp_server_posix.h"
  30. #include "stream_peer_tcp_posix.h"
  31. #ifdef UNIX_ENABLED
  32. #include <poll.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <netdb.h>
  39. #include <sys/types.h>
  40. #ifndef NO_FCNTL
  41. #ifdef __HAIKU__
  42. #include <fcntl.h>
  43. #else
  44. #include <sys/fcntl.h>
  45. #endif
  46. #else
  47. #include <sys/ioctl.h>
  48. #endif
  49. #ifdef JAVASCRIPT_ENABLED
  50. #include <arpa/inet.h>
  51. #endif
  52. #include <netinet/in.h>
  53. #include <sys/socket.h>
  54. #include <assert.h>
  55. TCP_Server* TCPServerPosix::_create() {
  56. return memnew(TCPServerPosix);
  57. };
  58. void TCPServerPosix::make_default() {
  59. TCP_Server::_create = TCPServerPosix::_create;
  60. };
  61. Error TCPServerPosix::listen(uint16_t p_port, IP_Address::AddrType p_type, const List<String> *p_accepted_hosts) {
  62. int sockfd;
  63. int family = p_type == IP_Address::TYPE_IPV6 ? AF_INET6 : AF_INET;
  64. sockfd = socket(family, SOCK_STREAM, 0);
  65. ERR_FAIL_COND_V(sockfd == -1, FAILED);
  66. #ifndef NO_FCNTL
  67. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  68. #else
  69. int bval = 1;
  70. ioctl(sockfd, FIONBIO, &bval);
  71. #endif
  72. int reuse=1;
  73. if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
  74. WARN_PRINT("REUSEADDR failed!")
  75. }
  76. sockaddr_storage addr = {0};
  77. if (p_type == IP_Address::TYPE_IPV4) {
  78. struct sockaddr_in* addr4 = (struct sockaddr_in*)&addr;
  79. addr4->sin_family = AF_INET;
  80. addr4->sin_port = htons(p_port);
  81. addr4->sin_addr.s_addr = INADDR_ANY;
  82. } else {
  83. struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&addr;
  84. addr6->sin6_family = AF_INET6;
  85. addr6->sin6_port = htons(p_port);
  86. addr6->sin6_addr = in6addr_any;
  87. };
  88. // automatically fill with my IP TODO: use p_accepted_hosts
  89. if (bind(sockfd, (struct sockaddr *)&addr, sizeof addr) != -1) {
  90. if (::listen(sockfd, 1) == -1) {
  91. close(sockfd);
  92. ERR_FAIL_V(FAILED);
  93. };
  94. }
  95. else {
  96. return ERR_ALREADY_IN_USE;
  97. };
  98. if (listen_sockfd != -1) {
  99. stop();
  100. };
  101. listen_sockfd = sockfd;
  102. return OK;
  103. };
  104. bool TCPServerPosix::is_connection_available() const {
  105. if (listen_sockfd == -1) {
  106. return false;
  107. };
  108. struct pollfd pfd;
  109. pfd.fd = listen_sockfd;
  110. pfd.events = POLLIN;
  111. pfd.revents = 0;
  112. int ret = poll(&pfd, 1, 0);
  113. ERR_FAIL_COND_V(ret < 0, FAILED);
  114. if (ret && (pfd.revents & POLLIN)) {
  115. return true;
  116. };
  117. return false;
  118. };
  119. Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
  120. if (!is_connection_available()) {
  121. return Ref<StreamPeerTCP>();
  122. };
  123. struct sockaddr_storage their_addr;
  124. socklen_t size = sizeof(their_addr);
  125. int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &size);
  126. ERR_FAIL_COND_V(fd == -1, Ref<StreamPeerTCP>());
  127. #ifndef NO_FCNTL
  128. fcntl(fd, F_SETFL, O_NONBLOCK);
  129. #else
  130. int bval = 1;
  131. ioctl(fd, FIONBIO, &bval);
  132. #endif
  133. Ref<StreamPeerTCPPosix> conn = memnew(StreamPeerTCPPosix);
  134. IP_Address ip;
  135. if (their_addr.ss_family == AF_INET) {
  136. ip.type = IP_Address::TYPE_IPV4;
  137. struct sockaddr_in* addr4 = (struct sockaddr_in*)&their_addr;
  138. ip.field32[0] = (uint32_t)addr4->sin_addr.s_addr;
  139. conn->set_socket(fd, ip, ntohs(addr4->sin_port));
  140. } else if (their_addr.ss_family == AF_INET6) {
  141. ip.type = IP_Address::TYPE_IPV6;
  142. struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&their_addr;
  143. copymem(&addr6->sin6_addr.s6_addr, ip.field8, 16);
  144. conn->set_socket(fd, ip, ntohs(addr6->sin6_port));
  145. };
  146. return conn;
  147. };
  148. void TCPServerPosix::stop() {
  149. if (listen_sockfd != -1) {
  150. int ret = close(listen_sockfd);
  151. ERR_FAIL_COND(ret!=0);
  152. };
  153. listen_sockfd = -1;
  154. };
  155. TCPServerPosix::TCPServerPosix() {
  156. listen_sockfd = -1;
  157. };
  158. TCPServerPosix::~TCPServerPosix() {
  159. stop();
  160. };
  161. #endif