1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_IP_PATTERN_H_ 6 #define NET_BASE_IP_PATTERN_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/strings/string_piece.h" 13 #include "net/base/net_export.h" 14 #include "net/base/net_util.h" 15 16 namespace net { 17 18 // IPPatterns are used to match IP address resolutions for possible augmentation 19 // by a MappedIPResolver, which uses IPMappingRules. 20 class NET_EXPORT IPPattern { 21 public: 22 IPPattern(); 23 ~IPPattern(); 24 25 // Parse a textual pattern (IP_PATTERN as defined in ip_mapping_rules.h) into 26 // |this| and return true if the parsing was successful. 27 bool ParsePattern(const std::string& ip_pattern); 28 // Test to see if the current pattern in |this| matches the given |address| 29 // and return true if it matches. 30 bool Match(const IPAddressNumber& address) const; 31 is_ipv4()32 bool is_ipv4() const { return is_ipv4_; } 33 34 private: 35 class ComponentPattern; 36 typedef std::vector<std::string> Strings; 37 typedef std::vector<ComponentPattern*> ComponentPatternList; 38 39 // IPv6 addresses have 8 components, while IPv4 addresses have 4 components. 40 // ComponentPattern is used to define patterns to match individual components. 41 bool ParseComponentPattern(const base::StringPiece& text, 42 ComponentPattern* pattern) const; 43 // Convert IP component to an int. Assume hex vs decimal for IPV6 vs V4. 44 bool ValueTextToInt(const base::StringPiece& input, uint32* output) const; 45 46 bool is_ipv4_; 47 // The |ip_mask_| indicates, for each component, if this pattern requires an 48 // exact match (OCTET in IPv4, or 4 hex digits in IPv6). 49 // For each true element there is an entry in |component_values_|, and false 50 // means that an entry from our list of ComponentPattern instances must be 51 // applied. 52 std::vector<bool> ip_mask_; 53 // The vector of fixed values that are requried. 54 // Other values may be restricted by the component_patterns_; 55 // The class invariant is: 56 // ip_mask_.size() == component_patterns_.size() 57 // + size(our ComponentPattern list) 58 std::vector<uint32> component_values_; 59 // If only one component position was specified using a range, then this 60 // list will only have 1 element (i.e., we only have patterns for each element 61 // of ip_mask_ that is false.) 62 // We own these elements, and need to destroy them all when we are destroyed. 63 ComponentPatternList component_patterns_; 64 65 DISALLOW_COPY_AND_ASSIGN(IPPattern); 66 }; 67 68 } // namespace net 69 70 #endif // NET_BASE_IP_PATTERN_H_ 71