-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
119 lines (96 loc) · 4.05 KB
/
main.cpp
File metadata and controls
119 lines (96 loc) · 4.05 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* This file is distributed under the MIT License.
* See "LICENSE" for details.
* Copyright 2023, Dennis Börm (allspark@wormhole.eu)
*/
#include <wormhole/sysinfo/NetlinkSocket.hpp>
#include <fmt/color.h>
#include <fmt/format.h>
#include <fmt/os.h>
using namespace wormhole::sysinfo;
outcome::std_result<void> example()
{
BOOST_OUTCOME_TRY(auto socket, Netlink::Socket::open({}));
BOOST_OUTCOME_TRY(auto id, socket.send_request<Netlink::Message::LinkRequest>(AF_UNSPEC));
fmt::print("Request id: {}\n", id);
BOOST_OUTCOME_TRY(auto response, socket.receive<Netlink::Message::LinkRequest>(Netlink::Socket::ReceiveMode::Wait));
fmt::print("Response id: {}\n", response.id);
fmt::print("{}\n", fmt::join(response.data, "\n"));
return outcome::success();
}
outcome::std_result<void> network_routes()
{
constexpr static Netlink::Socket::GroupList groups{Netlink::Socket::GroupIpV4Route{}, Netlink::Socket::GroupIpV6Route{}, Netlink::Socket::GroupIpV4Address{}, Netlink::Socket::GroupIpV6Address{}};
BOOST_OUTCOME_TRY(auto socket, Netlink::Socket::open(groups));
{
BOOST_OUTCOME_TRY(auto id, socket.send_request<Netlink::Message::LinkRequest>(AF_UNSPEC));
fmt::print("LinkRequest id: {}\n", id);
BOOST_OUTCOME_TRY(auto response, socket.receive<Netlink::Message::LinkRequest>(Netlink::Socket::ReceiveMode::Wait));
fmt::print("Response id: {}\n", response.id);
fmt::print("{}\n", fmt::join(response.data, "\n"));
}
{
BOOST_OUTCOME_TRY(auto id, socket.send_request<Netlink::Message::RouteRequest>(AF_INET));
fmt::print("RouteRequest IpV4 id: {}\n", id);
BOOST_OUTCOME_TRY(auto response, socket.receive<Netlink::Message::RouteRequest>(Netlink::Socket::ReceiveMode::Wait));
fmt::print("Response id: {}\n", response.id);
fmt::print("{}\n", fmt::join(response.data, "\n"));
}
{
BOOST_OUTCOME_TRY(auto id, socket.send_request<Netlink::Message::RouteRequest>(AF_INET6));
fmt::print("RouteRequest IpV6 id: {}\n", id);
BOOST_OUTCOME_TRY(auto response, socket.receive<Netlink::Message::RouteRequest>(Netlink::Socket::ReceiveMode::Wait));
fmt::print("Response id: {}\n", response.id);
fmt::print("{}\n", fmt::join(response.data, "\n"));
}
{
BOOST_OUTCOME_TRY(auto id, socket.send_request<Netlink::Message::AddressRequest>(AF_INET));
fmt::print("AddressRequest IpV6 id: {}\n", id);
BOOST_OUTCOME_TRY(auto response, socket.receive<Netlink::Message::AddressRequest>(Netlink::Socket::ReceiveMode::Wait));
fmt::print("Response id: {}\n", response.id);
fmt::print("{}\n", fmt::join(response.data, "\n"));
}
{
BOOST_OUTCOME_TRY(auto id, socket.send_request<Netlink::Message::AddressRequest>(AF_INET6));
fmt::print("AddressRequest IpV6 id: {}\n", id);
BOOST_OUTCOME_TRY(auto response, socket.receive<Netlink::Message::AddressRequest>(Netlink::Socket::ReceiveMode::Wait));
fmt::print("Response id: {}\n", response.id);
fmt::print("{}\n", fmt::join(response.data, "\n"));
}
std::size_t count{4};
do
{
fmt::print("listening to multicast groups ({})\n", count);
BOOST_OUTCOME_TRY(auto msg, socket.receive(Netlink::Socket::ReceiveMode::Wait));
std::visit(helper::overloaded{[](Address const& item)
{
fmt::print("{}\n", item);
},
[](Route const& item)
{
fmt::print("{}\n", item);
},
[](Interface const& item)
{
fmt::print("{}\n", item);
},
[](auto const& response)
{
fmt::print("Response id: {}\n", response.id);
fmt::print("{}\n", fmt::join(response.data, "\n"));
}},
msg);
--count;
} while (count > 0);
return outcome::success();
}
int main()
{
auto result = network_routes();
// auto result = example();
if (result.has_failure())
{
fmt::print(fmt::fg(fmt::color::red), "example failed: {}\n", result.as_failure().error().message());
return -1;
}
}