48b4c725
Peter M. Groen
Setting up Socket-pp
|
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
bool inet6_address::is_set() const
{
static const auto EMPTY_ADDR = sockaddr_in6{};
return std::memcmp(&addr_, &EMPTY_ADDR, SZ) != 0;
}
in6_addr inet6_address::resolve_name(const string& saddr)
{
#if !defined(_WIN32)
in6_addr ia;
if (::inet_pton(ADDRESS_FAMILY, saddr.c_str(), &ia) == 1)
return ia;
#endif
addrinfo *res, hints = addrinfo{};
hints.ai_family = ADDRESS_FAMILY;
hints.ai_socktype = SOCK_STREAM;
int gai_err = ::getaddrinfo(saddr.c_str(), NULL, &hints, &res);
#if !defined(_WIN32)
if (gai_err == EAI_SYSTEM)
throw sys_error();
#endif
if (gai_err != 0)
throw getaddrinfo_error(gai_err, saddr);
auto ipv6 = reinterpret_cast<sockaddr_in6*>(res->ai_addr);
auto addr = ipv6->sin6_addr;
freeaddrinfo(res);
return addr;
}
void inet6_address::create(const in6_addr& addr, in_port_t port)
{
addr_ = sockaddr_in6{};
addr_.sin6_family = AF_INET6;
addr_.sin6_flowinfo = 0;
addr_.sin6_addr = addr;
addr_.sin6_port = htons(port);
}
void inet6_address::create(const string& saddr, in_port_t port)
{
addr_ = sockaddr_in6{};
addr_.sin6_family = AF_INET6;
addr_.sin6_flowinfo = 0;
addr_.sin6_addr = resolve_name(saddr.c_str());
addr_.sin6_port = htons(port);
}
string inet6_address::to_string() const
{
char buf[INET6_ADDRSTRLEN];
auto str = inet_ntop(AF_INET6, (void*) &(addr_.sin6_addr),
buf, INET6_ADDRSTRLEN);
return std::string("[") + std::string(str ? str : "<unknown>")
+ "]:" + std::to_string(unsigned(port()));
}
ostream& operator<<(ostream& os, const inet6_address& addr)
{
char buf[INET6_ADDRSTRLEN];
auto str = inet_ntop(AF_INET6, (void*) &(addr.sockaddr_in6_ptr()->sin6_addr),
buf, INET6_ADDRSTRLEN);
os << "[" << (str ? str : "<unknown>") << "]:" << unsigned(addr.port());
return os;
}
|