net_socket_unix.cpp 18 KB

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