-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathRoute.cpp
More file actions
58 lines (49 loc) · 1.5 KB
/
Route.cpp
File metadata and controls
58 lines (49 loc) · 1.5 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
// Copyright (C) Microsoft Corporation. All rights reserved.
#include <iostream>
#include "Route.h"
#include "Utils.h"
Route::Route(int routeFamily, const std::optional<Address>& routeNextHop, int routeInterface, bool isRouteDefault, const std::optional<Address>& routeDestination, int routeMetric) :
family(routeFamily), via(routeNextHop), dev(routeInterface), defaultRoute(isRouteDefault), to(routeDestination), metric(routeMetric)
{
// For onlink routes, ensure the via field is empty
if (via.has_value() && ((family == AF_INET && via->Addr() == "0.0.0.0") || (family == AF_INET6 && via->Addr() == "::")))
{
via.reset();
}
}
std::ostream& operator<<(std::ostream& out, const Route& route)
{
if (route.defaultRoute)
{
out << "default ";
}
if (route.to.has_value())
{
out << route.to.value() << " ";
}
if (route.via.has_value())
{
out << " via " << route.via.value() << " ";
}
return out << "dev " << route.dev << " metric " << route.metric;
}
bool Route::IsOnlink() const
{
return !via.has_value();
}
bool Route::IsMulticast() const
{
if (!to.has_value())
{
return false;
}
if (family == AF_INET)
{
in_addr address = {};
Syscall(::inet_pton, to->Family(), to->Addr().c_str(), &address);
return IN_MULTICAST(ntohl(address.s_addr));
}
in6_addr address = {};
Syscall(::inet_pton, to->Family(), to->Addr().c_str(), &address);
return IN6_IS_ADDR_MULTICAST(&address);
}