diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-02-12 23:31:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-12 23:31:40 +0100 |
commit | 70b9aa379d99c78f6db87344e3002808dac70bfa (patch) | |
tree | 4f83bb38a0fe8b636640cb9b4eb0cd0b82a0b9c6 /core/io | |
parent | 117a83fcb916cb02777dea73fb642216fd2e1d79 (diff) | |
parent | 5dc7c920bf1c4bb160d39e13ad6136d80badd7ae (diff) |
Merge pull request #7581 from Faless/v6_wild_bind
TCP/UDP listen bind to address and bugfixes
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/http_client.cpp | 9 | ||||
-rw-r--r-- | core/io/http_client.h | 2 | ||||
-rw-r--r-- | core/io/ip.cpp | 2 | ||||
-rw-r--r-- | core/io/ip_address.cpp | 27 | ||||
-rw-r--r-- | core/io/ip_address.h | 9 | ||||
-rw-r--r-- | core/io/packet_peer_udp.cpp | 14 | ||||
-rw-r--r-- | core/io/packet_peer_udp.h | 7 | ||||
-rw-r--r-- | core/io/stream_peer_tcp.cpp | 11 | ||||
-rw-r--r-- | core/io/stream_peer_tcp.h | 3 | ||||
-rw-r--r-- | core/io/tcp_server.cpp | 20 | ||||
-rw-r--r-- | core/io/tcp_server.h | 6 |
11 files changed, 45 insertions, 65 deletions
diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index fd06d27c1f..750c458dac 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -29,14 +29,9 @@ #include "http_client.h" #include "io/stream_peer_ssl.h" -void HTTPClient::set_ip_type(IP::Type p_type) { - ip_type = p_type; -} - Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl,bool p_verify_host){ close(); - tcp_connection->set_ip_type(ip_type); conn_port=p_port; conn_host=p_host; @@ -66,7 +61,7 @@ Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl,b status=STATUS_CONNECTING; } else { //is hostname - resolving=IP::get_singleton()->resolve_hostname_queue_item(conn_host, ip_type); + resolving=IP::get_singleton()->resolve_hostname_queue_item(conn_host); status=STATUS_RESOLVING; } @@ -641,7 +636,6 @@ Error HTTPClient::_get_http_data(uint8_t* p_buffer, int p_bytes,int &r_received) void HTTPClient::_bind_methods() { - ClassDB::bind_method(_MD("set_ip_type","ip_type"),&HTTPClient::set_ip_type); ClassDB::bind_method(_MD("connect_to_host:Error","host","port","use_ssl","verify_host"),&HTTPClient::connect_to_host,DEFVAL(false),DEFVAL(true)); ClassDB::bind_method(_MD("set_connection","connection:StreamPeer"),&HTTPClient::set_connection); ClassDB::bind_method(_MD("get_connection:StreamPeer"),&HTTPClient::get_connection); @@ -768,7 +762,6 @@ String HTTPClient::query_string_from_dict(const Dictionary& p_dict) { HTTPClient::HTTPClient(){ - ip_type = IP::TYPE_ANY; tcp_connection = StreamPeerTCP::create_ref(); resolving = IP::RESOLVER_INVALID_ID; status=STATUS_DISCONNECTED; diff --git a/core/io/http_client.h b/core/io/http_client.h index 496d22530b..4b0c1b730f 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -132,7 +132,6 @@ public: private: - IP::Type ip_type; Status status; IP::ResolverID resolving; int conn_port; @@ -165,7 +164,6 @@ private: public: - void set_ip_type(IP::Type p_type); //Error connect_and_get(const String& p_url,bool p_verify_host=true); //connects to a full url and perform request Error connect_to_host(const String &p_host,int p_port,bool p_ssl=false,bool p_verify_host=true); diff --git a/core/io/ip.cpp b/core/io/ip.cpp index 0eb1f221c9..13e5494dd4 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -82,7 +82,7 @@ struct _IP_ResolverPrivate { continue; queue[i].response=IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type); - if (queue[i].response==IP_Address()) + if (!queue[i].response.is_valid()) queue[i].status=IP::RESOLVER_STATUS_ERROR; else queue[i].status=IP::RESOLVER_STATUS_DONE; diff --git a/core/io/ip_address.cpp b/core/io/ip_address.cpp index 1fda7fed7b..69c7df619d 100644 --- a/core/io/ip_address.cpp +++ b/core/io/ip_address.cpp @@ -38,6 +38,9 @@ IP_Address::operator Variant() const { IP_Address::operator String() const { + if(!valid) + return ""; + if(is_ipv4()) // IPv4 address mapped to IPv6 return itos(field8[12])+"."+itos(field8[13])+"."+itos(field8[14])+"."+itos(field8[15]); @@ -171,6 +174,8 @@ void IP_Address::_parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret void IP_Address::clear() { memset(&field8[0], 0, sizeof(field8)); + valid = false; + wildcard = false; }; bool IP_Address::is_ipv4() const{ @@ -184,6 +189,7 @@ const uint8_t *IP_Address::get_ipv4() const{ void IP_Address::set_ipv4(const uint8_t *p_ip) { clear(); + valid = true; field16[5]=0xffff; field32[3]=*((const uint32_t *)p_ip); } @@ -194,6 +200,7 @@ const uint8_t *IP_Address::get_ipv6() const{ void IP_Address::set_ipv6(const uint8_t *p_buf) { clear(); + valid = true; for (int i=0; i<16; i++) field8[i] = p_buf[i]; } @@ -201,14 +208,25 @@ void IP_Address::set_ipv6(const uint8_t *p_buf) { IP_Address::IP_Address(const String& p_string) { clear(); - if (p_string.find(":") >= 0) { + if (p_string == "*") { + // Wildcard (not a vaild IP) + wildcard = true; + + } else if (p_string.find(":") >= 0) { + // IPv6 _parse_ipv6(p_string); - } else { - // Mapped to IPv6 + valid = true; + + } else if (p_string.get_slice_count(".") == 4) { + // IPv4 (mapped to IPv6 internally) field16[5] = 0xffff; _parse_ipv4(p_string, 0, &field8[12]); - }; + valid = true; + + } else { + ERR_PRINT("Invalid IP address"); + } } _FORCE_INLINE_ static void _32_to_buf(uint8_t* p_dst, uint32_t p_n) { @@ -222,6 +240,7 @@ _FORCE_INLINE_ static void _32_to_buf(uint8_t* p_dst, uint32_t p_n) { IP_Address::IP_Address(uint32_t p_a,uint32_t p_b,uint32_t p_c,uint32_t p_d, bool is_v6) { clear(); + valid = true; if (!is_v6) { // Mapped to IPv6 field16[5]=0xffff; diff --git a/core/io/ip_address.h b/core/io/ip_address.h index 87f32b0ac2..257836601a 100644 --- a/core/io/ip_address.h +++ b/core/io/ip_address.h @@ -41,6 +41,9 @@ private: uint32_t field32[4]; }; + bool valid; + bool wildcard; + protected: void _parse_ipv6(const String& p_string); void _parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret); @@ -48,12 +51,16 @@ protected: public: //operator Variant() const; bool operator==(const IP_Address& p_ip) const { + if (p_ip.valid != valid) return false; + if (!valid) return false; for (int i=0; i<4; i++) if (field32[i] != p_ip.field32[i]) return false; return true; } bool operator!=(const IP_Address& p_ip) const { + if (p_ip.valid != valid) return true; + if (!valid) return true; for (int i=0; i<4; i++) if (field32[i] != p_ip.field32[i]) return true; @@ -61,6 +68,8 @@ public: } void clear(); + bool is_wildcard() const {return wildcard;} + bool is_valid() const {return valid;} bool is_ipv4() const; const uint8_t *get_ipv4() const; void set_ipv4(const uint8_t *p_ip); diff --git a/core/io/packet_peer_udp.cpp b/core/io/packet_peer_udp.cpp index 9fec807bfb..769a513bb3 100644 --- a/core/io/packet_peer_udp.cpp +++ b/core/io/packet_peer_udp.cpp @@ -42,8 +42,8 @@ Error PacketPeerUDP::_set_dest_address(const String& p_address, int p_port) { if (p_address.is_valid_ip_address()) { ip=p_address; } else { - ip=IP::get_singleton()->resolve_hostname(p_address, ip_type); - if (ip==IP_Address()) + ip=IP::get_singleton()->resolve_hostname(p_address); + if (!ip.is_valid()) return ERR_CANT_RESOLVE; } @@ -51,15 +51,9 @@ Error PacketPeerUDP::_set_dest_address(const String& p_address, int p_port) { return OK; } -void PacketPeerUDP::set_ip_type(IP::Type p_type) { - close(); - ip_type = p_type; -} - void PacketPeerUDP::_bind_methods() { - ClassDB::bind_method(_MD("set_ip_type","ip_type"),&PacketPeerUDP::set_ip_type); - ClassDB::bind_method(_MD("listen:Error","port", "recv_buf_size"),&PacketPeerUDP::listen,DEFVAL(65536)); + ClassDB::bind_method(_MD("listen:Error","port", "bind_address", "recv_buf_size"),&PacketPeerUDP::listen,DEFVAL("*"),DEFVAL(65536)); ClassDB::bind_method(_MD("close"),&PacketPeerUDP::close); ClassDB::bind_method(_MD("wait:Error"),&PacketPeerUDP::wait); ClassDB::bind_method(_MD("is_listening"),&PacketPeerUDP::is_listening); @@ -87,5 +81,5 @@ PacketPeerUDP* PacketPeerUDP::create() { PacketPeerUDP::PacketPeerUDP() { - ip_type = IP::TYPE_ANY; + } diff --git a/core/io/packet_peer_udp.h b/core/io/packet_peer_udp.h index 17952b4ac1..726406887c 100644 --- a/core/io/packet_peer_udp.h +++ b/core/io/packet_peer_udp.h @@ -38,19 +38,16 @@ class PacketPeerUDP : public PacketPeer { protected: - IP::Type ip_type; - static PacketPeerUDP* (*_create)(); static void _bind_methods(); String _get_packet_ip() const; - virtual Error _set_dest_address(const String& p_address,int p_port); + Error _set_dest_address(const String& p_address,int p_port); public: - virtual void set_ip_type(IP::Type p_type); - virtual Error listen(int p_port, int p_recv_buffer_size=65536)=0; + virtual Error listen(int p_port, IP_Address p_bind_address=IP_Address("*"), int p_recv_buffer_size=65536)=0; virtual void close()=0; virtual Error wait()=0; virtual bool is_listening() const=0; diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index 0a59c32995..96594ef65a 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -36,8 +36,8 @@ Error StreamPeerTCP::_connect(const String& p_address,int p_port) { if (p_address.is_valid_ip_address()) { ip=p_address; } else { - ip=IP::get_singleton()->resolve_hostname(p_address, ip_type); - if (ip==IP_Address()) + ip=IP::get_singleton()->resolve_hostname(p_address); + if (!ip.is_valid()) return ERR_CANT_RESOLVE; } @@ -45,14 +45,8 @@ Error StreamPeerTCP::_connect(const String& p_address,int p_port) { return OK; } -void StreamPeerTCP::set_ip_type(IP::Type p_type) { - disconnect_from_host(); - ip_type = p_type; -} - void StreamPeerTCP::_bind_methods() { - ClassDB::bind_method(_MD("set_ip_type","ip_type"),&StreamPeerTCP::set_ip_type); ClassDB::bind_method(_MD("connect_to_host","host","port"),&StreamPeerTCP::_connect); ClassDB::bind_method(_MD("is_connected_to_host"),&StreamPeerTCP::is_connected_to_host); ClassDB::bind_method(_MD("get_status"),&StreamPeerTCP::get_status); @@ -83,7 +77,6 @@ StreamPeerTCP* StreamPeerTCP::create() { StreamPeerTCP::StreamPeerTCP() { - ip_type = IP::TYPE_ANY; } StreamPeerTCP::~StreamPeerTCP() { diff --git a/core/io/stream_peer_tcp.h b/core/io/stream_peer_tcp.h index 2b25f31739..4401378743 100644 --- a/core/io/stream_peer_tcp.h +++ b/core/io/stream_peer_tcp.h @@ -51,15 +51,12 @@ public: protected: - IP::Type ip_type; - virtual Error _connect(const String& p_address, int p_port); static StreamPeerTCP* (*_create)(); static void _bind_methods(); public: - virtual void set_ip_type(IP::Type p_type); virtual Error connect_to_host(const IP_Address& p_host, uint16_t p_port)=0; //read/write from streampeer diff --git a/core/io/tcp_server.cpp b/core/io/tcp_server.cpp index bfa5dce58f..cb82b3fc55 100644 --- a/core/io/tcp_server.cpp +++ b/core/io/tcp_server.cpp @@ -44,25 +44,9 @@ TCP_Server* TCP_Server::create() { return _create(); } -Error TCP_Server::_listen(uint16_t p_port, PoolVector<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, hosts.size()?&hosts:NULL); - -} - -void TCP_Server::set_ip_type(IP::Type p_type) { - stop(); - ip_type = p_type; -} - void TCP_Server::_bind_methods() { - ClassDB::bind_method(_MD("set_ip_type","ip_type"),&TCP_Server::set_ip_type); - ClassDB::bind_method(_MD("listen","port","accepted_hosts"),&TCP_Server::_listen,DEFVAL(PoolVector<String>())); + ClassDB::bind_method(_MD("listen","port","bind_address"),&TCP_Server::listen,DEFVAL("*")); ClassDB::bind_method(_MD("is_connection_available"),&TCP_Server::is_connection_available); ClassDB::bind_method(_MD("take_connection"),&TCP_Server::take_connection); ClassDB::bind_method(_MD("stop"),&TCP_Server::stop); @@ -72,5 +56,5 @@ void TCP_Server::_bind_methods() { TCP_Server::TCP_Server() { - ip_type = IP::TYPE_ANY; + } diff --git a/core/io/tcp_server.h b/core/io/tcp_server.h index 3d7b3ddd8d..3cbd8c58cf 100644 --- a/core/io/tcp_server.h +++ b/core/io/tcp_server.h @@ -38,17 +38,13 @@ class TCP_Server : public Reference { GDCLASS( TCP_Server, Reference ); protected: - IP::Type ip_type; - static TCP_Server* (*_create)(); //bind helper - Error _listen(uint16_t p_port, PoolVector<String> p_accepted_hosts=PoolVector<String>()); static void _bind_methods(); public: - virtual void set_ip_type(IP::Type p_type); - virtual Error listen(uint16_t p_port, const List<String> *p_accepted_hosts=NULL)=0; + virtual Error listen(uint16_t p_port, const IP_Address p_bind_address=IP_Address("*"))=0; virtual bool is_connection_available() const=0; virtual Ref<StreamPeerTCP> take_connection()=0; |