net_socket_unix.cpp 18 KB

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