summaryrefslogtreecommitdiff
path: root/drivers/unix
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/unix')
-rw-r--r--drivers/unix/dir_access_unix.cpp7
-rw-r--r--drivers/unix/ip_unix.cpp78
-rw-r--r--drivers/unix/ip_unix.h2
-rw-r--r--drivers/unix/net_socket_posix.cpp92
-rw-r--r--drivers/unix/net_socket_posix.h5
5 files changed, 137 insertions, 47 deletions
diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp
index e011176806..251bab5783 100644
--- a/drivers/unix/dir_access_unix.cpp
+++ b/drivers/unix/dir_access_unix.cpp
@@ -100,10 +100,7 @@ bool DirAccessUnix::dir_exists(String p_dir) {
struct stat flags;
bool success = (stat(p_dir.utf8().get_data(), &flags) == 0);
- if (success && S_ISDIR(flags.st_mode))
- return true;
-
- return false;
+ return (success && S_ISDIR(flags.st_mode));
}
uint64_t DirAccessUnix::get_modified_time(String p_file) {
@@ -316,7 +313,7 @@ Error DirAccessUnix::change_dir(String p_dir) {
// try_dir is the directory we are trying to change into
String try_dir = "";
if (p_dir.is_rel_path()) {
- String next_dir = current_dir + "/" + p_dir;
+ String next_dir = current_dir.plus_file(p_dir);
next_dir = next_dir.simplify_path();
try_dir = next_dir;
} else {
diff --git a/drivers/unix/ip_unix.cpp b/drivers/unix/ip_unix.cpp
index b5feaabc32..ce66f07a19 100644
--- a/drivers/unix/ip_unix.cpp
+++ b/drivers/unix/ip_unix.cpp
@@ -72,11 +72,13 @@
#ifdef __FreeBSD__
#include <netinet/in.h>
#endif
+#include <net/if.h> // Order is important on OpenBSD, leave as last
#endif
static IP_Address _sockaddr2ip(struct sockaddr *p_addr) {
IP_Address ip;
+
if (p_addr->sa_family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
ip.set_ipv4((uint8_t *)&(addr->sin_addr));
@@ -129,24 +131,42 @@ IP_Address IP_Unix::_resolve_hostname(const String &p_hostname, Type p_type) {
#if defined(UWP_ENABLED)
-void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
+void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
using namespace Windows::Networking;
using namespace Windows::Networking::Connectivity;
+ // Returns addresses, not interfaces.
auto hostnames = NetworkInformation::GetHostNames();
for (int i = 0; i < hostnames->Size; i++) {
- if (hostnames->GetAt(i)->Type == HostNameType::Ipv4 || hostnames->GetAt(i)->Type == HostNameType::Ipv6 && hostnames->GetAt(i)->IPInformation != nullptr) {
+ auto hostname = hostnames->GetAt(i);
+
+ if (hostname->Type != HostNameType::Ipv4 && hostname->Type != HostNameType::Ipv6)
+ continue;
- r_addresses->push_back(IP_Address(String(hostnames->GetAt(i)->CanonicalName->Data())));
+ String name = hostname->RawName->Data();
+ Map<String, Interface_Info>::Element *E = r_interfaces->find(name);
+ if (!E) {
+ Interface_Info info;
+ info.name = name;
+ info.name_friendly = hostname->DisplayName->Data();
+ info.index = 0;
+ E = r_interfaces->insert(name, info);
+ ERR_CONTINUE(!E);
}
+
+ Interface_Info &info = E->get();
+
+ IP_Address ip = IP_Address(hostname->CanonicalName->Data());
+ info.ip_addresses.push_front(ip);
}
-};
+}
+
#else
-void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
+void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
ULONG buf_size = 1024;
IP_ADAPTER_ADDRESSES *addrs;
@@ -173,29 +193,23 @@ void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
while (adapter != NULL) {
+ Interface_Info info;
+ info.name = adapter->AdapterName;
+ info.name_friendly = adapter->FriendlyName;
+ info.index = String::num_uint64(adapter->IfIndex);
+
IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
while (address != NULL) {
-
- IP_Address ip;
-
- if (address->Address.lpSockaddr->sa_family == AF_INET) {
-
- SOCKADDR_IN *ipv4 = reinterpret_cast<SOCKADDR_IN *>(address->Address.lpSockaddr);
-
- ip.set_ipv4((uint8_t *)&(ipv4->sin_addr));
- r_addresses->push_back(ip);
-
- } else if (address->Address.lpSockaddr->sa_family == AF_INET6) { // ipv6
-
- SOCKADDR_IN6 *ipv6 = reinterpret_cast<SOCKADDR_IN6 *>(address->Address.lpSockaddr);
-
- ip.set_ipv6(ipv6->sin6_addr.s6_addr);
- r_addresses->push_back(ip);
- };
-
+ int family = address->Address.lpSockaddr->sa_family;
+ if (family != AF_INET && family != AF_INET6)
+ continue;
+ info.ip_addresses.push_front(_sockaddr2ip(address->Address.lpSockaddr));
address = address->Next;
- };
+ }
adapter = adapter->Next;
+ // Only add interface if it has at least one IP
+ if (info.ip_addresses.size() > 0)
+ r_interfaces->insert(info.name, info);
};
memfree(addrs);
@@ -205,7 +219,7 @@ void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
#else // UNIX
-void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
+void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
struct ifaddrs *ifAddrStruct = NULL;
struct ifaddrs *ifa = NULL;
@@ -222,8 +236,18 @@ void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
if (family != AF_INET && family != AF_INET6)
continue;
- IP_Address ip = _sockaddr2ip(ifa->ifa_addr);
- r_addresses->push_back(ip);
+ Map<String, Interface_Info>::Element *E = r_interfaces->find(ifa->ifa_name);
+ if (!E) {
+ Interface_Info info;
+ info.name = ifa->ifa_name;
+ info.name_friendly = ifa->ifa_name;
+ info.index = String::num_uint64(if_nametoindex(ifa->ifa_name));
+ E = r_interfaces->insert(ifa->ifa_name, info);
+ ERR_CONTINUE(!E);
+ }
+
+ Interface_Info &info = E->get();
+ info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr));
}
if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
diff --git a/drivers/unix/ip_unix.h b/drivers/unix/ip_unix.h
index e36535146e..f9bb626cc4 100644
--- a/drivers/unix/ip_unix.h
+++ b/drivers/unix/ip_unix.h
@@ -43,7 +43,7 @@ class IP_Unix : public IP {
static IP *_create_unix();
public:
- virtual void get_local_addresses(List<IP_Address> *r_addresses) const;
+ virtual void get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const;
static void make_default();
IP_Unix();
diff --git a/drivers/unix/net_socket_posix.cpp b/drivers/unix/net_socket_posix.cpp
index 37abccbe27..f8e771aea0 100644
--- a/drivers/unix/net_socket_posix.cpp
+++ b/drivers/unix/net_socket_posix.cpp
@@ -55,8 +55,12 @@
#include <netinet/tcp.h>
-#if defined(__APPLE__)
-#define MSG_NOSIGNAL SO_NOSIGPIPE
+// 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
// Some custom defines to minimize ifdefs
@@ -79,10 +83,6 @@
#define SOCK_IOCTL ioctlsocket
#define SOCK_CLOSE closesocket
-// Windows doesn't have this flag
-#ifndef MSG_NOSIGNAL
-#define MSG_NOSIGNAL 0
-#endif
// Workaround missing flag in MinGW
#if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET)
#define SIO_UDP_NETRESET _WSAIOW(IOC_VENDOR, 15)
@@ -221,10 +221,59 @@ bool NetSocketPosix::_can_use_ip(const IP_Address p_ip, const bool p_for_bind) c
}
// Check if socket support this IP type.
IP::Type type = p_ip.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
- if (_ip_type != IP::TYPE_ANY && !p_ip.is_wildcard() && _ip_type != type) {
- return false;
+ return !(_ip_type != IP::TYPE_ANY && !p_ip.is_wildcard() && _ip_type != type);
+}
+
+_FORCE_INLINE_ Error NetSocketPosix::_change_multicast_group(IP_Address p_ip, String p_if_name, bool p_add) {
+
+ 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;
+
+ IP_Address if_ip;
+ uint32_t if_v6id = 0;
+ Map<String, IP::Interface_Info> if_info;
+ IP::get_singleton()->get_local_interfaces(&if_info);
+ for (Map<String, IP::Interface_Info>::Element *E = if_info.front(); E; E = E->next()) {
+ IP::Interface_Info &c = E->get();
+ if (c.name != p_if_name)
+ continue;
+
+ if_v6id = (uint32_t)c.index.to_int64();
+ if (type == IP::TYPE_IPV6)
+ break; // IPv6 uses index.
+
+ for (List<IP_Address>::Element *F = c.ip_addresses.front(); F; F = F->next()) {
+ if (!F->get().is_ipv4())
+ continue; // Wrong IP type
+ if_ip = F->get();
+ break;
+ }
+ break;
}
- return true;
+
+ 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;
+ copymem(&greq.imr_multiaddr, p_ip.get_ipv4(), 4);
+ copymem(&greq.imr_interface, if_ip.get_ipv4(), 4);
+ 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;
+ copymem(&greq.ipv6mr_multiaddr, p_ip.get_ipv6(), 16);
+ 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;
}
void NetSocketPosix::_set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream) {
@@ -285,6 +334,13 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
}
}
#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");
+ }
+#endif
return OK;
}
@@ -306,7 +362,7 @@ Error NetSocketPosix::bind(IP_Address p_addr, uint16_t p_port) {
sockaddr_storage addr;
size_t addr_size = _set_addr_storage(&addr, p_addr, p_port, _ip_type);
- if (::bind(_sock, (struct sockaddr *)&addr, addr_size) == SOCK_EMPTY) {
+ if (::bind(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
close();
ERR_FAIL_V(ERR_UNAVAILABLE);
}
@@ -317,7 +373,7 @@ Error NetSocketPosix::bind(IP_Address p_addr, uint16_t p_port) {
Error NetSocketPosix::listen(int p_max_pending) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
- if (::listen(_sock, p_max_pending) == SOCK_EMPTY) {
+ if (::listen(_sock, p_max_pending) != 0) {
close();
ERR_FAIL_V(FAILED);
@@ -334,7 +390,7 @@ Error NetSocketPosix::connect_to_host(IP_Address p_host, uint16_t p_port) {
struct sockaddr_storage addr;
size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
- if (::connect(_sock, (struct sockaddr *)&addr, addr_size) == SOCK_EMPTY) {
+ if (::connect(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
NetError err = _get_socket_error();
@@ -421,7 +477,7 @@ Error NetSocketPosix::poll(PollType p_type, int p_timeout) const {
pfd.events = POLLOUT;
break;
case POLL_TYPE_IN_OUT:
- pfd.events = POLLOUT || POLLIN;
+ pfd.events = POLLOUT | POLLIN;
}
int ret = ::poll(&pfd, 1, p_timeout);
@@ -489,8 +545,10 @@ 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;
+#ifdef MSG_NOSIGNAL
if (_is_stream)
flags = MSG_NOSIGNAL;
+#endif
r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags);
if (r_sent < 0) {
@@ -625,3 +683,11 @@ Ref<NetSocket> NetSocketPosix::accept(IP_Address &r_ip, uint16_t &r_port) {
ns->set_blocking_enabled(false);
return Ref<NetSocket>(ns);
}
+
+Error NetSocketPosix::join_multicast_group(const IP_Address &p_multi_address, String p_if_name) {
+ return _change_multicast_group(p_multi_address, p_if_name, true);
+}
+
+Error NetSocketPosix::leave_multicast_group(const IP_Address &p_multi_address, String p_if_name) {
+ return _change_multicast_group(p_multi_address, p_if_name, false);
+}
diff --git a/drivers/unix/net_socket_posix.h b/drivers/unix/net_socket_posix.h
index b7fb3fdc94..ce6dc00d42 100644
--- a/drivers/unix/net_socket_posix.h
+++ b/drivers/unix/net_socket_posix.h
@@ -60,6 +60,7 @@ private:
NetError _get_socket_error();
void _set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream);
+ _FORCE_INLINE_ Error _change_multicast_group(IP_Address p_ip, String p_if_name, bool p_add);
protected:
static NetSocket *_create_func();
@@ -76,7 +77,7 @@ public:
virtual void close();
virtual Error bind(IP_Address p_addr, uint16_t p_port);
virtual Error listen(int p_max_pending);
- virtual Error connect_to_host(IP_Address p_addr, uint16_t p_port);
+ virtual Error connect_to_host(IP_Address p_host, uint16_t p_port);
virtual Error poll(PollType p_type, int timeout) const;
virtual Error recv(uint8_t *p_buffer, int p_len, int &r_read);
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port);
@@ -93,6 +94,8 @@ public:
virtual void set_tcp_no_delay_enabled(bool p_enabled);
virtual void set_reuse_address_enabled(bool p_enabled);
virtual void set_reuse_port_enabled(bool p_enabled);
+ virtual Error join_multicast_group(const IP_Address &p_multi_address, String p_if_name);
+ virtual Error leave_multicast_group(const IP_Address &p_multi_address, String p_if_name);
NetSocketPosix();
~NetSocketPosix();