-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrepd.cpp
More file actions
2402 lines (1868 loc) · 60.9 KB
/
Copy pathtrepd.cpp
File metadata and controls
2402 lines (1868 loc) · 60.9 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
#include <array>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
namespace {
constexpr std::uint16_t default_port = 4242;
constexpr std::uint32_t default_distance = 1200;
constexpr std::uint8_t trep_version = 1;
constexpr std::uint8_t trep_route_protocol = 100;
constexpr int reconnect_seconds = 2;
constexpr int connect_timeout_ms = 5000;
constexpr int io_timeout_seconds = 5;
constexpr int tcp_user_timeout_ms = 10000;
constexpr int netlink_timeout_seconds = 5;
constexpr int netlink_dump_attempts = 3;
constexpr int debounce_ms = 250;
volatile sig_atomic_t stop_requested = 0;
bool debug_logging = false;
bool quiet_logging = false;
template <typename... Args>
void log_info(Args&&... args) {
if (quiet_logging) return;
(std::cerr << ... << std::forward<Args>(args)) << "\n";
}
template <typename... Args>
void log_debug(Args&&... args) {
if (quiet_logging or not debug_logging) return;
(std::cerr << ... << std::forward<Args>(args)) << "\n";
}
void on_signal(int) {
stop_requested = 1;
}
[[noreturn]] void fail_errno(const std::string& what) {
throw std::runtime_error(
what + ": " + std::strerror(errno));
}
class ScopedFd {
public:
explicit ScopedFd(int fd = -1) : fd_(fd) {}
~ScopedFd() {
reset();
}
ScopedFd(const ScopedFd&) = delete;
ScopedFd& operator=(const ScopedFd&) = delete;
ScopedFd(ScopedFd&& other) noexcept : fd_(other.release()) {}
ScopedFd& operator=(ScopedFd&& other) noexcept {
if (this != &other) {
reset(other.release());
}
return *this;
}
int get() const { return fd_; }
int release() {
const int fd = fd_;
fd_ = -1;
return fd;
}
void reset(int fd = -1) {
if (fd_ >= 0) {
::close(fd_);
}
fd_ = fd;
}
private:
int fd_ = -1;
};
struct Address {
int family = AF_UNSPEC;
in_addr ipv4 {};
in6_addr ipv6 {};
};
struct Prefix4 {
std::uint32_t network_be = 0;
std::uint8_t length = 0;
};
struct Prefix6 {
in6_addr network {};
std::uint8_t length = 0;
};
struct ExportPolicy4 {
std::vector<Prefix4> filters;
std::vector<Prefix4> exact_routes;
std::vector<Prefix4> export_filters;
std::vector<Prefix4> import_filters;
bool export_connected = false;
bool export_static = false;
bool export_default = false;
};
struct ExportPolicy6 {
std::vector<Prefix6> filters;
std::vector<Prefix6> exact_routes;
std::vector<Prefix6> export_filters;
std::vector<Prefix6> import_filters;
bool export_connected = false;
bool export_static = false;
bool export_default = false;
};
enum class MessageType : std::uint8_t {
hello = 1,
sync_begin = 2,
route4 = 3,
route6 = 4,
sync_end = 5,
};
#pragma pack(push, 1)
struct WireHeader {
char magic[4];
std::uint8_t version;
std::uint8_t type;
std::uint16_t payload_length_be;
};
struct WireRoute4 {
std::uint8_t prefix_length;
std::uint8_t reserved[3];
std::uint32_t network_be;
};
struct WireRoute6 {
std::uint8_t prefix_length;
std::uint8_t reserved[3];
in6_addr network;
};
#pragma pack(pop)
static_assert(sizeof(WireHeader) == 8);
static_assert(sizeof(WireRoute4) == 8);
static_assert(sizeof(WireRoute6) == 20);
std::uint32_t prefix_mask4(std::uint8_t length) {
if (length == 0) {
return 0;
}
return htonl(0xffffffffU << (32U - length));
}
void mask_prefix6(in6_addr& address, std::uint8_t length) {
for (unsigned byte = 0; byte < 16; ++byte) {
const unsigned bit_offset = byte * 8;
if (length >= bit_offset + 8) {
continue;
}
if (length <= bit_offset) {
address.s6_addr[byte] = 0;
continue;
}
const unsigned keep_bits = length - bit_offset;
const auto mask = static_cast<std::uint8_t>(
0xffU << (8U - keep_bits));
address.s6_addr[byte] &= mask;
}
}
Address parse_address(const std::string& text) {
Address address;
if (::inet_pton(AF_INET, text.c_str(), &address.ipv4) == 1) {
address.family = AF_INET;
return address;
}
if (::inet_pton(AF_INET6, text.c_str(), &address.ipv6) == 1) {
address.family = AF_INET6;
return address;
}
throw std::runtime_error("invalid address: " + text);
}
std::uint64_t parse_unsigned(
const std::string& text,
std::uint64_t maximum,
const std::string& what) {
if (
text.empty() or
text.find_first_not_of("0123456789") != std::string::npos) {
throw std::runtime_error("bad " + what + ": " + text);
}
std::uint64_t value = 0;
try {
value = std::stoull(text);
} catch (const std::exception&) {
throw std::runtime_error("bad " + what + ": " + text);
}
if (value > maximum) {
throw std::runtime_error("bad " + what + ": " + text);
}
return value;
}
std::string address_to_string(const Address& address) {
char buffer[INET6_ADDRSTRLEN] {};
const void* data =
address.family == AF_INET
? static_cast<const void*>(&address.ipv4)
: static_cast<const void*>(&address.ipv6);
if (::inet_ntop(
address.family,
data,
buffer,
sizeof(buffer)) == nullptr) {
return "?";
}
return buffer;
}
std::string ipv4_to_string(std::uint32_t address_be) {
in_addr address {};
address.s_addr = address_be;
char buffer[INET_ADDRSTRLEN] {};
if (::inet_ntop(
AF_INET,
&address,
buffer,
sizeof(buffer)) == nullptr) {
return "?";
}
return buffer;
}
std::string ipv6_to_string(const in6_addr& address) {
char buffer[INET6_ADDRSTRLEN] {};
if (::inet_ntop(
AF_INET6,
&address,
buffer,
sizeof(buffer)) == nullptr) {
return "?";
}
return buffer;
}
int compare_addresses(const Address& left, const Address& right) {
if (left.family != right.family) {
throw std::runtime_error("local/peer family mismatch");
}
if (left.family == AF_INET) {
const std::uint32_t left_value = ntohl(left.ipv4.s_addr);
const std::uint32_t right_value = ntohl(right.ipv4.s_addr);
if (left_value < right_value) {
return -1;
}
if (left_value > right_value) {
return 1;
}
return 0;
}
return std::memcmp(
&left.ipv6,
&right.ipv6,
sizeof(in6_addr));
}
Prefix4 parse_prefix4(const std::string& text) {
const auto slash = text.find('/');
if (slash == std::string::npos) {
throw std::runtime_error("prefix needs /len: " + text);
}
in_addr address {};
if (::inet_pton(
AF_INET,
text.substr(0, slash).c_str(),
&address) != 1) {
throw std::runtime_error("bad IPv4 prefix: " + text);
}
const auto length = parse_unsigned(
text.substr(slash + 1), 32, "IPv4 prefix length");
Prefix4 prefix;
prefix.length = static_cast<std::uint8_t>(length);
prefix.network_be =
address.s_addr & prefix_mask4(prefix.length);
return prefix;
}
Prefix6 parse_prefix6(const std::string& text) {
const auto slash = text.find('/');
if (slash == std::string::npos) {
throw std::runtime_error("prefix needs /len: " + text);
}
Prefix6 prefix;
if (::inet_pton(
AF_INET6,
text.substr(0, slash).c_str(),
&prefix.network) != 1) {
throw std::runtime_error("bad IPv6 prefix: " + text);
}
const auto length = parse_unsigned(
text.substr(slash + 1), 128, "IPv6 prefix length");
prefix.length = static_cast<std::uint8_t>(length);
mask_prefix6(prefix.network, prefix.length);
return prefix;
}
std::string prefix_to_string(const Prefix4& prefix) {
return
ipv4_to_string(prefix.network_be) +
"/" +
std::to_string(prefix.length);
}
std::string prefix_to_string(const Prefix6& prefix) {
return
ipv6_to_string(prefix.network) +
"/" +
std::to_string(prefix.length);
}
std::uint64_t prefix_key(const Prefix4& prefix) {
return
(static_cast<std::uint64_t>(
ntohl(prefix.network_be)) << 8U) |
prefix.length;
}
std::string prefix_key(const Prefix6& prefix) {
std::string key(
reinterpret_cast<const char*>(prefix.network.s6_addr),
16);
key.push_back(static_cast<char>(prefix.length));
return key;
}
bool prefix_equal(const Prefix4& left, const Prefix4& right) {
return
left.length == right.length and
left.network_be == right.network_be;
}
bool prefix_equal(const Prefix6& left, const Prefix6& right) {
return
left.length == right.length and
std::memcmp(
&left.network,
&right.network,
sizeof(in6_addr)) == 0;
}
bool prefix_is_within(const Prefix4& prefix, const Prefix4& filter) {
if (prefix.length < filter.length) {
return false;
}
return
(prefix.network_be & prefix_mask4(filter.length)) ==
filter.network_be;
}
bool prefix_is_within(const Prefix6& prefix, const Prefix6& filter) {
if (prefix.length < filter.length) {
return false;
}
in6_addr masked = prefix.network;
mask_prefix6(masked, filter.length);
return
std::memcmp(
&masked,
&filter.network,
sizeof(in6_addr)) == 0;
}
class Netlink {
public:
explicit Netlink(unsigned interface_index)
: interface_index_(interface_index) {
ScopedFd request_socket(
::socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE));
if (request_socket.get() < 0) {
fail_errno("netlink socket");
}
timeval netlink_timeout {
netlink_timeout_seconds,
0,
};
if (::setsockopt(
request_socket.get(),
SOL_SOCKET,
SO_RCVTIMEO,
&netlink_timeout,
sizeof(netlink_timeout)) != 0) {
fail_errno("netlink SO_RCVTIMEO");
}
if (::setsockopt(
request_socket.get(),
SOL_SOCKET,
SO_SNDTIMEO,
&netlink_timeout,
sizeof(netlink_timeout)) != 0) {
fail_errno("netlink SO_SNDTIMEO");
}
ScopedFd watch_socket(
::socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE));
if (watch_socket.get() < 0) {
fail_errno("netlink watch socket");
}
sockaddr_nl address {};
address.nl_family = AF_NETLINK;
address.nl_groups =
RTMGRP_IPV4_ROUTE |
RTMGRP_IPV6_ROUTE;
if (::bind(
watch_socket.get(),
reinterpret_cast<sockaddr*>(&address),
sizeof(address)) != 0) {
fail_errno("netlink watch bind");
}
request_fd_ = request_socket.release();
watch_fd_ = watch_socket.release();
}
~Netlink() {
if (request_fd_ >= 0) {
::close(request_fd_);
}
if (watch_fd_ >= 0) {
::close(watch_fd_);
}
}
int watch_fd() const {
return watch_fd_;
}
void set_interface_index(unsigned interface_index) {
interface_index_ = interface_index;
}
void drain_watch() {
std::array<char, 16384> buffer {};
while (true) {
const ssize_t received = ::recv(
watch_fd_,
buffer.data(),
buffer.size(),
MSG_DONTWAIT);
if (received > 0) {
continue;
}
if (
received < 0 and
errno == EINTR) {
continue;
}
return;
}
}
std::vector<Prefix4> dump_routes(const ExportPolicy4& policy) {
std::vector<Prefix4> result;
std::unordered_set<std::uint64_t> seen;
bool complete = false;
for (int attempt = 0; attempt < netlink_dump_attempts; ++attempt) {
result.clear();
seen.clear();
complete = dump_routes(
AF_INET,
[&](const rtmsg& route, const std::uint8_t* data, int length) {
if (
route.rtm_family != AF_INET or
route.rtm_table != RT_TABLE_MAIN or
route.rtm_type != RTN_UNICAST or
route.rtm_protocol == trep_route_protocol) {
return;
}
if (route_uses_interface(route, data, length)) {
return;
}
Prefix4 prefix;
prefix.length = route.rtm_dst_len;
for_each_attribute(
data,
length,
[&](const rtattr& attribute) {
if (
attribute.rta_type == RTA_DST and
RTA_PAYLOAD(&attribute) >=
sizeof(prefix.network_be)) {
std::memcpy(
&prefix.network_be,
RTA_DATA(
const_cast<rtattr*>(&attribute)),
sizeof(prefix.network_be));
}
});
prefix.network_be &= prefix_mask4(prefix.length);
if (
not matches_policy(route, prefix, policy) or
not matches_export_filter(prefix, policy.export_filters)) {
return;
}
if (seen.insert(prefix_key(prefix)).second) {
result.push_back(prefix);
}
});
if (complete) {
break;
}
log_debug("trepd: interrupted IPv4 route dump, retrying");
}
if (not complete) {
throw std::runtime_error(
"netlink IPv4 route dump repeatedly interrupted");
}
for (const Prefix4& prefix : policy.exact_routes) {
if (seen.insert(prefix_key(prefix)).second) {
result.push_back(prefix);
}
}
return result;
}
std::vector<Prefix6> dump_routes(const ExportPolicy6& policy) {
std::vector<Prefix6> result;
std::unordered_set<std::string> seen;
bool complete = false;
for (int attempt = 0; attempt < netlink_dump_attempts; ++attempt) {
result.clear();
seen.clear();
complete = dump_routes(
AF_INET6,
[&](const rtmsg& route, const std::uint8_t* data, int length) {
if (
route.rtm_family != AF_INET6 or
route.rtm_table != RT_TABLE_MAIN or
route.rtm_type != RTN_UNICAST or
route.rtm_protocol == trep_route_protocol) {
return;
}
if (route_uses_interface(route, data, length)) {
return;
}
Prefix6 prefix;
prefix.length = route.rtm_dst_len;
for_each_attribute(
data,
length,
[&](const rtattr& attribute) {
if (
attribute.rta_type == RTA_DST and
RTA_PAYLOAD(&attribute) >=
sizeof(prefix.network)) {
std::memcpy(
&prefix.network,
RTA_DATA(
const_cast<rtattr*>(&attribute)),
sizeof(prefix.network));
}
});
mask_prefix6(prefix.network, prefix.length);
if (
not matches_policy(route, prefix, policy) or
not matches_export_filter(prefix, policy.export_filters)) {
return;
}
if (seen.insert(prefix_key(prefix)).second) {
result.push_back(prefix);
}
});
if (complete) {
break;
}
log_debug("trepd: interrupted IPv6 route dump, retrying");
}
if (not complete) {
throw std::runtime_error(
"netlink IPv6 route dump repeatedly interrupted");
}
for (const Prefix6& prefix : policy.exact_routes) {
if (seen.insert(prefix_key(prefix)).second) {
result.push_back(prefix);
}
}
return result;
}
void add_route(const Prefix4& prefix, std::uint32_t priority) {
modify_route(
AF_INET,
prefix.length,
&prefix.network_be,
sizeof(prefix.network_be),
RTM_NEWROUTE,
NLM_F_REQUEST |
NLM_F_ACK |
NLM_F_CREATE |
NLM_F_EXCL,
priority);
}
void add_route(const Prefix6& prefix, std::uint32_t priority) {
modify_route(
AF_INET6,
prefix.length,
&prefix.network,
sizeof(prefix.network),
RTM_NEWROUTE,
NLM_F_REQUEST |
NLM_F_ACK |
NLM_F_CREATE |
NLM_F_EXCL,
priority);
}
void delete_route(const Prefix4& prefix, std::uint32_t priority) {
modify_route(
AF_INET,
prefix.length,
&prefix.network_be,
sizeof(prefix.network_be),
RTM_DELROUTE,
NLM_F_REQUEST | NLM_F_ACK,
priority);
}
void delete_route(const Prefix6& prefix, std::uint32_t priority) {
modify_route(
AF_INET6,
prefix.length,
&prefix.network,
sizeof(prefix.network),
RTM_DELROUTE,
NLM_F_REQUEST | NLM_F_ACK,
priority);
}
private:
bool route_uses_interface(
const rtmsg& route,
const std::uint8_t* data,
int length) const {
if (route.rtm_protocol != RTPROT_KERNEL) {
return false;
}
auto* attribute = reinterpret_cast<const rtattr*>(data);
while (RTA_OK(attribute, length)) {
if (
attribute->rta_type == RTA_OIF and
RTA_PAYLOAD(attribute) >= sizeof(interface_index_)) {
unsigned output_interface = 0;
std::memcpy(
&output_interface,
RTA_DATA(attribute),
sizeof(output_interface));
return output_interface == interface_index_;
}
attribute = RTA_NEXT(attribute, length);
}
return false;
}
bool matches_export_filter(
const Prefix4& prefix,
const std::vector<Prefix4>& filters) {
if (filters.empty()) {
return true;
}
for (const Prefix4& filter : filters) {
if (prefix_is_within(prefix, filter)) {
return true;
}
}
return false;
}
bool matches_export_filter(
const Prefix6& prefix,
const std::vector<Prefix6>& filters) {
if (filters.empty()) {
return true;
}
for (const Prefix6& filter : filters) {
if (prefix_is_within(prefix, filter)) {
return true;
}
}
return false;
}
template<typename Fn>
void for_each_attribute(
const std::uint8_t* data,
int length,
Fn&& callback) {
auto* attribute = reinterpret_cast<rtattr*>(
const_cast<std::uint8_t*>(data));
while (RTA_OK(attribute, length)) {
callback(*attribute);
attribute = RTA_NEXT(attribute, length);
}
}
bool matches_policy(
const rtmsg& route,
const Prefix4& prefix,
const ExportPolicy4& policy) {
if (prefix.length == 0) {
if (policy.export_default) {
return true;
}
for (const Prefix4& exact : policy.exact_routes) {
if (prefix_equal(prefix, exact)) {
return true;
}
}
return false;
}
bool accepted = false;
if (
policy.export_static and
route.rtm_scope != RT_SCOPE_LINK and
(
route.rtm_protocol == RTPROT_STATIC or
route.rtm_protocol == RTPROT_BOOT or
route.rtm_protocol == RTPROT_UNSPEC)) {
accepted = true;
}
if (
not accepted and
policy.export_connected and
route.rtm_protocol == RTPROT_KERNEL) {
accepted = true;
}
if (not accepted) {
for (const Prefix4& filter : policy.filters) {
if (prefix_is_within(prefix, filter)) {
accepted = true;
break;
}
}
}
return accepted;
}
bool matches_policy(
const rtmsg& route,
const Prefix6& prefix,
const ExportPolicy6& policy) {
if (prefix.length == 0) {
if (policy.export_default) {
return true;
}
for (const Prefix6& exact : policy.exact_routes) {
if (prefix_equal(prefix, exact)) {
return true;
}
}
return false;
}
bool accepted = false;
if (
policy.export_static and
route.rtm_scope != RT_SCOPE_LINK and
(
route.rtm_protocol == RTPROT_STATIC or
route.rtm_protocol == RTPROT_BOOT or
route.rtm_protocol == RTPROT_UNSPEC)) {
accepted = true;
}
if (
not accepted and
policy.export_connected and
route.rtm_protocol == RTPROT_KERNEL) {
accepted = true;
}
if (not accepted) {
for (const Prefix6& filter : policy.filters) {
if (prefix_is_within(prefix, filter)) {
accepted = true;
break;
}
}
}
return accepted;
}
template<typename Fn>
bool dump_routes(int family, Fn&& callback) {
struct Request {
nlmsghdr header {};
rtmsg route {};
} request;
request.header.nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg));
request.header.nlmsg_type = RTM_GETROUTE;
request.header.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
request.header.nlmsg_seq = ++sequence_;
request.route.rtm_family = static_cast<std::uint8_t>(family);
request.route.rtm_table = RT_TABLE_MAIN;
send_netlink(&request, request.header.nlmsg_len);
bool done = false;
bool interrupted = false;
while (not done) {
std::array<std::uint8_t, 16384> buffer {};
const ssize_t received = ::recv(
request_fd_,
buffer.data(),
buffer.size(),
0);
if (received < 0) {
if (errno == EINTR) {
continue;
}
fail_errno("netlink dump recv");
}
int remaining = static_cast<int>(received);
for (
auto* header = reinterpret_cast<nlmsghdr*>(buffer.data());
NLMSG_OK(header, remaining);
header = NLMSG_NEXT(header, remaining)) {
if (header->nlmsg_seq != sequence_) {
continue;
}