net_socket_unix.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /**************************************************************************/
  2. /* net_socket_unix.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. // Some proprietary Unix-derived platforms don't expose Unix sockets
  31. // so this allows skipping this file to reimplement this API differently.
  32. #if defined(UNIX_ENABLED) && !defined(UNIX_SOCKET_UNAVAILABLE)
  33. #include "net_socket_unix.h"
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <netdb.h>
  37. #include <netinet/in.h>
  38. #include <netinet/tcp.h>
  39. #include <poll.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/socket.h>
  45. #include <sys/types.h>
  46. #include <unistd.h>
  47. #ifdef WEB_ENABLED
  48. #include <arpa/inet.h>
  49. #endif
  50. // BSD calls this flag IPV6_JOIN_GROUP
  51. #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP)
  52. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  53. #endif
  54. #if !defined(IPV6_DROP_MEMBERSHIP) && defined(IPV6_LEAVE_GROUP)
  55. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  56. #endif
  57. size_t NetSocketUnix::_set_addr_storage(struct sockaddr_storage *p_addr, const IPAddress &p_ip, uint16_t p_port, IP::Type p_ip_type) {
  58. memset(p_addr, 0, sizeof(struct sockaddr_storage));
  59. if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket.
  60. // IPv6 only socket with IPv4 address.
  61. ERR_FAIL_COND_V(!p_ip.is_wildcard() && p_ip_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0);
  62. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  63. addr6->sin6_family = AF_INET6;
  64. addr6->sin6_port = htons(p_port);
  65. if (p_ip.is_valid()) {
  66. memcpy(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16);
  67. } else {
  68. addr6->sin6_addr = in6addr_any;
  69. }
  70. return sizeof(sockaddr_in6);
  71. } else { // IPv4 socket.
  72. // IPv4 socket with IPv6 address.
  73. ERR_FAIL_COND_V(!p_ip.is_wildcard() && !p_ip.is_ipv4(), 0);
  74. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  75. addr4->sin_family = AF_INET;
  76. addr4->sin_port = htons(p_port); // Short, network byte order.
  77. if (p_ip.is_valid()) {
  78. memcpy(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 4);
  79. } else {
  80. addr4->sin_addr.s_addr = INADDR_ANY;
  81. }
  82. return sizeof(sockaddr_in);
  83. }
  84. }
  85. void NetSocketUnix::_set_ip_port(struct sockaddr_storage *p_addr, IPAddress *r_ip, uint16_t *r_port) {
  86. if (p_addr->ss_family == AF_INET) {
  87. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  88. if (r_ip) {
  89. r_ip->set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
  90. }
  91. if (r_port) {
  92. *r_port = ntohs(addr4->sin_port);
  93. }
  94. } else if (p_addr->ss_family == AF_INET6) {
  95. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  96. if (r_ip) {
  97. r_ip->set_ipv6(addr6->sin6_addr.s6_addr);
  98. }
  99. if (r_port) {
  100. *r_port = ntohs(addr6->sin6_port);
  101. }
  102. }
  103. }
  104. NetSocket *NetSocketUnix::_create_func() {
  105. return memnew(NetSocketUnix);
  106. }
  107. void NetSocketUnix::make_default() {
  108. _create = _create_func;
  109. }
  110. void NetSocketUnix::cleanup() {
  111. }
  112. NetSocketUnix::NetSocketUnix() {
  113. }
  114. NetSocketUnix::~NetSocketUnix() {
  115. close();
  116. }
  117. // Silence a warning reported in GH-27594.
  118. // EAGAIN and EWOULDBLOCK have the same value on most platforms, but it's not guaranteed.
  119. GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wlogical-op")
  120. NetSocketUnix::NetError NetSocketUnix::_get_socket_error() const {
  121. if (errno == EISCONN) {
  122. return ERR_NET_IS_CONNECTED;
  123. }
  124. if (errno == EINPROGRESS || errno == EALREADY) {
  125. return ERR_NET_IN_PROGRESS;
  126. }
  127. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  128. return ERR_NET_WOULD_BLOCK;
  129. }
  130. if (errno == EADDRINUSE || errno == EINVAL || errno == EADDRNOTAVAIL) {
  131. return ERR_NET_ADDRESS_INVALID_OR_UNAVAILABLE;
  132. }
  133. if (errno == EACCES) {
  134. return ERR_NET_UNAUTHORIZED;
  135. }
  136. if (errno == ENOBUFS) {
  137. return ERR_NET_BUFFER_TOO_SMALL;
  138. }
  139. print_verbose("Socket error: " + itos(errno) + ".");
  140. return ERR_NET_OTHER;
  141. }
  142. GODOT_GCC_WARNING_POP
  143. bool NetSocketUnix::_can_use_ip(const IPAddress &p_ip, const bool p_for_bind) const {
  144. if (p_for_bind && !(p_ip.is_valid() || p_ip.is_wildcard())) {
  145. return false;
  146. } else if (!p_for_bind && !p_ip.is_valid()) {
  147. return false;
  148. }
  149. // Check if socket support this IP type.
  150. IP::Type type = p_ip.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  151. return !(_ip_type != IP::TYPE_ANY && !p_ip.is_wildcard() && _ip_type != type);
  152. }
  153. _FORCE_INLINE_ Error NetSocketUnix::_change_multicast_group(IPAddress p_ip, String p_if_name, bool p_add) {
  154. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  155. ERR_FAIL_COND_V(!_can_use_ip(p_ip, false), ERR_INVALID_PARAMETER);
  156. // Need to force level and af_family to IP(v4) when using dual stacking and provided multicast group is IPv4.
  157. IP::Type type = _ip_type == IP::TYPE_ANY && p_ip.is_ipv4() ? IP::TYPE_IPV4 : _ip_type;
  158. // This needs to be the proper level for the multicast group, no matter if the socket is dual stacking.
  159. int level = type == IP::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
  160. int ret = -1;
  161. IPAddress if_ip;
  162. uint32_t if_v6id = 0;
  163. HashMap<String, IP::Interface_Info> if_info;
  164. IP::get_singleton()->get_local_interfaces(&if_info);
  165. for (KeyValue<String, IP::Interface_Info> &E : if_info) {
  166. IP::Interface_Info &c = E.value;
  167. if (c.name != p_if_name) {
  168. continue;
  169. }
  170. if_v6id = (uint32_t)c.index.to_int();
  171. if (type == IP::TYPE_IPV6) {
  172. break; // IPv6 uses index.
  173. }
  174. for (const IPAddress &F : c.ip_addresses) {
  175. if (!F.is_ipv4()) {
  176. continue; // Wrong IP type.
  177. }
  178. if_ip = F;
  179. break;
  180. }
  181. break;
  182. }
  183. if (level == IPPROTO_IP) {
  184. ERR_FAIL_COND_V(!if_ip.is_valid(), ERR_INVALID_PARAMETER);
  185. struct ip_mreq greq;
  186. int sock_opt = p_add ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
  187. memcpy(&greq.imr_multiaddr, p_ip.get_ipv4(), 4);
  188. memcpy(&greq.imr_interface, if_ip.get_ipv4(), 4);
  189. ret = setsockopt(_sock, level, sock_opt, (const char *)&greq, sizeof(greq));
  190. } else {
  191. struct ipv6_mreq greq;
  192. int sock_opt = p_add ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
  193. memcpy(&greq.ipv6mr_multiaddr, p_ip.get_ipv6(), 16);
  194. greq.ipv6mr_interface = if_v6id;
  195. ret = setsockopt(_sock, level, sock_opt, (const char *)&greq, sizeof(greq));
  196. }
  197. ERR_FAIL_COND_V(ret != 0, FAILED);
  198. return OK;
  199. }
  200. void NetSocketUnix::_set_socket(int p_sock, IP::Type p_ip_type, bool p_is_stream) {
  201. _sock = p_sock;
  202. _ip_type = p_ip_type;
  203. _is_stream = p_is_stream;
  204. // Disable descriptor sharing with subprocesses.
  205. _set_close_exec_enabled(true);
  206. }
  207. void NetSocketUnix::_set_close_exec_enabled(bool p_enabled) {
  208. // Enable close on exec to avoid sharing with subprocesses. Off by default on Windows.
  209. int opts = fcntl(_sock, F_GETFD);
  210. fcntl(_sock, F_SETFD, opts | FD_CLOEXEC);
  211. }
  212. Error NetSocketUnix::open(Type p_sock_type, IP::Type &ip_type) {
  213. ERR_FAIL_COND_V(is_open(), ERR_ALREADY_IN_USE);
  214. ERR_FAIL_COND_V(ip_type > IP::TYPE_ANY || ip_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
  215. #if defined(__OpenBSD__)
  216. // OpenBSD does not support dual stacking, fallback to IPv4 only.
  217. if (ip_type == IP::TYPE_ANY) {
  218. ip_type = IP::TYPE_IPV4;
  219. }
  220. #endif
  221. int family = ip_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
  222. int protocol = p_sock_type == TYPE_TCP ? IPPROTO_TCP : IPPROTO_UDP;
  223. int type = p_sock_type == TYPE_TCP ? SOCK_STREAM : SOCK_DGRAM;
  224. _sock = socket(family, type, protocol);
  225. if (_sock == -1 && ip_type == IP::TYPE_ANY) {
  226. // Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
  227. // in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
  228. ip_type = IP::TYPE_IPV4;
  229. family = AF_INET;
  230. _sock = socket(family, type, protocol);
  231. }
  232. ERR_FAIL_COND_V(_sock == -1, FAILED);
  233. _ip_type = ip_type;
  234. if (family == AF_INET6) {
  235. // Select IPv4 over IPv6 mapping.
  236. set_ipv6_only_enabled(ip_type != IP::TYPE_ANY);
  237. }
  238. if (protocol == IPPROTO_UDP) {
  239. // Make sure to disable broadcasting for UDP sockets.
  240. // Depending on the OS, this option might or might not be enabled by default. Let's normalize it.
  241. set_broadcasting_enabled(false);
  242. }
  243. _is_stream = p_sock_type == TYPE_TCP;
  244. // Disable descriptor sharing with subprocesses.
  245. _set_close_exec_enabled(true);
  246. #if defined(SO_NOSIGPIPE)
  247. // Disable SIGPIPE (should only be relevant to stream sockets, but seems to affect UDP too on iOS).
  248. int par = 1;
  249. if (setsockopt(_sock, SOL_SOCKET, SO_NOSIGPIPE, &par, sizeof(int)) != 0) {
  250. print_verbose("Unable to turn off SIGPIPE on socket.");
  251. }
  252. #endif
  253. return OK;
  254. }
  255. void NetSocketUnix::close() {
  256. if (_sock != -1) {
  257. ::close(_sock);
  258. }
  259. _sock = -1;
  260. _ip_type = IP::TYPE_NONE;
  261. _is_stream = false;
  262. }
  263. Error NetSocketUnix::bind(IPAddress p_addr, uint16_t p_port) {
  264. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  265. ERR_FAIL_COND_V(!_can_use_ip(p_addr, true), ERR_INVALID_PARAMETER);
  266. sockaddr_storage addr;
  267. size_t addr_size = _set_addr_storage(&addr, p_addr, p_port, _ip_type);
  268. if (::bind(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
  269. NetError err = _get_socket_error();
  270. print_verbose("Failed to bind socket. Error: " + itos(err) + ".");
  271. close();
  272. return ERR_UNAVAILABLE;
  273. }
  274. return OK;
  275. }
  276. Error NetSocketUnix::listen(int p_max_pending) {
  277. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  278. if (::listen(_sock, p_max_pending) != 0) {
  279. _get_socket_error();
  280. print_verbose("Failed to listen from socket.");
  281. close();
  282. return FAILED;
  283. }
  284. return OK;
  285. }
  286. Error NetSocketUnix::connect_to_host(IPAddress p_host, uint16_t p_port) {
  287. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  288. ERR_FAIL_COND_V(!_can_use_ip(p_host, false), ERR_INVALID_PARAMETER);
  289. struct sockaddr_storage addr;
  290. size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
  291. if (::connect(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
  292. NetError err = _get_socket_error();
  293. switch (err) {
  294. // We are already connected.
  295. case ERR_NET_IS_CONNECTED:
  296. return OK;
  297. // Still waiting to connect, try again in a while.
  298. case ERR_NET_WOULD_BLOCK:
  299. case ERR_NET_IN_PROGRESS:
  300. return ERR_BUSY;
  301. default:
  302. print_verbose("Connection to remote host failed.");
  303. close();
  304. return FAILED;
  305. }
  306. }
  307. return OK;
  308. }
  309. Error NetSocketUnix::poll(PollType p_type, int p_timeout) const {
  310. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  311. struct pollfd pfd;
  312. pfd.fd = _sock;
  313. pfd.events = POLLIN;
  314. pfd.revents = 0;
  315. switch (p_type) {
  316. case POLL_TYPE_IN:
  317. pfd.events = POLLIN;
  318. break;
  319. case POLL_TYPE_OUT:
  320. pfd.events = POLLOUT;
  321. break;
  322. case POLL_TYPE_IN_OUT:
  323. pfd.events = POLLOUT | POLLIN;
  324. }
  325. int ret = ::poll(&pfd, 1, p_timeout);
  326. if (ret < 0 || pfd.revents & POLLERR) {
  327. _get_socket_error();
  328. print_verbose("Error when polling socket.");
  329. return FAILED;
  330. }
  331. if (ret == 0) {
  332. return ERR_BUSY;
  333. }
  334. return OK;
  335. }
  336. Error NetSocketUnix::recv(uint8_t *p_buffer, int p_len, int &r_read) {
  337. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  338. r_read = ::recv(_sock, p_buffer, p_len, 0);
  339. if (r_read < 0) {
  340. NetError err = _get_socket_error();
  341. if (err == ERR_NET_WOULD_BLOCK) {
  342. return ERR_BUSY;
  343. }
  344. if (err == ERR_NET_BUFFER_TOO_SMALL) {
  345. return ERR_OUT_OF_MEMORY;
  346. }
  347. return FAILED;
  348. }
  349. return OK;
  350. }
  351. Error NetSocketUnix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port, bool p_peek) {
  352. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  353. struct sockaddr_storage from;
  354. socklen_t len = sizeof(struct sockaddr_storage);
  355. memset(&from, 0, len);
  356. r_read = ::recvfrom(_sock, p_buffer, p_len, p_peek ? MSG_PEEK : 0, (struct sockaddr *)&from, &len);
  357. if (r_read < 0) {
  358. NetError err = _get_socket_error();
  359. if (err == ERR_NET_WOULD_BLOCK) {
  360. return ERR_BUSY;
  361. }
  362. if (err == ERR_NET_BUFFER_TOO_SMALL) {
  363. return ERR_OUT_OF_MEMORY;
  364. }
  365. return FAILED;
  366. }
  367. if (from.ss_family == AF_INET) {
  368. struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
  369. r_ip.set_ipv4((uint8_t *)&sin_from->sin_addr);
  370. r_port = ntohs(sin_from->sin_port);
  371. } else if (from.ss_family == AF_INET6) {
  372. struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
  373. r_ip.set_ipv6((uint8_t *)&s6_from->sin6_addr);
  374. r_port = ntohs(s6_from->sin6_port);
  375. } else {
  376. // Unsupported socket family, should never happen.
  377. ERR_FAIL_V(FAILED);
  378. }
  379. return OK;
  380. }
  381. Error NetSocketUnix::send(const uint8_t *p_buffer, int p_len, int &r_sent) {
  382. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  383. int flags = 0;
  384. #ifdef MSG_NOSIGNAL
  385. if (_is_stream) {
  386. flags = MSG_NOSIGNAL;
  387. }
  388. #endif
  389. r_sent = ::send(_sock, p_buffer, p_len, flags);
  390. if (r_sent < 0) {
  391. NetError err = _get_socket_error();
  392. if (err == ERR_NET_WOULD_BLOCK) {
  393. return ERR_BUSY;
  394. }
  395. if (err == ERR_NET_BUFFER_TOO_SMALL) {
  396. return ERR_OUT_OF_MEMORY;
  397. }
  398. return FAILED;
  399. }
  400. return OK;
  401. }
  402. Error NetSocketUnix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
  403. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  404. struct sockaddr_storage addr;
  405. size_t addr_size = _set_addr_storage(&addr, p_ip, p_port, _ip_type);
  406. r_sent = ::sendto(_sock, p_buffer, p_len, 0, (struct sockaddr *)&addr, addr_size);
  407. if (r_sent < 0) {
  408. NetError err = _get_socket_error();
  409. if (err == ERR_NET_WOULD_BLOCK) {
  410. return ERR_BUSY;
  411. }
  412. if (err == ERR_NET_BUFFER_TOO_SMALL) {
  413. return ERR_OUT_OF_MEMORY;
  414. }
  415. return FAILED;
  416. }
  417. return OK;
  418. }
  419. Error NetSocketUnix::set_broadcasting_enabled(bool p_enabled) {
  420. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  421. // IPv6 has no broadcast support.
  422. if (_ip_type == IP::TYPE_IPV6) {
  423. return ERR_UNAVAILABLE;
  424. }
  425. int par = p_enabled ? 1 : 0;
  426. if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, &par, sizeof(int)) != 0) {
  427. WARN_PRINT("Unable to change broadcast setting.");
  428. return FAILED;
  429. }
  430. return OK;
  431. }
  432. void NetSocketUnix::set_blocking_enabled(bool p_enabled) {
  433. ERR_FAIL_COND(!is_open());
  434. int ret = 0;
  435. int opts = fcntl(_sock, F_GETFL);
  436. if (p_enabled) {
  437. ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK);
  438. } else {
  439. ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK);
  440. }
  441. if (ret != 0) {
  442. WARN_PRINT("Unable to change non-block mode.");
  443. }
  444. }
  445. void NetSocketUnix::set_ipv6_only_enabled(bool p_enabled) {
  446. ERR_FAIL_COND(!is_open());
  447. // This option is only available in IPv6 sockets.
  448. ERR_FAIL_COND(_ip_type == IP::TYPE_IPV4);
  449. int par = p_enabled ? 1 : 0;
  450. if (setsockopt(_sock, IPPROTO_IPV6, IPV6_V6ONLY, &par, sizeof(int)) != 0) {
  451. WARN_PRINT("Unable to change IPv4 address mapping over IPv6 option.");
  452. }
  453. }
  454. void NetSocketUnix::set_tcp_no_delay_enabled(bool p_enabled) {
  455. ERR_FAIL_COND(!is_open());
  456. ERR_FAIL_COND(!_is_stream); // Not TCP.
  457. int par = p_enabled ? 1 : 0;
  458. if (setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, &par, sizeof(int)) < 0) {
  459. WARN_PRINT("Unable to set TCP no delay option.");
  460. }
  461. }
  462. void NetSocketUnix::set_reuse_address_enabled(bool p_enabled) {
  463. ERR_FAIL_COND(!is_open());
  464. int par = p_enabled ? 1 : 0;
  465. if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &par, sizeof(int)) < 0) {
  466. WARN_PRINT("Unable to set socket REUSEADDR option.");
  467. }
  468. }
  469. bool NetSocketUnix::is_open() const {
  470. return _sock != -1;
  471. }
  472. int NetSocketUnix::get_available_bytes() const {
  473. ERR_FAIL_COND_V(!is_open(), -1);
  474. int len;
  475. int ret = ioctl(_sock, FIONREAD, &len);
  476. if (ret == -1) {
  477. _get_socket_error();
  478. print_verbose("Error when checking available bytes on socket.");
  479. return -1;
  480. }
  481. return len;
  482. }
  483. Error NetSocketUnix::get_socket_address(IPAddress *r_ip, uint16_t *r_port) const {
  484. ERR_FAIL_COND_V(!is_open(), FAILED);
  485. struct sockaddr_storage saddr;
  486. socklen_t len = sizeof(saddr);
  487. if (getsockname(_sock, (struct sockaddr *)&saddr, &len) != 0) {
  488. _get_socket_error();
  489. print_verbose("Error when reading local socket address.");
  490. return FAILED;
  491. }
  492. _set_ip_port(&saddr, r_ip, r_port);
  493. return OK;
  494. }
  495. Ref<NetSocket> NetSocketUnix::accept(IPAddress &r_ip, uint16_t &r_port) {
  496. Ref<NetSocket> out;
  497. ERR_FAIL_COND_V(!is_open(), out);
  498. struct sockaddr_storage their_addr;
  499. socklen_t size = sizeof(their_addr);
  500. int fd = ::accept(_sock, (struct sockaddr *)&their_addr, &size);
  501. if (fd == -1) {
  502. _get_socket_error();
  503. print_verbose("Error when accepting socket connection.");
  504. return out;
  505. }
  506. _set_ip_port(&their_addr, &r_ip, &r_port);
  507. NetSocketUnix *ns = memnew(NetSocketUnix);
  508. ns->_set_socket(fd, _ip_type, _is_stream);
  509. ns->set_blocking_enabled(false);
  510. return Ref<NetSocket>(ns);
  511. }
  512. Error NetSocketUnix::join_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) {
  513. return _change_multicast_group(p_multi_address, p_if_name, true);
  514. }
  515. Error NetSocketUnix::leave_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) {
  516. return _change_multicast_group(p_multi_address, p_if_name, false);
  517. }
  518. #endif // UNIX_ENABLED && !UNIX_SOCKET_UNAVAILABLE