ip.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*************************************************************************/
  2. /* ip.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "ip.h"
  31. #include "core/hash_map.h"
  32. #include "core/os/semaphore.h"
  33. #include "core/os/thread.h"
  34. VARIANT_ENUM_CAST(IP::ResolverStatus);
  35. /************* RESOLVER ******************/
  36. struct _IP_ResolverPrivate {
  37. struct QueueItem {
  38. volatile IP::ResolverStatus status;
  39. IP_Address response;
  40. String hostname;
  41. IP::Type type;
  42. void clear() {
  43. status = IP::RESOLVER_STATUS_NONE;
  44. response = IP_Address();
  45. type = IP::TYPE_NONE;
  46. hostname = "";
  47. };
  48. QueueItem() {
  49. clear();
  50. };
  51. };
  52. QueueItem queue[IP::RESOLVER_MAX_QUERIES];
  53. IP::ResolverID find_empty_id() const {
  54. for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) {
  55. if (queue[i].status == IP::RESOLVER_STATUS_NONE)
  56. return i;
  57. }
  58. return IP::RESOLVER_INVALID_ID;
  59. }
  60. Mutex mutex;
  61. Semaphore sem;
  62. Thread *thread;
  63. //Semaphore* semaphore;
  64. bool thread_abort;
  65. void resolve_queues() {
  66. for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) {
  67. if (queue[i].status != IP::RESOLVER_STATUS_WAITING)
  68. continue;
  69. queue[i].response = IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
  70. if (!queue[i].response.is_valid())
  71. queue[i].status = IP::RESOLVER_STATUS_ERROR;
  72. else
  73. queue[i].status = IP::RESOLVER_STATUS_DONE;
  74. }
  75. }
  76. static void _thread_function(void *self) {
  77. _IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self;
  78. while (!ipr->thread_abort) {
  79. ipr->sem.wait();
  80. MutexLock lock(ipr->mutex);
  81. ipr->resolve_queues();
  82. }
  83. }
  84. HashMap<String, IP_Address> cache;
  85. static String get_cache_key(String p_hostname, IP::Type p_type) {
  86. return itos(p_type) + p_hostname;
  87. }
  88. };
  89. IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
  90. MutexLock lock(resolver->mutex);
  91. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  92. if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
  93. IP_Address res = resolver->cache[key];
  94. return res;
  95. }
  96. IP_Address res = _resolve_hostname(p_hostname, p_type);
  97. resolver->cache[key] = res;
  98. return res;
  99. }
  100. IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
  101. MutexLock lock(resolver->mutex);
  102. ResolverID id = resolver->find_empty_id();
  103. if (id == RESOLVER_INVALID_ID) {
  104. WARN_PRINT("Out of resolver queries");
  105. return id;
  106. }
  107. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  108. resolver->queue[id].hostname = p_hostname;
  109. resolver->queue[id].type = p_type;
  110. if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
  111. resolver->queue[id].response = resolver->cache[key];
  112. resolver->queue[id].status = IP::RESOLVER_STATUS_DONE;
  113. } else {
  114. resolver->queue[id].response = IP_Address();
  115. resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING;
  116. if (resolver->thread)
  117. resolver->sem.post();
  118. else
  119. resolver->resolve_queues();
  120. }
  121. return id;
  122. }
  123. IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
  124. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE);
  125. MutexLock lock(resolver->mutex);
  126. if (resolver->queue[p_id].status == IP::RESOLVER_STATUS_NONE) {
  127. ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE");
  128. resolver->mutex.unlock();
  129. return IP::RESOLVER_STATUS_NONE;
  130. }
  131. return resolver->queue[p_id].status;
  132. }
  133. IP_Address IP::get_resolve_item_address(ResolverID p_id) const {
  134. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address());
  135. MutexLock lock(resolver->mutex);
  136. if (resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE) {
  137. ERR_PRINT("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
  138. resolver->mutex.unlock();
  139. return IP_Address();
  140. }
  141. return resolver->queue[p_id].response;
  142. }
  143. void IP::erase_resolve_item(ResolverID p_id) {
  144. ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
  145. MutexLock lock(resolver->mutex);
  146. resolver->queue[p_id].status = IP::RESOLVER_STATUS_NONE;
  147. }
  148. void IP::clear_cache(const String &p_hostname) {
  149. MutexLock lock(resolver->mutex);
  150. if (p_hostname.empty()) {
  151. resolver->cache.clear();
  152. } else {
  153. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
  154. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV4));
  155. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6));
  156. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY));
  157. }
  158. }
  159. Array IP::_get_local_addresses() const {
  160. Array addresses;
  161. List<IP_Address> ip_addresses;
  162. get_local_addresses(&ip_addresses);
  163. for (List<IP_Address>::Element *E = ip_addresses.front(); E; E = E->next()) {
  164. addresses.push_back(E->get());
  165. }
  166. return addresses;
  167. }
  168. Array IP::_get_local_interfaces() const {
  169. Array results;
  170. Map<String, Interface_Info> interfaces;
  171. get_local_interfaces(&interfaces);
  172. for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
  173. Interface_Info &c = E->get();
  174. Dictionary rc;
  175. rc["name"] = c.name;
  176. rc["friendly"] = c.name_friendly;
  177. rc["index"] = c.index;
  178. Array ips;
  179. for (const List<IP_Address>::Element *F = c.ip_addresses.front(); F; F = F->next()) {
  180. ips.push_front(F->get());
  181. }
  182. rc["addresses"] = ips;
  183. results.push_front(rc);
  184. }
  185. return results;
  186. }
  187. void IP::get_local_addresses(List<IP_Address> *r_addresses) const {
  188. Map<String, Interface_Info> interfaces;
  189. get_local_interfaces(&interfaces);
  190. for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
  191. for (const List<IP_Address>::Element *F = E->get().ip_addresses.front(); F; F = F->next()) {
  192. r_addresses->push_front(F->get());
  193. }
  194. }
  195. }
  196. void IP::_bind_methods() {
  197. ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY));
  198. ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY));
  199. ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status);
  200. ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address);
  201. ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item);
  202. ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses);
  203. ClassDB::bind_method(D_METHOD("get_local_interfaces"), &IP::_get_local_interfaces);
  204. ClassDB::bind_method(D_METHOD("clear_cache", "hostname"), &IP::clear_cache, DEFVAL(""));
  205. BIND_ENUM_CONSTANT(RESOLVER_STATUS_NONE);
  206. BIND_ENUM_CONSTANT(RESOLVER_STATUS_WAITING);
  207. BIND_ENUM_CONSTANT(RESOLVER_STATUS_DONE);
  208. BIND_ENUM_CONSTANT(RESOLVER_STATUS_ERROR);
  209. BIND_CONSTANT(RESOLVER_MAX_QUERIES);
  210. BIND_CONSTANT(RESOLVER_INVALID_ID);
  211. BIND_ENUM_CONSTANT(TYPE_NONE);
  212. BIND_ENUM_CONSTANT(TYPE_IPV4);
  213. BIND_ENUM_CONSTANT(TYPE_IPV6);
  214. BIND_ENUM_CONSTANT(TYPE_ANY);
  215. }
  216. IP *IP::singleton = nullptr;
  217. IP *IP::get_singleton() {
  218. return singleton;
  219. }
  220. IP *(*IP::_create)() = nullptr;
  221. IP *IP::create() {
  222. ERR_FAIL_COND_V_MSG(singleton, nullptr, "IP singleton already exist.");
  223. ERR_FAIL_COND_V(!_create, nullptr);
  224. return _create();
  225. }
  226. IP::IP() {
  227. singleton = this;
  228. resolver = memnew(_IP_ResolverPrivate);
  229. #ifndef NO_THREADS
  230. resolver->thread_abort = false;
  231. resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver);
  232. #else
  233. resolver->thread = nullptr;
  234. #endif
  235. }
  236. IP::~IP() {
  237. #ifndef NO_THREADS
  238. if (resolver->thread) {
  239. resolver->thread_abort = true;
  240. resolver->sem.post();
  241. Thread::wait_to_finish(resolver->thread);
  242. memdelete(resolver->thread);
  243. }
  244. #endif
  245. memdelete(resolver);
  246. }