-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.cpp
57 lines (48 loc) · 1.6 KB
/
init.cpp
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
#include <bitcoin/protocol.h>
#include "init.h"
#include "addrseed.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string>
#include <vector>
#include <arpa/inet.h>
bool lookup(const char *hostname, std::vector<CService> &addrs, uint16_t port=8333)
{
struct addrinfo hint;
bzero(&hint, sizeof(hint));
hint.ai_family = PF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
hint.ai_flags = AI_ADDRCONFIG;
struct addrinfo *results;
int error = getaddrinfo(hostname, nullptr, &hint, &results);
if (error) {
printf("getaddrinfo for %s failed: %s\n", hostname, gai_strerror(error));
return false;
}
for (auto result = results; result != nullptr; result = result->ai_next) {
if (result->ai_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)result->ai_addr;
addrs.push_back(CService(sin->sin_addr, port));
} else if (result->ai_family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)result->ai_addr;
addrs.push_back(CService(sin6->sin6_addr, port));
}
}
freeaddrinfo(results);
return true;
}
void initDNSSeedAddr(const std::vector<std::string> &seedNodes)
{
std::vector<CService> addrs;
for (auto node: seedNodes) {
lookup(node.c_str(), addrs);
}
// struct in_addr inaddr;
// inet_pton(AF_INET, "192.168.0.5", &inaddr);
// addrs.push_back(CService(CNetAddr(inaddr), 8333));
for (auto addr: addrs) {
CAddrSeed::getInstance().addNewAddr(addr);
}
}