net_socket_posix.cpp 17 KB

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