Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2013 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #ifndef SHILL_IP_ADDRESS_STORE_H_
     18 #define SHILL_IP_ADDRESS_STORE_H_
     19 
     20 #include <random>
     21 #include <set>
     22 
     23 #include "shill/net/ip_address.h"
     24 
     25 namespace shill {
     26 
     27 struct IPAddressLTIgnorePrefix {
     28   bool operator () (const IPAddress& lhs, const IPAddress& rhs) const;
     29 };
     30 
     31 // Stores a set of IP addresses used by ConnectionHealthChecker to check
     32 // connectivity when there is a chance that the service has run out-of-credits.
     33 // The IP addresses are populated (using DNS queries) opportunistically and
     34 // must be persistent so that they can be used in an out-of-credit scenario
     35 // (when DNS queries would also fail).
     36 // To make the store persistent across Device resets (e.g. suspend-resume), it
     37 // is owned by the Manager.
     38 // Currently, this is a thin wrapper around an STL container.
     39 class IPAddressStore {
     40  public:
     41   typedef std::set<IPAddress, IPAddressLTIgnorePrefix> IPAddresses;
     42 
     43   IPAddressStore();
     44   virtual ~IPAddressStore();
     45 
     46   // Add a new IP address if it does not already exist.
     47   virtual void AddUnique(const IPAddress& ip);
     48   virtual void Remove(const IPAddress& ip);
     49   virtual void Clear();
     50   virtual bool Contains(const IPAddress& ip) const;
     51   virtual size_t Count() const;
     52   virtual bool Empty() const;
     53   const IPAddresses& GetIPAddresses() const { return ip_addresses_; }
     54 
     55   virtual IPAddress GetRandomIP();
     56 
     57  protected:
     58   friend class IPAddressStoreTest;
     59 
     60  private:
     61   IPAddresses ip_addresses_;
     62   std::default_random_engine random_engine_;
     63 
     64   DISALLOW_COPY_AND_ASSIGN(IPAddressStore);
     65 };
     66 
     67 }  // namespace shill
     68 
     69 #endif  // SHILL_IP_ADDRESS_STORE_H_
     70