Line data Source code
1 : #include "source/extensions/matching/input_matchers/ip/matcher.h" 2 : 3 : #include "source/common/network/utility.h" 4 : 5 : namespace Envoy { 6 : namespace Extensions { 7 : namespace Matching { 8 : namespace InputMatchers { 9 : namespace IP { 10 : 11 : namespace { 12 : 13 0 : MatcherStats generateStats(absl::string_view prefix, Stats::Scope& scope) { 14 0 : return MatcherStats{IP_MATCHER_STATS(POOL_COUNTER_PREFIX(scope, prefix))}; 15 0 : } 16 : 17 : } // namespace 18 : 19 : Matcher::Matcher(std::vector<Network::Address::CidrRange> const& ranges, 20 : absl::string_view stat_prefix, 21 : Stats::Scope& stat_scope) 22 : : // We could put "false" instead of "true". What matters is that the IP 23 : // belongs to the trie. We could further optimize the storage of LcTrie in 24 : // this case by implementing an LcTrie<void> specialization that doesn't 25 : // store any associated data. 26 0 : trie_({{true, ranges}}), stats_(generateStats(stat_prefix, stat_scope)) {} 27 : 28 0 : bool Matcher::match(const Envoy::Matcher::MatchingDataType& input) { 29 0 : if (absl::holds_alternative<absl::monostate>(input)) { 30 0 : return false; 31 0 : } 32 0 : const std::string& ip_str = absl::get<std::string>(input); 33 0 : if (ip_str.empty()) { 34 0 : return false; 35 0 : } 36 0 : const auto ip = Network::Utility::parseInternetAddressNoThrow(ip_str); 37 0 : if (!ip) { 38 0 : stats_.ip_parsing_failed_.inc(); 39 0 : ENVOY_LOG(debug, "IP matcher: unable to parse address '{}'", ip_str); 40 0 : return false; 41 0 : } 42 0 : return !trie_.getData(ip).empty(); 43 0 : } 44 : 45 : } // namespace IP 46 : } // namespace InputMatchers 47 : } // namespace Matching 48 : } // namespace Extensions 49 : } // namespace Envoy