Repository navigation
Expand file tree
/
Copy pathinterface_watcher.cpp
More file actions
144 lines (127 loc) · 4.29 KB
/
Copy pathinterface_watcher.cpp
File metadata and controls
144 lines (127 loc) · 4.29 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: GPL-2.0-only WITH GoodNet-linking-exception
/// @file plugins/links/ice/interface_watcher.cpp
#include "interface_watcher.hpp"
#include <asio/bind_executor.hpp>
#include <asio/post.hpp>
#include <cerrno>
#include <cstring>
#include <system_error>
#ifdef __linux__
# include <linux/netlink.h>
# include <linux/rtnetlink.h>
# include <sys/socket.h>
# include <unistd.h>
# include <poll.h>
# include <fcntl.h>
#endif
namespace gn::link::ice {
InterfaceWatcher::InterfaceWatcher(asio::io_context& io,
std::chrono::milliseconds debounce,
Callback on_change)
: io_(io),
debounce_(debounce),
on_change_(std::move(on_change)),
debounce_timer_(io) {}
InterfaceWatcher::~InterfaceWatcher() {
stop();
}
bool InterfaceWatcher::start() {
#ifdef __linux__
if (started_.exchange(true)) return true;
const int fd = ::socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
if (fd < 0) {
started_.store(false);
return false;
}
sockaddr_nl addr{};
addr.nl_family = AF_NETLINK;
addr.nl_groups = RTMGRP_LINK
| RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR;
if (::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
::close(fd);
sock_.store(-1, std::memory_order_release);
started_.store(false);
return false;
}
sock_.store(fd, std::memory_order_release);
running_.store(true, std::memory_order_release);
reader_ = std::thread([this] { run_loop(); });
return true;
#else
(void)on_change_;
return false;
#endif
}
void InterfaceWatcher::stop() {
#ifdef __linux__
if (!started_.load()) return;
running_.store(false, std::memory_order_release);
/// Swap the fd out atomically so the reader thread's next
/// `pfd.fd = sock_.load(...)` snapshot sees -1, and `::shutdown` /
/// `::close` only run once even if `stop` is invoked concurrently.
const int fd = sock_.exchange(-1, std::memory_order_acq_rel);
if (fd >= 0) {
::shutdown(fd, SHUT_RDWR);
::close(fd);
}
if (reader_.joinable()) reader_.join();
debounce_timer_.cancel();
started_.store(false);
#endif
}
#ifdef __linux__
void InterfaceWatcher::run_loop() {
/// Read in fixed-size chunks; the kernel rounds messages up to
/// netlink alignment so a single recv may carry multiple events.
/// We don't need the contents — any RTM_NEW* / RTM_DEL* on the
/// subscribed groups is enough to trigger a debounced re-gather.
constexpr std::size_t kBufSize = 8192;
std::vector<std::uint8_t> buf(kBufSize);
while (running_.load(std::memory_order_acquire)) {
const int fd = sock_.load(std::memory_order_acquire);
if (fd < 0) break;
struct pollfd pfd{};
pfd.fd = fd;
pfd.events = POLLIN;
const int pr = ::poll(&pfd, 1, 500);
if (pr <= 0) continue;
if (!(pfd.revents & POLLIN)) continue;
const ssize_t got = ::recv(fd, buf.data(), buf.size(), 0);
if (got <= 0) continue;
bool relevant = false;
auto* nh = reinterpret_cast<struct nlmsghdr*>(buf.data());
unsigned int remaining = static_cast<unsigned int>(got);
for (; NLMSG_OK(nh, remaining)
; nh = NLMSG_NEXT(nh, remaining)) {
if (nh->nlmsg_type == NLMSG_DONE) break;
if (nh->nlmsg_type == NLMSG_ERROR) continue;
switch (nh->nlmsg_type) {
case RTM_NEWLINK:
case RTM_DELLINK:
case RTM_NEWADDR:
case RTM_DELADDR:
relevant = true;
break;
default:
break;
}
}
if (relevant) {
schedule_callback();
}
}
}
void InterfaceWatcher::schedule_callback() {
asio::post(io_, [this] {
if (!running_.load(std::memory_order_acquire)) return;
debounce_timer_.expires_after(debounce_);
debounce_timer_.async_wait([this](const std::error_code& wait_ec) {
if (wait_ec) return;
if (!running_.load(std::memory_order_acquire)) return;
if (on_change_) on_change_();
});
});
}
#endif
} // namespace gn::link::ice