-
Notifications
You must be signed in to change notification settings - Fork 23
/
dnsresolver.h
58 lines (50 loc) · 1.57 KB
/
dnsresolver.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
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef DNSRESOLVER_H
#define DNSRESOLVER_H
#include <netdb.h>
#include <arpa/inet.h>
#include <string>
#include <list>
#include <chrono>
#include "listener.h"
/**
* @brief A class for storing an address up to in6 size. So, it's used for IPv4 and IPv6.
*/
class FMQSockaddr_in6
{
struct sockaddr_in6 val;
std::string text;
public:
FMQSockaddr_in6(struct sockaddr *addr);
const struct sockaddr *getSockaddr() const;
socklen_t getSize() const;
void setPort(uint16_t port);
const std::string &getText() const;
int getFamily() const;
};
/**
* @brief The DnsResolver class does async DNS with getaddrinfo_a.
*
* Note that getaddrinfo_a uses threads. Had we known that, we would have probably rolled out our own. That
* would also have prevented the fork problem:
*
* It turns out that getaddrinfo_a is not compatible with fork(). If it's been used once, the forked process can't
* do DNS queries anymore. This is an issue for the test binary only at this point.
*/
class DnsResolver
{
struct gaicb lookup;
struct addrinfo request;
std::string curName;
std::chrono::time_point<std::chrono::steady_clock> getResultsTimeout;
void freeStuff();
public:
DnsResolver();
DnsResolver(const DnsResolver &other) = delete;
DnsResolver(DnsResolver &&other) = delete;
DnsResolver &operator=(const DnsResolver &other) = delete;
~DnsResolver();
void query(const std::string &text, ListenerProtocol protocol, std::chrono::milliseconds timeout);
std::list<FMQSockaddr_in6> getResult();
bool idle() const;
};
#endif // DNSRESOLVER_H