2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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"
|
|
|
|
|
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace td {
|
2019-01-24 18:08:29 +01:00
|
|
|
|
|
|
|
class GetHostByNameActor final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2019-01-24 18:21:00 +01:00
|
|
|
enum class ResolveType { Native, Google, All };
|
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-01-24 18:21:00 +01:00
|
|
|
ResolveType type{ResolveType::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-01-24 14:07:11 +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
|
|
|
|
2019-01-24 14:07:11 +01:00
|
|
|
struct ResolveOptions {
|
2019-01-24 18:21:00 +01:00
|
|
|
ResolveType type{ResolveType::Native};
|
2019-01-24 18:08:29 +01:00
|
|
|
bool prefer_ipv6{false};
|
2019-02-04 00:29:37 +01:00
|
|
|
int32 scheduler_id{-1};
|
2019-01-24 13:18:23 +01:00
|
|
|
};
|
2019-01-24 14:07:11 +01:00
|
|
|
static TD_WARN_UNUSED_RESULT ActorOwn<> resolve(std::string host, ResolveOptions options, Promise<IPAddress> promise);
|
2019-01-24 13:18:23 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
struct Value {
|
2019-01-24 18:08:29 +01:00
|
|
|
Result<IPAddress> ip;
|
2018-12-31 20:04:05 +01:00
|
|
|
double expire_at;
|
2018-06-27 18:24:42 +02:00
|
|
|
|
2019-01-24 13:18:23 +01:00
|
|
|
ActorOwn<> query;
|
|
|
|
std::vector<std::pair<int, Promise<IPAddress>>> promises;
|
|
|
|
|
2019-01-24 18:08:29 +01:00
|
|
|
Value(Result<IPAddress> ip, double expire_at) : ip(std::move(ip)), expire_at(expire_at) {
|
2018-06-27 18:24:42 +02:00
|
|
|
}
|
2019-01-24 13:18:23 +01:00
|
|
|
|
2019-01-24 18:08:29 +01:00
|
|
|
Result<IPAddress> get_ip_port(int port) {
|
2019-01-24 13:18:23 +01:00
|
|
|
auto res = ip.clone();
|
|
|
|
if (res.is_ok()) {
|
|
|
|
res.ok_ref().set_port(port);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
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-01-24 14:07:11 +01:00
|
|
|
Options options_;
|
2018-07-01 03:12:20 +02:00
|
|
|
|
2019-01-24 13:18:23 +01:00
|
|
|
void on_result(std::string host, bool prefer_ipv6, Result<IPAddress> res);
|
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
|