1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-24 15:26:15 +00:00

IP_Address now handle IPv4 and IPv6 transparently

IP_Address changes:
- Converts to and from String transparently while handling IPv4 as IPv6
  mapped (::ffff:[IP]) address internally.
- Completely remove AddrType enum.
- Setting/Getting of ip array is now only possible through dedicated functions
  (ie. set_ipv4, get_ipv4, set_ipv6, get_ipv6)
- Add function to know if the address is a valid IPv4 (for IP implementation and enet)

(cherry picked from commit 1aff508dd9)
This commit is contained in:
Fabio Alessandrelli
2016-12-05 16:32:38 +01:00
parent 7ef71b9013
commit 00fdcf3cd0
10 changed files with 107 additions and 95 deletions

View File

@@ -76,12 +76,14 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size
uint32_t size;
uint8_t type;
rb.read(&type, 1, true);
if (type == IP_Address::TYPE_IPV4) {
rb.read((uint8_t*)&packet_ip.field8,4,true);
packet_ip.type = IP_Address::TYPE_IPV4;
if (type == IP::TYPE_IPV4) {
uint8_t ip[4];
rb.read(ip,4,true);
packet_ip.set_ipv4(ip);
} else {
rb.read((uint8_t*)&packet_ip.field8,16,true);
packet_ip.type = IP_Address::TYPE_IPV6;
uint8_t ipv6[16];
rb.read(ipv6,16,true);
packet_ip.set_ipv6(ipv6);
};
rb.read((uint8_t*)&packet_port,4,true);
rb.read((uint8_t*)&size,4,true);
@@ -94,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){
ERR_FAIL_COND_V(peer_addr.type == IP_Address::TYPE_NONE, ERR_UNCONFIGURED);
ERR_FAIL_COND_V(peer_addr == IP_Address(), ERR_UNCONFIGURED);
int sock = _get_socket();
ERR_FAIL_COND_V( sock == -1, FAILED );
@@ -163,7 +165,7 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) {
uint32_t port = 0;
if (from.ss_family == AF_INET) {
uint8_t type = (uint8_t)IP_Address::TYPE_IPV4;
uint8_t type = (uint8_t)IP::TYPE_IPV4;
rb.write(&type, 1);
struct sockaddr_in* sin_from = (struct sockaddr_in*)&from;
rb.write((uint8_t*)&sin_from->sin_addr, 4);
@@ -171,7 +173,7 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) {
} else if (from.ss_family == AF_INET6) {
uint8_t type = (uint8_t)IP_Address::TYPE_IPV6;
uint8_t type = (uint8_t)IP::TYPE_IPV6;
rb.write(&type, 1);
struct sockaddr_in6* s6_from = (struct sockaddr_in6*)&from;
@@ -181,7 +183,7 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) {
} else {
// WARN_PRINT("Ignoring packet with unknown address family");
uint8_t type = (uint8_t)IP_Address::TYPE_NONE;
uint8_t type = (uint8_t)IP::TYPE_NONE;
rb.write(&type, 1);
};