master
h 60 lines 2.14 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2 #pragma once
3
4 #include <functional>
5 #include "NetlinkChannel.h"
6 #include "Rule.h"
7 #include "Operation.h"
8
9 struct RuleMessage
10 {
11 rtmsg rule;
12 utils::IntegerAttribute tableId;
13 } __attribute__((packed));
14
15 // The below a means to ensure that messages have a common set of fields. It just means
16 // that a type T must inherit from RuleMessage, and any functions that reference
17 // DerivedRuleMessage can be assured that they can safely access the fields in RuleMessage.
18 template <typename TMessage>
19 concept DerivedRuleMessage = std::is_base_of<RuleMessage, TMessage>::value;
20
21 class IpRuleManager
22 {
23 public:
24 /*
25 Implements netlink equivalent of "ip rule <operation> iif <interface> ipproto <protocol> prio <priority> table <table>".
26 */
27 void ModifyLoopbackRule(const Rule& rule, Operation operation);
28
29 /*
30 Implements netlink equivalent of "ip rule <operation> from <source IP> iif lo ipproto <protocol> prio <priority> table <table>".
31 */
32 void ModifyLoopbackRuleWithSourceAddress(const Rule& rule, Operation operation);
33
34 /*
35 Implements netlink equivalent of "ip rule <operation> ipproto <protocol> prio <priority> table <table>".
36 */
37 void ModifyRoutingTablePriorityWithProtocol(const Rule& rule, Operation operation);
38
39 /*
40 Implements netlink equivalent of "ip rule <operation> prio <priority> table <table>".
41 */
42 void ModifyRoutingTablePriority(const Rule& rule, Operation operation);
43
44 /*
45 Implements netlink equivalent of "ip rule show".
46 */
47 std::vector<Rule> ListRules(int family = AF_INET, int tableId = RT_TABLE_UNSPEC);
48
49 private:
50 /*
51 Creates the message using the routine, then sends the message via netlink.
52 */
53 template <DerivedRuleMessage TMessage>
54 void SendMessage(unsigned char family, int routingTable, int operation, int flags, const std::function<void(TMessage&)>& routine = [](auto&) {});
55
56 template <typename TAddr>
57 void ModifyLoopbackRuleWithSourceAddressImpl(const Rule& rule, Operation operation);
58
59 NetlinkChannel m_channel;
60 };