Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS 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 SHILL_IP_ADDRESS_STORE_H_ |
| 6 | #define SHILL_IP_ADDRESS_STORE_H_ |
| 7 | |
Alex Vakulenko | 37c5d94 | 2014-06-17 18:17:43 -0700 | [diff] [blame] | 8 | #include <random> |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 9 | #include <set> |
| 10 | |
| 11 | #include "shill/ip_address.h" |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | struct IPAddressLTIgnorePrefix { |
| 16 | bool operator () (const IPAddress &lhs, const IPAddress &rhs) const; |
| 17 | }; |
| 18 | |
| 19 | // Stores a set of IP addresses used by ConnectionHealthChecker to check |
| 20 | // connectivity when there is a chance that the service has run out-of-credits. |
| 21 | // The IP addresses are populated (using DNS queries) opportunistically and |
| 22 | // must be persistent so that they can be used in an out-of-credit scenario |
| 23 | // (when DNS queries would also fail). |
| 24 | // To make the store persistent across Device resets (e.g. suspend-resume), it |
| 25 | // is owned by the Manager. |
| 26 | // Currently, this is a thin wrapper around an STL container. |
| 27 | class IPAddressStore { |
| 28 | public: |
| 29 | typedef std::set<IPAddress, IPAddressLTIgnorePrefix> IPAddresses; |
| 30 | |
| 31 | IPAddressStore(); |
| 32 | virtual ~IPAddressStore(); |
| 33 | |
| 34 | // Add a new IP address if it does not already exist. |
| 35 | virtual void AddUnique(const IPAddress &ip); |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 36 | virtual void Remove(const IPAddress &ip); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 37 | virtual void Clear(); |
Samuel Tan | aba1000 | 2014-08-18 17:13:56 -0700 | [diff] [blame] | 38 | virtual bool Contains(const IPAddress &ip) const; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 39 | virtual size_t Count() const; |
| 40 | virtual bool Empty() const; |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 41 | const IPAddresses &GetIPAddresses() const { return ip_addresses_; } |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 42 | |
| 43 | virtual IPAddress GetRandomIP(); |
| 44 | |
| 45 | protected: |
| 46 | friend class IPAddressStoreTest; |
| 47 | |
| 48 | private: |
| 49 | IPAddresses ip_addresses_; |
Alex Vakulenko | 37c5d94 | 2014-06-17 18:17:43 -0700 | [diff] [blame] | 50 | std::default_random_engine random_engine_; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(IPAddressStore); |
| 53 | }; |
| 54 | |
| 55 | } // namespace shill |
| 56 | |
| 57 | #endif // SHILL_IP_ADDRESS_STORE_H_ |