-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathinterface.cpp
More file actions
255 lines (229 loc) · 7.92 KB
/
Copy pathinterface.cpp
File metadata and controls
255 lines (229 loc) · 7.92 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
//
// interface.cpp
//
// Copyright (c) 2019 2025 Andrea Bondavalli. All rights reserved.
//
// 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
// 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/>.
// MIT License
//
#include <boost/asio.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <fstream>
#include <utility>
#include <iostream>
#include <ifaddrs.h>
#include "log.hpp"
using namespace boost::asio;
using namespace boost::asio::ip;
bool is_interface_ip(const std::string& interface_name,
const std::string& addr) {
struct ifaddrs *ifap, *ifa;
struct sockaddr_in* sa;
char* saddr;
bool found(false);
getifaddrs(&ifap);
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
sa = (struct sockaddr_in*)ifa->ifa_addr;
saddr = inet_ntoa(sa->sin_addr);
if (interface_name == ifa->ifa_name && addr == saddr)
found = true;
}
}
freeifaddrs(ifap);
return found;
}
std::pair<uint32_t, std::string> get_interface_ip(
const std::string& interface_name) {
if (interface_name.empty()) {
return {0, ""};
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
BOOST_LOG_TRIVIAL(warning)
<< "Cannot retrieve IP address for interface " << interface_name;
return {0, ""};
}
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
close(fd);
BOOST_LOG_TRIVIAL(warning)
<< "Cannot retrieve IP address for interface " << interface_name;
return {0, ""};
}
close(fd);
struct sockaddr_in* sockaddr =
reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr);
uint32_t addr = ntohl(sockaddr->sin_addr.s_addr);
std::string str_addr(ip::address_v4(addr).to_string());
/*BOOST_LOG_TRIVIAL(debug) << "interface " << interface_name
<< " IP address " << str_addr;*/
return {addr, str_addr};
}
std::tuple<uint32_t, std::string, bool> get_new_interface_ip(
const std::string& interface_name,
const std::string& curr_addr) {
if (is_interface_ip(interface_name, curr_addr)) {
uint32_t ip_addr;
inet_pton(AF_INET, curr_addr.c_str(), &ip_addr);
return {ntohl(ip_addr), curr_addr, false};
}
auto [ip_addr, ip_str] = get_interface_ip(interface_name);
BOOST_LOG_TRIVIAL(info) << "interface " << interface_name
<< " new IP address <" << ip_str << ">";
return {ip_addr, ip_str,
!ip_str.empty() && !curr_addr.empty() && curr_addr != ip_str};
}
std::pair<std::array<uint8_t, 6>, std::string> get_interface_mac(
const std::string& interface_name) {
std::array<uint8_t, 6> mac{0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
BOOST_LOG_TRIVIAL(error)
<< "Cannot retrieve MAC address for interface " << interface_name;
return {mac, ""};
}
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
close(fd);
BOOST_LOG_TRIVIAL(error)
<< "Cannot retrieve MAC address for interface " << interface_name;
return {mac, ""};
}
close(fd);
uint8_t* sa = reinterpret_cast<uint8_t*>(ifr.ifr_hwaddr.sa_data);
std::copy(sa, sa + 6, std::begin(mac));
char str_mac[18];
sprintf(str_mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", mac[0], mac[1], mac[2],
mac[3], mac[4], mac[5]);
/*BOOST_LOG_TRIVIAL(debug) << "interface " << interface_name
<< " MAC address " << str_mac;*/
return {mac, str_mac};
}
int get_interface_index(const std::string& interface_name) {
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
BOOST_LOG_TRIVIAL(warning)
<< "Cannot retrieve index for interface " << interface_name;
return -1;
}
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
close(fd);
BOOST_LOG_TRIVIAL(warning)
<< "Cannot retrieve index for interface " << interface_name;
return -1;
}
close(fd);
/*BOOST_LOG_TRIVIAL(debug) << "interface " << interface_name
<< " index " << ifr.ifr_ifindex;*/
return ifr.ifr_ifindex;
}
std::pair<std::array<uint8_t, 6>, std::string> get_mac_from_arp_cache(
const std::string& ip) {
const std::string arpProcPath("/proc/net/arp");
std::array<uint8_t, 6> mac{0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
std::ifstream stream(arpProcPath);
while (stream) {
std::string line;
std::vector<std::string> tokens;
std::getline(stream, line);
if (line.find(ip) == std::string::npos) {
continue;
}
boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
/* check that IP is on the correct interface */
if (tokens.size() >= 6 /*&& tokens[5] == interface_name*/) {
std::vector<std::string> vec;
/* parse MAC */
boost::split(vec, tokens[3], boost::is_any_of(":"));
int j = 0;
bool check = false;
for (auto const& item : vec) {
mac[j] = strtol(item.c_str(), nullptr, 16);
check = check | (mac[j] != 0);
j++;
}
if (check) {
return {mac, tokens[3]};
}
}
}
BOOST_LOG_TRIVIAL(debug)
<< "get_mac_from_arp_cache:: cannot retrieve MAC for IP " << ip;
return {mac, ""};
}
bool ping(const std::string& ip) {
static uint16_t sequence_number(0);
uint16_t identifier(0xABAB);
uint8_t buffer[10];
// Create an ICMP header for an echo request.
buffer[0] = 0x8; // echo request
buffer[1] = 0x0; // code
memcpy(buffer + 2, &identifier, 2); // identifier
memcpy(buffer + 4, &sequence_number, 2); // sequence number
memcpy(buffer + 6, "ping", 4); // body
// this requires root priv
try {
io_context io_service;
icmp::socket socket{io_service, icmp::v4()};
ip::icmp::endpoint destination(ip::icmp::v4(),
#if BOOST_VERSION < 108700
ip::address_v4::from_string(ip).to_ulong());
#else
ip::make_address(ip).to_v4().to_uint());
#endif
socket.send_to(boost::asio::buffer(buffer, sizeof buffer), destination);
} catch (...) {
BOOST_LOG_TRIVIAL(error) << "ping:: send_to() failed";
return false;
}
return true;
}
bool echo_try_connect(const std::string& dest_ip) {
ip::tcp::iostream s;
BOOST_LOG_TRIVIAL(debug) << "echo_connect:: connecting to " << dest_ip;
#if BOOST_VERSION < 106600
s.expires_from_now(boost::posix_time::seconds(1));
#else
s.expires_after(boost::asio::chrono::seconds(1));
#endif
s.connect(dest_ip, "7");
if (!s || s.error()) {
BOOST_LOG_TRIVIAL(debug)
<< "echo_connect:: unable to connect to " << dest_ip;
return false;
}
s.close();
return true;
}
std::array<uint8_t, 6> get_mcast_mac_addr(uint32_t mcast_ip) {
// As defined by IANA, the most significant 24 bits of an IPv4 multicast
// MAC address are 0x01005E. // Bit 25 is 0, and the other 23 bits are the
// least significant 23 bits of an IPv4 multicast address.
return {0x01,
0x00,
0x5e,
static_cast<uint8_t>((mcast_ip >> 16) & 0x7F),
static_cast<uint8_t>(mcast_ip >> 8),
static_cast<uint8_t>(mcast_ip)};
}