forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_resolve_udp6.cpp
74 lines (61 loc) · 1.92 KB
/
net_resolve_udp6.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @author github.com/luncliff (luncliff@gmail.com)
* @brief get a list of address for IPv6, UDP unspecified
*/
#undef NDEBUG
#include <array>
#include <cassert>
#include <coroutine/net.h>
using namespace std;
using namespace coro;
// see 'external/sockets'
void socket_setup() noexcept(false);
void socket_teardown() noexcept;
array<sockaddr_in6, 1> addresses{};
uint32_t resolve_udp_unspecified(addrinfo& hint) {
hint.ai_flags = AI_ALL | AI_NUMERICHOST | AI_NUMERICSERV;
size_t count = 0u;
if (const auto ec = get_address(hint, "::", "9283", addresses)) {
fputs(gai_strerror(ec), stderr);
return ec;
}
for (const sockaddr_in6& ep : addresses) {
assert(ep.sin6_family == AF_INET6);
assert(ep.sin6_port == htons(9283));
bool unspec = IN6_IS_ADDR_UNSPECIFIED(addressof(ep.sin6_addr));
assert(unspec);
++count;
}
assert(count > 0);
return EXIT_SUCCESS;
}
uint32_t resolve_udp_v4mapped(addrinfo& hint) {
hint.ai_flags = AI_ALL | AI_V4MAPPED | AI_NUMERICHOST | AI_NUMERICSERV;
size_t count = 0u;
if (const auto ec = get_address(hint, //
"::ffff:192.168.0.1", "9287", addresses)) {
fputs(gai_strerror(ec), stderr);
return ec;
}
for (const sockaddr_in6& ep : addresses) {
assert(ep.sin6_family == AF_INET6);
assert(ep.sin6_port == htons(9287));
bool v4mapped = IN6_IS_ADDR_V4MAPPED(addressof(ep.sin6_addr));
assert(v4mapped);
++count;
}
assert(count > 0);
return EXIT_SUCCESS;
}
int main(int, char* []) {
socket_setup();
auto on_return = gsl::finally([]() { socket_teardown(); });
addrinfo hint{};
hint.ai_family = AF_INET6;
hint.ai_socktype = SOCK_DGRAM;
if (auto ec = resolve_udp_unspecified(hint))
return ec;
if (auto ec = resolve_udp_v4mapped(hint))
return ec;
return EXIT_SUCCESS;
}