-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathRoutingTable.h
More file actions
80 lines (61 loc) · 2.64 KB
/
RoutingTable.h
File metadata and controls
80 lines (61 loc) · 2.64 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
// Copyright (C) Microsoft Corporation. All rights reserved.
#pragma once
#include <functional>
#include "NetlinkChannel.h"
#include "Route.h"
#include "Operation.h"
struct RouteMessage
{
rtmsg route;
utils::IntegerAttribute tableId;
utils::IntegerAttribute dev;
} __attribute__((packed));
// The below is a means to ensure that messages have a common set of fields. It just means
// that a type T must inherit from RouteMessage, and any functions that reference
// DerivedRouteMessage can be assured that they can safely access the fields in RouteMessage.
template <typename TMessage>
concept DerivedRouteMessage = std::is_base_of<RouteMessage, TMessage>::value;
class RoutingTable
{
public:
RoutingTable(int table);
RoutingTable(const RoutingTable&) = delete;
RoutingTable(RoutingTable&&) = delete;
const RoutingTable& operator=(const RoutingTable&) const = delete;
const RoutingTable& operator=(RoutingTable&&) const = delete;
void ChangeTableId(int newTableId);
void ModifyRoute(const Route& route, Operation action);
std::vector<Route> ListRoutes(int family);
void RemoveAll(int addressFamily);
private:
/*
Implement netlink equivalent of
"ip route <operation> table <id> <destination> via <gateway> src <source> dev <interface> onlink".
Note: the preferred source is set equal to destination. See comments in method implementation for more
details.
*/
template <typename TAddr>
void ModifyLoopbackRouteImpl(const Route& route, int operation, int flags);
template <typename TAddr>
void ModifyDefaultRouteImpl(const Route& route, int operation, int flags);
template <typename TAddr>
void ModifyDefaultLinkLocalRouteImpl(const Route& route, int operation, int flags);
template <typename TAddr>
void ModifyLinkLocalRouteImpl(const Route& route, int operation, int flags);
/*
By "offlink route", we mean a route with a specific destination prefix and a specific next hop.
Contrast this to a default route which has no specific destination prefix and a link local
route which has no specific next hop.
*/
template <typename TAddr>
void ModifyOfflinkRouteImpl(const Route& route, int operation, int flags);
template <typename TAddr>
void ModifyRouteImpl(const Route& route, Operation action);
/*
Creates the message using the routine, then sends the message via netlink.
*/
template <DerivedRouteMessage TMessage>
void SendMessage(const Route& route, int operation, int flags, const std::function<void(TMessage&)>& routine = [](auto&) {});
NetlinkChannel m_channel;
int m_table;
};