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