Use an instance variable for ip_type in raw sockets

PacketPeerUDP/StreamPeerTCP/TCP_Server now uses an instance variable
to store the selected ip_type (IPv4/IPv6/ANY, where ANY = dual stack).
All calls to resolve addresses, sending/receving data, connecting/listening
will use that socket type.
This commit is contained in:
Fabio Alessandrelli 2016-11-30 20:45:19 +01:00
parent 311f1f165b
commit 95bdd97768
18 changed files with 68 additions and 51 deletions

View File

@ -38,13 +38,13 @@ String PacketPeerUDP::_get_packet_ip() const {
return get_packet_address();
}
Error PacketPeerUDP::_set_send_address(const String& p_address,int p_port,IP_Address::AddrType p_type) {
Error PacketPeerUDP::_set_send_address(const String& p_address, int p_port) {
IP_Address ip;
if (p_address.is_valid_ip_address()) {
ip=p_address;
} else {
ip=IP::get_singleton()->resolve_hostname(p_address, p_type);
ip=IP::get_singleton()->resolve_hostname(p_address, ip_type);
if (ip==IP_Address())
return ERR_CANT_RESOLVE;
}
@ -55,14 +55,14 @@ Error PacketPeerUDP::_set_send_address(const String& p_address,int p_port,IP_Add
void PacketPeerUDP::_bind_methods() {
ObjectTypeDB::bind_method(_MD("listen:Error","port","ip_type", "recv_buf_size"),&PacketPeerUDP::listen,DEFVAL(IP_Address::TYPE_ANY),DEFVAL(65536));
ObjectTypeDB::bind_method(_MD("listen:Error","port", "recv_buf_size"),&PacketPeerUDP::listen,DEFVAL(65536));
ObjectTypeDB::bind_method(_MD("close"),&PacketPeerUDP::close);
ObjectTypeDB::bind_method(_MD("wait:Error"),&PacketPeerUDP::wait);
ObjectTypeDB::bind_method(_MD("is_listening"),&PacketPeerUDP::is_listening);
ObjectTypeDB::bind_method(_MD("get_packet_ip"),&PacketPeerUDP::_get_packet_ip);
//ObjectTypeDB::bind_method(_MD("get_packet_address"),&PacketPeerUDP::_get_packet_address);
ObjectTypeDB::bind_method(_MD("get_packet_port"),&PacketPeerUDP::get_packet_port);
ObjectTypeDB::bind_method(_MD("set_send_address","host","port","ip_type"),&PacketPeerUDP::_set_send_address,DEFVAL(IP_Address::TYPE_ANY));
ObjectTypeDB::bind_method(_MD("set_send_address","host","port"),&PacketPeerUDP::_set_send_address);
}
@ -83,4 +83,5 @@ PacketPeerUDP* PacketPeerUDP::create() {
PacketPeerUDP::PacketPeerUDP()
{
ip_type = IP_Address::TYPE_ANY;
}

View File

@ -37,16 +37,18 @@ class PacketPeerUDP : public PacketPeer {
protected:
IP_Address::AddrType ip_type;
static PacketPeerUDP* (*_create)();
static void _bind_methods();
String _get_packet_ip() const;
virtual Error _set_send_address(const String& p_address,int p_port, IP_Address::AddrType p_address_type = IP_Address::TYPE_ANY);
virtual Error _set_send_address(const String& p_address,int p_port);
public:
virtual Error listen(int p_port, IP_Address::AddrType p_address_type = IP_Address::TYPE_ANY, int p_recv_buffer_size=65536)=0;
virtual Error listen(int p_port, int p_recv_buffer_size=65536)=0;
virtual void close()=0;
virtual Error wait()=0;
virtual bool is_listening() const=0;

View File

@ -32,13 +32,13 @@ StreamPeerTCP* (*StreamPeerTCP::_create)()=NULL;
VARIANT_ENUM_CAST(IP_Address::AddrType);
Error StreamPeerTCP::_connect(const String& p_address,int p_port,IP_Address::AddrType p_type) {
Error StreamPeerTCP::_connect(const String& p_address,int p_port) {
IP_Address ip;
if (p_address.is_valid_ip_address()) {
ip=p_address;
} else {
ip=IP::get_singleton()->resolve_hostname(p_address, p_type);
ip=IP::get_singleton()->resolve_hostname(p_address, ip_type);
if (ip==IP_Address())
return ERR_CANT_RESOLVE;
}
@ -49,7 +49,7 @@ Error StreamPeerTCP::_connect(const String& p_address,int p_port,IP_Address::Add
void StreamPeerTCP::_bind_methods() {
ObjectTypeDB::bind_method(_MD("connect","host","port","ip_type"),&StreamPeerTCP::_connect,DEFVAL(IP_Address::TYPE_ANY));
ObjectTypeDB::bind_method(_MD("connect","host","port"),&StreamPeerTCP::_connect);
ObjectTypeDB::bind_method(_MD("is_connected"),&StreamPeerTCP::is_connected);
ObjectTypeDB::bind_method(_MD("get_status"),&StreamPeerTCP::get_status);
ObjectTypeDB::bind_method(_MD("get_connected_host"),&StreamPeerTCP::get_connected_host);
@ -79,6 +79,7 @@ StreamPeerTCP* StreamPeerTCP::create() {
StreamPeerTCP::StreamPeerTCP() {
ip_type = IP_Address::TYPE_ANY;
}
StreamPeerTCP::~StreamPeerTCP() {

View File

@ -51,7 +51,9 @@ public:
protected:
virtual Error _connect(const String& p_address, int p_port, IP_Address::AddrType p_type = IP_Address::TYPE_ANY);
IP_Address::AddrType ip_type;
virtual Error _connect(const String& p_address, int p_port);
static StreamPeerTCP* (*_create)();
static void _bind_methods();

View File

@ -46,19 +46,19 @@ TCP_Server* TCP_Server::create() {
return _create();
}
Error TCP_Server::_listen(uint16_t p_port, IP_Address::AddrType p_type, DVector<String> p_accepted_hosts) {
Error TCP_Server::_listen(uint16_t p_port, DVector<String> p_accepted_hosts) {
List<String> hosts;
for(int i=0;i<p_accepted_hosts.size();i++)
hosts.push_back(p_accepted_hosts.get(i));
return listen(p_port,p_type, hosts.size()?&hosts:NULL);
return listen(p_port, hosts.size()?&hosts:NULL);
}
void TCP_Server::_bind_methods() {
ObjectTypeDB::bind_method(_MD("listen","port","ip_type", "accepted_hosts"),&TCP_Server::_listen,DEFVAL(IP_Address::TYPE_ANY),DEFVAL(DVector<String>()));
ObjectTypeDB::bind_method(_MD("listen","port","accepted_hosts"),&TCP_Server::_listen,DEFVAL(DVector<String>()));
ObjectTypeDB::bind_method(_MD("is_connection_available"),&TCP_Server::is_connection_available);
ObjectTypeDB::bind_method(_MD("take_connection"),&TCP_Server::take_connection);
ObjectTypeDB::bind_method(_MD("stop"),&TCP_Server::stop);
@ -68,4 +68,5 @@ void TCP_Server::_bind_methods() {
TCP_Server::TCP_Server()
{
ip_type = IP_Address::TYPE_ANY;
}

View File

@ -38,14 +38,16 @@ class TCP_Server : public Reference {
OBJ_TYPE( TCP_Server, Reference );
protected:
IP_Address::AddrType ip_type;
static TCP_Server* (*_create)();
//bind helper
Error _listen(uint16_t p_port, IP_Address::AddrType p_type = IP_Address::TYPE_ANY ,DVector<String> p_accepted_hosts=DVector<String>());
Error _listen(uint16_t p_port, DVector<String> p_accepted_hosts=DVector<String>());
static void _bind_methods();
public:
virtual Error listen(uint16_t p_port, IP_Address::AddrType p_type = IP_Address::TYPE_ANY, const List<String> *p_accepted_hosts=NULL)=0;
virtual Error listen(uint16_t p_port, const List<String> *p_accepted_hosts=NULL)=0;
virtual bool is_connection_available() const=0;
virtual Ref<StreamPeerTCP> take_connection()=0;

View File

@ -96,7 +96,7 @@ 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);
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);
@ -119,15 +119,15 @@ 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) {
if(ip_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) {
@ -136,7 +136,7 @@ Error PacketPeerUDPPosix::listen(int p_port, IP_Address::AddrType p_type, int p_
}
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();
@ -225,12 +225,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 +259,7 @@ PacketPeerUDPPosix::PacketPeerUDPPosix() {
packet_port=0;
queue_count=0;
peer_port=0;
ip_type = IP_Address::TYPE_ANY;
}
PacketPeerUDPPosix::~PacketPeerUDPPosix() {

View File

@ -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;

View File

@ -122,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_Address::AddrType p_ip_type) {
ip_type = p_ip_type;
sockfd = p_sockfd;
#ifndef NO_FCNTL
fcntl(sockfd, F_SETFL, O_NONBLOCK);
@ -142,7 +143,7 @@ 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);
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();
@ -392,6 +393,7 @@ StreamPeerTCPPosix::StreamPeerTCPPosix() {
sockfd = -1;
status = STATUS_NONE;
peer_port = 0;
ip_type = IP_Address::TYPE_ANY;
};
StreamPeerTCPPosix::~StreamPeerTCPPosix() {

View File

@ -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_Address::AddrType p_ip_type);
virtual IP_Address get_connected_host() const;
virtual uint16_t get_connected_port() const;

View File

@ -68,14 +68,14 @@ 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) {
if(ip_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) {
@ -95,7 +95,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 +164,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 +183,7 @@ void TCPServerPosix::stop() {
TCPServerPosix::TCPServerPosix() {
listen_sockfd = -1;
ip_type = IP_Address::TYPE_ANY;
};
TCPServerPosix::~TCPServerPosix() {

View File

@ -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();

View File

@ -71,7 +71,7 @@ Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer,int &r_buffer_si
}
Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer,int p_buffer_size){
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);
@ -112,14 +112,14 @@ void PacketPeerUDPWinsock::_set_blocking(bool p_blocking) {
};
}
Error PacketPeerUDPWinsock::listen(int p_port, IP_Address::AddrType p_type, int p_recv_buffer_size) {
Error PacketPeerUDPWinsock::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) {
if(ip_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) {
@ -128,7 +128,7 @@ Error PacketPeerUDPWinsock::listen(int p_port, IP_Address::AddrType p_type, int
}
struct 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();
@ -242,12 +242,12 @@ int PacketPeerUDPWinsock::get_packet_port() const{
return packet_port;
}
int PacketPeerUDPWinsock::_get_socket(IP_Address::AddrType p_type) {
int PacketPeerUDPWinsock::_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;
}
@ -277,6 +277,7 @@ PacketPeerUDPWinsock::PacketPeerUDPWinsock() {
packet_port=0;
queue_count=0;
peer_port=0;
ip_type = IP_Address::TYPE_ANY;
}
PacketPeerUDPWinsock::~PacketPeerUDPWinsock() {

View File

@ -50,7 +50,7 @@ class PacketPeerUDPWinsock : 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();
@ -67,7 +67,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;

View File

@ -289,8 +289,9 @@ void StreamPeerWinsock::disconnect() {
peer_port = 0;
};
void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port) {
void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP_Address::AddrType p_ip_type) {
ip_type = p_ip_type;
sockfd = p_sockfd;
status = STATUS_CONNECTING;
peer_host = p_host;
@ -301,7 +302,7 @@ Error StreamPeerWinsock::connect(const IP_Address& p_host, uint16_t p_port) {
ERR_FAIL_COND_V( p_host.type == IP_Address::TYPE_NONE, ERR_INVALID_PARAMETER);
sockfd = _socket_create(p_host.type, SOCK_STREAM, IPPROTO_TCP);
sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == INVALID_SOCKET) {
ERR_PRINT("Socket creation failed!");
disconnect();
@ -367,6 +368,7 @@ StreamPeerWinsock::StreamPeerWinsock() {
sockfd = INVALID_SOCKET;
status = STATUS_NONE;
peer_port = 0;
ip_type = IP_Address::TYPE_ANY;
};
StreamPeerWinsock::~StreamPeerWinsock() {

View File

@ -68,7 +68,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_Address::AddrType p_ip_type);
virtual IP_Address get_connected_host() const;
virtual uint16_t get_connected_port() const;

View File

@ -63,13 +63,13 @@ void TCPServerWinsock::cleanup() {
};
Error TCPServerWinsock::listen(uint16_t p_port, IP_Address::AddrType p_type,const List<String> *p_accepted_hosts) {
Error TCPServerWinsock::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 == INVALID_SOCKET, FAILED);
if(p_type == IP_Address::TYPE_IPV6) {
if(ip_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) {
@ -85,7 +85,7 @@ Error TCPServerWinsock::listen(uint16_t p_port, IP_Address::AddrType p_type,cons
};
struct sockaddr_storage my_addr;
size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, p_type, p_accepted_hosts);
size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, ip_type, p_accepted_hosts);
int reuse=1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
@ -158,7 +158,7 @@ Ref<StreamPeerTCP> TCPServerWinsock::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;
};
@ -176,6 +176,7 @@ void TCPServerWinsock::stop() {
TCPServerWinsock::TCPServerWinsock() {
listen_sockfd = INVALID_SOCKET;
ip_type = IP_Address::TYPE_ANY;
};
TCPServerWinsock::~TCPServerWinsock() {

View File

@ -39,7 +39,7 @@ class TCPServerWinsock : 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();