net_socket_posix.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*************************************************************************/
  2. /* net_socket_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 "net_socket_posix.h"
  31. #if defined(UNIX_ENABLED)
  32. #include <errno.h>
  33. #include <netdb.h>
  34. #include <poll.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sys/ioctl.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. #include <netinet/in.h>
  51. #include <sys/socket.h>
  52. #ifdef JAVASCRIPT_ENABLED
  53. #include <arpa/inet.h>
  54. #endif
  55. #include <netinet/tcp.h>
  56. #if defined(OSX_ENABLED) || defined(IPHONE_ENABLED)
  57. #define MSG_NOSIGNAL SO_NOSIGPIPE
  58. #endif
  59. // Some custom defines to minimize ifdefs
  60. #define SOCK_EMPTY -1
  61. #define SOCK_BUF(x) x
  62. #define SOCK_CBUF(x) x
  63. #define SOCK_IOCTL ioctl
  64. #define SOCK_CLOSE ::close
  65. /* Windows */
  66. #elif defined(WINDOWS_ENABLED)
  67. #include <winsock2.h>
  68. #include <ws2tcpip.h>
  69. #include <mswsock.h>
  70. // Some custom defines to minimize ifdefs
  71. #define SOCK_EMPTY INVALID_SOCKET
  72. #define SOCK_BUF(x) (char *)(x)
  73. #define SOCK_CBUF(x) (const char *)(x)
  74. #define SOCK_IOCTL ioctlsocket
  75. #define SOCK_CLOSE closesocket
  76. // Windows doesn't have this flag
  77. #ifndef MSG_NOSIGNAL
  78. #define MSG_NOSIGNAL 0
  79. #endif
  80. // Workaround missing flag in MinGW
  81. #if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET)
  82. #define SIO_UDP_NETRESET _WSAIOW(IOC_VENDOR, 15)
  83. #endif
  84. #endif
  85. static size_t _set_addr_storage(struct sockaddr_storage *p_addr, const IP_Address &p_ip, uint16_t p_port, IP::Type p_ip_type) {
  86. memset(p_addr, 0, sizeof(struct sockaddr_storage));
  87. if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket
  88. // IPv6 only socket with IPv4 address
  89. ERR_FAIL_COND_V(!p_ip.is_wildcard() && p_ip_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0);
  90. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  91. addr6->sin6_family = AF_INET6;
  92. addr6->sin6_port = htons(p_port);
  93. if (p_ip.is_valid()) {
  94. copymem(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16);
  95. } else {
  96. addr6->sin6_addr = in6addr_any;
  97. }
  98. return sizeof(sockaddr_in6);
  99. } else { // IPv4 socket
  100. // IPv4 socket with IPv6 address
  101. ERR_FAIL_COND_V(!p_ip.is_ipv4(), 0);
  102. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  103. addr4->sin_family = AF_INET;
  104. addr4->sin_port = htons(p_port); // short, network byte order
  105. if (p_ip.is_valid()) {
  106. copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 4);
  107. } else {
  108. addr4->sin_addr.s_addr = INADDR_ANY;
  109. }
  110. copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 16);
  111. return sizeof(sockaddr_in);
  112. }
  113. }
  114. static void _set_ip_port(IP_Address &r_ip, uint16_t &r_port, struct sockaddr_storage *p_addr) {
  115. if (p_addr->ss_family == AF_INET) {
  116. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  117. r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
  118. r_port = ntohs(addr4->sin_port);
  119. } else if (p_addr->ss_family == AF_INET6) {
  120. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  121. r_ip.set_ipv6(addr6->sin6_addr.s6_addr);
  122. r_port = ntohs(addr6->sin6_port);
  123. };
  124. }
  125. NetSocket *NetSocketPosix::_create_func() {
  126. return memnew(NetSocketPosix);
  127. }
  128. void NetSocketPosix::make_default() {
  129. #if defined(WINDOWS_ENABLED)
  130. if (_create == NULL) {
  131. WSADATA data;
  132. WSAStartup(MAKEWORD(2, 2), &data);
  133. }
  134. #endif
  135. _create = _create_func;
  136. }
  137. void NetSocketPosix::cleanup() {
  138. #if defined(WINDOWS_ENABLED)
  139. if (_create != NULL) {
  140. WSACleanup();
  141. }
  142. _create = NULL;
  143. #endif
  144. }
  145. NetSocketPosix::NetSocketPosix() {
  146. _sock = SOCK_EMPTY;
  147. _ip_type = IP::TYPE_NONE;
  148. _is_stream = false;
  149. }
  150. NetSocketPosix::~NetSocketPosix() {
  151. close();
  152. }
  153. NetSocketPosix::NetError NetSocketPosix::_get_socket_error() {
  154. #if defined(WINDOWS_ENABLED)
  155. int err = WSAGetLastError();
  156. if (err == WSAEISCONN)
  157. return ERR_NET_IS_CONNECTED;
  158. if (err == WSAEINPROGRESS || err == WSAEALREADY)
  159. return ERR_NET_IN_PROGRESS;
  160. if (err == WSAEWOULDBLOCK)
  161. return ERR_NET_WOULD_BLOCK;
  162. ERR_PRINTS("Socket error: " + itos(err));
  163. return ERR_NET_OTHER;
  164. #else
  165. if (errno == EISCONN)
  166. return ERR_NET_IS_CONNECTED;
  167. if (errno == EINPROGRESS || errno == EALREADY)
  168. return ERR_NET_IN_PROGRESS;
  169. if (errno == EAGAIN || errno == EWOULDBLOCK)
  170. return ERR_NET_WOULD_BLOCK;
  171. ERR_PRINTS("Socket error: " + itos(errno));
  172. return ERR_NET_OTHER;
  173. #endif
  174. }
  175. bool NetSocketPosix::_can_use_ip(const IP_Address p_ip, const bool p_for_bind) const {
  176. if (p_for_bind && !(p_ip.is_valid() || p_ip.is_wildcard())) {
  177. return false;
  178. } else if (!p_for_bind && !p_ip.is_valid()) {
  179. return false;
  180. }
  181. // Check if socket support this IP type.
  182. IP::Type type = p_ip.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  183. if (_ip_type != IP::TYPE_ANY && !p_ip.is_wildcard() && _ip_type != type) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. void NetSocketPosix::_set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream) {
  189. _sock = p_sock;
  190. _ip_type = p_ip_type;
  191. _is_stream = p_is_stream;
  192. }
  193. Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
  194. ERR_FAIL_COND_V(is_open(), ERR_ALREADY_IN_USE);
  195. ERR_FAIL_COND_V(ip_type > IP::TYPE_ANY || ip_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
  196. #if defined(__OpenBSD__)
  197. // OpenBSD does not support dual stacking, fallback to IPv4 only.
  198. if (ip_type == IP::TYPE_ANY)
  199. ip_type = IP::TYPE_IPV4;
  200. #endif
  201. int family = ip_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
  202. int protocol = p_sock_type == TYPE_TCP ? IPPROTO_TCP : IPPROTO_UDP;
  203. int type = p_sock_type == TYPE_TCP ? SOCK_STREAM : SOCK_DGRAM;
  204. _sock = socket(family, type, protocol);
  205. if (_sock == SOCK_EMPTY && ip_type == IP::TYPE_ANY) {
  206. // Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
  207. // in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
  208. ip_type = IP::TYPE_IPV4;
  209. family = AF_INET;
  210. _sock = socket(family, type, protocol);
  211. }
  212. ERR_FAIL_COND_V(_sock == SOCK_EMPTY, FAILED);
  213. _ip_type = ip_type;
  214. if (family == AF_INET6) {
  215. // Select IPv4 over IPv6 mapping
  216. set_ipv6_only_enabled(ip_type != IP::TYPE_ANY);
  217. }
  218. if (protocol == IPPROTO_UDP && ip_type != IP::TYPE_IPV6) {
  219. // Enable broadcasting for UDP sockets if it's not IPv6 only (IPv6 has no broadcast option).
  220. set_broadcasting_enabled(true);
  221. }
  222. _is_stream = p_sock_type == TYPE_TCP;
  223. #if defined(WINDOWS_ENABLED)
  224. if (!_is_stream) {
  225. // Disable windows feature/bug reporting WSAECONNRESET/WSAENETRESET when
  226. // recv/recvfrom and an ICMP reply was received from a previous send/sendto.
  227. unsigned long disable = 0;
  228. if (ioctlsocket(_sock, SIO_UDP_CONNRESET, &disable) == SOCKET_ERROR) {
  229. print_verbose("Unable to turn off UDP WSAECONNRESET behaviour on Windows");
  230. }
  231. if (ioctlsocket(_sock, SIO_UDP_NETRESET, &disable) == SOCKET_ERROR) {
  232. // This feature seems not to be supported on wine.
  233. print_verbose("Unable to turn off UDP WSAENETRESET behaviour on Windows");
  234. }
  235. }
  236. #endif
  237. return OK;
  238. }
  239. void NetSocketPosix::close() {
  240. if (_sock != SOCK_EMPTY)
  241. SOCK_CLOSE(_sock);
  242. _sock = SOCK_EMPTY;
  243. _ip_type = IP::TYPE_NONE;
  244. _is_stream = false;
  245. }
  246. Error NetSocketPosix::bind(IP_Address p_addr, uint16_t p_port) {
  247. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  248. ERR_FAIL_COND_V(!_can_use_ip(p_addr, true), ERR_INVALID_PARAMETER);
  249. sockaddr_storage addr;
  250. size_t addr_size = _set_addr_storage(&addr, p_addr, p_port, _ip_type);
  251. if (::bind(_sock, (struct sockaddr *)&addr, addr_size) == SOCK_EMPTY) {
  252. close();
  253. ERR_FAIL_V(ERR_UNAVAILABLE);
  254. }
  255. return OK;
  256. }
  257. Error NetSocketPosix::listen(int p_max_pending) {
  258. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  259. if (::listen(_sock, p_max_pending) == SOCK_EMPTY) {
  260. close();
  261. ERR_FAIL_V(FAILED);
  262. };
  263. return OK;
  264. }
  265. Error NetSocketPosix::connect_to_host(IP_Address p_host, uint16_t p_port) {
  266. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  267. ERR_FAIL_COND_V(!_can_use_ip(p_host, false), ERR_INVALID_PARAMETER);
  268. struct sockaddr_storage addr;
  269. size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
  270. if (::connect(_sock, (struct sockaddr *)&addr, addr_size) == SOCK_EMPTY) {
  271. NetError err = _get_socket_error();
  272. switch (err) {
  273. // We are already connected
  274. case ERR_NET_IS_CONNECTED:
  275. return OK;
  276. // Still waiting to connect, try again in a while
  277. case ERR_NET_WOULD_BLOCK:
  278. case ERR_NET_IN_PROGRESS:
  279. return ERR_BUSY;
  280. default:
  281. ERR_PRINT("Connection to remote host failed!");
  282. close();
  283. return FAILED;
  284. }
  285. }
  286. return OK;
  287. }
  288. Error NetSocketPosix::poll(PollType p_type, int p_timeout) const {
  289. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  290. #if defined(WINDOWS_ENABLED)
  291. bool ready = false;
  292. fd_set rd, wr, ex;
  293. fd_set *rdp = NULL;
  294. fd_set *wrp = NULL;
  295. FD_ZERO(&rd);
  296. FD_ZERO(&wr);
  297. FD_ZERO(&ex);
  298. FD_SET(_sock, &ex);
  299. struct timeval timeout = { p_timeout, 0 };
  300. // For blocking operation, pass NULL timeout pointer to select.
  301. struct timeval *tp = NULL;
  302. if (p_timeout >= 0) {
  303. // If timeout is non-negative, we want to specify the timeout instead.
  304. tp = &timeout;
  305. }
  306. switch (p_type) {
  307. case POLL_TYPE_IN:
  308. FD_SET(_sock, &rd);
  309. rdp = &rd;
  310. break;
  311. case POLL_TYPE_OUT:
  312. FD_SET(_sock, &wr);
  313. wrp = &wr;
  314. break;
  315. case POLL_TYPE_IN_OUT:
  316. FD_SET(_sock, &rd);
  317. FD_SET(_sock, &wr);
  318. rdp = &rd;
  319. wrp = &wr;
  320. }
  321. int ret = select(1, rdp, wrp, &ex, tp);
  322. ERR_FAIL_COND_V(ret == SOCKET_ERROR, FAILED);
  323. if (ret == 0)
  324. return ERR_BUSY;
  325. ERR_FAIL_COND_V(FD_ISSET(_sock, &ex), FAILED);
  326. if (rdp && FD_ISSET(_sock, rdp))
  327. ready = true;
  328. if (wrp && FD_ISSET(_sock, wrp))
  329. ready = true;
  330. return ready ? OK : ERR_BUSY;
  331. #else
  332. struct pollfd pfd;
  333. pfd.fd = _sock;
  334. pfd.events = POLLIN;
  335. pfd.revents = 0;
  336. switch (p_type) {
  337. case POLL_TYPE_IN:
  338. pfd.events = POLLIN;
  339. break;
  340. case POLL_TYPE_OUT:
  341. pfd.events = POLLOUT;
  342. break;
  343. case POLL_TYPE_IN_OUT:
  344. pfd.events = POLLOUT || POLLIN;
  345. }
  346. int ret = ::poll(&pfd, 1, p_timeout);
  347. ERR_FAIL_COND_V(ret < 0, FAILED);
  348. ERR_FAIL_COND_V(pfd.revents & POLLERR, FAILED);
  349. if (ret == 0)
  350. return ERR_BUSY;
  351. return OK;
  352. #endif
  353. }
  354. Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) {
  355. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  356. r_read = ::recv(_sock, SOCK_BUF(p_buffer), p_len, 0);
  357. if (r_read < 0) {
  358. NetError err = _get_socket_error();
  359. if (err == ERR_NET_WOULD_BLOCK)
  360. return ERR_BUSY;
  361. return FAILED;
  362. }
  363. return OK;
  364. }
  365. Error NetSocketPosix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port) {
  366. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  367. struct sockaddr_storage from;
  368. socklen_t len = sizeof(struct sockaddr_storage);
  369. memset(&from, 0, len);
  370. r_read = ::recvfrom(_sock, SOCK_BUF(p_buffer), p_len, 0, (struct sockaddr *)&from, &len);
  371. if (r_read < 0) {
  372. NetError err = _get_socket_error();
  373. if (err == ERR_NET_WOULD_BLOCK)
  374. return ERR_BUSY;
  375. return FAILED;
  376. }
  377. if (from.ss_family == AF_INET) {
  378. struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
  379. r_ip.set_ipv4((uint8_t *)&sin_from->sin_addr);
  380. r_port = ntohs(sin_from->sin_port);
  381. } else if (from.ss_family == AF_INET6) {
  382. struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
  383. r_ip.set_ipv6((uint8_t *)&s6_from->sin6_addr);
  384. r_port = ntohs(s6_from->sin6_port);
  385. } else {
  386. // Unsupported socket family, should never happen.
  387. ERR_FAIL_V(FAILED);
  388. }
  389. return OK;
  390. }
  391. Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) {
  392. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  393. int flags = 0;
  394. if (_is_stream)
  395. flags = MSG_NOSIGNAL;
  396. r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags);
  397. if (r_sent < 0) {
  398. NetError err = _get_socket_error();
  399. if (err == ERR_NET_WOULD_BLOCK)
  400. return ERR_BUSY;
  401. return FAILED;
  402. }
  403. return OK;
  404. }
  405. Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP_Address p_ip, uint16_t p_port) {
  406. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  407. struct sockaddr_storage addr;
  408. size_t addr_size = _set_addr_storage(&addr, p_ip, p_port, _ip_type);
  409. r_sent = ::sendto(_sock, SOCK_CBUF(p_buffer), p_len, 0, (struct sockaddr *)&addr, addr_size);
  410. if (r_sent < 0) {
  411. NetError err = _get_socket_error();
  412. if (err == ERR_NET_WOULD_BLOCK)
  413. return ERR_BUSY;
  414. return FAILED;
  415. }
  416. return OK;
  417. }
  418. void NetSocketPosix::set_broadcasting_enabled(bool p_enabled) {
  419. ERR_FAIL_COND(!is_open());
  420. // IPv6 has no broadcast support.
  421. ERR_FAIL_COND(_ip_type == IP::TYPE_IPV6);
  422. int par = p_enabled ? 1 : 0;
  423. if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, SOCK_CBUF(&par), sizeof(int)) != 0) {
  424. WARN_PRINT("Unable to change broadcast setting");
  425. }
  426. }
  427. void NetSocketPosix::set_blocking_enabled(bool p_enabled) {
  428. ERR_FAIL_COND(!is_open());
  429. int ret = 0;
  430. #if defined(WINDOWS_ENABLED) || defined(NO_FCNTL)
  431. unsigned long par = p_enabled ? 0 : 1;
  432. ret = SOCK_IOCTL(_sock, FIONBIO, &par);
  433. #else
  434. int opts = fcntl(_sock, F_GETFL);
  435. if (p_enabled)
  436. ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK);
  437. else
  438. ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK);
  439. #endif
  440. if (ret != 0)
  441. WARN_PRINT("Unable to change non-block mode");
  442. }
  443. void NetSocketPosix::set_ipv6_only_enabled(bool p_enabled) {
  444. ERR_FAIL_COND(!is_open());
  445. // This option is only avaiable in IPv6 sockets.
  446. ERR_FAIL_COND(_ip_type == IP::TYPE_IPV4);
  447. int par = p_enabled ? 1 : 0;
  448. if (setsockopt(_sock, IPPROTO_IPV6, IPV6_V6ONLY, SOCK_CBUF(&par), sizeof(int)) != 0) {
  449. WARN_PRINT("Unable to change IPv4 address mapping over IPv6 option");
  450. }
  451. }
  452. void NetSocketPosix::set_tcp_no_delay_enabled(bool p_enabled) {
  453. ERR_FAIL_COND(!is_open());
  454. ERR_FAIL_COND(_ip_type != TYPE_TCP);
  455. int par = p_enabled ? 1 : 0;
  456. if (setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, SOCK_CBUF(&par), sizeof(int)) < 0) {
  457. ERR_PRINT("Unable to set TCP no delay option");
  458. }
  459. }
  460. void NetSocketPosix::set_reuse_address_enabled(bool p_enabled) {
  461. ERR_FAIL_COND(!is_open());
  462. int par = p_enabled ? 1 : 0;
  463. if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, SOCK_CBUF(&par), sizeof(int)) < 0) {
  464. WARN_PRINT("Unable to set socket REUSEADDR option!");
  465. }
  466. }
  467. void NetSocketPosix::set_reuse_port_enabled(bool p_enabled) {
  468. // Windows does not have this option, as it is always ON when setting REUSEADDR.
  469. #ifndef WINDOWS_ENABLED
  470. ERR_FAIL_COND(!is_open());
  471. int par = p_enabled ? 1 : 0;
  472. if (setsockopt(_sock, SOL_SOCKET, SO_REUSEPORT, SOCK_CBUF(&par), sizeof(int)) < 0) {
  473. WARN_PRINT("Unable to set socket REUSEPORT option!");
  474. }
  475. #endif
  476. }
  477. bool NetSocketPosix::is_open() const {
  478. return _sock != SOCK_EMPTY;
  479. }
  480. int NetSocketPosix::get_available_bytes() const {
  481. ERR_FAIL_COND_V(_sock == SOCK_EMPTY, -1);
  482. unsigned long len;
  483. int ret = SOCK_IOCTL(_sock, FIONREAD, &len);
  484. ERR_FAIL_COND_V(ret == -1, 0);
  485. return len;
  486. }
  487. Ref<NetSocket> NetSocketPosix::accept(IP_Address &r_ip, uint16_t &r_port) {
  488. Ref<NetSocket> out;
  489. ERR_FAIL_COND_V(!is_open(), out);
  490. struct sockaddr_storage their_addr;
  491. socklen_t size = sizeof(their_addr);
  492. SOCKET_TYPE fd = ::accept(_sock, (struct sockaddr *)&their_addr, &size);
  493. ERR_FAIL_COND_V(fd == SOCK_EMPTY, out);
  494. _set_ip_port(r_ip, r_port, &their_addr);
  495. NetSocketPosix *ns = memnew(NetSocketPosix);
  496. ns->_set_socket(fd, _ip_type, _is_stream);
  497. ns->set_blocking_enabled(false);
  498. return Ref<NetSocket>(ns);
  499. }