2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* ip.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "ip.h"
|
|
|
|
#include "hash_map.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "os/semaphore.h"
|
|
|
|
#include "os/thread.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-12-15 18:42:58 +00:00
|
|
|
VARIANT_ENUM_CAST(IP::ResolverStatus);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
/************* RESOLVER ******************/
|
|
|
|
|
|
|
|
struct _IP_ResolverPrivate {
|
|
|
|
|
|
|
|
struct QueueItem {
|
|
|
|
|
|
|
|
volatile IP::ResolverStatus status;
|
|
|
|
IP_Address response;
|
|
|
|
String hostname;
|
2016-12-01 05:34:05 +00:00
|
|
|
IP::Type type;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void clear() {
|
|
|
|
status = IP::RESOLVER_STATUS_NONE;
|
|
|
|
response = IP_Address();
|
2016-12-01 05:34:05 +00:00
|
|
|
type = IP::TYPE_NONE;
|
2017-03-05 15:44:50 +00:00
|
|
|
hostname = "";
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
QueueItem() {
|
|
|
|
clear();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
QueueItem queue[IP::RESOLVER_MAX_QUERIES];
|
|
|
|
|
|
|
|
IP::ResolverID find_empty_id() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) {
|
|
|
|
if (queue[i].status == IP::RESOLVER_STATUS_NONE)
|
2014-02-10 01:10:30 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return IP::RESOLVER_INVALID_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
Semaphore *sem;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Thread *thread;
|
2014-02-10 01:10:30 +00:00
|
|
|
//Semaphore* semaphore;
|
|
|
|
bool thread_abort;
|
|
|
|
|
|
|
|
void resolve_queues() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (queue[i].status != IP::RESOLVER_STATUS_WAITING)
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2017-03-05 15:44:50 +00:00
|
|
|
queue[i].response = IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-17 08:22:56 +00:00
|
|
|
if (!queue[i].response.is_valid())
|
2017-03-05 15:44:50 +00:00
|
|
|
queue[i].status = IP::RESOLVER_STATUS_ERROR;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
queue[i].status = IP::RESOLVER_STATUS_DONE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _thread_function(void *self) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (!ipr->thread_abort) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ipr->sem->wait();
|
|
|
|
GLOBAL_LOCK_FUNCTION;
|
|
|
|
ipr->resolve_queues();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap<String, IP_Address> cache;
|
|
|
|
|
2016-12-08 11:18:18 +00:00
|
|
|
static String get_cache_key(String p_hostname, IP::Type p_type) {
|
|
|
|
return itos(p_type) + p_hostname;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-10-18 21:53:18 +00:00
|
|
|
GLOBAL_LOCK_FUNCTION;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-12-08 11:18:18 +00:00
|
|
|
String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
|
|
|
|
if (resolver->cache.has(key))
|
|
|
|
return resolver->cache[key];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-10-18 21:53:18 +00:00
|
|
|
IP_Address res = _resolve_hostname(p_hostname, p_type);
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->cache[key] = res;
|
2014-02-10 01:10:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-10-18 21:53:18 +00:00
|
|
|
GLOBAL_LOCK_FUNCTION;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ResolverID id = resolver->find_empty_id();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (id == RESOLVER_INVALID_ID) {
|
2014-02-10 01:10:30 +00:00
|
|
|
WARN_PRINT("Out of resolver queries");
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2016-12-08 11:18:18 +00:00
|
|
|
String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->queue[id].hostname = p_hostname;
|
2016-10-18 21:53:18 +00:00
|
|
|
resolver->queue[id].type = p_type;
|
2016-12-08 11:18:18 +00:00
|
|
|
if (resolver->cache.has(key)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->queue[id].response = resolver->cache[key];
|
|
|
|
resolver->queue[id].status = IP::RESOLVER_STATUS_DONE;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->queue[id].response = IP_Address();
|
|
|
|
resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (resolver->thread)
|
|
|
|
resolver->sem->post();
|
|
|
|
else
|
|
|
|
resolver->resolve_queues();
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
GLOBAL_LOCK_FUNCTION;
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(resolver->queue[p_id].status == IP::RESOLVER_STATUS_NONE, IP::RESOLVER_STATUS_NONE);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return resolver->queue[p_id].status;
|
|
|
|
}
|
|
|
|
IP_Address IP::get_resolve_item_address(ResolverID p_id) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
GLOBAL_LOCK_FUNCTION;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE) {
|
|
|
|
ERR_EXPLAIN("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
|
|
|
|
ERR_FAIL_COND_V(resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE, IP_Address());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resolver->queue[p_id].response;
|
|
|
|
}
|
|
|
|
void IP::erase_resolve_item(ResolverID p_id) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
GLOBAL_LOCK_FUNCTION;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->queue[p_id].status = IP::RESOLVER_STATUS_NONE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 10:04:10 +00:00
|
|
|
void IP::clear_cache(const String &p_hostname) {
|
|
|
|
|
|
|
|
if (p_hostname.empty()) {
|
|
|
|
resolver->cache.clear();
|
|
|
|
} else {
|
2016-12-08 11:18:18 +00:00
|
|
|
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
|
|
|
|
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV4));
|
|
|
|
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6));
|
|
|
|
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY));
|
2016-10-20 10:04:10 +00:00
|
|
|
}
|
|
|
|
};
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-05-24 04:35:47 +00:00
|
|
|
Array IP::_get_local_addresses() const {
|
|
|
|
|
|
|
|
Array addresses;
|
|
|
|
List<IP_Address> ip_addresses;
|
|
|
|
get_local_addresses(&ip_addresses);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<IP_Address>::Element *E = ip_addresses.front(); E; E = E->next()) {
|
2014-05-24 04:35:47 +00:00
|
|
|
addresses.push_back(E->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void IP::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY));
|
|
|
|
ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address);
|
|
|
|
ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_cache"), &IP::clear_cache, DEFVAL(""));
|
|
|
|
|
|
|
|
BIND_CONSTANT(RESOLVER_STATUS_NONE);
|
|
|
|
BIND_CONSTANT(RESOLVER_STATUS_WAITING);
|
|
|
|
BIND_CONSTANT(RESOLVER_STATUS_DONE);
|
|
|
|
BIND_CONSTANT(RESOLVER_STATUS_ERROR);
|
|
|
|
|
|
|
|
BIND_CONSTANT(RESOLVER_MAX_QUERIES);
|
|
|
|
BIND_CONSTANT(RESOLVER_INVALID_ID);
|
|
|
|
|
|
|
|
BIND_CONSTANT(TYPE_NONE);
|
|
|
|
BIND_CONSTANT(TYPE_IPV4);
|
|
|
|
BIND_CONSTANT(TYPE_IPV6);
|
|
|
|
BIND_CONSTANT(TYPE_ANY);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
IP *IP::singleton = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
IP *IP::get_singleton() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
IP *(*IP::_create)() = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
IP *IP::create() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(singleton, NULL);
|
|
|
|
ERR_FAIL_COND_V(!_create, NULL);
|
2014-02-10 01:10:30 +00:00
|
|
|
return _create();
|
|
|
|
}
|
|
|
|
|
|
|
|
IP::IP() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
singleton = this;
|
|
|
|
resolver = memnew(_IP_ResolverPrivate);
|
|
|
|
resolver->sem = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#ifndef NO_THREADS
|
|
|
|
|
|
|
|
//resolver->sem = Semaphore::create();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->sem = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (resolver->sem) {
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->thread_abort = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!resolver->thread)
|
|
|
|
memdelete(resolver->sem); //wtf
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->thread = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
resolver->sem = NULL;
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->thread = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
IP::~IP() {
|
|
|
|
|
|
|
|
#ifndef NO_THREADS
|
|
|
|
if (resolver->thread) {
|
2017-03-05 15:44:50 +00:00
|
|
|
resolver->thread_abort = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
resolver->sem->post();
|
|
|
|
Thread::wait_to_finish(resolver->thread);
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete(resolver->thread);
|
|
|
|
memdelete(resolver->sem);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
memdelete(resolver);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|