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 | #include "shill/ip_address_store.h" |
| 6 | |
| 7 | #include <iterator> |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <time.h> |
| 11 | |
| 12 | using std::advance; |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | // This is a less than comparison so that IPAddress can be stored in a set. |
| 17 | // We do not care about a semantically meaningful comparison. This is |
| 18 | // deterministic, and that's all that matters. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 19 | bool IPAddressLTIgnorePrefix::operator () (const IPAddress& lhs, |
| 20 | const IPAddress& rhs) const { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 21 | return lhs.ToString() < rhs.ToString(); |
| 22 | } |
| 23 | |
Alex Vakulenko | 37c5d94 | 2014-06-17 18:17:43 -0700 | [diff] [blame] | 24 | IPAddressStore::IPAddressStore() : random_engine_(time(nullptr)) { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | IPAddressStore::~IPAddressStore() {} |
| 28 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 29 | void IPAddressStore::AddUnique(const IPAddress& ip) { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 30 | ip_addresses_.insert(ip); |
| 31 | } |
| 32 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 33 | void IPAddressStore::Remove(const IPAddress& ip) { |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 34 | ip_addresses_.erase(ip); |
| 35 | } |
| 36 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 37 | void IPAddressStore::Clear() { |
| 38 | ip_addresses_.clear(); |
| 39 | } |
| 40 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 41 | bool IPAddressStore::Contains(const IPAddress& ip) const { |
Samuel Tan | aba1000 | 2014-08-18 17:13:56 -0700 | [diff] [blame] | 42 | return ip_addresses_.find(ip) != ip_addresses_.end(); |
| 43 | } |
| 44 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 45 | size_t IPAddressStore::Count() const { |
| 46 | return ip_addresses_.size(); |
| 47 | } |
| 48 | |
| 49 | bool IPAddressStore::Empty() const { |
| 50 | return ip_addresses_.empty(); |
| 51 | } |
| 52 | |
| 53 | IPAddress IPAddressStore::GetRandomIP() { |
| 54 | if (ip_addresses_.empty()) |
| 55 | return IPAddress(IPAddress::kFamilyUnknown); |
Alex Vakulenko | 37c5d94 | 2014-06-17 18:17:43 -0700 | [diff] [blame] | 56 | std::uniform_int_distribution<int> uniform_rand(0, ip_addresses_.size() - 1); |
| 57 | int index = uniform_rand(random_engine_); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 58 | IPAddresses::const_iterator cit = ip_addresses_.begin(); |
| 59 | advance(cit, index); |
| 60 | return *cit; |
| 61 | } |
| 62 | |
| 63 | } // namespace shill |