diff options
author | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2017-01-17 09:22:56 +0100 |
---|---|---|
committer | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2017-01-23 20:15:20 +0100 |
commit | 98a7e2b4e09791705cd9dfd4d13611bc02fe47d4 (patch) | |
tree | 71b2de60c2871bec73915dfb1e1036c172fbee78 /core | |
parent | e4b9b37ccf8495be674bc15cf0bf9d76fe94e6be (diff) |
Convert validity checks of IP_Address to is_valid method.
Diffstat (limited to 'core')
-rw-r--r-- | core/io/ip.cpp | 2 | ||||
-rw-r--r-- | core/io/ip_address.cpp | 22 | ||||
-rw-r--r-- | core/io/ip_address.h | 7 | ||||
-rw-r--r-- | core/io/packet_peer_udp.cpp | 2 | ||||
-rw-r--r-- | core/io/stream_peer_tcp.cpp | 2 |
5 files changed, 28 insertions, 7 deletions
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..2d9708f5e5 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,7 @@ 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; }; bool IP_Address::is_ipv4() const{ @@ -184,6 +188,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 +199,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 +207,21 @@ 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.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 +235,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..3e86e0bcba 100644 --- a/core/io/ip_address.h +++ b/core/io/ip_address.h @@ -41,6 +41,8 @@ private: uint32_t field32[4]; }; + bool valid; + protected: void _parse_ipv6(const String& p_string); void _parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret); @@ -48,12 +50,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 +67,7 @@ public: } void clear(); + 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..4756d1699b 100644 --- a/core/io/packet_peer_udp.cpp +++ b/core/io/packet_peer_udp.cpp @@ -43,7 +43,7 @@ Error PacketPeerUDP::_set_dest_address(const String& p_address, int p_port) { ip=p_address; } else { ip=IP::get_singleton()->resolve_hostname(p_address, ip_type); - if (ip==IP_Address()) + if (!ip.is_valid()) return ERR_CANT_RESOLVE; } diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index 0a59c32995..1cf7842af7 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -37,7 +37,7 @@ Error StreamPeerTCP::_connect(const String& p_address,int p_port) { ip=p_address; } else { ip=IP::get_singleton()->resolve_hostname(p_address, ip_type); - if (ip==IP_Address()) + if (!ip.is_valid()) return ERR_CANT_RESOLVE; } |