ip.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*************************************************************************/
  2. /* ip.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/os/semaphore.h"
  32. #include "core/os/thread.h"
  33. #include "core/templates/hash_map.h"
  34. VARIANT_ENUM_CAST(IP::ResolverStatus);
  35. /************* RESOLVER ******************/
  36. struct _IP_ResolverPrivate {
  37. struct QueueItem {
  38. SafeNumeric<IP::ResolverStatus> status;
  39. IPAddress response;
  40. String hostname;
  41. IP::Type type;
  42. void clear() {
  43. status.set(IP::RESOLVER_STATUS_NONE);
  44. response = IPAddress();
  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.get() == IP::RESOLVER_STATUS_NONE) {
  56. return i;
  57. }
  58. }
  59. return IP::RESOLVER_INVALID_ID;
  60. }
  61. Mutex mutex;
  62. Semaphore sem;
  63. Thread thread;
  64. //Semaphore* semaphore;
  65. bool thread_abort;
  66. void resolve_queues() {
  67. for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) {
  68. if (queue[i].status.get() != IP::RESOLVER_STATUS_WAITING) {
  69. continue;
  70. }
  71. queue[i].response = IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
  72. if (!queue[i].response.is_valid()) {
  73. queue[i].status.set(IP::RESOLVER_STATUS_ERROR);
  74. } else {
  75. queue[i].status.set(IP::RESOLVER_STATUS_DONE);
  76. }
  77. }
  78. }
  79. static void _thread_function(void *self) {
  80. _IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self;
  81. while (!ipr->thread_abort) {
  82. ipr->sem.wait();
  83. MutexLock lock(ipr->mutex);
  84. ipr->resolve_queues();
  85. }
  86. }
  87. HashMap<String, IPAddress> cache;
  88. static String get_cache_key(String p_hostname, IP::Type p_type) {
  89. return itos(p_type) + p_hostname;
  90. }
  91. };
  92. IPAddress IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
  93. MutexLock lock(resolver->mutex);
  94. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  95. if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
  96. IPAddress res = resolver->cache[key];
  97. return res;
  98. }
  99. IPAddress res = _resolve_hostname(p_hostname, p_type);
  100. resolver->cache[key] = res;
  101. return res;
  102. }
  103. IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
  104. MutexLock lock(resolver->mutex);
  105. ResolverID id = resolver->find_empty_id();
  106. if (id == RESOLVER_INVALID_ID) {
  107. WARN_PRINT("Out of resolver queries");
  108. return id;
  109. }
  110. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  111. resolver->queue[id].hostname = p_hostname;
  112. resolver->queue[id].type = p_type;
  113. if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
  114. resolver->queue[id].response = resolver->cache[key];
  115. resolver->queue[id].status.set(IP::RESOLVER_STATUS_DONE);
  116. } else {
  117. resolver->queue[id].response = IPAddress();
  118. resolver->queue[id].status.set(IP::RESOLVER_STATUS_WAITING);
  119. if (resolver->thread.is_started()) {
  120. resolver->sem.post();
  121. } else {
  122. resolver->resolve_queues();
  123. }
  124. }
  125. return id;
  126. }
  127. IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
  128. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE);
  129. MutexLock lock(resolver->mutex);
  130. if (resolver->queue[p_id].status.get() == IP::RESOLVER_STATUS_NONE) {
  131. ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE");
  132. resolver->mutex.unlock();
  133. return IP::RESOLVER_STATUS_NONE;
  134. }
  135. return resolver->queue[p_id].status.get();
  136. }
  137. IPAddress IP::get_resolve_item_address(ResolverID p_id) const {
  138. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IPAddress());
  139. MutexLock lock(resolver->mutex);
  140. if (resolver->queue[p_id].status.get() != IP::RESOLVER_STATUS_DONE) {
  141. ERR_PRINT("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
  142. resolver->mutex.unlock();
  143. return IPAddress();
  144. }
  145. return resolver->queue[p_id].response;
  146. }
  147. void IP::erase_resolve_item(ResolverID p_id) {
  148. ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
  149. MutexLock lock(resolver->mutex);
  150. resolver->queue[p_id].status.set(IP::RESOLVER_STATUS_NONE);
  151. }
  152. void IP::clear_cache(const String &p_hostname) {
  153. MutexLock lock(resolver->mutex);
  154. if (p_hostname.is_empty()) {
  155. resolver->cache.clear();
  156. } else {
  157. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
  158. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV4));
  159. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6));
  160. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY));
  161. }
  162. }
  163. Array IP::_get_local_addresses() const {
  164. Array addresses;
  165. List<IPAddress> ip_addresses;
  166. get_local_addresses(&ip_addresses);
  167. for (List<IPAddress>::Element *E = ip_addresses.front(); E; E = E->next()) {
  168. addresses.push_back(E->get());
  169. }
  170. return addresses;
  171. }
  172. Array IP::_get_local_interfaces() const {
  173. Array results;
  174. Map<String, Interface_Info> interfaces;
  175. get_local_interfaces(&interfaces);
  176. for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
  177. Interface_Info &c = E->get();
  178. Dictionary rc;
  179. rc["name"] = c.name;
  180. rc["friendly"] = c.name_friendly;
  181. rc["index"] = c.index;
  182. Array ips;
  183. for (const List<IPAddress>::Element *F = c.ip_addresses.front(); F; F = F->next()) {
  184. ips.push_front(F->get());
  185. }
  186. rc["addresses"] = ips;
  187. results.push_front(rc);
  188. }
  189. return results;
  190. }
  191. void IP::get_local_addresses(List<IPAddress> *r_addresses) const {
  192. Map<String, Interface_Info> interfaces;
  193. get_local_interfaces(&interfaces);
  194. for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
  195. for (const List<IPAddress>::Element *F = E->get().ip_addresses.front(); F; F = F->next()) {
  196. r_addresses->push_front(F->get());
  197. }
  198. }
  199. }
  200. void IP::_bind_methods() {
  201. ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY));
  202. ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY));
  203. ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status);
  204. ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address);
  205. ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item);
  206. ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses);
  207. ClassDB::bind_method(D_METHOD("get_local_interfaces"), &IP::_get_local_interfaces);
  208. ClassDB::bind_method(D_METHOD("clear_cache", "hostname"), &IP::clear_cache, DEFVAL(""));
  209. BIND_ENUM_CONSTANT(RESOLVER_STATUS_NONE);
  210. BIND_ENUM_CONSTANT(RESOLVER_STATUS_WAITING);
  211. BIND_ENUM_CONSTANT(RESOLVER_STATUS_DONE);
  212. BIND_ENUM_CONSTANT(RESOLVER_STATUS_ERROR);
  213. BIND_CONSTANT(RESOLVER_MAX_QUERIES);
  214. BIND_CONSTANT(RESOLVER_INVALID_ID);
  215. BIND_ENUM_CONSTANT(TYPE_NONE);
  216. BIND_ENUM_CONSTANT(TYPE_IPV4);
  217. BIND_ENUM_CONSTANT(TYPE_IPV6);
  218. BIND_ENUM_CONSTANT(TYPE_ANY);
  219. }
  220. IP *IP::singleton = nullptr;
  221. IP *IP::get_singleton() {
  222. return singleton;
  223. }
  224. IP *(*IP::_create)() = nullptr;
  225. IP *IP::create() {
  226. ERR_FAIL_COND_V_MSG(singleton, nullptr, "IP singleton already exist.");
  227. ERR_FAIL_COND_V(!_create, nullptr);
  228. return _create();
  229. }
  230. IP::IP() {
  231. singleton = this;
  232. resolver = memnew(_IP_ResolverPrivate);
  233. resolver->thread_abort = false;
  234. resolver->thread.start(_IP_ResolverPrivate::_thread_function, resolver);
  235. }
  236. IP::~IP() {
  237. resolver->thread_abort = true;
  238. resolver->sem.post();
  239. resolver->thread.wait_to_finish();
  240. memdelete(resolver);
  241. }