Repository navigation
Expand file tree
/
Copy pathinterface.cpp
More file actions
198 lines (178 loc) · 6.17 KB
/
Copy pathinterface.cpp
File metadata and controls
198 lines (178 loc) · 6.17 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
//
// interface.cpp
//
// Copyright (c) 2019 2020 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 "log.hpp"
using namespace boost::asio;
using namespace boost::asio::ip;
std::pair<uint32_t, std::string> get_interface_ip(
const std::string& interface_name) {
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::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& interface_name,
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
<< " on interface " << interface_name;
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_service io_service;
icmp::socket socket{io_service, icmp::v4()};
ip::icmp::endpoint destination(ip::icmp::v4(),
ip::address_v4::from_string(ip).to_ulong());
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& ip) {
ip::tcp::iostream s;
BOOST_LOG_TRIVIAL(debug) << "echo_connect:: connecting to " << 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(ip, "7");
if (!s || s.error()) {
BOOST_LOG_TRIVIAL(debug) << "echo_connect:: unable to connect to " << ip;
return false;
}
s.close();
return true;
}