-
Notifications
You must be signed in to change notification settings - Fork 80
/
lldp.cpp
335 lines (298 loc) · 12.7 KB
/
lldp.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "lldp/lldp.h"
#include <iomanip> // for std::setfill
#include <sstream>
#include <boost/asio.hpp>
#include "bst/regex.h"
#include "slog/all_in_one.h" // for slog::manip_function, etc.
namespace lldp
{
static lldp_exception lldp_format_error(std::string message)
{
return{ "lldp format error - " + std::move(message) };
}
static lldp_exception lldp_parse_error(std::string message)
{
return{ "lldp parse error - " + std::move(message) };
}
namespace details
{
slog::manip_function<std::istream> const_char_of(std::initializer_list<char> cs)
{
return slog::manip_function<std::istream>([cs](std::istream& is)
{
if (cs.end() == std::find(cs.begin(), cs.end(), is.get())) is.setstate(std::ios_base::failbit);
});
}
}
const size_t ipv4_size(boost::asio::ip::address_v4::bytes_type().size());
const size_t ipv6_size(boost::asio::ip::address_v6::bytes_type().size());
const size_t mac_size(6);
// return empty if invalid; non-throwing
std::vector<uint8_t> make_mac_address(const std::string& mac_address)
{
std::vector<uint8_t> data;
// mac_address is xx-xx-xx-xx-xx-xx
std::istringstream is(mac_address);
is >> std::hex;
uint32_t d = 0;
for (size_t i = 0; i < mac_size; ++i)
{
if (0 != i) is >> details::const_char_of({ '-', ':' });
is >> std::setw(2) >> d;
data.push_back((uint8_t)d);
}
return !is.fail() ? data : std::vector<uint8_t>{};
}
// return empty if invalid; non-throwing
std::string parse_mac_address(const std::vector<uint8_t>& data, char separator)
{
if (data.size() < mac_size) return std::string{};
std::ostringstream os;
os << std::setfill('0') << std::hex << std::nouppercase;
for (size_t i = 0; i < mac_size; ++i)
{
if (0 != i) os << separator;
os << std::setw(2) << (uint32_t)data[i];
}
return os.str();
}
bool is_valid_network_address_data_size(network_address_family_number address_family, size_t data_size)
{
switch (address_family)
{
case network_address_family_numbers::ipv4:
return ipv4_size == data_size;
case network_address_family_numbers::ipv6:
return ipv6_size == data_size;
case network_address_family_numbers::mac:
return mac_size == data_size;
case network_address_family_numbers::dns:
case network_address_family_numbers::reserved:
default:
return true;
}
}
// make a network address, i.e. address_family and address data
// address can be an IPv4 or IPv6 or MAC address or DNS name
// return empty if invalid; non-throwing
std::vector<uint8_t> make_network_address(const std::string& address)
{
std::vector<uint8_t> data;
// see https://stackoverflow.com/a/3824105
static const bst::regex dns_regex(R"(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*\.?)");
static const bst::regex mac_regex(R"(([0-9a-fA-F]{2}-){5}([0-9a-fA-F]{2}))");
boost::system::error_code ec;
auto addr = boost::asio::ip::address::from_string(address, ec);
if (!ec)
{
if (addr.is_v4())
{
data.push_back(network_address_family_numbers::ipv4);
const auto network_address = addr.to_v4().to_bytes();
data.insert(data.end(), network_address.begin(), network_address.end());
}
else if (addr.is_v6())
{
data.push_back(network_address_family_numbers::ipv6);
const auto network_address = addr.to_v6().to_bytes();
data.insert(data.end(), network_address.begin(), network_address.end());
}
}
else if (bst::regex_match(address, mac_regex))
{
data.push_back(network_address_family_numbers::mac);
const auto mac_address = make_mac_address(address);
data.insert(data.end(), mac_address.begin(), mac_address.end());
}
else if (bst::regex_match(address, dns_regex))
{
data.push_back(network_address_family_numbers::dns);
data.insert(data.end(), address.begin(), address.end());
}
return data;
}
// data must be an IPv4 or IPv6 or MAC address or DNS name
// return empty if invalid; non-throwing
std::string parse_network_address(const std::vector<uint8_t>& data)
{
std::string address;
if (!data.empty())
{
// IANA Address Family Numbers enum value byte
network_address_family_number address_family(*data.begin());
auto first = std::next(data.begin()), last = data.end();
if (is_valid_network_address_data_size(address_family, std::distance(first, last)))
{
if (network_address_family_numbers::ipv4 == address_family)
{
boost::asio::ip::address_v4::bytes_type addr4_bytes;
std::copy_n(first, addr4_bytes.size(), addr4_bytes.begin());
address = boost::asio::ip::address_v4(addr4_bytes).to_string();
}
else if (network_address_family_numbers::ipv6 == address_family)
{
boost::asio::ip::address_v6::bytes_type addr6_bytes;
std::copy_n(first, addr6_bytes.size(), addr6_bytes.begin());
address = boost::asio::ip::address_v6(addr6_bytes).to_string();
}
else if (network_address_family_numbers::mac == address_family)
{
address = parse_mac_address({ first, last });
}
else if (network_address_family_numbers::dns == address_family)
{
address = std::string{ first, last };
}
}
}
return address;
}
// make a Chassis ID with the specified MAC address, or IPv4 or IPv6 address or DNS name
// return reserved otherwise; non-throwing
chassis_id make_chassis_id(const std::string& chassis_id)
{
auto data = make_mac_address(chassis_id);
if (!data.empty()) return{ chassis_id_subtypes::mac_address, std::move(data) };
data = make_network_address(chassis_id);
if (!data.empty()) return{ chassis_id_subtypes::network_address, std::move(data) };
return{ chassis_id_subtypes::reserved, {} };
}
// parse a Chassis ID
// return empty if invalid; non-throwing
std::string parse_chassis_id(const chassis_id& chassis_id)
{
switch (chassis_id.subtype)
{
case chassis_id_subtypes::mac_address:
return parse_mac_address(chassis_id.data);
case chassis_id_subtypes::network_address:
return parse_network_address(chassis_id.data);
case chassis_id_subtypes::chassis_component:
case chassis_id_subtypes::port_component:
case chassis_id_subtypes::interface_alias:
case chassis_id_subtypes::interface_name:
case chassis_id_subtypes::locally_assigned:
return std::string{ chassis_id.data.begin(), chassis_id.data.end() };
case chassis_id_subtypes::reserved:
default:
return{};
}
}
// make a Chassis ID with the specified MAC address
// may throw
chassis_id make_mac_address_chassis_id(const std::string& mac_address)
{
auto data = make_mac_address(mac_address);
if (data.empty()) throw lldp_format_error("invalid MAC address for Chassis ID");
return{ chassis_id_subtypes::mac_address, std::move(data) };
}
// parse a MAC address subtype Chassis ID
// may throw
std::string parse_mac_address_chassis_id(const chassis_id& chassis_id)
{
if (chassis_id_subtypes::mac_address != chassis_id.subtype) throw lldp_parse_error("wrong Chassis ID subtype");
auto mac_address = parse_mac_address(chassis_id.data);
if (mac_address.empty()) throw lldp_parse_error("invalid MAC address for Chassis ID");
return mac_address;
}
// make a Chassis ID with the specified IPv4 or IPv6 address
// may throw
chassis_id make_network_address_chassis_id(const std::string& address)
{
auto data = make_network_address(address);
if (data.empty()) throw lldp_format_error("invalid network address for Chassis ID");
return{ chassis_id_subtypes::network_address, std::move(data) };
}
// parse a network address subtype Chassis ID
// may throw
std::string parse_network_address_chassis_id(const chassis_id& chassis_id)
{
if (chassis_id_subtypes::network_address != chassis_id.subtype) throw lldp_parse_error("wrong Chassis ID subtype");
auto address = parse_network_address(chassis_id.data);
if (address.empty()) throw lldp_parse_error("invalid network address for Chassis ID");
return address;
}
// make a Port ID with the specified MAC address, or IPv4 or IPv6 address
// return reserved otherwise; non-throwing
port_id make_port_id(const std::string& port_id)
{
auto data = make_mac_address(port_id);
if (!data.empty()) return{ port_id_subtypes::mac_address, std::move(data) };
data = make_network_address(port_id);
if (!data.empty()) return{ port_id_subtypes::network_address, std::move(data) };
return{ port_id_subtypes::reserved,{} };
}
// parse a Port ID
// return empty if invalid; non-throwing
std::string parse_port_id(const port_id& port_id)
{
switch (port_id.subtype)
{
case port_id_subtypes::mac_address:
return parse_mac_address(port_id.data);
case port_id_subtypes::network_address:
return parse_network_address(port_id.data);
case port_id_subtypes::agent_circuit_id:
case port_id_subtypes::port_component:
case port_id_subtypes::interface_alias:
case port_id_subtypes::interface_name:
case port_id_subtypes::locally_assigned:
return std::string{ port_id.data.begin(), port_id.data.end() };
case port_id_subtypes::reserved:
default:
return{};
}
}
// make a Port ID with the specified MAC address
// may throw
port_id make_mac_address_port_id(const std::string& mac_address)
{
auto data = make_mac_address(mac_address);
if (data.empty()) throw lldp_format_error("invalid MAC address for Port ID");
return{ port_id_subtypes::mac_address, std::move(data) };
}
// parse a MAC address subtype Port ID
// may throw
std::string parse_mac_address_port_id(const port_id& port_id)
{
if (port_id_subtypes::mac_address != port_id.subtype) throw lldp_parse_error("wrong Port ID subtype");
auto mac_address = parse_mac_address(port_id.data);
if (mac_address.empty()) throw lldp_parse_error("invalid MAC address for Port ID");
return mac_address;
}
// make a Port ID with the specified IPv4 or IPv6 address
// may throw
port_id make_network_address_port_id(const std::string& address)
{
auto data = make_network_address(address);
if (data.empty()) throw lldp_format_error("invalid network address for Port ID");
return{ port_id_subtypes::network_address, std::move(data) };
}
// parse a network address subtype Port ID
// may throw
std::string parse_network_address_port_id(const port_id& port_id)
{
if (port_id_subtypes::network_address != port_id.subtype) throw lldp_parse_error("wrong Port ID subtype");
auto address = parse_network_address(port_id.data);
if (address.empty()) throw lldp_parse_error("invalid network address for Port ID");
return address;
}
network_address_family_number management_address::address_family() const
{
return !network_address.empty() ? network_address[0] : network_address_family_numbers::reserved;
}
// make a Management Address
// address must be an IPv4 or IPv6 address, a MAC address or DNS name
// may throw
management_address make_management_address(const std::string& address, interface_numbering_subtype interface_numbering_subtype, uint32_t interface_number, const std::vector<uint8_t>& object_identifier)
{
auto data = make_network_address(address);
if (data.empty()) throw lldp_format_error("invalid network address for Management Address");
return{
std::move(data),
interface_numbering_subtype,
interface_number,
object_identifier
};
}
}