| 1 | /**************************************************************************/ |
| 2 | /* ip.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #include "ip.h" |
| 32 | |
| 33 | #include "core/os/semaphore.h" |
| 34 | #include "core/os/thread.h" |
| 35 | #include "core/templates/hash_map.h" |
| 36 | #include "core/variant/typed_array.h" |
| 37 | |
| 38 | /************* RESOLVER ******************/ |
| 39 | |
| 40 | struct _IP_ResolverPrivate { |
| 41 | struct QueueItem { |
| 42 | SafeNumeric<IP::ResolverStatus> status; |
| 43 | |
| 44 | List<IPAddress> response; |
| 45 | |
| 46 | String hostname; |
| 47 | IP::Type type; |
| 48 | |
| 49 | void clear() { |
| 50 | status.set(IP::RESOLVER_STATUS_NONE); |
| 51 | response.clear(); |
| 52 | type = IP::TYPE_NONE; |
| 53 | hostname = ""; |
| 54 | } |
| 55 | |
| 56 | QueueItem() { |
| 57 | clear(); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | QueueItem queue[IP::RESOLVER_MAX_QUERIES]; |
| 62 | |
| 63 | IP::ResolverID find_empty_id() const { |
| 64 | for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) { |
| 65 | if (queue[i].status.get() == IP::RESOLVER_STATUS_NONE) { |
| 66 | return i; |
| 67 | } |
| 68 | } |
| 69 | return IP::RESOLVER_INVALID_ID; |
| 70 | } |
| 71 | |
| 72 | Mutex mutex; |
| 73 | Semaphore sem; |
| 74 | |
| 75 | Thread thread; |
| 76 | SafeFlag thread_abort; |
| 77 | |
| 78 | void resolve_queues() { |
| 79 | for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) { |
| 80 | if (queue[i].status.get() != IP::RESOLVER_STATUS_WAITING) { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | MutexLock lock(mutex); |
| 85 | List<IPAddress> response; |
| 86 | String hostname = queue[i].hostname; |
| 87 | IP::Type type = queue[i].type; |
| 88 | lock.temp_unlock(); |
| 89 | |
| 90 | // We should not lock while resolving the hostname, |
| 91 | // only when modifying the queue. |
| 92 | IP::get_singleton()->_resolve_hostname(response, hostname, type); |
| 93 | |
| 94 | lock.temp_relock(); |
| 95 | // Could have been completed by another function, or deleted. |
| 96 | if (queue[i].status.get() != IP::RESOLVER_STATUS_WAITING) { |
| 97 | continue; |
| 98 | } |
| 99 | // We might be overriding another result, but we don't care as long as the result is valid. |
| 100 | if (response.size()) { |
| 101 | String key = get_cache_key(hostname, type); |
| 102 | cache[key] = response; |
| 103 | } |
| 104 | queue[i].response = response; |
| 105 | queue[i].status.set(response.is_empty() ? IP::RESOLVER_STATUS_ERROR : IP::RESOLVER_STATUS_DONE); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void _thread_function(void *self) { |
| 110 | _IP_ResolverPrivate *ipr = static_cast<_IP_ResolverPrivate *>(self); |
| 111 | |
| 112 | while (!ipr->thread_abort.is_set()) { |
| 113 | ipr->sem.wait(); |
| 114 | ipr->resolve_queues(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | HashMap<String, List<IPAddress>> cache; |
| 119 | |
| 120 | static String get_cache_key(const String &p_hostname, IP::Type p_type) { |
| 121 | return itos(p_type) + p_hostname; |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | IPAddress IP::resolve_hostname(const String &p_hostname, IP::Type p_type) { |
| 126 | const PackedStringArray addresses = resolve_hostname_addresses(p_hostname, p_type); |
| 127 | return addresses.size() ? (IPAddress)addresses[0] : IPAddress(); |
| 128 | } |
| 129 | |
| 130 | PackedStringArray IP::resolve_hostname_addresses(const String &p_hostname, Type p_type) { |
| 131 | List<IPAddress> res; |
| 132 | String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type); |
| 133 | |
| 134 | { |
| 135 | MutexLock lock(resolver->mutex); |
| 136 | if (resolver->cache.has(key)) { |
| 137 | res = resolver->cache[key]; |
| 138 | } else { |
| 139 | // This should be run unlocked so the resolver thread can keep resolving |
| 140 | // other requests. |
| 141 | lock.temp_unlock(); |
| 142 | _resolve_hostname(res, p_hostname, p_type); |
| 143 | lock.temp_relock(); |
| 144 | // We might be overriding another result, but we don't care as long as the result is valid. |
| 145 | if (res.size()) { |
| 146 | resolver->cache[key] = res; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | PackedStringArray result; |
| 152 | for (const IPAddress &E : res) { |
| 153 | result.push_back(String(E)); |
| 154 | } |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) { |
| 159 | MutexLock lock(resolver->mutex); |
| 160 | |
| 161 | ResolverID id = resolver->find_empty_id(); |
| 162 | |
| 163 | if (id == RESOLVER_INVALID_ID) { |
| 164 | WARN_PRINT("Out of resolver queries"); |
| 165 | return id; |
| 166 | } |
| 167 | |
| 168 | String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type); |
| 169 | resolver->queue[id].hostname = p_hostname; |
| 170 | resolver->queue[id].type = p_type; |
| 171 | if (resolver->cache.has(key)) { |
| 172 | resolver->queue[id].response = resolver->cache[key]; |
| 173 | resolver->queue[id].status.set(IP::RESOLVER_STATUS_DONE); |
| 174 | } else { |
| 175 | resolver->queue[id].response = List<IPAddress>(); |
| 176 | resolver->queue[id].status.set(IP::RESOLVER_STATUS_WAITING); |
| 177 | if (resolver->thread.is_started()) { |
| 178 | resolver->sem.post(); |
| 179 | } else { |
| 180 | resolver->resolve_queues(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return id; |
| 185 | } |
| 186 | |
| 187 | IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const { |
| 188 | ERR_FAIL_INDEX_V_MSG(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE, vformat("Too many concurrent DNS resolver queries (%d, but should be %d at most). Try performing less network requests at once.", p_id, IP::RESOLVER_MAX_QUERIES)); |
| 189 | |
| 190 | IP::ResolverStatus res = resolver->queue[p_id].status.get(); |
| 191 | if (res == IP::RESOLVER_STATUS_NONE) { |
| 192 | ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE"); |
| 193 | return IP::RESOLVER_STATUS_NONE; |
| 194 | } |
| 195 | return res; |
| 196 | } |
| 197 | |
| 198 | IPAddress IP::get_resolve_item_address(ResolverID p_id) const { |
| 199 | ERR_FAIL_INDEX_V_MSG(p_id, IP::RESOLVER_MAX_QUERIES, IPAddress(), vformat("Too many concurrent DNS resolver queries (%d, but should be %d at most). Try performing less network requests at once.", p_id, IP::RESOLVER_MAX_QUERIES)); |
| 200 | |
| 201 | MutexLock lock(resolver->mutex); |
| 202 | |
| 203 | if (resolver->queue[p_id].status.get() != IP::RESOLVER_STATUS_DONE) { |
| 204 | ERR_PRINT(vformat("Resolve of '%s' didn't complete yet.", resolver->queue[p_id].hostname)); |
| 205 | return IPAddress(); |
| 206 | } |
| 207 | |
| 208 | List<IPAddress> res = resolver->queue[p_id].response; |
| 209 | |
| 210 | for (const IPAddress &E : res) { |
| 211 | if (E.is_valid()) { |
| 212 | return E; |
| 213 | } |
| 214 | } |
| 215 | return IPAddress(); |
| 216 | } |
| 217 | |
| 218 | Array IP::get_resolve_item_addresses(ResolverID p_id) const { |
| 219 | ERR_FAIL_INDEX_V_MSG(p_id, IP::RESOLVER_MAX_QUERIES, Array(), vformat("Too many concurrent DNS resolver queries (%d, but should be %d at most). Try performing less network requests at once.", p_id, IP::RESOLVER_MAX_QUERIES)); |
| 220 | MutexLock lock(resolver->mutex); |
| 221 | |
| 222 | if (resolver->queue[p_id].status.get() != IP::RESOLVER_STATUS_DONE) { |
| 223 | ERR_PRINT(vformat("Resolve of '%s' didn't complete yet.", resolver->queue[p_id].hostname)); |
| 224 | return Array(); |
| 225 | } |
| 226 | |
| 227 | List<IPAddress> res = resolver->queue[p_id].response; |
| 228 | |
| 229 | Array result; |
| 230 | for (const IPAddress &E : res) { |
| 231 | if (E.is_valid()) { |
| 232 | result.push_back(String(E)); |
| 233 | } |
| 234 | } |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | void IP::erase_resolve_item(ResolverID p_id) { |
| 239 | ERR_FAIL_INDEX_MSG(p_id, IP::RESOLVER_MAX_QUERIES, vformat("Too many concurrent DNS resolver queries (%d, but should be %d at most). Try performing less network requests at once.", p_id, IP::RESOLVER_MAX_QUERIES)); |
| 240 | |
| 241 | resolver->queue[p_id].status.set(IP::RESOLVER_STATUS_NONE); |
| 242 | } |
| 243 | |
| 244 | void IP::clear_cache(const String &p_hostname) { |
| 245 | MutexLock lock(resolver->mutex); |
| 246 | |
| 247 | if (p_hostname.is_empty()) { |
| 248 | resolver->cache.clear(); |
| 249 | } else { |
| 250 | resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE)); |
| 251 | resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV4)); |
| 252 | resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6)); |
| 253 | resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | PackedStringArray IP::_get_local_addresses() const { |
| 258 | PackedStringArray addresses; |
| 259 | List<IPAddress> ip_addresses; |
| 260 | get_local_addresses(&ip_addresses); |
| 261 | for (const IPAddress &E : ip_addresses) { |
| 262 | addresses.push_back(String(E)); |
| 263 | } |
| 264 | |
| 265 | return addresses; |
| 266 | } |
| 267 | |
| 268 | TypedArray<Dictionary> IP::_get_local_interfaces() const { |
| 269 | TypedArray<Dictionary> results; |
| 270 | HashMap<String, Interface_Info> interfaces; |
| 271 | get_local_interfaces(&interfaces); |
| 272 | for (KeyValue<String, Interface_Info> &E : interfaces) { |
| 273 | Interface_Info &c = E.value; |
| 274 | Dictionary rc; |
| 275 | rc["name"] = c.name; |
| 276 | rc["friendly"] = c.name_friendly; |
| 277 | rc["index"] = c.index; |
| 278 | |
| 279 | Array ips; |
| 280 | for (const IPAddress &F : c.ip_addresses) { |
| 281 | ips.push_front(F); |
| 282 | } |
| 283 | rc["addresses"] = ips; |
| 284 | |
| 285 | results.push_front(rc); |
| 286 | } |
| 287 | |
| 288 | return results; |
| 289 | } |
| 290 | |
| 291 | void IP::get_local_addresses(List<IPAddress> *r_addresses) const { |
| 292 | HashMap<String, Interface_Info> interfaces; |
| 293 | get_local_interfaces(&interfaces); |
| 294 | for (const KeyValue<String, Interface_Info> &E : interfaces) { |
| 295 | for (const IPAddress &F : E.value.ip_addresses) { |
| 296 | r_addresses->push_front(F); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void IP::_bind_methods() { |
| 302 | ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY)); |
| 303 | ClassDB::bind_method(D_METHOD("resolve_hostname_addresses", "host", "ip_type"), &IP::resolve_hostname_addresses, DEFVAL(IP::TYPE_ANY)); |
| 304 | ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY)); |
| 305 | ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status); |
| 306 | ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address); |
| 307 | ClassDB::bind_method(D_METHOD("get_resolve_item_addresses", "id"), &IP::get_resolve_item_addresses); |
| 308 | ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item); |
| 309 | ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses); |
| 310 | ClassDB::bind_method(D_METHOD("get_local_interfaces"), &IP::_get_local_interfaces); |
| 311 | ClassDB::bind_method(D_METHOD("clear_cache", "hostname"), &IP::clear_cache, DEFVAL("")); |
| 312 | |
| 313 | BIND_ENUM_CONSTANT(RESOLVER_STATUS_NONE); |
| 314 | BIND_ENUM_CONSTANT(RESOLVER_STATUS_WAITING); |
| 315 | BIND_ENUM_CONSTANT(RESOLVER_STATUS_DONE); |
| 316 | BIND_ENUM_CONSTANT(RESOLVER_STATUS_ERROR); |
| 317 | |
| 318 | BIND_CONSTANT(RESOLVER_MAX_QUERIES); |
| 319 | BIND_CONSTANT(RESOLVER_INVALID_ID); |
| 320 | |
| 321 | BIND_ENUM_CONSTANT(TYPE_NONE); |
| 322 | BIND_ENUM_CONSTANT(TYPE_IPV4); |
| 323 | BIND_ENUM_CONSTANT(TYPE_IPV6); |
| 324 | BIND_ENUM_CONSTANT(TYPE_ANY); |
| 325 | } |
| 326 | |
| 327 | IP *IP::get_singleton() { |
| 328 | return singleton; |
| 329 | } |
| 330 | |
| 331 | IP *(*IP::_create)() = nullptr; |
| 332 | |
| 333 | IP *IP::create() { |
| 334 | ERR_FAIL_COND_V_MSG(singleton, nullptr, "IP singleton already exists."); |
| 335 | ERR_FAIL_NULL_V(_create, nullptr); |
| 336 | return _create(); |
| 337 | } |
| 338 | |
| 339 | IP::IP() { |
| 340 | singleton = this; |
| 341 | resolver = memnew(_IP_ResolverPrivate); |
| 342 | |
| 343 | resolver->thread_abort.clear(); |
| 344 | resolver->thread.start(_IP_ResolverPrivate::_thread_function, resolver); |
| 345 | } |
| 346 | |
| 347 | IP::~IP() { |
| 348 | resolver->thread_abort.set(); |
| 349 | resolver->sem.post(); |
| 350 | resolver->thread.wait_to_finish(); |
| 351 | |
| 352 | memdelete(resolver); |
| 353 | } |