ip.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/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. SafeNumeric<IP::ResolverStatus> status;
  39. IP_Address response;
  40. String hostname;
  41. IP::Type type;
  42. void clear() {
  43. status.set(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.get() == 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.get() != IP::RESOLVER_STATUS_WAITING)
  68. continue;
  69. mutex.lock();
  70. String hostname = queue[i].hostname;
  71. IP::Type type = queue[i].type;
  72. mutex.unlock();
  73. // We should not lock while resolving the hostname,
  74. // only when modifying the queue.
  75. IP_Address response = IP::get_singleton()->resolve_hostname(hostname, type);
  76. MutexLock lock(mutex);
  77. queue[i].response = response;
  78. queue[i].status.set(response.is_valid() ? IP::RESOLVER_STATUS_DONE : IP::RESOLVER_STATUS_ERROR);
  79. }
  80. }
  81. static void _thread_function(void *self) {
  82. _IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self;
  83. while (!ipr->thread_abort) {
  84. ipr->sem.wait();
  85. ipr->resolve_queues();
  86. }
  87. }
  88. HashMap<String, IP_Address> cache;
  89. static String get_cache_key(String p_hostname, IP::Type p_type) {
  90. return itos(p_type) + p_hostname;
  91. }
  92. };
  93. IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
  94. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  95. IP_Address res;
  96. resolver->mutex.lock();
  97. if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
  98. res = resolver->cache[key];
  99. } else {
  100. // This should be run unlocked so the resolver thread can keep
  101. // resolving other requests.
  102. resolver->mutex.unlock();
  103. res = _resolve_hostname(p_hostname, p_type);
  104. resolver->mutex.lock();
  105. // We might be overriding another result, but we don't care (they are the
  106. // same hostname).
  107. resolver->cache[key] = res;
  108. }
  109. resolver->mutex.unlock();
  110. return res;
  111. }
  112. IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
  113. resolver->mutex.lock();
  114. ResolverID id = resolver->find_empty_id();
  115. if (id == RESOLVER_INVALID_ID) {
  116. WARN_PRINT("Out of resolver queries");
  117. resolver->mutex.unlock();
  118. return id;
  119. }
  120. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  121. resolver->queue[id].hostname = p_hostname;
  122. resolver->queue[id].type = p_type;
  123. if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
  124. resolver->queue[id].response = resolver->cache[key];
  125. resolver->queue[id].status.set(IP::RESOLVER_STATUS_DONE);
  126. } else {
  127. resolver->queue[id].response = IP_Address();
  128. resolver->queue[id].status.set(IP::RESOLVER_STATUS_WAITING);
  129. if (resolver->thread.is_started())
  130. resolver->sem.post();
  131. else
  132. resolver->resolve_queues();
  133. }
  134. resolver->mutex.unlock();
  135. return id;
  136. }
  137. IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
  138. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE);
  139. IP::ResolverStatus res = resolver->queue[p_id].status.get();
  140. if (res == IP::RESOLVER_STATUS_NONE) {
  141. ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE");
  142. }
  143. return res;
  144. }
  145. IP_Address IP::get_resolve_item_address(ResolverID p_id) const {
  146. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address());
  147. resolver->mutex.lock();
  148. if (resolver->queue[p_id].status.get() != IP::RESOLVER_STATUS_DONE) {
  149. ERR_PRINTS("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
  150. resolver->mutex.unlock();
  151. return IP_Address();
  152. }
  153. IP_Address res = resolver->queue[p_id].response;
  154. resolver->mutex.unlock();
  155. return res;
  156. }
  157. void IP::erase_resolve_item(ResolverID p_id) {
  158. ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
  159. resolver->queue[p_id].status.set(IP::RESOLVER_STATUS_NONE);
  160. }
  161. void IP::clear_cache(const String &p_hostname) {
  162. resolver->mutex.lock();
  163. if (p_hostname.empty()) {
  164. resolver->cache.clear();
  165. } else {
  166. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
  167. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV4));
  168. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6));
  169. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY));
  170. }
  171. resolver->mutex.unlock();
  172. }
  173. Array IP::_get_local_addresses() const {
  174. Array addresses;
  175. List<IP_Address> ip_addresses;
  176. get_local_addresses(&ip_addresses);
  177. for (List<IP_Address>::Element *E = ip_addresses.front(); E; E = E->next()) {
  178. addresses.push_back(E->get());
  179. }
  180. return addresses;
  181. }
  182. Array IP::_get_local_interfaces() const {
  183. Array results;
  184. Map<String, Interface_Info> interfaces;
  185. get_local_interfaces(&interfaces);
  186. for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
  187. Interface_Info &c = E->get();
  188. Dictionary rc;
  189. rc["name"] = c.name;
  190. rc["friendly"] = c.name_friendly;
  191. rc["index"] = c.index;
  192. Array ips;
  193. for (const List<IP_Address>::Element *F = c.ip_addresses.front(); F; F = F->next()) {
  194. ips.push_front(F->get());
  195. }
  196. rc["addresses"] = ips;
  197. results.push_front(rc);
  198. }
  199. return results;
  200. }
  201. void IP::get_local_addresses(List<IP_Address> *r_addresses) const {
  202. Map<String, Interface_Info> interfaces;
  203. get_local_interfaces(&interfaces);
  204. for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
  205. for (const List<IP_Address>::Element *F = E->get().ip_addresses.front(); F; F = F->next()) {
  206. r_addresses->push_front(F->get());
  207. }
  208. }
  209. }
  210. void IP::_bind_methods() {
  211. ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY));
  212. ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY));
  213. ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status);
  214. ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address);
  215. ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item);
  216. ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses);
  217. ClassDB::bind_method(D_METHOD("get_local_interfaces"), &IP::_get_local_interfaces);
  218. ClassDB::bind_method(D_METHOD("clear_cache", "hostname"), &IP::clear_cache, DEFVAL(""));
  219. BIND_ENUM_CONSTANT(RESOLVER_STATUS_NONE);
  220. BIND_ENUM_CONSTANT(RESOLVER_STATUS_WAITING);
  221. BIND_ENUM_CONSTANT(RESOLVER_STATUS_DONE);
  222. BIND_ENUM_CONSTANT(RESOLVER_STATUS_ERROR);
  223. BIND_CONSTANT(RESOLVER_MAX_QUERIES);
  224. BIND_CONSTANT(RESOLVER_INVALID_ID);
  225. BIND_ENUM_CONSTANT(TYPE_NONE);
  226. BIND_ENUM_CONSTANT(TYPE_IPV4);
  227. BIND_ENUM_CONSTANT(TYPE_IPV6);
  228. BIND_ENUM_CONSTANT(TYPE_ANY);
  229. }
  230. IP *IP::singleton = NULL;
  231. IP *IP::get_singleton() {
  232. return singleton;
  233. }
  234. IP *(*IP::_create)() = NULL;
  235. IP *IP::create() {
  236. ERR_FAIL_COND_V_MSG(singleton, NULL, "IP singleton already exist.");
  237. ERR_FAIL_COND_V(!_create, NULL);
  238. return _create();
  239. }
  240. IP::IP() {
  241. singleton = this;
  242. resolver = memnew(_IP_ResolverPrivate);
  243. resolver->thread_abort = false;
  244. resolver->thread.start(_IP_ResolverPrivate::_thread_function, resolver);
  245. }
  246. IP::~IP() {
  247. resolver->thread_abort = true;
  248. resolver->sem.post();
  249. resolver->thread.wait_to_finish();
  250. memdelete(resolver);
  251. }