ip.cpp 11 KB

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