2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2019-02-04 15:02:21 +01:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
2019-02-12 17:48:52 +01:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2019-01-24 18:08:29 +01:00
|
|
|
|
2019-02-04 15:02:21 +01:00
|
|
|
extern int VERBOSITY_NAME(dns_resolver);
|
|
|
|
|
2019-01-24 18:08:29 +01:00
|
|
|
class GetHostByNameActor final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2019-02-04 02:36:13 +01:00
|
|
|
enum class ResolverType { Native, Google };
|
2019-02-04 00:29:37 +01:00
|
|
|
|
2019-01-24 14:07:11 +01:00
|
|
|
struct Options {
|
2019-02-04 00:29:37 +01:00
|
|
|
static constexpr int32 DEFAULT_CACHE_TIME = 60 * 29; // 29 minutes
|
|
|
|
static constexpr int32 DEFAULT_ERROR_CACHE_TIME = 60 * 5; // 5 minutes
|
|
|
|
|
2019-02-04 02:36:13 +01:00
|
|
|
vector<ResolverType> resolver_types{ResolverType::Native};
|
2019-02-04 00:29:37 +01:00
|
|
|
int32 scheduler_id{-1};
|
|
|
|
int32 ok_timeout{DEFAULT_CACHE_TIME};
|
|
|
|
int32 error_timeout{DEFAULT_ERROR_CACHE_TIME};
|
2019-01-24 14:07:11 +01:00
|
|
|
};
|
2019-02-04 00:29:37 +01:00
|
|
|
|
2019-02-04 01:12:40 +01:00
|
|
|
explicit GetHostByNameActor(Options options);
|
2019-02-04 00:29:37 +01:00
|
|
|
|
2019-01-24 18:08:29 +01:00
|
|
|
void run(std::string host, int port, bool prefer_ipv6, Promise<IPAddress> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2019-02-04 03:39:20 +01:00
|
|
|
void on_query_result(std::string host, bool prefer_ipv6, Result<IPAddress> result);
|
2019-02-04 01:19:31 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct Value {
|
2019-01-24 18:08:29 +01:00
|
|
|
Result<IPAddress> ip;
|
2019-02-12 17:48:52 +01:00
|
|
|
double expires_at;
|
2018-06-27 18:24:42 +02:00
|
|
|
|
2019-02-12 17:48:52 +01:00
|
|
|
Value(Result<IPAddress> ip, double expires_at) : ip(std::move(ip)), expires_at(expires_at) {
|
2018-06-27 18:24:42 +02:00
|
|
|
}
|
2019-01-24 13:18:23 +01:00
|
|
|
|
2019-02-04 01:12:40 +01:00
|
|
|
Result<IPAddress> get_ip_port(int port) const {
|
2019-02-04 03:39:20 +01:00
|
|
|
auto result = ip.clone();
|
|
|
|
if (result.is_ok()) {
|
|
|
|
result.ok_ref().set_port(port);
|
2019-01-24 13:18:23 +01:00
|
|
|
}
|
2019-02-04 03:39:20 +01:00
|
|
|
return result;
|
2019-01-24 13:18:23 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-07-01 03:12:20 +02:00
|
|
|
std::unordered_map<string, Value> cache_[2];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-04 01:19:31 +01:00
|
|
|
struct Query {
|
|
|
|
ActorOwn<> query;
|
2019-02-04 02:32:32 +01:00
|
|
|
size_t pos = 0;
|
2019-02-04 03:39:20 +01:00
|
|
|
string real_host;
|
|
|
|
double begin_time;
|
2019-02-04 01:19:31 +01:00
|
|
|
std::vector<std::pair<int, Promise<IPAddress>>> promises;
|
|
|
|
};
|
|
|
|
std::unordered_map<string, Query> active_queries_[2];
|
2018-07-01 03:12:20 +02:00
|
|
|
|
2019-02-04 01:19:31 +01:00
|
|
|
Options options_;
|
2019-02-04 02:32:32 +01:00
|
|
|
|
|
|
|
void run_query(std::string host, bool prefer_ipv6, Query &query);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-10-15 10:23:51 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|