Convert validity checks of IP_Address to is_valid method.

(cherry picked from commit 98a7e2b4e0)
This commit is contained in:
Fabio Alessandrelli 2017-01-17 09:22:56 +01:00
parent aeffe74a27
commit 603105df18
9 changed files with 30 additions and 11 deletions

View file

@ -81,7 +81,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;

View file

@ -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]);
@ -170,6 +173,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 {
@ -183,6 +187,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);
}
@ -193,6 +198,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,13 +207,18 @@ IP_Address::IP_Address(const String &p_string) {
clear();
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) {
@ -221,6 +232,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;

View file

@ -40,6 +40,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);
@ -47,12 +49,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;
@ -60,6 +66,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);

View file

@ -43,7 +43,7 @@ Error PacketPeerUDP::_set_send_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;
}

View file

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

View file

@ -94,7 +94,7 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_siz
}
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();
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));
ERR_FAIL_COND_V(p_ip == IP_Address(), 0);
ERR_FAIL_COND_V(!p_ip.is_valid(), 0);
// IPv6 socket
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(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;
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);

View file

@ -297,7 +297,7 @@ void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port,
Error StreamPeerWinsock::connect(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;
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);