tcp_server_posix.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "drivers/unix/socket_helpers.h"
  56. TCP_Server* TCPServerPosix::_create() {
  57. return memnew(TCPServerPosix);
  58. };
  59. void TCPServerPosix::make_default() {
  60. TCP_Server::_create = TCPServerPosix::_create;
  61. };
  62. Error TCPServerPosix::listen(uint16_t p_port,const List<String> *p_accepted_hosts) {
  63. int sockfd;
  64. sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
  65. ERR_FAIL_COND_V(sockfd == -1, FAILED);
  66. if(ip_type == IP_Address::TYPE_IPV6) {
  67. // Use IPv6 only socket
  68. int yes = 1;
  69. if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&yes, sizeof(yes)) != 0) {
  70. WARN_PRINT("Unable to unset IPv4 address mapping over IPv6");
  71. }
  72. }
  73. #ifndef NO_FCNTL
  74. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  75. #else
  76. int bval = 1;
  77. ioctl(sockfd, FIONBIO, &bval);
  78. #endif
  79. int reuse=1;
  80. if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
  81. WARN_PRINT("REUSEADDR failed!")
  82. }
  83. struct sockaddr_storage addr;
  84. size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, p_accepted_hosts);
  85. // automatically fill with my IP TODO: use p_accepted_hosts
  86. if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
  87. if (::listen(sockfd, 1) == -1) {
  88. close(sockfd);
  89. ERR_FAIL_V(FAILED);
  90. };
  91. }
  92. else {
  93. return ERR_ALREADY_IN_USE;
  94. };
  95. if (listen_sockfd != -1) {
  96. stop();
  97. };
  98. listen_sockfd = sockfd;
  99. return OK;
  100. };
  101. bool TCPServerPosix::is_connection_available() const {
  102. if (listen_sockfd == -1) {
  103. return false;
  104. };
  105. struct pollfd pfd;
  106. pfd.fd = listen_sockfd;
  107. pfd.events = POLLIN;
  108. pfd.revents = 0;
  109. int ret = poll(&pfd, 1, 0);
  110. ERR_FAIL_COND_V(ret < 0, FAILED);
  111. if (ret && (pfd.revents & POLLIN)) {
  112. return true;
  113. };
  114. return false;
  115. };
  116. Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
  117. if (!is_connection_available()) {
  118. return Ref<StreamPeerTCP>();
  119. };
  120. struct sockaddr_storage their_addr;
  121. socklen_t size = sizeof(their_addr);
  122. int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &size);
  123. ERR_FAIL_COND_V(fd == -1, Ref<StreamPeerTCP>());
  124. #ifndef NO_FCNTL
  125. fcntl(fd, F_SETFL, O_NONBLOCK);
  126. #else
  127. int bval = 1;
  128. ioctl(fd, FIONBIO, &bval);
  129. #endif
  130. Ref<StreamPeerTCPPosix> conn = memnew(StreamPeerTCPPosix);
  131. IP_Address ip;
  132. int port;
  133. _set_ip_addr_port(ip, port, &their_addr);
  134. conn->set_socket(fd, ip, port, ip_type);
  135. return conn;
  136. };
  137. void TCPServerPosix::stop() {
  138. if (listen_sockfd != -1) {
  139. int ret = close(listen_sockfd);
  140. ERR_FAIL_COND(ret!=0);
  141. };
  142. listen_sockfd = -1;
  143. };
  144. TCPServerPosix::TCPServerPosix() {
  145. listen_sockfd = -1;
  146. ip_type = IP_Address::TYPE_ANY;
  147. };
  148. TCPServerPosix::~TCPServerPosix() {
  149. stop();
  150. };
  151. #endif