ip.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*************************************************************************/
  2. /* ip.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. mutex.lock();
  72. List<IPAddress> response;
  73. String hostname = queue[i].hostname;
  74. IP::Type type = queue[i].type;
  75. mutex.unlock();
  76. // We should not lock while resolving the hostname,
  77. // only when modifying the queue.
  78. IP::get_singleton()->_resolve_hostname(response, hostname, type);
  79. MutexLock lock(mutex);
  80. // Could have been completed by another function, or deleted.
  81. if (queue[i].status.get() != IP::RESOLVER_STATUS_WAITING) {
  82. continue;
  83. }
  84. queue[i].response = response;
  85. queue[i].status.set(response.is_empty() ? IP::RESOLVER_STATUS_ERROR : IP::RESOLVER_STATUS_DONE);
  86. }
  87. }
  88. static void _thread_function(void *self) {
  89. _IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self;
  90. while (!ipr->thread_abort) {
  91. ipr->sem.wait();
  92. ipr->resolve_queues();
  93. }
  94. }
  95. HashMap<String, List<IPAddress>> cache;
  96. static String get_cache_key(String p_hostname, IP::Type p_type) {
  97. return itos(p_type) + p_hostname;
  98. }
  99. };
  100. IPAddress IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
  101. List<IPAddress> res;
  102. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  103. resolver->mutex.lock();
  104. if (resolver->cache.has(key)) {
  105. res = resolver->cache[key];
  106. } else {
  107. // This should be run unlocked so the resolver thread can keep
  108. // resolving other requests.
  109. resolver->mutex.unlock();
  110. _resolve_hostname(res, p_hostname, p_type);
  111. resolver->mutex.lock();
  112. // We might be overriding another result, but we don't care (they are the
  113. // same hostname).
  114. resolver->cache[key] = res;
  115. }
  116. resolver->mutex.unlock();
  117. for (int i = 0; i < res.size(); ++i) {
  118. if (res[i].is_valid()) {
  119. return res[i];
  120. }
  121. }
  122. return IPAddress();
  123. }
  124. Array IP::resolve_hostname_addresses(const String &p_hostname, Type p_type) {
  125. List<IPAddress> res;
  126. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  127. resolver->mutex.lock();
  128. if (resolver->cache.has(key)) {
  129. res = resolver->cache[key];
  130. } else {
  131. // This should be run unlocked so the resolver thread can keep resolving
  132. // other requests.
  133. resolver->mutex.unlock();
  134. _resolve_hostname(res, p_hostname, p_type);
  135. resolver->mutex.lock();
  136. // We might be overriding another result, but we don't care (they are the
  137. // same hostname).
  138. resolver->cache[key] = res;
  139. }
  140. resolver->mutex.unlock();
  141. Array result;
  142. for (int i = 0; i < res.size(); ++i) {
  143. if (res[i].is_valid()) {
  144. result.push_back(String(res[i]));
  145. }
  146. }
  147. return result;
  148. }
  149. IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
  150. MutexLock lock(resolver->mutex);
  151. ResolverID id = resolver->find_empty_id();
  152. if (id == RESOLVER_INVALID_ID) {
  153. WARN_PRINT("Out of resolver queries");
  154. return id;
  155. }
  156. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  157. resolver->queue[id].hostname = p_hostname;
  158. resolver->queue[id].type = p_type;
  159. if (resolver->cache.has(key)) {
  160. resolver->queue[id].response = resolver->cache[key];
  161. resolver->queue[id].status.set(IP::RESOLVER_STATUS_DONE);
  162. } else {
  163. resolver->queue[id].response = List<IPAddress>();
  164. resolver->queue[id].status.set(IP::RESOLVER_STATUS_WAITING);
  165. if (resolver->thread.is_started()) {
  166. resolver->sem.post();
  167. } else {
  168. resolver->resolve_queues();
  169. }
  170. }
  171. return id;
  172. }
  173. IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
  174. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE);
  175. IP::ResolverStatus res = resolver->queue[p_id].status.get();
  176. if (res == IP::RESOLVER_STATUS_NONE) {
  177. ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE");
  178. return IP::RESOLVER_STATUS_NONE;
  179. }
  180. return res;
  181. }
  182. IPAddress IP::get_resolve_item_address(ResolverID p_id) const {
  183. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IPAddress());
  184. MutexLock lock(resolver->mutex);
  185. if (resolver->queue[p_id].status.get() != IP::RESOLVER_STATUS_DONE) {
  186. ERR_PRINT("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
  187. return IPAddress();
  188. }
  189. List<IPAddress> res = resolver->queue[p_id].response;
  190. for (int i = 0; i < res.size(); ++i) {
  191. if (res[i].is_valid()) {
  192. return res[i];
  193. }
  194. }
  195. return IPAddress();
  196. }
  197. Array IP::get_resolve_item_addresses(ResolverID p_id) const {
  198. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, Array());
  199. MutexLock lock(resolver->mutex);
  200. if (resolver->queue[p_id].status.get() != IP::RESOLVER_STATUS_DONE) {
  201. ERR_PRINT("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
  202. return Array();
  203. }
  204. List<IPAddress> res = resolver->queue[p_id].response;
  205. Array result;
  206. for (int i = 0; i < res.size(); ++i) {
  207. if (res[i].is_valid()) {
  208. result.push_back(String(res[i]));
  209. }
  210. }
  211. return result;
  212. }
  213. void IP::erase_resolve_item(ResolverID p_id) {
  214. ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
  215. resolver->queue[p_id].status.set(IP::RESOLVER_STATUS_NONE);
  216. }
  217. void IP::clear_cache(const String &p_hostname) {
  218. MutexLock lock(resolver->mutex);
  219. if (p_hostname.is_empty()) {
  220. resolver->cache.clear();
  221. } else {
  222. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
  223. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV4));
  224. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6));
  225. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY));
  226. }
  227. }
  228. Array IP::_get_local_addresses() const {
  229. Array addresses;
  230. List<IPAddress> ip_addresses;
  231. get_local_addresses(&ip_addresses);
  232. for (const IPAddress &E : ip_addresses) {
  233. addresses.push_back(E);
  234. }
  235. return addresses;
  236. }
  237. Array IP::_get_local_interfaces() const {
  238. Array results;
  239. Map<String, Interface_Info> interfaces;
  240. get_local_interfaces(&interfaces);
  241. for (KeyValue<String, Interface_Info> &E : interfaces) {
  242. Interface_Info &c = E.value;
  243. Dictionary rc;
  244. rc["name"] = c.name;
  245. rc["friendly"] = c.name_friendly;
  246. rc["index"] = c.index;
  247. Array ips;
  248. for (const IPAddress &F : c.ip_addresses) {
  249. ips.push_front(F);
  250. }
  251. rc["addresses"] = ips;
  252. results.push_front(rc);
  253. }
  254. return results;
  255. }
  256. void IP::get_local_addresses(List<IPAddress> *r_addresses) const {
  257. Map<String, Interface_Info> interfaces;
  258. get_local_interfaces(&interfaces);
  259. for (const KeyValue<String, Interface_Info> &E : interfaces) {
  260. for (const IPAddress &F : E.value.ip_addresses) {
  261. r_addresses->push_front(F);
  262. }
  263. }
  264. }
  265. void IP::_bind_methods() {
  266. ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY));
  267. ClassDB::bind_method(D_METHOD("resolve_hostname_addresses", "host", "ip_type"), &IP::resolve_hostname_addresses, DEFVAL(IP::TYPE_ANY));
  268. ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY));
  269. ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status);
  270. ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address);
  271. ClassDB::bind_method(D_METHOD("get_resolve_item_addresses", "id"), &IP::get_resolve_item_addresses);
  272. ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item);
  273. ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses);
  274. ClassDB::bind_method(D_METHOD("get_local_interfaces"), &IP::_get_local_interfaces);
  275. ClassDB::bind_method(D_METHOD("clear_cache", "hostname"), &IP::clear_cache, DEFVAL(""));
  276. BIND_ENUM_CONSTANT(RESOLVER_STATUS_NONE);
  277. BIND_ENUM_CONSTANT(RESOLVER_STATUS_WAITING);
  278. BIND_ENUM_CONSTANT(RESOLVER_STATUS_DONE);
  279. BIND_ENUM_CONSTANT(RESOLVER_STATUS_ERROR);
  280. BIND_CONSTANT(RESOLVER_MAX_QUERIES);
  281. BIND_CONSTANT(RESOLVER_INVALID_ID);
  282. BIND_ENUM_CONSTANT(TYPE_NONE);
  283. BIND_ENUM_CONSTANT(TYPE_IPV4);
  284. BIND_ENUM_CONSTANT(TYPE_IPV6);
  285. BIND_ENUM_CONSTANT(TYPE_ANY);
  286. }
  287. IP *IP::singleton = nullptr;
  288. IP *IP::get_singleton() {
  289. return singleton;
  290. }
  291. IP *(*IP::_create)() = nullptr;
  292. IP *IP::create() {
  293. ERR_FAIL_COND_V_MSG(singleton, nullptr, "IP singleton already exist.");
  294. ERR_FAIL_COND_V(!_create, nullptr);
  295. return _create();
  296. }
  297. IP::IP() {
  298. singleton = this;
  299. resolver = memnew(_IP_ResolverPrivate);
  300. resolver->thread_abort = false;
  301. resolver->thread.start(_IP_ResolverPrivate::_thread_function, resolver);
  302. }
  303. IP::~IP() {
  304. resolver->thread_abort = true;
  305. resolver->sem.post();
  306. resolver->thread.wait_to_finish();
  307. memdelete(resolver);
  308. }