summaryrefslogtreecommitdiff
path: root/core/io
diff options
context:
space:
mode:
authorAriel Manzur <ariel@godotengine.org>2016-10-18 18:53:18 -0300
committerAriel Manzur <ariel@godotengine.org>2016-10-18 18:53:18 -0300
commit887a897c02144f2d01896d3112bdae5ce7d6df5c (patch)
tree5557c275d7cf9d32312b66db8f1003676e318d16 /core/io
parent048bffd13a49067e646f65152c3cc6b87bacc1c3 (diff)
adding ipv6
Diffstat (limited to 'core/io')
-rw-r--r--core/io/ip.cpp22
-rw-r--r--core/io/ip.h14
-rw-r--r--core/io/ip_address.cpp172
-rw-r--r--core/io/ip_address.h38
-rw-r--r--core/io/packet_peer_udp.cpp9
-rw-r--r--core/io/packet_peer_udp.h3
-rw-r--r--core/io/tcp_server.cpp8
-rw-r--r--core/io/tcp_server.h4
8 files changed, 225 insertions, 45 deletions
diff --git a/core/io/ip.cpp b/core/io/ip.cpp
index a77aace07f..aba55687a5 100644
--- a/core/io/ip.cpp
+++ b/core/io/ip.cpp
@@ -32,6 +32,7 @@
#include "hash_map.h"
VARIANT_ENUM_CAST(IP::ResolverStatus);
+VARIANT_ENUM_CAST(IP_Address::AddrType);
/************* RESOLVER ******************/
@@ -43,10 +44,12 @@ struct _IP_ResolverPrivate {
volatile IP::ResolverStatus status;
IP_Address response;
String hostname;
+ IP_Address::AddrType type;
void clear() {
status = IP::RESOLVER_STATUS_NONE;
response = IP_Address();
+ type = IP_Address::TYPE_NONE;
hostname="";
};
@@ -78,9 +81,9 @@ struct _IP_ResolverPrivate {
if (queue[i].status!=IP::RESOLVER_STATUS_WAITING)
continue;
- queue[i].response=IP::get_singleton()->resolve_hostname(queue[i].hostname);
+ queue[i].response=IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
- if (queue[i].response.host==0)
+ if (queue[i].response.type==IP_Address::TYPE_NONE)
queue[i].status=IP::RESOLVER_STATUS_ERROR;
else
queue[i].status=IP::RESOLVER_STATUS_DONE;
@@ -109,21 +112,21 @@ struct _IP_ResolverPrivate {
-IP_Address IP::resolve_hostname(const String& p_hostname) {
+IP_Address IP::resolve_hostname(const String& p_hostname, IP_Address::AddrType p_type) {
- GLOBAL_LOCK_FUNCTION
+ GLOBAL_LOCK_FUNCTION;
if (resolver->cache.has(p_hostname))
return resolver->cache[p_hostname];
- IP_Address res = _resolve_hostname(p_hostname);
+ IP_Address res = _resolve_hostname(p_hostname, p_type);
resolver->cache[p_hostname]=res;
return res;
}
-IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname) {
+IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname, IP_Address::AddrType p_type) {
- GLOBAL_LOCK_FUNCTION
+ GLOBAL_LOCK_FUNCTION;
ResolverID id = resolver->find_empty_id();
@@ -133,6 +136,7 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname) {
}
resolver->queue[id].hostname=p_hostname;
+ resolver->queue[id].type = p_type;
if (resolver->cache.has(p_hostname)) {
resolver->queue[id].response=resolver->cache[p_hostname];
resolver->queue[id].status=IP::RESOLVER_STATUS_DONE;
@@ -145,10 +149,6 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname) {
resolver->resolve_queues();
}
-
-
-
-
return id;
}
diff --git a/core/io/ip.h b/core/io/ip.h
index 38c86e7ba3..9ffe01b1af 100644
--- a/core/io/ip.h
+++ b/core/io/ip.h
@@ -48,6 +48,14 @@ public:
RESOLVER_STATUS_ERROR,
};
+ enum AddressType {
+
+ ADDRESS_IPV4 = 1,
+ ADDRESS_IPV6 = 2,
+
+ ADDRESS_ANY = 3,
+ };
+
enum {
RESOLVER_MAX_QUERIES = 32,
RESOLVER_INVALID_ID=-1
@@ -65,7 +73,7 @@ protected:
static IP*singleton;
static void _bind_methods();
- virtual IP_Address _resolve_hostname(const String& p_hostname)=0;
+ virtual IP_Address _resolve_hostname(const String& p_hostname, IP_Address::AddrType p_type = IP_Address::TYPE_ANY)=0;
Array _get_local_addresses() const;
static IP* (*_create)();
@@ -73,9 +81,9 @@ public:
- IP_Address resolve_hostname(const String& p_hostname);
+ IP_Address resolve_hostname(const String& p_hostname, IP_Address::AddrType p_type = IP_Address::TYPE_ANY);
// async resolver hostname
- ResolverID resolve_hostname_queue_item(const String& p_hostname);
+ ResolverID resolve_hostname_queue_item(const String& p_hostname, IP_Address::AddrType p_type = IP_Address::TYPE_ANY);
ResolverStatus get_resolve_item_status(ResolverID p_id) const;
IP_Address get_resolve_item_address(ResolverID p_id) const;
virtual void get_local_addresses(List<IP_Address> *r_addresses) const=0;
diff --git a/core/io/ip_address.cpp b/core/io/ip_address.cpp
index 7a51bce7c6..2a8ab0ae00 100644
--- a/core/io/ip_address.cpp
+++ b/core/io/ip_address.cpp
@@ -32,29 +32,179 @@ IP_Address::operator Variant() const {
return operator String();
}*/
+
+#include <string.h>
+
IP_Address::operator String() const {
- return itos(field[0])+"."+itos(field[1])+"."+itos(field[2])+"."+itos(field[3]);
+ if (type == TYPE_NONE)
+ return "0.0.0.0";
+ if (type == TYPE_IPV4)
+ return itos(field8[0])+"."+itos(field8[1])+"."+itos(field8[2])+"."+itos(field8[3]);
+ else
+ return String::num_int64(field16[0], 16) +
+ ":" + String::num_int64(field16[1], 16) +
+ ":" + String::num_int64(field16[2], 16) +
+ ":" + String::num_int64(field16[3], 16) +
+ ":" + String::num_int64(field16[4], 16) +
+ ":" + String::num_int64(field16[5], 16) +
+ ":" + String::num_int64(field16[6], 16) +
+ ":" + String::num_int64(field16[7], 16);
}
-IP_Address::IP_Address(const String& p_string) {
+static uint16_t _parse_hex(const String& p_string, int p_start) {
+
+ uint16_t ret = 0;
+ for (int i=p_start; i<4; i++) {
+
+ if (i >= p_string.length()) {
+ break;
+ };
+
+ int n = 0;
+ CharType c = p_string[i];
+ if (c >= '0' && c <= '9') {
+
+ n = c - '0';
+ } else if (c >= 'a' && c <= 'f') {
+ n = 10 + (c - 'a');
+ } else if (c >= 'A' && c <= 'F') {
+ n = 10 + (c - 'A');
+ } else {
+ ERR_EXPLAIN("Invalid character in ipv6 address: " + p_string);
+ ERR_FAIL_V(0);
+ };
+ ret = ret << 4;
+ ret += n;
+ };
+
+ return ret;
+};
+
+void IP_Address::_parse_ipv6(const String& p_string) {
+
+ static const int parts_total = 8;
+ int parts[parts_total] = {0};
+ int parts_count = 0;
+ bool part_found = false;
+ bool part_skip = false;
+ bool part_ipv4 = false;
+ int parts_idx = 0;
+
+ for (int i=0; i<p_string.length(); i++) {
+
+ CharType c = p_string[i];
+ if (c == ':') {
+
+ if (i == 0) {
+ continue; // next must be a ":"
+ };
+ if (!part_found) {
+ part_skip = true;
+ parts[parts_idx++] = -1;
+ };
+ part_found = false;
+ } else if (c == '.') {
+
+ part_ipv4 = true;
+
+ } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
+ if (!part_found) {
+ parts[parts_idx++] = i;
+ part_found = true;
+ ++parts_count;
+ };
+ } else {
+
+ ERR_EXPLAIN("Invalid character in IPv6 address: " + p_string);
+ ERR_FAIL();
+ };
+ };
+
+ int parts_extra = 0;
+ if (part_skip) {
+ parts_extra = parts_total - parts_count;
+ };
+
+ int idx = 0;
+ for (int i=0; i<parts_idx; i++) {
- host=0;
- int slices = p_string.get_slice_count(".");
+ if (parts[i] == -1) {
+
+ for (int j=0; j<parts_extra; j++) {
+ field16[idx++] = 0;
+ };
+ continue;
+ };
+
+ if (part_ipv4 && i == parts_idx - 1) {
+ _parse_ipv4(p_string, parts[i], (uint8_t*)&field16[idx]); // should be the last one
+ } else {
+
+ field16[idx++] = _parse_hex(p_string, parts[i]);
+ };
+ };
+
+};
+
+void IP_Address::_parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret) {
+
+ String ip;
+ if (p_start != 0) {
+ ip = p_string.substr(p_start, p_string.length() - p_start);
+ } else {
+ ip = p_string;
+ };
+
+ int slices = ip.get_slice_count(".");
if (slices!=4) {
- ERR_EXPLAIN("Invalid IP Address String: "+p_string);
+ ERR_EXPLAIN("Invalid IP Address String: "+ip);
ERR_FAIL();
}
for(int i=0;i<4;i++) {
- field[i]=p_string.get_slicec('.',i).to_int();
+ p_ret[i]=ip.get_slicec('.',i).to_int();
}
+};
+
+void IP_Address::clear() {
+
+ memset(&field8[0], 0, sizeof(field8));
+};
+
+IP_Address::IP_Address(const String& p_string) {
+
+ clear();
+ if (p_string.find(":") >= 0) {
+
+ _parse_ipv6(p_string);
+ type = TYPE_IPV6;
+ } else {
+
+ _parse_ipv4(p_string, 0, &field8[0]);
+ type = TYPE_IPV4;
+ };
}
-IP_Address::IP_Address(uint8_t p_a,uint8_t p_b,uint8_t p_c,uint8_t p_d) {
+IP_Address::IP_Address(uint32_t p_a,uint32_t p_b,uint32_t p_c,uint32_t p_d, IP_Address::AddrType p_type) {
+
+ type = p_type;
+ memset(&field8[0], 0, sizeof(field8));
+ if (p_type == TYPE_IPV4) {
+ field8[0]=p_a;
+ field8[1]=p_b;
+ field8[2]=p_c;
+ field8[3]=p_d;
+ } else if (type == TYPE_IPV6) {
+
+ field32[0]=p_a;
+ field32[1]=p_b;
+ field32[2]=p_c;
+ field32[3]=p_d;
+ } else {
+ type = TYPE_NONE;
+ ERR_EXPLAIN("Invalid type specified for IP_Address (use TYPE_IPV4 or TYPE_IPV6");
+ ERR_FAIL();
+ };
- field[0]=p_a;
- field[1]=p_b;
- field[2]=p_c;
- field[3]=p_d;
}
diff --git a/core/io/ip_address.h b/core/io/ip_address.h
index 1292311729..fe13d70611 100644
--- a/core/io/ip_address.h
+++ b/core/io/ip_address.h
@@ -33,22 +33,48 @@
struct IP_Address {
+public:
+ enum AddrType {
+ TYPE_NONE = 0,
+ TYPE_IPV4 = 1,
+ TYPE_IPV6 = 2,
+
+ TYPE_ANY = 3,
+ };
+
+ AddrType type;
+
union {
- uint8_t field[4];
- uint32_t host;
+ uint8_t field8[16];
+ uint16_t field16[8];
+ uint32_t field32[4];
};
+protected:
+ void _parse_ipv6(const String& p_string);
+ void _parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret);
+
+public:
//operator Variant() const;
bool operator==(const IP_Address& p_ip) const {
- return host==p_ip.host;
+ 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 {
- return host!=p_ip.host;
+ for (int i=0; i<4; i++)
+ if (field32[i] != p_ip.field32[i])
+ return true;
+ return false;
}
+
+ void clear();
+
operator String() const;
IP_Address(const String& p_string);
- IP_Address(uint8_t p_a,uint8_t p_b,uint8_t p_c,uint8_t p_d);
- IP_Address() { host=0; }
+ IP_Address(uint32_t p_a,uint32_t p_b,uint32_t p_c,uint32_t p_d, AddrType p_type=TYPE_IPV4);
+ IP_Address() { clear(); type=TYPE_NONE; }
};
diff --git a/core/io/packet_peer_udp.cpp b/core/io/packet_peer_udp.cpp
index efc619e414..018dc77d91 100644
--- a/core/io/packet_peer_udp.cpp
+++ b/core/io/packet_peer_udp.cpp
@@ -29,14 +29,9 @@
#include "packet_peer_udp.h"
#include "io/ip.h"
-
PacketPeerUDP* (*PacketPeerUDP::_create)()=NULL;
-int PacketPeerUDP::_get_packet_address() const {
-
- IP_Address ip = get_packet_address();
- return ip.host;
-}
+VARIANT_ENUM_CAST(IP_Address::AddrType);
String PacketPeerUDP::_get_packet_ip() const {
@@ -65,7 +60,7 @@ void PacketPeerUDP::_bind_methods() {
ObjectTypeDB::bind_method(_MD("wait:Error"),&PacketPeerUDP::wait);
ObjectTypeDB::bind_method(_MD("is_listening"),&PacketPeerUDP::is_listening);
ObjectTypeDB::bind_method(_MD("get_packet_ip"),&PacketPeerUDP::_get_packet_ip);
- ObjectTypeDB::bind_method(_MD("get_packet_address"),&PacketPeerUDP::_get_packet_address);
+ //ObjectTypeDB::bind_method(_MD("get_packet_address"),&PacketPeerUDP::_get_packet_address);
ObjectTypeDB::bind_method(_MD("get_packet_port"),&PacketPeerUDP::get_packet_port);
ObjectTypeDB::bind_method(_MD("set_send_address","host","port"),&PacketPeerUDP::_set_send_address);
diff --git a/core/io/packet_peer_udp.h b/core/io/packet_peer_udp.h
index 70d92834fc..c0806a9e6a 100644
--- a/core/io/packet_peer_udp.h
+++ b/core/io/packet_peer_udp.h
@@ -40,14 +40,13 @@ protected:
static PacketPeerUDP* (*_create)();
static void _bind_methods();
- int _get_packet_address() const;
String _get_packet_ip() const;
virtual Error _set_send_address(const String& p_address,int p_port);
public:
- virtual Error listen(int p_port,int p_recv_buffer_size=65536)=0;
+ virtual Error listen(int p_port, IP_Address::AddrType p_address_type = IP_Address::TYPE_IPV4, 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/tcp_server.cpp b/core/io/tcp_server.cpp
index 274d20a48a..53d6e900f3 100644
--- a/core/io/tcp_server.cpp
+++ b/core/io/tcp_server.cpp
@@ -30,6 +30,8 @@
TCP_Server* (*TCP_Server::_create)()=NULL;
+VARIANT_ENUM_CAST(IP_Address::AddrType);
+
Ref<TCP_Server> TCP_Server::create_ref() {
if (!_create)
@@ -44,19 +46,19 @@ TCP_Server* TCP_Server::create() {
return _create();
}
-Error TCP_Server::_listen(uint16_t p_port,DVector<String> p_accepted_hosts) {
+Error TCP_Server::_listen(uint16_t p_port, IP_Address::AddrType p_type, DVector<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);
+ return listen(p_port,p_type, hosts.size()?&hosts:NULL);
}
void TCP_Server::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("listen","port","accepted_hosts"),&TCP_Server::_listen,DEFVAL(DVector<String>()));
+ ObjectTypeDB::bind_method(_MD("listen","port","accepted_hosts"),&TCP_Server::_listen,DEFVAL(IP_Address::TYPE_IPV4), DEFVAL(DVector<String>()));
ObjectTypeDB::bind_method(_MD("is_connection_available"),&TCP_Server::is_connection_available);
ObjectTypeDB::bind_method(_MD("take_connection"),&TCP_Server::take_connection);
ObjectTypeDB::bind_method(_MD("stop"),&TCP_Server::stop);
diff --git a/core/io/tcp_server.h b/core/io/tcp_server.h
index 512a7e640a..883a3ef2f6 100644
--- a/core/io/tcp_server.h
+++ b/core/io/tcp_server.h
@@ -41,11 +41,11 @@ protected:
static TCP_Server* (*_create)();
//bind helper
- Error _listen(uint16_t p_port,DVector<String> p_accepted_hosts=DVector<String>());
+ Error _listen(uint16_t p_port, IP_Address::AddrType p_type = IP_Address::TYPE_IPV4 ,DVector<String> p_accepted_hosts=DVector<String>());
static void _bind_methods();
public:
- virtual Error listen(uint16_t p_port,const List<String> *p_accepted_hosts=NULL)=0;
+ virtual Error listen(uint16_t p_port, IP_Address::AddrType p_type = IP_Address::TYPE_IPV4, const List<String> *p_accepted_hosts=NULL)=0;
virtual bool is_connection_available() const=0;
virtual Ref<StreamPeerTCP> take_connection()=0;