Skip to content

Commit f015d21

Browse files
author
Leonardo Parente
committed
Implement network interface scan and pick default tap
1 parent 30b6da2 commit f015d21

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
#if __has_include(<ifaddrs.h>)
6+
#include <net/if.h>
7+
#include <ifaddrs.h>
8+
#else
9+
// System without ifaddrs
10+
#endif
11+
#include <string>
12+
13+
#define INTERFACE_UP 0x1
14+
#define INTERFACE_LOOPBACK 0x8
15+
16+
namespace visor {
17+
18+
static inline std::string most_used_interface()
19+
{
20+
#if __has_include(<ifaddrs.h>)
21+
struct ifaddrs *ifaddr;
22+
if (getifaddrs(&ifaddr) == -1) {
23+
return std::string();
24+
}
25+
std::string interface;
26+
uint64_t most_used = 0;
27+
for (struct ifaddrs *ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
28+
if (ifa->ifa_addr == nullptr || (ifa->ifa_flags & IFF_LOOPBACK) || !(ifa->ifa_flags & IFF_UP)) {
29+
continue;
30+
}
31+
#if defined(AF_PACKET)
32+
if (int family = ifa->ifa_addr->sa_family; family == AF_PACKET && ifa->ifa_data != nullptr) {
33+
#elif defined(AF_LINK)
34+
if (int family = ifa->ifa_addr->sa_family; family == AF_LINK && ifa->ifa_da ta != nullptr) {
35+
#else
36+
if (false) {
37+
#endif
38+
uint32_t *stats = reinterpret_cast<uint32_t *>(ifa->ifa_data);
39+
// first 4 bytes are txpackets and next 4 bytes are rxpackets
40+
uint64_t packets = stats[0] + stats[1];
41+
if (packets > most_used) {
42+
most_used = packets;
43+
interface = std::string(ifa->ifa_name);
44+
}
45+
}
46+
}
47+
freeifaddrs(ifaddr);
48+
return interface;
49+
#else
50+
return std::string();
51+
#endif
52+
}
53+
54+
}

cmd/pktvisord/main.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "CrashpadHandler.h"
1010
#include "HandlerManager.h"
1111
#include "InputStreamManager.h"
12+
#include "NetworkInterfaceScan.h"
1213
#include "Policies.h"
1314
#include "handlers/static_plugins.h"
1415
#include "inputs/static_plugins.h"
@@ -571,7 +572,18 @@ int main(int argc, char *argv[])
571572
exit(EXIT_FAILURE);
572573
}
573574

575+
std::string iface;
574576
if (args["IFACE"]) {
577+
iface = args["IFACE"].asString();
578+
} else {
579+
580+
iface = visor::most_used_interface();
581+
if (!iface.empty()) {
582+
logger->info("Network Interface not set at startup, picked the most used interface: '{}'", iface);
583+
}
584+
}
585+
586+
if (!iface.empty()) {
575587
// pcap command line functionality, create default policy
576588
try {
577589
std::string bpf;
@@ -584,7 +596,7 @@ int main(int argc, char *argv[])
584596
host_spec = args["-H"].asString();
585597
}
586598

587-
auto policy_str = fmt::format(default_tap_policy, args["IFACE"].asString(), host_spec, bpf, periods, sample_rate);
599+
auto policy_str = fmt::format(default_tap_policy, iface, host_spec, bpf, periods, sample_rate);
588600
logger->debug(policy_str);
589601
svr->registry()->configure_from_str(policy_str);
590602

0 commit comments

Comments
 (0)