2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* net_socket_posix.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
#include "net_socket_posix.h"
|
|
|
|
|
2022-10-03 11:43:20 +02:00
|
|
|
// Some proprietary Unix-derived platforms don't expose Unix sockets
|
|
|
|
// so this allows skipping this file to reimplement this API differently.
|
2019-10-01 02:15:50 +02:00
|
|
|
#ifndef UNIX_SOCKET_UNAVAILABLE
|
2022-10-03 11:43:20 +02:00
|
|
|
|
2018-09-01 20:15:42 +02:00
|
|
|
#if defined(UNIX_ENABLED)
|
|
|
|
|
|
|
|
#include <errno.h>
|
2022-10-03 11:43:20 +02:00
|
|
|
#include <fcntl.h>
|
2018-09-01 20:15:42 +02:00
|
|
|
#include <netdb.h>
|
2022-10-03 11:43:20 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
2018-09-01 20:15:42 +02:00
|
|
|
#include <poll.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2022-10-03 11:43:20 +02:00
|
|
|
#include <sys/socket.h>
|
2018-09-01 20:15:42 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
#ifdef WEB_ENABLED
|
2018-09-01 20:15:42 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2019-06-20 11:36:32 +02:00
|
|
|
// BSD calls this flag IPV6_JOIN_GROUP
|
|
|
|
#if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP)
|
|
|
|
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
|
|
|
|
#endif
|
|
|
|
#if !defined(IPV6_DROP_MEMBERSHIP) && defined(IPV6_LEAVE_GROUP)
|
|
|
|
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
|
|
|
|
#endif
|
|
|
|
|
2018-09-01 20:15:42 +02:00
|
|
|
// 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_CLOSE ::close
|
2019-10-24 20:13:48 +02:00
|
|
|
#define SOCK_CONNECT(p_sock, p_addr, p_addr_len) ::connect(p_sock, p_addr, p_addr_len)
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
/* Windows */
|
|
|
|
#elif defined(WINDOWS_ENABLED)
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2018-09-22 02:04:29 +02:00
|
|
|
|
|
|
|
#include <mswsock.h>
|
2018-09-01 20:15:42 +02:00
|
|
|
// 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_CLOSE closesocket
|
2019-10-24 20:13:48 +02:00
|
|
|
// connect is broken on windows under certain conditions, reasons unknown:
|
|
|
|
// See https://github.com/godotengine/webrtc-native/issues/6
|
2020-04-02 01:20:12 +02:00
|
|
|
#define SOCK_CONNECT(p_sock, p_addr, p_addr_len) ::WSAConnect(p_sock, p_addr, p_addr_len, nullptr, nullptr, nullptr, nullptr)
|
2018-09-01 20:15:42 +02:00
|
|
|
|
2018-09-22 02:04:29 +02:00
|
|
|
// Workaround missing flag in MinGW
|
|
|
|
#if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET)
|
|
|
|
#define SIO_UDP_NETRESET _WSAIOW(IOC_VENDOR, 15)
|
|
|
|
#endif
|
2018-09-01 20:15:42 +02:00
|
|
|
|
2022-10-03 11:43:20 +02:00
|
|
|
#endif // UNIX_ENABLED
|
2018-09-01 20:15:42 +02:00
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
size_t NetSocketPosix::_set_addr_storage(struct sockaddr_storage *p_addr, const IPAddress &p_ip, uint16_t p_port, IP::Type p_ip_type) {
|
2018-09-01 20:15:42 +02:00
|
|
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
|
|
|
if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
|
|
|
addr6->sin6_family = AF_INET6;
|
|
|
|
addr6->sin6_port = htons(p_port);
|
|
|
|
if (p_ip.is_valid()) {
|
2021-04-27 16:19:21 +02:00
|
|
|
memcpy(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16);
|
2018-09-01 20:15:42 +02:00
|
|
|
} else {
|
|
|
|
addr6->sin6_addr = in6addr_any;
|
|
|
|
}
|
|
|
|
return sizeof(sockaddr_in6);
|
|
|
|
} else { // IPv4 socket
|
|
|
|
|
|
|
|
// IPv4 socket with IPv6 address
|
2018-11-18 14:54:40 +01:00
|
|
|
ERR_FAIL_COND_V(!p_ip.is_wildcard() && !p_ip.is_ipv4(), 0);
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
|
|
|
addr4->sin_family = AF_INET;
|
|
|
|
addr4->sin_port = htons(p_port); // short, network byte order
|
|
|
|
|
|
|
|
if (p_ip.is_valid()) {
|
2021-04-27 16:19:21 +02:00
|
|
|
memcpy(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 4);
|
2018-09-01 20:15:42 +02:00
|
|
|
} else {
|
|
|
|
addr4->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sizeof(sockaddr_in);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
void NetSocketPosix::_set_ip_port(struct sockaddr_storage *p_addr, IPAddress *r_ip, uint16_t *r_port) {
|
2018-09-01 20:15:42 +02:00
|
|
|
if (p_addr->ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
2021-03-21 11:15:30 +01:00
|
|
|
if (r_ip) {
|
|
|
|
r_ip->set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
|
|
|
|
}
|
|
|
|
if (r_port) {
|
|
|
|
*r_port = ntohs(addr4->sin_port);
|
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
} else if (p_addr->ss_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
2021-03-21 11:15:30 +01:00
|
|
|
if (r_ip) {
|
|
|
|
r_ip->set_ipv6(addr6->sin6_addr.s6_addr);
|
|
|
|
}
|
|
|
|
if (r_port) {
|
|
|
|
*r_port = ntohs(addr6->sin6_port);
|
|
|
|
}
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NetSocket *NetSocketPosix::_create_func() {
|
|
|
|
return memnew(NetSocketPosix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetSocketPosix::make_default() {
|
2018-09-13 15:47:00 +02:00
|
|
|
#if defined(WINDOWS_ENABLED)
|
2020-04-02 01:20:12 +02:00
|
|
|
if (_create == nullptr) {
|
2018-09-13 15:47:00 +02:00
|
|
|
WSADATA data;
|
|
|
|
WSAStartup(MAKEWORD(2, 2), &data);
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-01 20:15:42 +02:00
|
|
|
_create = _create_func;
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:47:00 +02:00
|
|
|
void NetSocketPosix::cleanup() {
|
|
|
|
#if defined(WINDOWS_ENABLED)
|
2020-04-02 01:20:12 +02:00
|
|
|
if (_create != nullptr) {
|
2018-09-13 15:47:00 +02:00
|
|
|
WSACleanup();
|
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
_create = nullptr;
|
2018-09-13 15:47:00 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-12-08 21:07:33 +01:00
|
|
|
NetSocketPosix::NetSocketPosix() :
|
2020-05-12 17:01:17 +02:00
|
|
|
_sock(SOCK_EMPTY) {
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NetSocketPosix::~NetSocketPosix() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:57:35 +02:00
|
|
|
// Silence a warning reported in GH-27594.
|
|
|
|
// EAGAIN and EWOULDBLOCK have the same value on most platforms, but it's not guaranteed.
|
2019-04-02 13:46:35 +02:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wlogical-op"
|
|
|
|
#endif
|
|
|
|
|
2019-12-01 08:57:34 +01:00
|
|
|
NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const {
|
2018-09-01 20:15:42 +02:00
|
|
|
#if defined(WINDOWS_ENABLED)
|
|
|
|
int err = WSAGetLastError();
|
2021-03-21 11:15:30 +01:00
|
|
|
if (err == WSAEISCONN) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_NET_IS_CONNECTED;
|
2021-03-21 11:15:30 +01:00
|
|
|
}
|
|
|
|
if (err == WSAEINPROGRESS || err == WSAEALREADY) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_NET_IN_PROGRESS;
|
2021-03-21 11:15:30 +01:00
|
|
|
}
|
|
|
|
if (err == WSAEWOULDBLOCK) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_NET_WOULD_BLOCK;
|
2021-03-21 11:15:30 +01:00
|
|
|
}
|
|
|
|
if (err == WSAEADDRINUSE || err == WSAEADDRNOTAVAIL) {
|
|
|
|
return ERR_NET_ADDRESS_INVALID_OR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
if (err == WSAEACCES) {
|
|
|
|
return ERR_NET_UNAUTHORIZED;
|
|
|
|
}
|
2019-12-01 08:57:34 +01:00
|
|
|
print_verbose("Socket error: " + itos(err));
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_NET_OTHER;
|
|
|
|
#else
|
2020-05-14 16:41:43 +02:00
|
|
|
if (errno == EISCONN) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_NET_IS_CONNECTED;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (errno == EINPROGRESS || errno == EALREADY) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_NET_IN_PROGRESS;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_NET_WOULD_BLOCK;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-03-21 11:15:30 +01:00
|
|
|
if (errno == EADDRINUSE || errno == EINVAL || errno == EADDRNOTAVAIL) {
|
|
|
|
return ERR_NET_ADDRESS_INVALID_OR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
if (errno == EACCES) {
|
|
|
|
return ERR_NET_UNAUTHORIZED;
|
|
|
|
}
|
2019-12-01 08:57:34 +01:00
|
|
|
print_verbose("Socket error: " + itos(errno));
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_NET_OTHER;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-02 13:46:35 +02:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
bool NetSocketPosix::_can_use_ip(const IPAddress &p_ip, const bool p_for_bind) const {
|
2018-09-01 20:15:42 +02:00
|
|
|
if (p_for_bind && !(p_ip.is_valid() || p_ip.is_wildcard())) {
|
|
|
|
return false;
|
|
|
|
} else if (!p_for_bind && !p_ip.is_valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check if socket support this IP type.
|
|
|
|
IP::Type type = p_ip.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
2019-06-26 15:08:25 +02:00
|
|
|
return !(_ip_type != IP::TYPE_ANY && !p_ip.is_wildcard() && _ip_type != type);
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
_FORCE_INLINE_ Error NetSocketPosix::_change_multicast_group(IPAddress p_ip, String p_if_name, bool p_add) {
|
2019-06-20 11:36:32 +02:00
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
int level = type == IP::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
|
|
|
|
int ret = -1;
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
IPAddress if_ip;
|
2019-06-20 11:36:32 +02:00
|
|
|
uint32_t if_v6id = 0;
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, IP::Interface_Info> if_info;
|
2019-06-20 11:36:32 +02:00
|
|
|
IP::get_singleton()->get_local_interfaces(&if_info);
|
2021-08-09 22:13:42 +02:00
|
|
|
for (KeyValue<String, IP::Interface_Info> &E : if_info) {
|
|
|
|
IP::Interface_Info &c = E.value;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (c.name != p_if_name) {
|
2019-06-20 11:36:32 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-20 11:36:32 +02:00
|
|
|
|
2020-05-13 11:31:51 +02:00
|
|
|
if_v6id = (uint32_t)c.index.to_int();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (type == IP::TYPE_IPV6) {
|
2019-06-20 11:36:32 +02:00
|
|
|
break; // IPv6 uses index.
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-20 11:36:32 +02:00
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const IPAddress &F : c.ip_addresses) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (!F.is_ipv4()) {
|
2019-06-20 11:36:32 +02:00
|
|
|
continue; // Wrong IP type
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-07-16 05:45:57 +02:00
|
|
|
if_ip = F;
|
2019-06-20 11:36:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level == IPPROTO_IP) {
|
|
|
|
ERR_FAIL_COND_V(!if_ip.is_valid(), ERR_INVALID_PARAMETER);
|
|
|
|
struct ip_mreq greq;
|
|
|
|
int sock_opt = p_add ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
|
2021-04-27 16:19:21 +02:00
|
|
|
memcpy(&greq.imr_multiaddr, p_ip.get_ipv4(), 4);
|
|
|
|
memcpy(&greq.imr_interface, if_ip.get_ipv4(), 4);
|
2019-06-20 11:36:32 +02:00
|
|
|
ret = setsockopt(_sock, level, sock_opt, (const char *)&greq, sizeof(greq));
|
|
|
|
} else {
|
|
|
|
struct ipv6_mreq greq;
|
|
|
|
int sock_opt = p_add ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
|
2021-04-27 16:19:21 +02:00
|
|
|
memcpy(&greq.ipv6mr_multiaddr, p_ip.get_ipv6(), 16);
|
2019-06-20 11:36:32 +02:00
|
|
|
greq.ipv6mr_interface = if_v6id;
|
|
|
|
ret = setsockopt(_sock, level, sock_opt, (const char *)&greq, sizeof(greq));
|
|
|
|
}
|
|
|
|
ERR_FAIL_COND_V(ret != 0, FAILED);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-09-01 20:15:42 +02:00
|
|
|
void NetSocketPosix::_set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream) {
|
|
|
|
_sock = p_sock;
|
|
|
|
_ip_type = p_ip_type;
|
|
|
|
_is_stream = p_is_stream;
|
2019-10-07 09:45:21 +02:00
|
|
|
// Disable descriptor sharing with subprocesses.
|
|
|
|
_set_close_exec_enabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
int opts = fcntl(_sock, F_GETFD);
|
|
|
|
fcntl(_sock, F_SETFD, opts | FD_CLOEXEC);
|
|
|
|
#endif
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
|
|
|
|
ERR_FAIL_COND_V(is_open(), ERR_ALREADY_IN_USE);
|
|
|
|
ERR_FAIL_COND_V(ip_type > IP::TYPE_ANY || ip_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
// OpenBSD does not support dual stacking, fallback to IPv4 only.
|
2022-02-16 13:56:32 +01:00
|
|
|
if (ip_type == IP::TYPE_ANY) {
|
2018-09-01 20:15:42 +02:00
|
|
|
ip_type = IP::TYPE_IPV4;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int family = ip_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
|
|
|
|
int protocol = p_sock_type == TYPE_TCP ? IPPROTO_TCP : IPPROTO_UDP;
|
|
|
|
int type = p_sock_type == TYPE_TCP ? SOCK_STREAM : SOCK_DGRAM;
|
|
|
|
_sock = socket(family, type, protocol);
|
|
|
|
|
|
|
|
if (_sock == SOCK_EMPTY && ip_type == IP::TYPE_ANY) {
|
|
|
|
// 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.
|
|
|
|
ip_type = IP::TYPE_IPV4;
|
|
|
|
family = AF_INET;
|
|
|
|
_sock = socket(family, type, protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(_sock == SOCK_EMPTY, FAILED);
|
|
|
|
_ip_type = ip_type;
|
|
|
|
|
|
|
|
if (family == AF_INET6) {
|
|
|
|
// Select IPv4 over IPv6 mapping
|
|
|
|
set_ipv6_only_enabled(ip_type != IP::TYPE_ANY);
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:36:09 +01:00
|
|
|
if (protocol == IPPROTO_UDP) {
|
|
|
|
// Make sure to disable broadcasting for UDP sockets.
|
|
|
|
// Depending on the OS, this option might or might not be enabled by default. Let's normalize it.
|
|
|
|
set_broadcasting_enabled(false);
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_is_stream = p_sock_type == TYPE_TCP;
|
2018-09-22 02:04:29 +02:00
|
|
|
|
2019-10-07 09:45:21 +02:00
|
|
|
// Disable descriptor sharing with subprocesses.
|
|
|
|
_set_close_exec_enabled(true);
|
|
|
|
|
2018-09-22 02:04:29 +02:00
|
|
|
#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) {
|
2020-11-20 09:52:31 +01:00
|
|
|
print_verbose("Unable to turn off UDP WSAECONNRESET behavior on Windows");
|
2018-09-22 02:04:29 +02:00
|
|
|
}
|
|
|
|
if (ioctlsocket(_sock, SIO_UDP_NETRESET, &disable) == SOCKET_ERROR) {
|
|
|
|
// This feature seems not to be supported on wine.
|
2020-11-20 09:52:31 +01:00
|
|
|
print_verbose("Unable to turn off UDP WSAENETRESET behavior on Windows");
|
2018-09-22 02:04:29 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-28 16:58:43 +02:00
|
|
|
#endif
|
|
|
|
#if defined(SO_NOSIGPIPE)
|
|
|
|
// Disable SIGPIPE (should only be relevant to stream sockets, but seems to affect UDP too on iOS)
|
|
|
|
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");
|
|
|
|
}
|
2018-09-22 02:04:29 +02:00
|
|
|
#endif
|
2018-09-01 20:15:42 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetSocketPosix::close() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_sock != SOCK_EMPTY) {
|
2018-09-01 20:15:42 +02:00
|
|
|
SOCK_CLOSE(_sock);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
_sock = SOCK_EMPTY;
|
|
|
|
_ip_type = IP::TYPE_NONE;
|
|
|
|
_is_stream = false;
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error NetSocketPosix::bind(IPAddress p_addr, uint16_t p_port) {
|
2018-09-01 20:15:42 +02:00
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
ERR_FAIL_COND_V(!_can_use_ip(p_addr, true), ERR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
sockaddr_storage addr;
|
|
|
|
size_t addr_size = _set_addr_storage(&addr, p_addr, p_port, _ip_type);
|
|
|
|
|
2019-06-15 12:11:30 +02:00
|
|
|
if (::bind(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
|
2021-03-21 11:15:30 +01:00
|
|
|
NetError err = _get_socket_error();
|
|
|
|
print_verbose("Failed to bind socket. Error: " + itos(err));
|
2018-09-01 20:15:42 +02:00
|
|
|
close();
|
2019-12-01 08:57:34 +01:00
|
|
|
return ERR_UNAVAILABLE;
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error NetSocketPosix::listen(int p_max_pending) {
|
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
|
2019-06-15 12:11:30 +02:00
|
|
|
if (::listen(_sock, p_max_pending) != 0) {
|
2019-12-01 08:57:34 +01:00
|
|
|
_get_socket_error();
|
|
|
|
print_verbose("Failed to listen from socket.");
|
2018-09-01 20:15:42 +02:00
|
|
|
close();
|
2019-12-01 08:57:34 +01:00
|
|
|
return FAILED;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error NetSocketPosix::connect_to_host(IPAddress p_host, uint16_t p_port) {
|
2018-09-01 20:15:42 +02:00
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
ERR_FAIL_COND_V(!_can_use_ip(p_host, false), ERR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
|
|
|
|
|
2019-10-24 20:13:48 +02:00
|
|
|
if (SOCK_CONNECT(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
|
2018-09-01 20:15:42 +02:00
|
|
|
NetError err = _get_socket_error();
|
|
|
|
|
|
|
|
switch (err) {
|
|
|
|
// We are already connected
|
|
|
|
case ERR_NET_IS_CONNECTED:
|
|
|
|
return OK;
|
|
|
|
// Still waiting to connect, try again in a while
|
|
|
|
case ERR_NET_WOULD_BLOCK:
|
|
|
|
case ERR_NET_IN_PROGRESS:
|
|
|
|
return ERR_BUSY;
|
|
|
|
default:
|
2019-12-01 08:57:34 +01:00
|
|
|
print_verbose("Connection to remote host failed!");
|
2018-09-01 20:15:42 +02:00
|
|
|
close();
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-09-21 21:49:50 +02:00
|
|
|
Error NetSocketPosix::poll(PollType p_type, int p_timeout) const {
|
2018-09-01 20:15:42 +02:00
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
|
2018-09-21 21:49:50 +02:00
|
|
|
#if defined(WINDOWS_ENABLED)
|
|
|
|
bool ready = false;
|
|
|
|
fd_set rd, wr, ex;
|
2020-04-02 01:20:12 +02:00
|
|
|
fd_set *rdp = nullptr;
|
|
|
|
fd_set *wrp = nullptr;
|
2018-09-21 21:49:50 +02:00
|
|
|
FD_ZERO(&rd);
|
|
|
|
FD_ZERO(&wr);
|
|
|
|
FD_ZERO(&ex);
|
|
|
|
FD_SET(_sock, &ex);
|
2021-04-26 07:23:39 +02:00
|
|
|
struct timeval timeout = { p_timeout / 1000, (p_timeout % 1000) * 1000 };
|
2021-06-07 10:17:32 +02:00
|
|
|
// For blocking operation, pass nullptr timeout pointer to select.
|
2020-04-02 01:20:12 +02:00
|
|
|
struct timeval *tp = nullptr;
|
2018-09-21 21:49:50 +02:00
|
|
|
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);
|
|
|
|
|
2019-12-01 08:57:34 +01:00
|
|
|
if (ret == SOCKET_ERROR) {
|
|
|
|
return FAILED;
|
|
|
|
}
|
2018-09-21 21:49:50 +02:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (ret == 0) {
|
2018-09-21 21:49:50 +02:00
|
|
|
return ERR_BUSY;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2018-09-21 21:49:50 +02:00
|
|
|
|
2019-12-01 08:57:34 +01:00
|
|
|
if (FD_ISSET(_sock, &ex)) {
|
|
|
|
_get_socket_error();
|
|
|
|
print_verbose("Exception when polling socket.");
|
|
|
|
return FAILED;
|
|
|
|
}
|
2018-09-21 21:49:50 +02:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (rdp && FD_ISSET(_sock, rdp)) {
|
2018-09-21 21:49:50 +02:00
|
|
|
ready = true;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
|
|
|
if (wrp && FD_ISSET(_sock, wrp)) {
|
2018-09-21 21:49:50 +02:00
|
|
|
ready = true;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2018-09-21 21:49:50 +02:00
|
|
|
|
|
|
|
return ready ? OK : ERR_BUSY;
|
|
|
|
#else
|
2018-09-01 20:15:42 +02:00
|
|
|
struct pollfd pfd;
|
|
|
|
pfd.fd = _sock;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
pfd.revents = 0;
|
|
|
|
|
|
|
|
switch (p_type) {
|
|
|
|
case POLL_TYPE_IN:
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
break;
|
|
|
|
case POLL_TYPE_OUT:
|
|
|
|
pfd.events = POLLOUT;
|
|
|
|
break;
|
|
|
|
case POLL_TYPE_IN_OUT:
|
2019-06-20 16:59:48 +02:00
|
|
|
pfd.events = POLLOUT | POLLIN;
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 21:49:50 +02:00
|
|
|
int ret = ::poll(&pfd, 1, p_timeout);
|
2018-09-01 20:15:42 +02:00
|
|
|
|
2019-12-01 08:57:34 +01:00
|
|
|
if (ret < 0 || pfd.revents & POLLERR) {
|
|
|
|
_get_socket_error();
|
|
|
|
print_verbose("Error when polling socket.");
|
|
|
|
return FAILED;
|
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (ret == 0) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_BUSY;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
return OK;
|
2018-09-21 21:49:50 +02:00
|
|
|
#endif
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) {
|
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
|
|
|
|
r_read = ::recv(_sock, SOCK_BUF(p_buffer), p_len, 0);
|
|
|
|
|
|
|
|
if (r_read < 0) {
|
|
|
|
NetError err = _get_socket_error();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (err == ERR_NET_WOULD_BLOCK) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_BUSY;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error NetSocketPosix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port, bool p_peek) {
|
2018-09-01 20:15:42 +02:00
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
|
|
|
|
struct sockaddr_storage from;
|
|
|
|
socklen_t len = sizeof(struct sockaddr_storage);
|
|
|
|
memset(&from, 0, len);
|
|
|
|
|
2019-08-22 19:00:12 +02:00
|
|
|
r_read = ::recvfrom(_sock, SOCK_BUF(p_buffer), p_len, p_peek ? MSG_PEEK : 0, (struct sockaddr *)&from, &len);
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
if (r_read < 0) {
|
|
|
|
NetError err = _get_socket_error();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (err == ERR_NET_WOULD_BLOCK) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_BUSY;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (from.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
|
|
|
|
r_ip.set_ipv4((uint8_t *)&sin_from->sin_addr);
|
|
|
|
r_port = ntohs(sin_from->sin_port);
|
|
|
|
} else if (from.ss_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
|
|
|
|
r_ip.set_ipv6((uint8_t *)&s6_from->sin6_addr);
|
|
|
|
r_port = ntohs(s6_from->sin6_port);
|
|
|
|
} else {
|
|
|
|
// Unsupported socket family, should never happen.
|
|
|
|
ERR_FAIL_V(FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) {
|
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
|
|
|
|
int flags = 0;
|
2019-06-28 16:58:43 +02:00
|
|
|
#ifdef MSG_NOSIGNAL
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_is_stream) {
|
2018-09-01 20:15:42 +02:00
|
|
|
flags = MSG_NOSIGNAL;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-28 16:58:43 +02:00
|
|
|
#endif
|
2018-09-01 20:15:42 +02:00
|
|
|
r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags);
|
|
|
|
|
|
|
|
if (r_sent < 0) {
|
|
|
|
NetError err = _get_socket_error();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (err == ERR_NET_WOULD_BLOCK) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_BUSY;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
|
2018-09-01 20:15:42 +02:00
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (r_sent < 0) {
|
|
|
|
NetError err = _get_socket_error();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (err == ERR_NET_WOULD_BLOCK) {
|
2018-09-01 20:15:42 +02:00
|
|
|
return ERR_BUSY;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:14:07 +01:00
|
|
|
Error NetSocketPosix::set_broadcasting_enabled(bool p_enabled) {
|
|
|
|
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
|
2018-09-01 20:15:42 +02:00
|
|
|
// IPv6 has no broadcast support.
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_ip_type == IP::TYPE_IPV6) {
|
2019-11-25 17:36:09 +01:00
|
|
|
return ERR_UNAVAILABLE;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
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");
|
2019-11-25 17:14:07 +01:00
|
|
|
return FAILED;
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
2019-11-25 17:14:07 +01:00
|
|
|
return OK;
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetSocketPosix::set_blocking_enabled(bool p_enabled) {
|
|
|
|
ERR_FAIL_COND(!is_open());
|
|
|
|
|
|
|
|
int ret = 0;
|
2022-10-03 11:43:20 +02:00
|
|
|
#if defined(WINDOWS_ENABLED)
|
2018-09-01 20:15:42 +02:00
|
|
|
unsigned long par = p_enabled ? 0 : 1;
|
|
|
|
ret = SOCK_IOCTL(_sock, FIONBIO, &par);
|
|
|
|
#else
|
|
|
|
int opts = fcntl(_sock, F_GETFL);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_enabled) {
|
2018-09-01 20:15:42 +02:00
|
|
|
ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2018-09-01 20:15:42 +02:00
|
|
|
ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
#endif
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (ret != 0) {
|
2018-09-01 20:15:42 +02:00
|
|
|
WARN_PRINT("Unable to change non-block mode");
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetSocketPosix::set_ipv6_only_enabled(bool p_enabled) {
|
|
|
|
ERR_FAIL_COND(!is_open());
|
2019-02-13 09:23:29 +01:00
|
|
|
// This option is only available in IPv6 sockets.
|
2018-09-01 20:15:42 +02:00
|
|
|
ERR_FAIL_COND(_ip_type == IP::TYPE_IPV4);
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetSocketPosix::set_tcp_no_delay_enabled(bool p_enabled) {
|
|
|
|
ERR_FAIL_COND(!is_open());
|
2018-09-27 10:43:12 +02:00
|
|
|
ERR_FAIL_COND(!_is_stream); // Not TCP
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetSocketPosix::set_reuse_address_enabled(bool p_enabled) {
|
|
|
|
ERR_FAIL_COND(!is_open());
|
|
|
|
|
2020-02-18 12:55:18 +01:00
|
|
|
// 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
|
2018-09-01 20:15:42 +02:00
|
|
|
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!");
|
|
|
|
}
|
2020-02-18 12:55:18 +01:00
|
|
|
#endif
|
2018-09-01 20:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetSocketPosix::set_reuse_port_enabled(bool p_enabled) {
|
|
|
|
ERR_FAIL_COND(!is_open());
|
|
|
|
|
2020-02-18 12:55:18 +01:00
|
|
|
// See comment above...
|
|
|
|
#ifdef WINDOWS_ENABLED
|
|
|
|
#define SO_REUSEPORT SO_REUSEADDR
|
|
|
|
#endif
|
2018-09-01 20:15:42 +02:00
|
|
|
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!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetSocketPosix::is_open() const {
|
|
|
|
return _sock != SOCK_EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetSocketPosix::get_available_bytes() const {
|
2019-12-01 08:57:34 +01:00
|
|
|
ERR_FAIL_COND_V(!is_open(), -1);
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
unsigned long len;
|
|
|
|
int ret = SOCK_IOCTL(_sock, FIONREAD, &len);
|
2019-12-01 08:57:34 +01:00
|
|
|
if (ret == -1) {
|
|
|
|
_get_socket_error();
|
|
|
|
print_verbose("Error when checking available bytes on socket.");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error NetSocketPosix::get_socket_address(IPAddress *r_ip, uint16_t *r_port) const {
|
2021-03-21 11:15:30 +01:00
|
|
|
ERR_FAIL_COND_V(!is_open(), FAILED);
|
|
|
|
|
|
|
|
struct sockaddr_storage saddr;
|
|
|
|
socklen_t len = sizeof(saddr);
|
|
|
|
if (getsockname(_sock, (struct sockaddr *)&saddr, &len) != 0) {
|
|
|
|
_get_socket_error();
|
|
|
|
print_verbose("Error when reading local socket address.");
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
_set_ip_port(&saddr, r_ip, r_port);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Ref<NetSocket> NetSocketPosix::accept(IPAddress &r_ip, uint16_t &r_port) {
|
2018-09-01 20:15:42 +02:00
|
|
|
Ref<NetSocket> out;
|
|
|
|
ERR_FAIL_COND_V(!is_open(), out);
|
|
|
|
|
|
|
|
struct sockaddr_storage their_addr;
|
|
|
|
socklen_t size = sizeof(their_addr);
|
|
|
|
SOCKET_TYPE fd = ::accept(_sock, (struct sockaddr *)&their_addr, &size);
|
2019-12-01 08:57:34 +01:00
|
|
|
if (fd == SOCK_EMPTY) {
|
|
|
|
_get_socket_error();
|
|
|
|
print_verbose("Error when accepting socket connection.");
|
|
|
|
return out;
|
|
|
|
}
|
2018-09-01 20:15:42 +02:00
|
|
|
|
2021-03-21 11:15:30 +01:00
|
|
|
_set_ip_port(&their_addr, &r_ip, &r_port);
|
2018-09-01 20:15:42 +02:00
|
|
|
|
|
|
|
NetSocketPosix *ns = memnew(NetSocketPosix);
|
|
|
|
ns->_set_socket(fd, _ip_type, _is_stream);
|
|
|
|
ns->set_blocking_enabled(false);
|
|
|
|
return Ref<NetSocket>(ns);
|
|
|
|
}
|
2019-06-20 11:36:32 +02:00
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error NetSocketPosix::join_multicast_group(const IPAddress &p_multi_address, String p_if_name) {
|
2019-07-01 12:59:42 +02:00
|
|
|
return _change_multicast_group(p_multi_address, p_if_name, true);
|
2019-06-20 11:36:32 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error NetSocketPosix::leave_multicast_group(const IPAddress &p_multi_address, String p_if_name) {
|
2019-07-01 12:59:42 +02:00
|
|
|
return _change_multicast_group(p_multi_address, p_if_name, false);
|
2019-06-20 11:36:32 +02:00
|
|
|
}
|
2022-10-03 11:43:20 +02:00
|
|
|
|
|
|
|
#endif // UNIX_SOCKET_UNAVAILABLE
|