2018-10-21 09:00:15 -05:00
|
|
|
#ifndef TUNSAFE_DNSRESOLVE_H_
|
|
|
|
#define TUNSAFE_DNSRESOLVE_H_
|
|
|
|
|
|
|
|
#include "tunsafe_threading.h"
|
|
|
|
#include "tunsafe_ipaddr.h"
|
|
|
|
|
|
|
|
class DnsBlocker;
|
|
|
|
|
|
|
|
class DnsResolverCanceller {
|
|
|
|
public:
|
2018-10-19 06:40:39 -05:00
|
|
|
DnsResolverCanceller() : cancel_(false), cancel_sleep_once_(false) {}
|
2018-10-21 09:00:15 -05:00
|
|
|
void Cancel();
|
2018-10-19 06:40:39 -05:00
|
|
|
void CancelSleepOnce();
|
2018-10-21 09:00:15 -05:00
|
|
|
void Reset() { cancel_ = false; }
|
|
|
|
bool is_cancelled() { return cancel_; }
|
|
|
|
public:
|
|
|
|
bool cancel_;
|
2018-10-19 06:40:39 -05:00
|
|
|
bool cancel_sleep_once_;
|
2018-10-21 09:00:15 -05:00
|
|
|
ConditionVariable condvar_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DnsResolver {
|
|
|
|
public:
|
|
|
|
explicit DnsResolver(DnsBlocker *dns_blocker);
|
|
|
|
~DnsResolver();
|
|
|
|
|
|
|
|
bool Resolve(const char *hostname, IpAddr *result);
|
|
|
|
void ClearCache();
|
|
|
|
|
2018-10-19 06:40:39 -05:00
|
|
|
void RetryNow();
|
2018-10-21 09:00:15 -05:00
|
|
|
void Cancel() { token_.Cancel(); }
|
|
|
|
void ResetCancel() { token_.Reset(); }
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
std::string name;
|
|
|
|
IpAddr ip;
|
|
|
|
Entry(const std::string &name, const IpAddr &ip) : name(name), ip(ip) {}
|
|
|
|
};
|
|
|
|
std::vector<Entry> cache_;
|
|
|
|
DnsBlocker *dns_blocker_;
|
|
|
|
DnsResolverCanceller token_;
|
2018-10-19 06:40:39 -05:00
|
|
|
int retry_attempt_;
|
2018-10-21 09:00:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // TUNSAFE_DNSRESOLVE_H_
|