TCP/UDP Listen sockets can now be set to IPv6 only

This commit is contained in:
Fabio Alessandrelli 2016-10-28 23:11:53 +02:00
parent 2f1c859272
commit eb27e993f0
6 changed files with 50 additions and 8 deletions

View file

@ -33,6 +33,13 @@
#include <string.h>
#ifdef WINDOWS_ENABLED
// Workaround mingw missing flags!
#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0x00000400
#endif
#ifndef AI_V4MAPPED
#define AI_V4MAPPED 0x00000800
#endif
#ifdef WINRT_ENABLED
#include <ws2tcpip.h>
#include <winsock2.h>
@ -90,8 +97,10 @@ IP_Address IP_Unix::_resolve_hostname(const String& p_hostname, IP_Address::Addr
hints.ai_family = AF_INET;
} else if (p_type == IP_Address::TYPE_IPV6) {
hints.ai_family = AF_INET6;
hints.ai_flags = 0;
} else {
hints.ai_family = AF_UNSPEC;
hints.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG);
};
int s = getaddrinfo(p_hostname.utf8().get_data(), NULL, &hints, &result);

View file

@ -119,15 +119,24 @@ int PacketPeerUDPPosix::get_max_packet_size() const{
return 512; // uhm maybe not
}
Error PacketPeerUDPPosix::listen(int p_port, IP_Address::AddrType p_address_type, int p_recv_buffer_size) {
Error PacketPeerUDPPosix::listen(int p_port, IP_Address::AddrType p_type, int p_recv_buffer_size) {
close();
int sock = _get_socket(p_address_type);
int sock = _get_socket(p_type);
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_address_type, NULL);
size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_type, NULL);
if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
close();

View file

@ -56,8 +56,8 @@ static int _socket_create(IP_Address::AddrType p_type, int type, int protocol) {
if(family == AF_INET6) {
// Ensure IPv4 over IPv6 is enabled
int v6_only = 0;
if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&v6_only, sizeof(v6_only)) != 0) {
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");
}
}

View file

@ -74,6 +74,14 @@ Error TCPServerPosix::listen(uint16_t p_port, IP_Address::AddrType p_type, const
sockfd = _socket_create(p_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

View file

@ -112,15 +112,23 @@ void PacketPeerUDPWinsock::_set_blocking(bool p_blocking) {
};
}
Error PacketPeerUDPWinsock::listen(int p_port, IP_Address::AddrType p_address_type, int p_recv_buffer_size) {
Error PacketPeerUDPWinsock::listen(int p_port, IP_Address::AddrType p_type, int p_recv_buffer_size) {
close();
int sock = _get_socket(p_address_type);
int sock = _get_socket(p_type);
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");
}
}
struct sockaddr_storage addr = {0};
size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_address_type, NULL);
size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_type, NULL);
if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
close();

View file

@ -69,6 +69,14 @@ Error TCPServerWinsock::listen(uint16_t p_port, IP_Address::AddrType p_type,cons
sockfd = _socket_create(p_type, SOCK_STREAM, IPPROTO_TCP);
ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, 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");
}
}
unsigned long par = 1;
if (ioctlsocket(sockfd, FIONBIO, &par)) {
perror("setting non-block mode");