Convert validity checks of IP_Address to is_valid method.

This commit is contained in:
Fabio Alessandrelli 2017-01-17 09:22:56 +01:00
parent e4b9b37ccf
commit 98a7e2b4e0
9 changed files with 32 additions and 11 deletions

View File

@ -82,7 +82,7 @@ struct _IP_ResolverPrivate {
continue; continue;
queue[i].response=IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type); 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; queue[i].status=IP::RESOLVER_STATUS_ERROR;
else else
queue[i].status=IP::RESOLVER_STATUS_DONE; queue[i].status=IP::RESOLVER_STATUS_DONE;

View File

@ -38,6 +38,9 @@ IP_Address::operator Variant() const {
IP_Address::operator String() const { IP_Address::operator String() const {
if(!valid)
return "";
if(is_ipv4()) if(is_ipv4())
// IPv4 address mapped to IPv6 // IPv4 address mapped to IPv6
return itos(field8[12])+"."+itos(field8[13])+"."+itos(field8[14])+"."+itos(field8[15]); 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() { void IP_Address::clear() {
memset(&field8[0], 0, sizeof(field8)); memset(&field8[0], 0, sizeof(field8));
valid = false;
}; };
bool IP_Address::is_ipv4() const{ 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) { void IP_Address::set_ipv4(const uint8_t *p_ip) {
clear(); clear();
valid = true;
field16[5]=0xffff; field16[5]=0xffff;
field32[3]=*((const uint32_t *)p_ip); 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) { void IP_Address::set_ipv6(const uint8_t *p_buf) {
clear(); clear();
valid = true;
for (int i=0; i<16; i++) for (int i=0; i<16; i++)
field8[i] = p_buf[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) { IP_Address::IP_Address(const String& p_string) {
clear(); clear();
if (p_string.find(":") >= 0) {
if (p_string.find(":") >= 0) {
// IPv6
_parse_ipv6(p_string); _parse_ipv6(p_string);
} else { valid = true;
// Mapped to IPv6
} else if (p_string.get_slice_count(".") == 4) {
// IPv4 (mapped to IPv6 internally)
field16[5] = 0xffff; field16[5] = 0xffff;
_parse_ipv4(p_string, 0, &field8[12]); _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) { _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) { IP_Address::IP_Address(uint32_t p_a,uint32_t p_b,uint32_t p_c,uint32_t p_d, bool is_v6) {
clear(); clear();
valid = true;
if (!is_v6) { if (!is_v6) {
// Mapped to IPv6 // Mapped to IPv6
field16[5]=0xffff; field16[5]=0xffff;

View File

@ -41,6 +41,8 @@ private:
uint32_t field32[4]; uint32_t field32[4];
}; };
bool valid;
protected: protected:
void _parse_ipv6(const String& p_string); void _parse_ipv6(const String& p_string);
void _parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret); void _parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret);
@ -48,12 +50,16 @@ protected:
public: public:
//operator Variant() const; //operator Variant() const;
bool operator==(const IP_Address& p_ip) 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++) for (int i=0; i<4; i++)
if (field32[i] != p_ip.field32[i]) if (field32[i] != p_ip.field32[i])
return false; return false;
return true; return true;
} }
bool operator!=(const IP_Address& p_ip) const { 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++) for (int i=0; i<4; i++)
if (field32[i] != p_ip.field32[i]) if (field32[i] != p_ip.field32[i])
return true; return true;
@ -61,6 +67,7 @@ public:
} }
void clear(); void clear();
bool is_valid() const {return valid;}
bool is_ipv4() const; bool is_ipv4() const;
const uint8_t *get_ipv4() const; const uint8_t *get_ipv4() const;
void set_ipv4(const uint8_t *p_ip); void set_ipv4(const uint8_t *p_ip);

View File

@ -43,7 +43,7 @@ Error PacketPeerUDP::_set_dest_address(const String& p_address, int p_port) {
ip=p_address; ip=p_address;
} else { } else {
ip=IP::get_singleton()->resolve_hostname(p_address, ip_type); ip=IP::get_singleton()->resolve_hostname(p_address, ip_type);
if (ip==IP_Address()) if (!ip.is_valid())
return ERR_CANT_RESOLVE; return ERR_CANT_RESOLVE;
} }

View File

@ -37,7 +37,7 @@ Error StreamPeerTCP::_connect(const String& p_address,int p_port) {
ip=p_address; ip=p_address;
} else { } else {
ip=IP::get_singleton()->resolve_hostname(p_address, ip_type); ip=IP::get_singleton()->resolve_hostname(p_address, ip_type);
if (ip==IP_Address()) if (!ip.is_valid())
return ERR_CANT_RESOLVE; return ERR_CANT_RESOLVE;
} }

View File

@ -96,7 +96,7 @@ 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){ Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
ERR_FAIL_COND_V(peer_addr == IP_Address(), ERR_UNCONFIGURED); ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
int sock = _get_socket(); int sock = _get_socket();
ERR_FAIL_COND_V( sock == -1, FAILED ); ERR_FAIL_COND_V( sock == -1, FAILED );

View File

@ -16,7 +16,7 @@ static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p
memset(p_addr, 0, sizeof(struct sockaddr_storage)); memset(p_addr, 0, sizeof(struct sockaddr_storage));
ERR_FAIL_COND_V(p_ip==IP_Address(),0); ERR_FAIL_COND_V(!p_ip.is_valid(),0);
// IPv6 socket // IPv6 socket
if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) { if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) {

View File

@ -142,7 +142,7 @@ void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port,
Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_port) { Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_port) {
ERR_FAIL_COND_V( p_host == IP_Address(), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V( !p_host.is_valid(), ERR_INVALID_PARAMETER);
sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);

View File

@ -299,7 +299,7 @@ void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port,
Error StreamPeerWinsock::connect_to_host(const IP_Address& p_host, uint16_t p_port) { Error StreamPeerWinsock::connect_to_host(const IP_Address& p_host, uint16_t p_port) {
ERR_FAIL_COND_V( p_host == IP_Address(), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V( !p_host.is_valid(), ERR_INVALID_PARAMETER);
sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);