Skip to content

Commit

Permalink
fixup! Add external ip connections counters for each service
Browse files Browse the repository at this point in the history
  • Loading branch information
lnarolski committed Nov 13, 2024
1 parent d8fa245 commit e0efd7c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
1 change: 0 additions & 1 deletion ebpfdiscoverysrv/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <csignal>
#include <future>
#include <chrono>
#include <thread>

#include <sys/stat.h>

Expand Down
40 changes: 20 additions & 20 deletions libebpfdiscoveryproto/test/TranslatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ TEST_F(ProtobufTranslatorTest, successfulTranslationToJson) {
service::Service service4{.pid = 4, .endpoint = "google.com/endpoint/3", .domain = "google.com", .scheme = "http", .internalClientsNumber = 1, .externalClientsNumber = 2};
service::Service service5{.pid = 5, .endpoint = "dynatrace.com/endpoint/4", .domain = "dynatrace.com", .scheme = "https", .internalClientsNumber = 1, .externalClientsNumber = 2};

internalServices.push_back(service1);
internalServices.push_back(service2);
internalServices.push_back(service3);
internalServices.push_back(service4);
internalServices.push_back(service5);
internalServices.emplace_back(service1);
internalServices.emplace_back(service2);
internalServices.emplace_back(service3);
internalServices.emplace_back(service4);
internalServices.emplace_back(service5);

const auto proto{internalToProto(internalServices, false)};
ASSERT_FALSE(proto.second);
Expand All @@ -51,18 +51,18 @@ TEST_F(ProtobufTranslatorTest, successfulTranslationToJson) {
}

TEST_F(ProtobufTranslatorTest, successfulTranslationToJsonNetworkCounters) {
std::unordered_map<std::array<uint8_t, 2>, std::chrono::time_point<std::chrono::steady_clock>, service::ArrayHasher> detectedExternalIPv416Networks = {
{{0xAC, 0x8F}, std::chrono::steady_clock::now()},
{{0xAC, 0xC7}, std::chrono::steady_clock::now()}
std::unordered_map<uint32_t, std::chrono::time_point<std::chrono::steady_clock>> detectedExternalIPv416Networks = {
{0xAC8F, std::chrono::steady_clock::now()},
{0xACC7, std::chrono::steady_clock::now()}
};
std::unordered_map<std::array<uint8_t, 3>, std::chrono::time_point<std::chrono::steady_clock>, service::ArrayHasher> detectedExternalIPv424Networks = {
{{0xAC, 0x8F, 0x04}, std::chrono::steady_clock::now()},
{{0xAC, 0x8F, 0x06}, std::chrono::steady_clock::now()},
{{0xAC, 0xC7, 0x2D}, std::chrono::steady_clock::now()}
std::unordered_map<uint32_t, std::chrono::time_point<std::chrono::steady_clock>> detectedExternalIPv424Networks = {
{0xAC8F04, std::chrono::steady_clock::now()},
{0xAC8F06, std::chrono::steady_clock::now()},
{0xACC72D, std::chrono::steady_clock::now()}
};
std::unordered_map<std::array<uint8_t, 10>, std::chrono::time_point<std::chrono::steady_clock>, service::ArrayHasher> detectedExternalIPv6Networks = {
{{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00}, std::chrono::steady_clock::now()},
{{0x12, 0x34, 0x23, 0x45, 0x34, 0x56, 0x45, 0x67, 0x56, 0x78}, std::chrono::steady_clock::now()}
std::unordered_map<std::array<uint8_t, service::ipv6NetworkPrefixBytesLen>, std::chrono::time_point<std::chrono::steady_clock>, service::ArrayHasher> detectedExternalIPv6Networks = {
{{0x20, 0x01, 0x48, 0x60, 0x48, 0x60}, std::chrono::steady_clock::now()},
{{0x12, 0x34, 0x23, 0x45, 0x34, 0x56}, std::chrono::steady_clock::now()}
};

std::vector<std::reference_wrapper<service::Service>> internalServices;
Expand All @@ -73,11 +73,11 @@ TEST_F(ProtobufTranslatorTest, successfulTranslationToJsonNetworkCounters) {
service::Service service4{.pid = 4, .endpoint = "google.com/endpoint/3", .domain = "google.com", .scheme = "http", .internalClientsNumber = 1, .externalClientsNumber = 2, .detectedExternalIPv6Networks = detectedExternalIPv6Networks};
service::Service service5{.pid = 5, .endpoint = "dynatrace.com/endpoint/4", .domain = "dynatrace.com", .scheme = "https", .internalClientsNumber = 1, .externalClientsNumber = 2, .detectedExternalIPv6Networks = detectedExternalIPv6Networks};

internalServices.push_back(service1);
internalServices.push_back(service2);
internalServices.push_back(service3);
internalServices.push_back(service4);
internalServices.push_back(service5);
internalServices.emplace_back(service1);
internalServices.emplace_back(service2);
internalServices.emplace_back(service3);
internalServices.emplace_back(service4);
internalServices.emplace_back(service5);

const auto proto{internalToProto(internalServices, true)};
ASSERT_FALSE(proto.second);
Expand Down
7 changes: 4 additions & 3 deletions libservice/headers/service/Service.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct ArrayHasher {
}
};

static constexpr uint8_t ipv6NetworkPrefixBytesLen = 6;
struct Service {
uint32_t pid;
std::string endpoint;
Expand All @@ -44,9 +45,9 @@ struct Service {
uint32_t internalClientsNumber{0u};
uint32_t externalClientsNumber{0u};

std::unordered_map<std::array<uint8_t, 2>, std::chrono::time_point<std::chrono::steady_clock>, ArrayHasher> detectedExternalIPv416Networks;
std::unordered_map<std::array<uint8_t, 3>, std::chrono::time_point<std::chrono::steady_clock>, ArrayHasher> detectedExternalIPv424Networks;
std::unordered_map<std::array<uint8_t, 10>, std::chrono::time_point<std::chrono::steady_clock>, ArrayHasher> detectedExternalIPv6Networks;
std::unordered_map<uint32_t, std::chrono::time_point<std::chrono::steady_clock>> detectedExternalIPv416Networks;
std::unordered_map<uint32_t, std::chrono::time_point<std::chrono::steady_clock>> detectedExternalIPv424Networks;
std::unordered_map<std::array<uint8_t, ipv6NetworkPrefixBytesLen>, std::chrono::time_point<std::chrono::steady_clock>, ArrayHasher> detectedExternalIPv6Networks;

bool operator==(const Service& other) const {
return pid == other.pid && endpoint == other.endpoint && domain == other.domain && scheme == other.scheme && internalClientsNumber == other.internalClientsNumber &&
Expand Down
8 changes: 4 additions & 4 deletions libservice/src/Aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ static void incrementServiceClientsNumber(
if (enableNetworkCounters) {
try {
if (isIpv6) {
std::array<uint8_t, 10> networkIPv6{};
std::memcpy(networkIPv6.data(), std::get<in6_addr>(clientAddrBinary).s6_addr, 10);
std::array<uint8_t, service::ipv6NetworkPrefixBytesLen> networkIPv6{};
std::memcpy(networkIPv6.data(), std::get<in6_addr>(clientAddrBinary).s6_addr, service::ipv6NetworkPrefixBytesLen);

if (auto it = service.detectedExternalIPv6Networks.find(networkIPv6); it != service.detectedExternalIPv6Networks.end()) {
it->second = currentTime;
} else {
service.detectedExternalIPv6Networks[networkIPv6] = currentTime;
}
} else {
std::array<uint8_t, 3> network24 = {static_cast<uint8_t>(std::get<in_addr>(clientAddrBinary).s_addr & 0xFF), static_cast<uint8_t>((std::get<in_addr>(clientAddrBinary).s_addr >> 8) & 0xFF), static_cast<uint8_t>((std::get<in_addr>(clientAddrBinary).s_addr >> 16) & 0xFF)};
uint32_t network24 = (std::get<in_addr>(clientAddrBinary).s_addr & 0xFFFFFF);
if (auto it = service.detectedExternalIPv424Networks.find(network24); it != service.detectedExternalIPv424Networks.end()) {
it->second = currentTime;
} else {
service.detectedExternalIPv424Networks[network24] = currentTime;
}

std::array<uint8_t, 2> network16 = {network24[0], network24[1]};
uint32_t network16 = (std::get<in_addr>(clientAddrBinary).s_addr & 0xFFFF);
if (auto it = service.detectedExternalIPv416Networks.find(network16); it != service.detectedExternalIPv416Networks.end()) {
it->second = currentTime;
} else {
Expand Down
20 changes: 10 additions & 10 deletions libservice/test/AggregatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,18 @@ TEST_F(ServiceAggregatorTest, aggregateNetworkCounters) {
aggregator.newRequest(request.first, request.second);
}

const std::unordered_map<std::array<uint8_t, 2>, std::chrono::time_point<std::chrono::steady_clock>, service::ArrayHasher> detectedExternalIPv416Networks = {
{{0xAC, 0x8F}, std::chrono::time_point<std::chrono::steady_clock>{}},
{{0xAC, 0xC7}, std::chrono::time_point<std::chrono::steady_clock>{}}
const std::unordered_map<uint32_t, std::chrono::time_point<std::chrono::steady_clock>> detectedExternalIPv416Networks = {
{0x8FAC, std::chrono::time_point<std::chrono::steady_clock>{}},
{0xC7AC, std::chrono::time_point<std::chrono::steady_clock>{}}
};
const std::unordered_map<std::array<uint8_t, 3>, std::chrono::time_point<std::chrono::steady_clock>, service::ArrayHasher> detectedExternalIPv424Networks = {
{{0xAC, 0x8F, 0x04}, std::chrono::time_point<std::chrono::steady_clock>{}},
{{0xAC, 0x8F, 0x06}, std::chrono::time_point<std::chrono::steady_clock>{}},
{{0xAC, 0xC7, 0x2D}, std::chrono::time_point<std::chrono::steady_clock>{}}
const std::unordered_map<uint32_t, std::chrono::time_point<std::chrono::steady_clock>> detectedExternalIPv424Networks = {
{0x048FAC, std::chrono::time_point<std::chrono::steady_clock>{}},
{0x068FAC, std::chrono::time_point<std::chrono::steady_clock>{}},
{0x2DC7AC, std::chrono::time_point<std::chrono::steady_clock>{}}
};
const std::unordered_map<std::array<uint8_t, 10>, std::chrono::time_point<std::chrono::steady_clock>, service::ArrayHasher> detectedExternalIPv6Networks = {
{{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00}, std::chrono::time_point<std::chrono::steady_clock>{}},
{{0x12, 0x34, 0x23, 0x45, 0x34, 0x56, 0x45, 0x67, 0x56, 0x78}, std::chrono::time_point<std::chrono::steady_clock>{}}
const std::unordered_map<std::array<uint8_t, service::ipv6NetworkPrefixBytesLen>, std::chrono::time_point<std::chrono::steady_clock>, service::ArrayHasher> detectedExternalIPv6Networks = {
{{0x20, 0x01, 0x48, 0x60, 0x48, 0x60}, std::chrono::time_point<std::chrono::steady_clock>{}},
{{0x12, 0x34, 0x23, 0x45, 0x34, 0x56}, std::chrono::time_point<std::chrono::steady_clock>{}}
};

{
Expand Down

0 comments on commit e0efd7c

Please sign in to comment.