Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions libs/visor_utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ static void split(const std::string &s, char delim, Out result)
}
}

std::pair<bool, IPv4subnetList::const_iterator> match_subnet(IPv4subnetList &ipv4_list, uint32_t ipv4_val)
std::optional<IPv4subnetList::const_iterator> match_subnet(IPv4subnetList &ipv4_list, uint32_t ipv4_val)
{
if (ipv4_val && !ipv4_list.empty()) {
in_addr ipv4{};
std::memcpy(&ipv4, &ipv4_val, sizeof(in_addr));
for (IPv4subnetList::const_iterator it = ipv4_list.begin(); it != ipv4_list.end(); ++it) {
uint8_t cidr = it->cidr;
if (cidr == 0) {
return {true, it};
return it;
}
uint32_t mask = htonl((0xFFFFFFFFu) << (32 - cidr));
if (!((ipv4.s_addr ^ it->addr.s_addr) & mask)) {
return {true, it};
return it;
}
}
}
return {false, IPv4subnetList::const_iterator()};
return std::nullopt;
}

std::pair<bool, IPv6subnetList::const_iterator> match_subnet(IPv6subnetList &ipv6_list, const uint8_t *ipv6_val)
std::optional<IPv6subnetList::const_iterator> match_subnet(IPv6subnetList &ipv6_list, const uint8_t *ipv6_val)
{
if (ipv6_val && !ipv6_list.empty()) {
in6_addr ipv6{};
Expand All @@ -55,21 +55,21 @@ std::pair<bool, IPv6subnetList::const_iterator> match_subnet(IPv6subnetList &ipv
result = subSubnetByte == subThisByte;
}
if (result) {
return {true, it};
return it;
}
}
}
return {false, IPv6subnetList::const_iterator()};
return std::nullopt;
}

bool match_subnet(IPv4subnetList &ipv4_list, IPv6subnetList &ipv6_list, const std::string &ip_val)
{
pcpp::IPv4Address ipv4;
pcpp::IPv6Address ipv6;
if (ipv4 = pcpp::IPv4Address(ip_val); ipv4.isValid()) {
return match_subnet(ipv4_list, ipv4.toInt()).first;
return match_subnet(ipv4_list, ipv4.toInt()).has_value();
} else if (ipv6 = pcpp::IPv6Address(ip_val); ipv6.isValid()) {
return match_subnet(ipv6_list, ipv6.toBytes()).first;
return match_subnet(ipv6_list, ipv6.toBytes()).has_value();
}
return false;
}
Expand Down
9 changes: 5 additions & 4 deletions libs/visor_utils/utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#pragma once

Expand All @@ -19,6 +19,7 @@
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
Expand Down Expand Up @@ -58,7 +59,7 @@ bool ipv6_to_sockaddr(const pcpp::IPv6Address &ip, struct sockaddr_in6 *sa);

std::vector<std::string> split_str_to_vec_str(const std::string &spec, const char &delimiter);
void parse_host_specs(const std::vector<std::string> &host_list, IPv4subnetList &ipv4_list, IPv6subnetList &ipv6_list);
std::pair<bool, IPv4subnetList::const_iterator> match_subnet(IPv4subnetList &ipv4_list, uint32_t ipv4_val);
std::pair<bool, IPv6subnetList::const_iterator> match_subnet(IPv6subnetList &ipv6_list, const uint8_t *ipv6_val);
std::optional<IPv4subnetList::const_iterator> match_subnet(IPv4subnetList &ipv4_list, uint32_t ipv4_val);
std::optional<IPv6subnetList::const_iterator> match_subnet(IPv6subnetList &ipv6_list, const uint8_t *ipv6_val);
bool match_subnet(IPv4subnetList &ipv4_list, IPv6subnetList &ipv6_list, const std::string &ip_val);
}
18 changes: 9 additions & 9 deletions src/handlers/flow/FlowStreamHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ static std::string ip_summarization(const std::string &val, SummaryData *summary
if (summary) {
pcpp::IPv4Address ipv4;
pcpp::IPv6Address ipv6;
if (ipv4 = pcpp::IPv4Address(val); ipv4.isValid() && match_subnet(summary->ipv4_exclude_summary, ipv4.toInt()).first) {
if (ipv4 = pcpp::IPv4Address(val); ipv4.isValid() && match_subnet(summary->ipv4_exclude_summary, ipv4.toInt()).has_value()) {
return val;
} else if (ipv6 = pcpp::IPv6Address(val); ipv6.isValid() && match_subnet(summary->ipv6_exclude_summary, ipv6.toBytes()).first) {
} else if (ipv6 = pcpp::IPv6Address(val); ipv6.isValid() && match_subnet(summary->ipv6_exclude_summary, ipv6.toBytes()).has_value()) {
return val;
}
if (summary->type == IpSummary::ByASN && HandlerModulePlugin::asn->enabled()) {
Expand All @@ -45,12 +45,12 @@ static std::string ip_summarization(const std::string &val, SummaryData *summary
return asn;
} else if (summary->type == IpSummary::BySubnet) {
if (ipv4.isValid()) {
if (auto [match, subnet] = match_subnet(summary->ipv4_summary, ipv4.toInt()); match) {
return subnet->str;
if (auto subnet = match_subnet(summary->ipv4_summary, ipv4.toInt()); subnet.has_value()) {
return subnet.value()->str;
}
} else if (ipv6.isValid()) {
if (auto [match, subnet] = match_subnet(summary->ipv6_summary, ipv6.toBytes()); match) {
return subnet->str;
if (auto subnet = match_subnet(summary->ipv6_summary, ipv6.toBytes()); subnet.has_value()) {
return subnet.value()->str;
}
}
}
Expand Down Expand Up @@ -408,9 +408,9 @@ void FlowStreamHandler::process_netflow_cb(const std::string &senderIP, const NF
bool FlowStreamHandler::_filtering(FlowData &flow, const std::string &device_id)
{
if (_f_enabled[Filters::OnlyIps]) {
if (flow.is_ipv6 && !match_subnet(_only_ipv6_list, flow.ipv6_in.toBytes()).first && !match_subnet(_only_ipv6_list, flow.ipv6_out.toBytes()).first) {
if (flow.is_ipv6 && !match_subnet(_only_ipv6_list, flow.ipv6_in.toBytes()).has_value() && !match_subnet(_only_ipv6_list, flow.ipv6_out.toBytes()).has_value()) {
return true;
} else if (!match_subnet(_only_ipv4_list, flow.ipv4_in.toInt()).first && !match_subnet(_only_ipv4_list, flow.ipv4_out.toInt()).first) {
} else if (!match_subnet(_only_ipv4_list, flow.ipv4_in.toInt()).has_value() && !match_subnet(_only_ipv4_list, flow.ipv4_out.toInt()).has_value()) {
return true;
}
}
Expand Down Expand Up @@ -840,7 +840,7 @@ void FlowMetricsBucket::to_opentelemetry(metrics::v1::ScopeMetrics &scope, Metri
auto end_ts = end_tstamp();

std::shared_lock r_lock(_mutex);

SummaryData *summary{nullptr};
if (_summary_data && _summary_data->type != IpSummary::None) {
summary = _summary_data;
Expand Down
8 changes: 4 additions & 4 deletions src/inputs/pcap/PcapInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@ void PcapInputStream::process_raw_packet(pcpp::RawPacket *rawPacket)
auto IP4layer = packet.getLayerOfType<pcpp::IPv4Layer>();
auto IP6layer = packet.getLayerOfType<pcpp::IPv6Layer>();
if (IP4layer) {
if (lib::utils::match_subnet(_hostIPv4, IP4layer->getDstIPv4Address().toInt()).first) {
if (lib::utils::match_subnet(_hostIPv4, IP4layer->getDstIPv4Address().toInt()).has_value()) {
_packet_dir_cache = PacketDirection::toHost;
} else if (lib::utils::match_subnet(_hostIPv4, IP4layer->getSrcIPv4Address().toInt()).first) {
} else if (lib::utils::match_subnet(_hostIPv4, IP4layer->getSrcIPv4Address().toInt()).has_value()) {
_packet_dir_cache = PacketDirection::fromHost;
}
} else if (IP6layer) {
if (lib::utils::match_subnet(_hostIPv6, IP6layer->getDstIPv6Address().toBytes()).first) {
if (lib::utils::match_subnet(_hostIPv6, IP6layer->getDstIPv6Address().toBytes()).has_value()) {
_packet_dir_cache = PacketDirection::toHost;
} else if (lib::utils::match_subnet(_hostIPv6, IP6layer->getSrcIPv6Address().toBytes()).first) {
} else if (lib::utils::match_subnet(_hostIPv6, IP6layer->getSrcIPv6Address().toBytes()).has_value()) {
_packet_dir_cache = PacketDirection::fromHost;
}
}
Expand Down