net_socket_winsock.cpp 18 KB

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