blob: 1e79ad8f9cbe8d4fbed535b4fc876304c79aaa64 [file] [log] [blame]
Prathmesh Prabhuba99b592013-04-17 15:13:14 -07001// 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 Vakulenko37c5d942014-06-17 18:17:43 -07008#include <random>
Prathmesh Prabhuba99b592013-04-17 15:13:14 -07009#include <set>
10
11#include "shill/ip_address.h"
12
13namespace shill {
14
15struct 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.
27class 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 Tanfe734672014-08-07 15:50:48 -070036 virtual void Remove(const IPAddress &ip);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -070037 virtual void Clear();
Samuel Tanaba10002014-08-18 17:13:56 -070038 virtual bool Contains(const IPAddress &ip) const;
Prathmesh Prabhuba99b592013-04-17 15:13:14 -070039 virtual size_t Count() const;
40 virtual bool Empty() const;
Samuel Tanfe734672014-08-07 15:50:48 -070041 const IPAddresses &GetIPAddresses() const { return ip_addresses_; }
Prathmesh Prabhuba99b592013-04-17 15:13:14 -070042
43 virtual IPAddress GetRandomIP();
44
45 protected:
46 friend class IPAddressStoreTest;
47
48 private:
49 IPAddresses ip_addresses_;
Alex Vakulenko37c5d942014-06-17 18:17:43 -070050 std::default_random_engine random_engine_;
Prathmesh Prabhuba99b592013-04-17 15:13:14 -070051
52 DISALLOW_COPY_AND_ASSIGN(IPAddressStore);
53};
54
55} // namespace shill
56
57#endif // SHILL_IP_ADDRESS_STORE_H_