This repository has been archived by the owner on Feb 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrdiscmon.cpp
120 lines (107 loc) · 3.29 KB
/
rdiscmon.cpp
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
/*
* Copyright 2014 Andrew Ayer
*
* Based, in part, on code from NetworkManager, Copyright (C) 2013 Red Hat, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rdisc.hpp"
#include "util.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
class Rdisc_monitor : public Base_rdisc {
static const char* dhcp_level_to_string (Dhcp_level dhcp_level)
{
switch (dhcp_level) {
case DHCP_LEVEL_NONE:
return "none";
case DHCP_LEVEL_OTHERCONF:
return "otherconf";
case DHCP_LEVEL_MANAGED:
return "managed";
default:
return "INVALID";
}
}
virtual void dhcp_level_changed (Dhcp_level dhcp_level)
{
std::clog << "DHCP level = " << dhcp_level_to_string(dhcp_level) << std::endl;
}
virtual void mtu_changed (int mtu)
{
std::clog << "MTU = " << mtu << std::endl;
}
virtual void gateway_changed (const Gateway& gateway)
{
char addrstr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &gateway.address, addrstr, sizeof(addrstr));
std::clog << "Gateway = " << addrstr << " (valid_lft ";
if (gateway.lifetime == FOREVER) {
std::clog << "forever";
} else {
std::clog << gateway.lifetime;
}
std::clog << ")" << std::endl;
}
virtual void onlink_prefix_changed (const Onlink_prefix& prefix)
{
char addrstr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &prefix.prefix, addrstr, sizeof(addrstr));
std::clog << "Onlink prefix = " << addrstr << "/" << prefix.prefix_len << " (valid_lft ";
if (prefix.lifetime == FOREVER) {
std::clog << "forever";
} else {
std::clog << prefix.lifetime;
}
std::clog << ")" << std::endl;
}
virtual void autoconf_prefix_changed (const Autoconf_prefix& prefix)
{
char addrstr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &prefix.prefix, addrstr, sizeof(addrstr));
std::clog << "Autoconf prefix = " << addrstr << "/" << prefix.prefix_len << " (valid_lft ";
if (prefix.lifetime == FOREVER) {
std::clog << "forever";
} else {
std::clog << prefix.lifetime;
}
std::clog << " preferred_lft ";
if (prefix.preferred_lifetime == FOREVER) {
std::clog << "forever";
} else {
std::clog << prefix.preferred_lifetime;
}
std::clog << ")" << std::endl;
}
public:
Rdisc_monitor (int arg_ifindex, const std::string& arg_ifname) : Base_rdisc(arg_ifindex, arg_ifname) { }
};
int main (int argc, char** argv)
{
if (argc != 2) {
std::clog << "Usage: " << argv[0] << " INTERFACE" << std::endl;
return 2;
}
const char* interface_name = argv[1];
int interface_index = get_ifindex(interface_name);
if (interface_index == -1) {
std::clog << argv[0] << ": " << interface_name << ": No such interface" << std::endl;
return 1;
}
Rdisc_monitor mon(interface_index, interface_name);
mon.set_enable_debug(true);
mon.run();
return 0;
}