summaryrefslogtreecommitdiff
path: root/core/io
diff options
context:
space:
mode:
Diffstat (limited to 'core/io')
-rw-r--r--core/io/http_client.cpp7
-rw-r--r--core/io/http_client.h2
-rw-r--r--core/io/ip.cpp37
-rw-r--r--core/io/ip.h16
-rw-r--r--core/io/ip_address.cpp186
-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
10 files changed, 258 insertions, 52 deletions
diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp
index 2a831dd992..e3289b452c 100644
--- a/core/io/http_client.cpp
+++ b/core/io/http_client.cpp
@@ -29,8 +29,9 @@
#include "http_client.h"
#include "io/stream_peer_ssl.h"
+VARIANT_ENUM_CAST(IP_Address::AddrType);
-Error HTTPClient::connect(const String &p_host, int p_port, bool p_ssl,bool p_verify_host){
+Error HTTPClient::connect(const String &p_host, int p_port, bool p_ssl,bool p_verify_host, IP_Address::AddrType p_addr_type){
close();
conn_port=p_port;
@@ -62,7 +63,7 @@ Error HTTPClient::connect(const String &p_host, int p_port, bool p_ssl,bool p_ve
status=STATUS_CONNECTING;
} else {
//is hostname
- resolving=IP::get_singleton()->resolve_hostname_queue_item(conn_host);
+ resolving=IP::get_singleton()->resolve_hostname_queue_item(conn_host, p_addr_type);
status=STATUS_RESOLVING;
}
@@ -635,7 +636,7 @@ Error HTTPClient::_get_http_data(uint8_t* p_buffer, int p_bytes,int &r_received)
void HTTPClient::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("connect:Error","host","port","use_ssl","verify_host"),&HTTPClient::connect,DEFVAL(false),DEFVAL(true));
+ ObjectTypeDB::bind_method(_MD("connect:Error","host","port","use_ssl","verify_host"),&HTTPClient::connect,DEFVAL(false),DEFVAL(true),DEFVAL(IP_Address::TYPE_ANY));
ObjectTypeDB::bind_method(_MD("set_connection","connection:StreamPeer"),&HTTPClient::set_connection);
ObjectTypeDB::bind_method(_MD("get_connection:StreamPeer"),&HTTPClient::get_connection);
ObjectTypeDB::bind_method(_MD("request_raw","method","url","headers","body"),&HTTPClient::request_raw);
diff --git a/core/io/http_client.h b/core/io/http_client.h
index 32d2e72101..ba464c34c7 100644
--- a/core/io/http_client.h
+++ b/core/io/http_client.h
@@ -165,7 +165,7 @@ public:
//Error connect_and_get(const String& p_url,bool p_verify_host=true); //connects to a full url and perform request
- Error connect(const String &p_host,int p_port,bool p_ssl=false,bool p_verify_host=true);
+ Error connect(const String &p_host,int p_port,bool p_ssl=false,bool p_verify_host=true, IP_Address::AddrType p_addr_type = IP_Address::TYPE_ANY);
void set_connection(const Ref<StreamPeer>& p_connection);
Ref<StreamPeer> get_connection() const;
diff --git a/core/io/ip.cpp b/core/io/ip.cpp
index a77aace07f..4ee1b281c4 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,23 @@ 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];
+ if (resolver->cache[p_hostname].type & p_type != 0)
+ return resolver->cache[p_hostname];
+ // requested type is different from type in cache. continue resolution, if successful it'll overwrite cache
- 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,7 +138,8 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname) {
}
resolver->queue[id].hostname=p_hostname;
- if (resolver->cache.has(p_hostname)) {
+ resolver->queue[id].type = p_type;
+ if (resolver->cache.has(p_hostname) && (resolver->cache[p_hostname].type & p_type) != 0) {
resolver->queue[id].response=resolver->cache[p_hostname];
resolver->queue[id].status=IP::RESOLVER_STATUS_DONE;
} else {
@@ -145,10 +151,6 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname) {
resolver->resolve_queues();
}
-
-
-
-
return id;
}
@@ -187,6 +189,14 @@ void IP::erase_resolve_item(ResolverID p_id) {
}
+void IP::clear_cache(const String &p_hostname) {
+
+ if (p_hostname.empty()) {
+ resolver->cache.clear();
+ } else {
+ resolver->cache.erase(p_hostname);
+ }
+};
Array IP::_get_local_addresses() const {
@@ -208,6 +218,7 @@ void IP::_bind_methods() {
ObjectTypeDB::bind_method(_MD("get_resolve_item_address","id"),&IP::get_resolve_item_address);
ObjectTypeDB::bind_method(_MD("erase_resolve_item","id"),&IP::erase_resolve_item);
ObjectTypeDB::bind_method(_MD("get_local_addresses"),&IP::_get_local_addresses);
+ ObjectTypeDB::bind_method(_MD("clear_cache"),&IP::clear_cache, DEFVAL(""));
BIND_CONSTANT( RESOLVER_STATUS_NONE );
BIND_CONSTANT( RESOLVER_STATUS_WAITING );
diff --git a/core/io/ip.h b/core/io/ip.h
index 38c86e7ba3..742dd0e740 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,14 +81,16 @@ 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;
void erase_resolve_item(ResolverID p_id);
+ void clear_cache(const String& p_hostname = "");
+
static IP* get_singleton();
static IP* create();
diff --git a/core/io/ip_address.cpp b/core/io/ip_address.cpp
index 7a51bce7c6..9887cd132b 100644
--- a/core/io/ip_address.cpp
+++ b/core/io/ip_address.cpp
@@ -32,29 +32,191 @@ IP_Address::operator Variant() const {
return operator String();
}*/
+
+#include <string.h>
+#include <stdio.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 {
+ String ret;
+ for (int i=0; i<8; i++) {
+ if (i > 0)
+ ret = ret + ":";
+ uint16_t num = (field8[i*2] << 8) + field8[i*2+1];
+ ret = ret + String::num_int64(num, 16);
+ };
+
+ return ret;
+ };
}
-IP_Address::IP_Address(const String& p_string) {
+static void _parse_hex(const String& p_string, int p_start, uint8_t* p_dst) {
+
+ uint16_t ret = 0;
+ for (int i=p_start; i<p_start + 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 if (c == ':') {
+ break;
+ } else {
+ ERR_EXPLAIN("Invalid character in ipv6 address: " + p_string);
+ ERR_FAIL();
+ };
+ ret = ret << 4;
+ ret += n;
+ };
+
+ p_dst[0] = ret >> 8;
+ p_dst[1] = ret & 0xff;
+};
+
+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 {
+ _parse_hex(p_string, parts[i], (uint8_t*)&(field16[idx++]));
+ };
+ };
+
+};
+
+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) {
+_FORCE_INLINE_ static void _32_to_buf(uint8_t* p_dst, uint32_t p_n) {
+
+ p_dst[0] = (p_n >> 24) & 0xff;
+ p_dst[1] = (p_n >> 16) & 0xff;
+ p_dst[2] = (p_n >> 8) & 0xff;
+ p_dst[3] = (p_n >> 0) & 0xff;
+};
+
+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) {
+
+ _32_to_buf(&field8[0], p_a);
+ _32_to_buf(&field8[4], p_b);
+ _32_to_buf(&field8[8], p_c);
+ _32_to_buf(&field8[12], 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;