net_socket_posix.cpp 16 KB

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