forked from TunSafe/TunSafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunsafe_dnsresolve.h
45 lines (38 loc) · 1015 Bytes
/
tunsafe_dnsresolve.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef TUNSAFE_DNSRESOLVE_H_
#define TUNSAFE_DNSRESOLVE_H_
#include "tunsafe_threading.h"
#include "tunsafe_ipaddr.h"
class DnsBlocker;
class DnsResolverCanceller {
public:
DnsResolverCanceller() : cancel_(false), cancel_sleep_once_(false) {}
void Cancel();
void CancelSleepOnce();
void Reset() { cancel_ = false; }
bool is_cancelled() { return cancel_; }
public:
bool cancel_;
bool cancel_sleep_once_;
ConditionVariable condvar_;
};
class DnsResolver {
public:
explicit DnsResolver(DnsBlocker *dns_blocker);
~DnsResolver();
bool Resolve(const char *hostname, IpAddr *result);
void ClearCache();
void RetryNow();
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_;
int retry_attempt_;
};
#endif // TUNSAFE_DNSRESOLVE_H_