summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2017-01-02 15:51:45 +0100
committerGitHub <noreply@github.com>2017-01-02 15:51:45 +0100
commit0b2771bd6561152cdb0e650538a572a242068f01 (patch)
treeb44c1ab2d50f14521cb7d9cafcee37b551871c66 /drivers
parent6428ebd91faa757aff419e3f6d1ccc63b699dd11 (diff)
parentd194e1c48e5d161f0310ee17e63f1951e2c50de6 (diff)
Merge pull request #7271 from Faless/ipv6_cleanup
Fixes and improvementes for IPv6 implementation.
Diffstat (limited to 'drivers')
-rw-r--r--drivers/unix/ip_unix.cpp27
-rw-r--r--drivers/unix/ip_unix.h2
-rw-r--r--drivers/unix/packet_peer_udp_posix.cpp43
-rw-r--r--drivers/unix/packet_peer_udp_posix.h4
-rw-r--r--drivers/unix/socket_helpers.h48
-rw-r--r--drivers/unix/stream_peer_tcp_posix.cpp19
-rw-r--r--drivers/unix/stream_peer_tcp_posix.h2
-rw-r--r--drivers/unix/tcp_server_posix.cpp16
-rw-r--r--drivers/unix/tcp_server_posix.h2
9 files changed, 79 insertions, 84 deletions
diff --git a/drivers/unix/ip_unix.cpp b/drivers/unix/ip_unix.cpp
index f1f130e30f..0ebd593953 100644
--- a/drivers/unix/ip_unix.cpp
+++ b/drivers/unix/ip_unix.cpp
@@ -37,9 +37,6 @@
#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0x00000400
#endif
- #ifndef AI_V4MAPPED
- #define AI_V4MAPPED 0x00000800
- #endif
#ifdef UWP_ENABLED
#include <ws2tcpip.h>
#include <winsock2.h>
@@ -75,32 +72,29 @@ 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.field32[0] = *((unsigned long*)&addr->sin_addr);
- ip.type = IP_Address::TYPE_IPV4;
+ ip.set_ipv4((uint8_t *)&(addr->sin_addr));
} else {
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
- for (int i=0; i<16; i++)
- ip.field8[i] = addr6->sin6_addr.s6_addr[i];
- ip.type = IP_Address::TYPE_IPV6;
+ ip.set_ipv6(addr6->sin6_addr.s6_addr);
};
return ip;
};
-IP_Address IP_Unix::_resolve_hostname(const String& p_hostname, IP_Address::AddrType p_type) {
+IP_Address IP_Unix::_resolve_hostname(const String& p_hostname, Type p_type) {
struct addrinfo hints;
struct addrinfo* result;
memset(&hints, 0, sizeof(struct addrinfo));
- if (p_type == IP_Address::TYPE_IPV4) {
+ if (p_type == TYPE_IPV4) {
hints.ai_family = AF_INET;
- } else if (p_type == IP_Address::TYPE_IPV6) {
+ } else if (p_type == TYPE_IPV6) {
hints.ai_family = AF_INET6;
hints.ai_flags = 0;
} else {
hints.ai_family = AF_UNSPEC;
- hints.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG);
+ hints.ai_flags = AI_ADDRCONFIG;
};
int s = getaddrinfo(p_hostname.utf8().get_data(), NULL, &hints, &result);
@@ -184,15 +178,12 @@ void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
SOCKADDR_IN* ipv4 = reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
- ip.field32[0] = *((unsigned long*)&ipv4->sin_addr);
- ip.type = IP_Address::TYPE_IPV4;
+ ip.set_ipv4((uint8_t *)&(ipv4->sin_addr));
} else { // ipv6
SOCKADDR_IN6* ipv6 = reinterpret_cast<SOCKADDR_IN6*>(address->Address.lpSockaddr);
- for (int i=0; i<16; i++) {
- ip.field8[i] = ipv6->sin6_addr.s6_addr[i];
- };
- ip.type = IP_Address::TYPE_IPV6;
+
+ ip.set_ipv6(ipv6->sin6_addr.s6_addr);
};
diff --git a/drivers/unix/ip_unix.h b/drivers/unix/ip_unix.h
index c901265a2b..00870d5492 100644
--- a/drivers/unix/ip_unix.h
+++ b/drivers/unix/ip_unix.h
@@ -36,7 +36,7 @@
class IP_Unix : public IP {
OBJ_TYPE(IP_Unix, IP);
- virtual IP_Address _resolve_hostname(const String& p_hostname, IP_Address::AddrType p_type);
+ virtual IP_Address _resolve_hostname(const String& p_hostname, IP::Type p_type);
static IP* _create_unix();
public:
diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp
index 2a2d8ba426..4d9ef6cdae 100644
--- a/drivers/unix/packet_peer_udp_posix.cpp
+++ b/drivers/unix/packet_peer_udp_posix.cpp
@@ -76,12 +76,14 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size
uint32_t size;
uint8_t type;
rb.read(&type, 1, true);
- if (type == IP_Address::TYPE_IPV4) {
- rb.read((uint8_t*)&packet_ip.field8,4,true);
- packet_ip.type = IP_Address::TYPE_IPV4;
+ if (type == IP::TYPE_IPV4) {
+ uint8_t ip[4];
+ rb.read(ip,4,true);
+ packet_ip.set_ipv4(ip);
} else {
- rb.read((uint8_t*)&packet_ip.field8,16,true);
- packet_ip.type = IP_Address::TYPE_IPV6;
+ uint8_t ipv6[16];
+ rb.read(ipv6,16,true);
+ packet_ip.set_ipv6(ipv6);
};
rb.read((uint8_t*)&packet_port,4,true);
rb.read((uint8_t*)&size,4,true);
@@ -94,12 +96,12 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size
}
Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
- ERR_FAIL_COND_V(peer_addr.type == IP_Address::TYPE_NONE, ERR_UNCONFIGURED);
+ ERR_FAIL_COND_V(peer_addr == IP_Address(), ERR_UNCONFIGURED);
- int sock = _get_socket(peer_addr.type);
+ int sock = _get_socket();
ERR_FAIL_COND_V( sock == -1, FAILED );
struct sockaddr_storage addr;
- size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port);
+ size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);
errno = 0;
int err;
@@ -119,24 +121,16 @@ int PacketPeerUDPPosix::get_max_packet_size() const{
return 512; // uhm maybe not
}
-Error PacketPeerUDPPosix::listen(int p_port, IP_Address::AddrType p_type, int p_recv_buffer_size) {
+Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size) {
close();
- int sock = _get_socket(p_type);
+ int sock = _get_socket();
if (sock == -1 )
return ERR_CANT_CREATE;
- if(p_type == IP_Address::TYPE_IPV6) {
- // Use IPv6 only socket
- int yes = 1;
- if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&yes, sizeof(yes)) != 0) {
- WARN_PRINT("Unable to unset IPv4 address mapping over IPv6");
- }
- }
-
sockaddr_storage addr = {0};
- size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_type, NULL);
+ size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, NULL);
if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
close();
@@ -171,7 +165,7 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) {
uint32_t port = 0;
if (from.ss_family == AF_INET) {
- uint8_t type = (uint8_t)IP_Address::TYPE_IPV4;
+ uint8_t type = (uint8_t)IP::TYPE_IPV4;
rb.write(&type, 1);
struct sockaddr_in* sin_from = (struct sockaddr_in*)&from;
rb.write((uint8_t*)&sin_from->sin_addr, 4);
@@ -179,7 +173,7 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) {
} else if (from.ss_family == AF_INET6) {
- uint8_t type = (uint8_t)IP_Address::TYPE_IPV6;
+ uint8_t type = (uint8_t)IP::TYPE_IPV6;
rb.write(&type, 1);
struct sockaddr_in6* s6_from = (struct sockaddr_in6*)&from;
@@ -189,7 +183,7 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) {
} else {
// WARN_PRINT("Ignoring packet with unknown address family");
- uint8_t type = (uint8_t)IP_Address::TYPE_NONE;
+ uint8_t type = (uint8_t)IP::TYPE_NONE;
rb.write(&type, 1);
};
@@ -225,12 +219,12 @@ int PacketPeerUDPPosix::get_packet_port() const{
return packet_port;
}
-int PacketPeerUDPPosix::_get_socket(IP_Address::AddrType p_type) {
+int PacketPeerUDPPosix::_get_socket() {
if (sockfd != -1)
return sockfd;
- sockfd = _socket_create(p_type, SOCK_DGRAM, IPPROTO_UDP);
+ sockfd = _socket_create(ip_type, SOCK_DGRAM, IPPROTO_UDP);
return sockfd;
}
@@ -259,6 +253,7 @@ PacketPeerUDPPosix::PacketPeerUDPPosix() {
packet_port=0;
queue_count=0;
peer_port=0;
+ ip_type = IP::TYPE_ANY;
}
PacketPeerUDPPosix::~PacketPeerUDPPosix() {
diff --git a/drivers/unix/packet_peer_udp_posix.h b/drivers/unix/packet_peer_udp_posix.h
index 8449dd2331..89b8886cf5 100644
--- a/drivers/unix/packet_peer_udp_posix.h
+++ b/drivers/unix/packet_peer_udp_posix.h
@@ -52,7 +52,7 @@ class PacketPeerUDPPosix : public PacketPeerUDP {
IP_Address peer_addr;
int peer_port;
- _FORCE_INLINE_ int _get_socket(IP_Address::AddrType p_type);
+ _FORCE_INLINE_ int _get_socket();
static PacketPeerUDP* _create();
virtual Error _poll(bool p_block);
@@ -65,7 +65,7 @@ public:
virtual int get_max_packet_size() const;
- virtual Error listen(int p_port, IP_Address::AddrType p_address_type, int p_recv_buffer_size=65536);
+ virtual Error listen(int p_port, int p_recv_buffer_size=65536);
virtual void close();
virtual Error wait();
virtual bool is_listening() const;
diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h
index 5e8e8dfd7a..962f228c3c 100644
--- a/drivers/unix/socket_helpers.h
+++ b/drivers/unix/socket_helpers.h
@@ -16,31 +16,42 @@
// helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this
-static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port) {
+static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port, IP::Type p_sock_type = IP::TYPE_ANY) {
memset(p_addr, 0, sizeof(struct sockaddr_storage));
- if (p_ip.type == IP_Address::TYPE_IPV6) {
+
+ ERR_FAIL_COND_V(p_ip==IP_Address(),0);
+
+ // IPv6 socket
+ if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) {
+
+ // IPv6 only socket with IPv4 address
+ ERR_FAIL_COND_V(p_sock_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);
- copymem(&addr6->sin6_addr.s6_addr, p_ip.field8, 16);
+ copymem(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16);
return sizeof(sockaddr_in6);
- } else {
+ } else { // IPv4 socket
+ // IPv4 socket with IPv6 address
+ ERR_FAIL_COND_V(!p_ip.is_ipv4(),0);
+
+ uint32_t ipv4 = *((uint32_t *)p_ip.get_ipv4());
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
- addr4->sin_family = AF_INET; // host byte order
+ addr4->sin_family = AF_INET;
addr4->sin_port = htons(p_port); // short, network byte order
- addr4->sin_addr = *((struct in_addr*)&p_ip.field32[0]);
+ copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 16);
return sizeof(sockaddr_in);
};
};
-static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP_Address::AddrType p_address_type, const List<String> *p_accepted_hosts) {
+static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP::Type p_sock_type, const List<String> *p_accepted_hosts) {
memset(p_addr, 0, sizeof(struct sockaddr_storage));
- if (p_address_type == IP_Address::TYPE_IPV4) {
+ if (p_sock_type == IP::TYPE_IPV4) {
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
addr4->sin_family = AF_INET;
addr4->sin_port = htons(p_port);
@@ -56,20 +67,20 @@ static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port,
};
};
-static int _socket_create(IP_Address::AddrType p_type, int type, int protocol) {
+static int _socket_create(IP::Type p_type, int type, int protocol) {
- ERR_FAIL_COND_V(p_type > IP_Address::TYPE_ANY || p_type < IP_Address::TYPE_NONE, ERR_INVALID_PARAMETER);
+ ERR_FAIL_COND_V(p_type > IP::TYPE_ANY || p_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
- int family = p_type == IP_Address::TYPE_IPV4 ? AF_INET : AF_INET6;
+ int family = p_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
int sockfd = socket(family, type, protocol);
ERR_FAIL_COND_V( sockfd == -1, -1 );
if(family == AF_INET6) {
- // Ensure IPv4 over IPv6 is enabled
- int no = 0;
- if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&no, sizeof(no)) != 0) {
- WARN_PRINT("Unable to set IPv4 address mapping over IPv6");
+ // Select IPv4 over IPv6 mapping
+ int opt = p_type != IP::TYPE_ANY;
+ if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&opt, sizeof(opt)) != 0) {
+ WARN_PRINT("Unable to set/unset IPv4 address mapping over IPv6");
}
}
@@ -80,19 +91,16 @@ static int _socket_create(IP_Address::AddrType p_type, int type, int protocol) {
static void _set_ip_addr_port(IP_Address& r_ip, int& r_port, struct sockaddr_storage* p_addr) {
if (p_addr->ss_family == AF_INET) {
- r_ip.type = IP_Address::TYPE_IPV4;
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
- r_ip.field32[0] = (uint32_t)addr4->sin_addr.s_addr;
+ r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
r_port = ntohs(addr4->sin_port);
} else if (p_addr->ss_family == AF_INET6) {
- r_ip.type = IP_Address::TYPE_IPV6;
-
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
- copymem(&addr6->sin6_addr.s6_addr, r_ip.field8, 16);
+ r_ip.set_ipv6(addr6->sin6_addr.s6_addr);
r_port = ntohs(addr6->sin6_port);
};
diff --git a/drivers/unix/stream_peer_tcp_posix.cpp b/drivers/unix/stream_peer_tcp_posix.cpp
index 76f654a277..f2a1417920 100644
--- a/drivers/unix/stream_peer_tcp_posix.cpp
+++ b/drivers/unix/stream_peer_tcp_posix.cpp
@@ -98,7 +98,7 @@ Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
};
struct sockaddr_storage their_addr;
- size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port);
+ size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, ip_type);
if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1) {
@@ -107,7 +107,12 @@ Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
return OK;
};
- return OK;
+ if (errno == EINPROGRESS || errno == EALREADY) {
+ return OK;
+ }
+
+ status = STATUS_ERROR;
+ return ERR_CONNECTION_ERROR;
} else {
status = STATUS_CONNECTED;
@@ -117,8 +122,9 @@ Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
return OK;
};
-void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port) {
+void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type) {
+ ip_type = p_ip_type;
sockfd = p_sockfd;
#ifndef NO_FCNTL
fcntl(sockfd, F_SETFL, O_NONBLOCK);
@@ -135,9 +141,9 @@ void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port)
Error StreamPeerTCPPosix::connect(const IP_Address& p_host, uint16_t p_port) {
- ERR_FAIL_COND_V( p_host.type == IP_Address::TYPE_NONE, ERR_INVALID_PARAMETER);
+ ERR_FAIL_COND_V( p_host == IP_Address(), ERR_INVALID_PARAMETER);
- sockfd = _socket_create(p_host.type, SOCK_STREAM, IPPROTO_TCP);
+ sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == -1) {
ERR_PRINT("Socket creation failed!");
disconnect();
@@ -153,7 +159,7 @@ Error StreamPeerTCPPosix::connect(const IP_Address& p_host, uint16_t p_port) {
#endif
struct sockaddr_storage their_addr;
- size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port);
+ size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, ip_type);
errno = 0;
if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1 && errno != EINPROGRESS) {
@@ -387,6 +393,7 @@ StreamPeerTCPPosix::StreamPeerTCPPosix() {
sockfd = -1;
status = STATUS_NONE;
peer_port = 0;
+ ip_type = IP::TYPE_ANY;
};
StreamPeerTCPPosix::~StreamPeerTCPPosix() {
diff --git a/drivers/unix/stream_peer_tcp_posix.h b/drivers/unix/stream_peer_tcp_posix.h
index df0a49da61..1df509cac4 100644
--- a/drivers/unix/stream_peer_tcp_posix.h
+++ b/drivers/unix/stream_peer_tcp_posix.h
@@ -69,7 +69,7 @@ public:
virtual int get_available_bytes() const;
- void set_socket(int p_sockfd, IP_Address p_host, int p_port);
+ void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type);
virtual IP_Address get_connected_host() const;
virtual uint16_t get_connected_port() const;
diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp
index ef971c0c70..0178f08b8c 100644
--- a/drivers/unix/tcp_server_posix.cpp
+++ b/drivers/unix/tcp_server_posix.cpp
@@ -68,20 +68,13 @@ void TCPServerPosix::make_default() {
TCP_Server::_create = TCPServerPosix::_create;
};
-Error TCPServerPosix::listen(uint16_t p_port, IP_Address::AddrType p_type, const List<String> *p_accepted_hosts) {
+Error TCPServerPosix::listen(uint16_t p_port,const List<String> *p_accepted_hosts) {
int sockfd;
- sockfd = _socket_create(p_type, SOCK_STREAM, IPPROTO_TCP);
+ sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
ERR_FAIL_COND_V(sockfd == -1, FAILED);
- if(p_type == IP_Address::TYPE_IPV6) {
- // Use IPv6 only socket
- int yes = 1;
- if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&yes, sizeof(yes)) != 0) {
- WARN_PRINT("Unable to unset IPv4 address mapping over IPv6");
- }
- }
#ifndef NO_FCNTL
fcntl(sockfd, F_SETFL, O_NONBLOCK);
#else
@@ -95,7 +88,7 @@ Error TCPServerPosix::listen(uint16_t p_port, IP_Address::AddrType p_type, const
}
struct sockaddr_storage addr;
- size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_type, p_accepted_hosts);
+ size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, p_accepted_hosts);
// automatically fill with my IP TODO: use p_accepted_hosts
@@ -164,7 +157,7 @@ Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
int port;
_set_ip_addr_port(ip, port, &their_addr);
- conn->set_socket(fd, ip, port);
+ conn->set_socket(fd, ip, port, ip_type);
return conn;
};
@@ -183,6 +176,7 @@ void TCPServerPosix::stop() {
TCPServerPosix::TCPServerPosix() {
listen_sockfd = -1;
+ ip_type = IP::TYPE_ANY;
};
TCPServerPosix::~TCPServerPosix() {
diff --git a/drivers/unix/tcp_server_posix.h b/drivers/unix/tcp_server_posix.h
index 03020f3822..6f9fa8cb5b 100644
--- a/drivers/unix/tcp_server_posix.h
+++ b/drivers/unix/tcp_server_posix.h
@@ -40,7 +40,7 @@ class TCPServerPosix : public TCP_Server {
public:
- virtual Error listen(uint16_t p_port, IP_Address::AddrType p_type, const List<String> *p_accepted_hosts=NULL);
+ virtual Error listen(uint16_t p_port,const List<String> *p_accepted_hosts=NULL);
virtual bool is_connection_available() const;
virtual Ref<StreamPeerTCP> take_connection();