|
@@ -1,5 +1,5 @@
|
|
/**************************************************************************/
|
|
/**************************************************************************/
|
|
-/* net_socket_posix.cpp */
|
|
|
|
|
|
+/* net_socket_unix.cpp */
|
|
/**************************************************************************/
|
|
/**************************************************************************/
|
|
/* This file is part of: */
|
|
/* This file is part of: */
|
|
/* GODOT ENGINE */
|
|
/* GODOT ENGINE */
|
|
@@ -28,13 +28,11 @@
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
/**************************************************************************/
|
|
/**************************************************************************/
|
|
|
|
|
|
-#include "net_socket_posix.h"
|
|
|
|
-
|
|
|
|
// Some proprietary Unix-derived platforms don't expose Unix sockets
|
|
// Some proprietary Unix-derived platforms don't expose Unix sockets
|
|
// so this allows skipping this file to reimplement this API differently.
|
|
// so this allows skipping this file to reimplement this API differently.
|
|
-#ifndef UNIX_SOCKET_UNAVAILABLE
|
|
|
|
|
|
+#if defined(UNIX_ENABLED) && !defined(UNIX_SOCKET_UNAVAILABLE)
|
|
|
|
|
|
-#if defined(UNIX_ENABLED)
|
|
|
|
|
|
+#include "net_socket_unix.h"
|
|
|
|
|
|
#include <errno.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <fcntl.h>
|
|
@@ -62,44 +60,11 @@
|
|
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
|
|
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
|
|
#endif
|
|
#endif
|
|
|
|
|
|
-// Some custom defines to minimize ifdefs
|
|
|
|
-#define SOCK_EMPTY -1
|
|
|
|
-#define SOCK_BUF(x) x
|
|
|
|
-#define SOCK_CBUF(x) x
|
|
|
|
-#define SOCK_IOCTL ioctl
|
|
|
|
-#define SOCK_FIONREAD_LEN_TYPE int
|
|
|
|
-#define SOCK_CLOSE ::close
|
|
|
|
-#define SOCK_CONNECT(p_sock, p_addr, p_addr_len) ::connect(p_sock, p_addr, p_addr_len)
|
|
|
|
-
|
|
|
|
-/* Windows */
|
|
|
|
-#elif defined(WINDOWS_ENABLED)
|
|
|
|
-#include <winsock2.h>
|
|
|
|
-#include <ws2tcpip.h>
|
|
|
|
-
|
|
|
|
-#include <mswsock.h>
|
|
|
|
-// Some custom defines to minimize ifdefs
|
|
|
|
-#define SOCK_EMPTY INVALID_SOCKET
|
|
|
|
-#define SOCK_BUF(x) (char *)(x)
|
|
|
|
-#define SOCK_CBUF(x) (const char *)(x)
|
|
|
|
-#define SOCK_IOCTL ioctlsocket
|
|
|
|
-#define SOCK_FIONREAD_LEN_TYPE unsigned long
|
|
|
|
-#define SOCK_CLOSE closesocket
|
|
|
|
-// connect is broken on windows under certain conditions, reasons unknown:
|
|
|
|
-// See https://github.com/godotengine/webrtc-native/issues/6
|
|
|
|
-#define SOCK_CONNECT(p_sock, p_addr, p_addr_len) ::WSAConnect(p_sock, p_addr, p_addr_len, nullptr, nullptr, nullptr, nullptr)
|
|
|
|
-
|
|
|
|
-// Workaround missing flag in MinGW
|
|
|
|
-#if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET)
|
|
|
|
-#define SIO_UDP_NETRESET _WSAIOW(IOC_VENDOR, 15)
|
|
|
|
-#endif
|
|
|
|
-
|
|
|
|
-#endif // UNIX_ENABLED
|
|
|
|
-
|
|
|
|
size_t NetSocketPosix::_set_addr_storage(struct sockaddr_storage *p_addr, const IPAddress &p_ip, uint16_t p_port, IP::Type p_ip_type) {
|
|
size_t NetSocketPosix::_set_addr_storage(struct sockaddr_storage *p_addr, const IPAddress &p_ip, uint16_t p_port, IP::Type p_ip_type) {
|
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
|
- if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket
|
|
|
|
|
|
+ if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket.
|
|
|
|
|
|
- // IPv6 only socket with IPv4 address
|
|
|
|
|
|
+ // IPv6 only socket with IPv4 address.
|
|
ERR_FAIL_COND_V(!p_ip.is_wildcard() && p_ip_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0);
|
|
ERR_FAIL_COND_V(!p_ip.is_wildcard() && p_ip_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0);
|
|
|
|
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
|
@@ -111,14 +76,14 @@ size_t NetSocketPosix::_set_addr_storage(struct sockaddr_storage *p_addr, const
|
|
addr6->sin6_addr = in6addr_any;
|
|
addr6->sin6_addr = in6addr_any;
|
|
}
|
|
}
|
|
return sizeof(sockaddr_in6);
|
|
return sizeof(sockaddr_in6);
|
|
- } else { // IPv4 socket
|
|
|
|
|
|
+ } else { // IPv4 socket.
|
|
|
|
|
|
- // IPv4 socket with IPv6 address
|
|
|
|
|
|
+ // IPv4 socket with IPv6 address.
|
|
ERR_FAIL_COND_V(!p_ip.is_wildcard() && !p_ip.is_ipv4(), 0);
|
|
ERR_FAIL_COND_V(!p_ip.is_wildcard() && !p_ip.is_ipv4(), 0);
|
|
|
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
|
addr4->sin_family = AF_INET;
|
|
addr4->sin_family = AF_INET;
|
|
- addr4->sin_port = htons(p_port); // short, network byte order
|
|
|
|
|
|
+ addr4->sin_port = htons(p_port); // Short, network byte order.
|
|
|
|
|
|
if (p_ip.is_valid()) {
|
|
if (p_ip.is_valid()) {
|
|
memcpy(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 4);
|
|
memcpy(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 4);
|
|
@@ -155,26 +120,13 @@ NetSocket *NetSocketPosix::_create_func() {
|
|
}
|
|
}
|
|
|
|
|
|
void NetSocketPosix::make_default() {
|
|
void NetSocketPosix::make_default() {
|
|
-#if defined(WINDOWS_ENABLED)
|
|
|
|
- if (_create == nullptr) {
|
|
|
|
- WSADATA data;
|
|
|
|
- WSAStartup(MAKEWORD(2, 2), &data);
|
|
|
|
- }
|
|
|
|
-#endif
|
|
|
|
_create = _create_func;
|
|
_create = _create_func;
|
|
}
|
|
}
|
|
|
|
|
|
void NetSocketPosix::cleanup() {
|
|
void NetSocketPosix::cleanup() {
|
|
-#if defined(WINDOWS_ENABLED)
|
|
|
|
- if (_create != nullptr) {
|
|
|
|
- WSACleanup();
|
|
|
|
- }
|
|
|
|
- _create = nullptr;
|
|
|
|
-#endif
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-NetSocketPosix::NetSocketPosix() :
|
|
|
|
- _sock(SOCK_EMPTY) {
|
|
|
|
|
|
+NetSocketPosix::NetSocketPosix() {
|
|
}
|
|
}
|
|
|
|
|
|
NetSocketPosix::~NetSocketPosix() {
|
|
NetSocketPosix::~NetSocketPosix() {
|
|
@@ -189,29 +141,6 @@ NetSocketPosix::~NetSocketPosix() {
|
|
#endif
|
|
#endif
|
|
|
|
|
|
NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const {
|
|
NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const {
|
|
-#if defined(WINDOWS_ENABLED)
|
|
|
|
- int err = WSAGetLastError();
|
|
|
|
- if (err == WSAEISCONN) {
|
|
|
|
- return ERR_NET_IS_CONNECTED;
|
|
|
|
- }
|
|
|
|
- if (err == WSAEINPROGRESS || err == WSAEALREADY) {
|
|
|
|
- return ERR_NET_IN_PROGRESS;
|
|
|
|
- }
|
|
|
|
- if (err == WSAEWOULDBLOCK) {
|
|
|
|
- return ERR_NET_WOULD_BLOCK;
|
|
|
|
- }
|
|
|
|
- if (err == WSAEADDRINUSE || err == WSAEADDRNOTAVAIL) {
|
|
|
|
- return ERR_NET_ADDRESS_INVALID_OR_UNAVAILABLE;
|
|
|
|
- }
|
|
|
|
- if (err == WSAEACCES) {
|
|
|
|
- return ERR_NET_UNAUTHORIZED;
|
|
|
|
- }
|
|
|
|
- if (err == WSAEMSGSIZE || err == WSAENOBUFS) {
|
|
|
|
- return ERR_NET_BUFFER_TOO_SMALL;
|
|
|
|
- }
|
|
|
|
- print_verbose("Socket error: " + itos(err));
|
|
|
|
- return ERR_NET_OTHER;
|
|
|
|
-#else
|
|
|
|
if (errno == EISCONN) {
|
|
if (errno == EISCONN) {
|
|
return ERR_NET_IS_CONNECTED;
|
|
return ERR_NET_IS_CONNECTED;
|
|
}
|
|
}
|
|
@@ -230,9 +159,8 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const {
|
|
if (errno == ENOBUFS) {
|
|
if (errno == ENOBUFS) {
|
|
return ERR_NET_BUFFER_TOO_SMALL;
|
|
return ERR_NET_BUFFER_TOO_SMALL;
|
|
}
|
|
}
|
|
- print_verbose("Socket error: " + itos(errno));
|
|
|
|
|
|
+ print_verbose("Socket error: " + itos(errno) + ".");
|
|
return ERR_NET_OTHER;
|
|
return ERR_NET_OTHER;
|
|
-#endif
|
|
|
|
}
|
|
}
|
|
|
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
|
@@ -254,7 +182,7 @@ _FORCE_INLINE_ Error NetSocketPosix::_change_multicast_group(IPAddress p_ip, Str
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
ERR_FAIL_COND_V(!_can_use_ip(p_ip, false), ERR_INVALID_PARAMETER);
|
|
ERR_FAIL_COND_V(!_can_use_ip(p_ip, false), ERR_INVALID_PARAMETER);
|
|
|
|
|
|
- // Need to force level and af_family to IP(v4) when using dual stacking and provided multicast group is IPv4
|
|
|
|
|
|
+ // Need to force level and af_family to IP(v4) when using dual stacking and provided multicast group is IPv4.
|
|
IP::Type type = _ip_type == IP::TYPE_ANY && p_ip.is_ipv4() ? IP::TYPE_IPV4 : _ip_type;
|
|
IP::Type type = _ip_type == IP::TYPE_ANY && p_ip.is_ipv4() ? IP::TYPE_IPV4 : _ip_type;
|
|
// This needs to be the proper level for the multicast group, no matter if the socket is dual stacking.
|
|
// This needs to be the proper level for the multicast group, no matter if the socket is dual stacking.
|
|
int level = type == IP::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
|
|
int level = type == IP::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
|
|
@@ -277,7 +205,7 @@ _FORCE_INLINE_ Error NetSocketPosix::_change_multicast_group(IPAddress p_ip, Str
|
|
|
|
|
|
for (const IPAddress &F : c.ip_addresses) {
|
|
for (const IPAddress &F : c.ip_addresses) {
|
|
if (!F.is_ipv4()) {
|
|
if (!F.is_ipv4()) {
|
|
- continue; // Wrong IP type
|
|
|
|
|
|
+ continue; // Wrong IP type.
|
|
}
|
|
}
|
|
if_ip = F;
|
|
if_ip = F;
|
|
break;
|
|
break;
|
|
@@ -304,7 +232,7 @@ _FORCE_INLINE_ Error NetSocketPosix::_change_multicast_group(IPAddress p_ip, Str
|
|
return OK;
|
|
return OK;
|
|
}
|
|
}
|
|
|
|
|
|
-void NetSocketPosix::_set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream) {
|
|
|
|
|
|
+void NetSocketPosix::_set_socket(int p_sock, IP::Type p_ip_type, bool p_is_stream) {
|
|
_sock = p_sock;
|
|
_sock = p_sock;
|
|
_ip_type = p_ip_type;
|
|
_ip_type = p_ip_type;
|
|
_is_stream = p_is_stream;
|
|
_is_stream = p_is_stream;
|
|
@@ -313,11 +241,9 @@ void NetSocketPosix::_set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_
|
|
}
|
|
}
|
|
|
|
|
|
void NetSocketPosix::_set_close_exec_enabled(bool p_enabled) {
|
|
void NetSocketPosix::_set_close_exec_enabled(bool p_enabled) {
|
|
-#ifndef WINDOWS_ENABLED
|
|
|
|
// Enable close on exec to avoid sharing with subprocesses. Off by default on Windows.
|
|
// Enable close on exec to avoid sharing with subprocesses. Off by default on Windows.
|
|
int opts = fcntl(_sock, F_GETFD);
|
|
int opts = fcntl(_sock, F_GETFD);
|
|
fcntl(_sock, F_SETFD, opts | FD_CLOEXEC);
|
|
fcntl(_sock, F_SETFD, opts | FD_CLOEXEC);
|
|
-#endif
|
|
|
|
}
|
|
}
|
|
|
|
|
|
Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
|
|
Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
|
|
@@ -336,7 +262,7 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
|
|
int type = p_sock_type == TYPE_TCP ? SOCK_STREAM : SOCK_DGRAM;
|
|
int type = p_sock_type == TYPE_TCP ? SOCK_STREAM : SOCK_DGRAM;
|
|
_sock = socket(family, type, protocol);
|
|
_sock = socket(family, type, protocol);
|
|
|
|
|
|
- if (_sock == SOCK_EMPTY && ip_type == IP::TYPE_ANY) {
|
|
|
|
|
|
+ if (_sock == -1 && ip_type == IP::TYPE_ANY) {
|
|
// Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
|
|
// Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
|
|
// in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
|
|
// in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
|
|
ip_type = IP::TYPE_IPV4;
|
|
ip_type = IP::TYPE_IPV4;
|
|
@@ -344,11 +270,11 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
|
|
_sock = socket(family, type, protocol);
|
|
_sock = socket(family, type, protocol);
|
|
}
|
|
}
|
|
|
|
|
|
- ERR_FAIL_COND_V(_sock == SOCK_EMPTY, FAILED);
|
|
|
|
|
|
+ ERR_FAIL_COND_V(_sock == -1, FAILED);
|
|
_ip_type = ip_type;
|
|
_ip_type = ip_type;
|
|
|
|
|
|
if (family == AF_INET6) {
|
|
if (family == AF_INET6) {
|
|
- // Select IPv4 over IPv6 mapping
|
|
|
|
|
|
+ // Select IPv4 over IPv6 mapping.
|
|
set_ipv6_only_enabled(ip_type != IP::TYPE_ANY);
|
|
set_ipv6_only_enabled(ip_type != IP::TYPE_ANY);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -363,36 +289,22 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
|
|
// Disable descriptor sharing with subprocesses.
|
|
// Disable descriptor sharing with subprocesses.
|
|
_set_close_exec_enabled(true);
|
|
_set_close_exec_enabled(true);
|
|
|
|
|
|
-#if defined(WINDOWS_ENABLED)
|
|
|
|
- if (!_is_stream) {
|
|
|
|
- // Disable windows feature/bug reporting WSAECONNRESET/WSAENETRESET when
|
|
|
|
- // recv/recvfrom and an ICMP reply was received from a previous send/sendto.
|
|
|
|
- unsigned long disable = 0;
|
|
|
|
- if (ioctlsocket(_sock, SIO_UDP_CONNRESET, &disable) == SOCKET_ERROR) {
|
|
|
|
- print_verbose("Unable to turn off UDP WSAECONNRESET behavior on Windows");
|
|
|
|
- }
|
|
|
|
- if (ioctlsocket(_sock, SIO_UDP_NETRESET, &disable) == SOCKET_ERROR) {
|
|
|
|
- // This feature seems not to be supported on wine.
|
|
|
|
- print_verbose("Unable to turn off UDP WSAENETRESET behavior on Windows");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-#endif
|
|
|
|
#if defined(SO_NOSIGPIPE)
|
|
#if defined(SO_NOSIGPIPE)
|
|
- // Disable SIGPIPE (should only be relevant to stream sockets, but seems to affect UDP too on iOS)
|
|
|
|
|
|
+ // Disable SIGPIPE (should only be relevant to stream sockets, but seems to affect UDP too on iOS).
|
|
int par = 1;
|
|
int par = 1;
|
|
- if (setsockopt(_sock, SOL_SOCKET, SO_NOSIGPIPE, SOCK_CBUF(&par), sizeof(int)) != 0) {
|
|
|
|
- print_verbose("Unable to turn off SIGPIPE on socket");
|
|
|
|
|
|
+ if (setsockopt(_sock, SOL_SOCKET, SO_NOSIGPIPE, &par, sizeof(int)) != 0) {
|
|
|
|
+ print_verbose("Unable to turn off SIGPIPE on socket.");
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
return OK;
|
|
return OK;
|
|
}
|
|
}
|
|
|
|
|
|
void NetSocketPosix::close() {
|
|
void NetSocketPosix::close() {
|
|
- if (_sock != SOCK_EMPTY) {
|
|
|
|
- SOCK_CLOSE(_sock);
|
|
|
|
|
|
+ if (_sock != -1) {
|
|
|
|
+ ::close(_sock);
|
|
}
|
|
}
|
|
|
|
|
|
- _sock = SOCK_EMPTY;
|
|
|
|
|
|
+ _sock = -1;
|
|
_ip_type = IP::TYPE_NONE;
|
|
_ip_type = IP::TYPE_NONE;
|
|
_is_stream = false;
|
|
_is_stream = false;
|
|
}
|
|
}
|
|
@@ -406,7 +318,7 @@ Error NetSocketPosix::bind(IPAddress p_addr, uint16_t p_port) {
|
|
|
|
|
|
if (::bind(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
|
|
if (::bind(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
|
|
NetError err = _get_socket_error();
|
|
NetError err = _get_socket_error();
|
|
- print_verbose("Failed to bind socket. Error: " + itos(err));
|
|
|
|
|
|
+ print_verbose("Failed to bind socket. Error: " + itos(err) + ".");
|
|
close();
|
|
close();
|
|
return ERR_UNAVAILABLE;
|
|
return ERR_UNAVAILABLE;
|
|
}
|
|
}
|
|
@@ -434,19 +346,19 @@ Error NetSocketPosix::connect_to_host(IPAddress p_host, uint16_t p_port) {
|
|
struct sockaddr_storage addr;
|
|
struct sockaddr_storage addr;
|
|
size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
|
|
size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
|
|
|
|
|
|
- if (SOCK_CONNECT(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
|
|
|
|
|
|
+ if (::connect(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
|
|
NetError err = _get_socket_error();
|
|
NetError err = _get_socket_error();
|
|
|
|
|
|
switch (err) {
|
|
switch (err) {
|
|
- // We are already connected
|
|
|
|
|
|
+ // We are already connected.
|
|
case ERR_NET_IS_CONNECTED:
|
|
case ERR_NET_IS_CONNECTED:
|
|
return OK;
|
|
return OK;
|
|
- // Still waiting to connect, try again in a while
|
|
|
|
|
|
+ // Still waiting to connect, try again in a while.
|
|
case ERR_NET_WOULD_BLOCK:
|
|
case ERR_NET_WOULD_BLOCK:
|
|
case ERR_NET_IN_PROGRESS:
|
|
case ERR_NET_IN_PROGRESS:
|
|
return ERR_BUSY;
|
|
return ERR_BUSY;
|
|
default:
|
|
default:
|
|
- print_verbose("Connection to remote host failed!");
|
|
|
|
|
|
+ print_verbose("Connection to remote host failed.");
|
|
close();
|
|
close();
|
|
return FAILED;
|
|
return FAILED;
|
|
}
|
|
}
|
|
@@ -458,63 +370,6 @@ Error NetSocketPosix::connect_to_host(IPAddress p_host, uint16_t p_port) {
|
|
Error NetSocketPosix::poll(PollType p_type, int p_timeout) const {
|
|
Error NetSocketPosix::poll(PollType p_type, int p_timeout) const {
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
|
|
-#if defined(WINDOWS_ENABLED)
|
|
|
|
- bool ready = false;
|
|
|
|
- fd_set rd, wr, ex;
|
|
|
|
- fd_set *rdp = nullptr;
|
|
|
|
- fd_set *wrp = nullptr;
|
|
|
|
- FD_ZERO(&rd);
|
|
|
|
- FD_ZERO(&wr);
|
|
|
|
- FD_ZERO(&ex);
|
|
|
|
- FD_SET(_sock, &ex);
|
|
|
|
- struct timeval timeout = { p_timeout / 1000, (p_timeout % 1000) * 1000 };
|
|
|
|
- // For blocking operation, pass nullptr timeout pointer to select.
|
|
|
|
- struct timeval *tp = nullptr;
|
|
|
|
- if (p_timeout >= 0) {
|
|
|
|
- // If timeout is non-negative, we want to specify the timeout instead.
|
|
|
|
- tp = &timeout;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- switch (p_type) {
|
|
|
|
- case POLL_TYPE_IN:
|
|
|
|
- FD_SET(_sock, &rd);
|
|
|
|
- rdp = &rd;
|
|
|
|
- break;
|
|
|
|
- case POLL_TYPE_OUT:
|
|
|
|
- FD_SET(_sock, &wr);
|
|
|
|
- wrp = ≀
|
|
|
|
- break;
|
|
|
|
- case POLL_TYPE_IN_OUT:
|
|
|
|
- FD_SET(_sock, &rd);
|
|
|
|
- FD_SET(_sock, &wr);
|
|
|
|
- rdp = &rd;
|
|
|
|
- wrp = ≀
|
|
|
|
- }
|
|
|
|
- int ret = select(1, rdp, wrp, &ex, tp);
|
|
|
|
-
|
|
|
|
- if (ret == SOCKET_ERROR) {
|
|
|
|
- return FAILED;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (ret == 0) {
|
|
|
|
- return ERR_BUSY;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (FD_ISSET(_sock, &ex)) {
|
|
|
|
- _get_socket_error();
|
|
|
|
- print_verbose("Exception when polling socket.");
|
|
|
|
- return FAILED;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (rdp && FD_ISSET(_sock, rdp)) {
|
|
|
|
- ready = true;
|
|
|
|
- }
|
|
|
|
- if (wrp && FD_ISSET(_sock, wrp)) {
|
|
|
|
- ready = true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return ready ? OK : ERR_BUSY;
|
|
|
|
-#else
|
|
|
|
struct pollfd pfd;
|
|
struct pollfd pfd;
|
|
pfd.fd = _sock;
|
|
pfd.fd = _sock;
|
|
pfd.events = POLLIN;
|
|
pfd.events = POLLIN;
|
|
@@ -544,13 +399,12 @@ Error NetSocketPosix::poll(PollType p_type, int p_timeout) const {
|
|
}
|
|
}
|
|
|
|
|
|
return OK;
|
|
return OK;
|
|
-#endif
|
|
|
|
}
|
|
}
|
|
|
|
|
|
Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) {
|
|
Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) {
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
|
|
- r_read = ::recv(_sock, SOCK_BUF(p_buffer), p_len, 0);
|
|
|
|
|
|
+ r_read = ::recv(_sock, p_buffer, p_len, 0);
|
|
|
|
|
|
if (r_read < 0) {
|
|
if (r_read < 0) {
|
|
NetError err = _get_socket_error();
|
|
NetError err = _get_socket_error();
|
|
@@ -575,7 +429,7 @@ Error NetSocketPosix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddr
|
|
socklen_t len = sizeof(struct sockaddr_storage);
|
|
socklen_t len = sizeof(struct sockaddr_storage);
|
|
memset(&from, 0, len);
|
|
memset(&from, 0, len);
|
|
|
|
|
|
- r_read = ::recvfrom(_sock, SOCK_BUF(p_buffer), p_len, p_peek ? MSG_PEEK : 0, (struct sockaddr *)&from, &len);
|
|
|
|
|
|
+ r_read = ::recvfrom(_sock, p_buffer, p_len, p_peek ? MSG_PEEK : 0, (struct sockaddr *)&from, &len);
|
|
|
|
|
|
if (r_read < 0) {
|
|
if (r_read < 0) {
|
|
NetError err = _get_socket_error();
|
|
NetError err = _get_socket_error();
|
|
@@ -615,7 +469,7 @@ Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) {
|
|
flags = MSG_NOSIGNAL;
|
|
flags = MSG_NOSIGNAL;
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
- r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags);
|
|
|
|
|
|
+ r_sent = ::send(_sock, p_buffer, p_len, flags);
|
|
|
|
|
|
if (r_sent < 0) {
|
|
if (r_sent < 0) {
|
|
NetError err = _get_socket_error();
|
|
NetError err = _get_socket_error();
|
|
@@ -637,7 +491,7 @@ Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP
|
|
|
|
|
|
struct sockaddr_storage addr;
|
|
struct sockaddr_storage addr;
|
|
size_t addr_size = _set_addr_storage(&addr, p_ip, p_port, _ip_type);
|
|
size_t addr_size = _set_addr_storage(&addr, p_ip, p_port, _ip_type);
|
|
- r_sent = ::sendto(_sock, SOCK_CBUF(p_buffer), p_len, 0, (struct sockaddr *)&addr, addr_size);
|
|
|
|
|
|
+ r_sent = ::sendto(_sock, p_buffer, p_len, 0, (struct sockaddr *)&addr, addr_size);
|
|
|
|
|
|
if (r_sent < 0) {
|
|
if (r_sent < 0) {
|
|
NetError err = _get_socket_error();
|
|
NetError err = _get_socket_error();
|
|
@@ -662,8 +516,8 @@ Error NetSocketPosix::set_broadcasting_enabled(bool p_enabled) {
|
|
}
|
|
}
|
|
|
|
|
|
int par = p_enabled ? 1 : 0;
|
|
int par = p_enabled ? 1 : 0;
|
|
- if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, SOCK_CBUF(&par), sizeof(int)) != 0) {
|
|
|
|
- WARN_PRINT("Unable to change broadcast setting");
|
|
|
|
|
|
+ if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, &par, sizeof(int)) != 0) {
|
|
|
|
+ WARN_PRINT("Unable to change broadcast setting.");
|
|
return FAILED;
|
|
return FAILED;
|
|
}
|
|
}
|
|
return OK;
|
|
return OK;
|
|
@@ -673,20 +527,15 @@ void NetSocketPosix::set_blocking_enabled(bool p_enabled) {
|
|
ERR_FAIL_COND(!is_open());
|
|
ERR_FAIL_COND(!is_open());
|
|
|
|
|
|
int ret = 0;
|
|
int ret = 0;
|
|
-#if defined(WINDOWS_ENABLED)
|
|
|
|
- unsigned long par = p_enabled ? 0 : 1;
|
|
|
|
- ret = SOCK_IOCTL(_sock, FIONBIO, &par);
|
|
|
|
-#else
|
|
|
|
int opts = fcntl(_sock, F_GETFL);
|
|
int opts = fcntl(_sock, F_GETFL);
|
|
if (p_enabled) {
|
|
if (p_enabled) {
|
|
ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK);
|
|
ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK);
|
|
} else {
|
|
} else {
|
|
ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK);
|
|
ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK);
|
|
}
|
|
}
|
|
-#endif
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
if (ret != 0) {
|
|
- WARN_PRINT("Unable to change non-block mode");
|
|
|
|
|
|
+ WARN_PRINT("Unable to change non-block mode.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -696,56 +545,39 @@ void NetSocketPosix::set_ipv6_only_enabled(bool p_enabled) {
|
|
ERR_FAIL_COND(_ip_type == IP::TYPE_IPV4);
|
|
ERR_FAIL_COND(_ip_type == IP::TYPE_IPV4);
|
|
|
|
|
|
int par = p_enabled ? 1 : 0;
|
|
int par = p_enabled ? 1 : 0;
|
|
- if (setsockopt(_sock, IPPROTO_IPV6, IPV6_V6ONLY, SOCK_CBUF(&par), sizeof(int)) != 0) {
|
|
|
|
- WARN_PRINT("Unable to change IPv4 address mapping over IPv6 option");
|
|
|
|
|
|
+ if (setsockopt(_sock, IPPROTO_IPV6, IPV6_V6ONLY, &par, sizeof(int)) != 0) {
|
|
|
|
+ WARN_PRINT("Unable to change IPv4 address mapping over IPv6 option.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void NetSocketPosix::set_tcp_no_delay_enabled(bool p_enabled) {
|
|
void NetSocketPosix::set_tcp_no_delay_enabled(bool p_enabled) {
|
|
ERR_FAIL_COND(!is_open());
|
|
ERR_FAIL_COND(!is_open());
|
|
- ERR_FAIL_COND(!_is_stream); // Not TCP
|
|
|
|
|
|
+ ERR_FAIL_COND(!_is_stream); // Not TCP.
|
|
|
|
|
|
int par = p_enabled ? 1 : 0;
|
|
int par = p_enabled ? 1 : 0;
|
|
- if (setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, SOCK_CBUF(&par), sizeof(int)) < 0) {
|
|
|
|
- ERR_PRINT("Unable to set TCP no delay option");
|
|
|
|
|
|
+ if (setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, &par, sizeof(int)) < 0) {
|
|
|
|
+ WARN_PRINT("Unable to set TCP no delay option.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void NetSocketPosix::set_reuse_address_enabled(bool p_enabled) {
|
|
void NetSocketPosix::set_reuse_address_enabled(bool p_enabled) {
|
|
ERR_FAIL_COND(!is_open());
|
|
ERR_FAIL_COND(!is_open());
|
|
|
|
|
|
-// On Windows, enabling SO_REUSEADDR actually would also enable reuse port, very bad on TCP. Denying...
|
|
|
|
-// Windows does not have this option, SO_REUSEADDR in this magical world means SO_REUSEPORT
|
|
|
|
-#ifndef WINDOWS_ENABLED
|
|
|
|
- int par = p_enabled ? 1 : 0;
|
|
|
|
- if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, SOCK_CBUF(&par), sizeof(int)) < 0) {
|
|
|
|
- WARN_PRINT("Unable to set socket REUSEADDR option!");
|
|
|
|
- }
|
|
|
|
-#endif
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetSocketPosix::set_reuse_port_enabled(bool p_enabled) {
|
|
|
|
- ERR_FAIL_COND(!is_open());
|
|
|
|
-
|
|
|
|
-// See comment above...
|
|
|
|
-#ifdef WINDOWS_ENABLED
|
|
|
|
-#define SO_REUSEPORT SO_REUSEADDR
|
|
|
|
-#endif
|
|
|
|
int par = p_enabled ? 1 : 0;
|
|
int par = p_enabled ? 1 : 0;
|
|
- if (setsockopt(_sock, SOL_SOCKET, SO_REUSEPORT, SOCK_CBUF(&par), sizeof(int)) < 0) {
|
|
|
|
- WARN_PRINT("Unable to set socket REUSEPORT option!");
|
|
|
|
|
|
+ if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &par, sizeof(int)) < 0) {
|
|
|
|
+ WARN_PRINT("Unable to set socket REUSEADDR option.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
bool NetSocketPosix::is_open() const {
|
|
bool NetSocketPosix::is_open() const {
|
|
- return _sock != SOCK_EMPTY;
|
|
|
|
|
|
+ return _sock != -1;
|
|
}
|
|
}
|
|
|
|
|
|
int NetSocketPosix::get_available_bytes() const {
|
|
int NetSocketPosix::get_available_bytes() const {
|
|
ERR_FAIL_COND_V(!is_open(), -1);
|
|
ERR_FAIL_COND_V(!is_open(), -1);
|
|
|
|
|
|
- SOCK_FIONREAD_LEN_TYPE len;
|
|
|
|
- int ret = SOCK_IOCTL(_sock, FIONREAD, &len);
|
|
|
|
|
|
+ int len;
|
|
|
|
+ int ret = ioctl(_sock, FIONREAD, &len);
|
|
if (ret == -1) {
|
|
if (ret == -1) {
|
|
_get_socket_error();
|
|
_get_socket_error();
|
|
print_verbose("Error when checking available bytes on socket.");
|
|
print_verbose("Error when checking available bytes on socket.");
|
|
@@ -774,8 +606,8 @@ Ref<NetSocket> NetSocketPosix::accept(IPAddress &r_ip, uint16_t &r_port) {
|
|
|
|
|
|
struct sockaddr_storage their_addr;
|
|
struct sockaddr_storage their_addr;
|
|
socklen_t size = sizeof(their_addr);
|
|
socklen_t size = sizeof(their_addr);
|
|
- SOCKET_TYPE fd = ::accept(_sock, (struct sockaddr *)&their_addr, &size);
|
|
|
|
- if (fd == SOCK_EMPTY) {
|
|
|
|
|
|
+ int fd = ::accept(_sock, (struct sockaddr *)&their_addr, &size);
|
|
|
|
+ if (fd == -1) {
|
|
_get_socket_error();
|
|
_get_socket_error();
|
|
print_verbose("Error when accepting socket connection.");
|
|
print_verbose("Error when accepting socket connection.");
|
|
return out;
|
|
return out;
|
|
@@ -797,4 +629,4 @@ Error NetSocketPosix::leave_multicast_group(const IPAddress &p_multi_address, co
|
|
return _change_multicast_group(p_multi_address, p_if_name, false);
|
|
return _change_multicast_group(p_multi_address, p_if_name, false);
|
|
}
|
|
}
|
|
|
|
|
|
-#endif // UNIX_SOCKET_UNAVAILABLE
|
|
|
|
|
|
+#endif // UNIX_ENABLED && !UNIX_SOCKET_UNAVAILABLE
|