[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 1 | // Copyright (c) 2013 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_DNS_MDNS_CACHE_H_ |
| 6 | #define NET_DNS_MDNS_CACHE_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/callback.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 13 | #include "base/macros.h" |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 15 | #include "base/time/time.h" |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 16 | #include "net/base/net_export.h" |
| 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | class ParsedDnsRecord; |
| 21 | class RecordParsed; |
| 22 | |
| 23 | // mDNS Cache |
| 24 | // This is a cache of mDNS records. It keeps track of expiration times and is |
| 25 | // guaranteed not to return expired records. It also has facilities for timely |
| 26 | // record expiration. |
| 27 | class NET_EXPORT_PRIVATE MDnsCache { |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 28 | public: |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 29 | // Key type for the record map. It is a 3-tuple of type, name and optional |
| 30 | // value ordered by type, then name, then optional value. This allows us to |
| 31 | // query for all records of a certain type and name, while also allowing us |
| 32 | // to set records of a certain type, name and optionally value as unique. |
| 33 | class Key { |
| 34 | public: |
| 35 | Key(unsigned type, const std::string& name, const std::string& optional); |
| 36 | Key(const Key&); |
| 37 | Key& operator=(const Key&); |
| 38 | ~Key(); |
| 39 | bool operator<(const Key& key) const; |
| 40 | bool operator==(const Key& key) const; |
| 41 | |
| 42 | unsigned type() const { return type_; } |
| 43 | const std::string& name() const { return name_; } |
| 44 | const std::string& optional() const { return optional_; } |
| 45 | |
| 46 | // Create the cache key corresponding to |record|. |
| 47 | static Key CreateFor(const RecordParsed* record); |
| 48 | private: |
| 49 | unsigned type_; |
| 50 | std::string name_; |
| 51 | std::string optional_; |
| 52 | }; |
| 53 | |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 54 | typedef base::Callback<void(const RecordParsed*)> RecordRemovedCallback; |
| 55 | |
| 56 | enum UpdateType { |
| 57 | RecordAdded, |
| 58 | RecordChanged, |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 59 | RecordRemoved, |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 60 | NoChange |
| 61 | }; |
| 62 | |
| 63 | MDnsCache(); |
| 64 | ~MDnsCache(); |
| 65 | |
| 66 | // Return value indicates whether the record was added, changed |
| 67 | // (existed previously with different value) or not changed (existed |
| 68 | // previously with same value). |
| 69 | UpdateType UpdateDnsRecord(scoped_ptr<const RecordParsed> record); |
| 70 | |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 71 | // Check cache for record with key |key|. Return the record if it exists, or |
| 72 | // NULL if it doesn't. |
| 73 | const RecordParsed* LookupKey(const Key& key); |
| 74 | |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 75 | // Return records with type |type| and name |name|. Expired records will not |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 76 | // be returned. If |type| is zero, return all records with name |name|. |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 77 | void FindDnsRecords(unsigned type, |
| 78 | const std::string& name, |
| 79 | std::vector<const RecordParsed*>* records, |
| 80 | base::Time now) const; |
| 81 | |
| 82 | // Remove expired records, call |record_removed_callback| for every removed |
| 83 | // record. |
| 84 | void CleanupRecords(base::Time now, |
| 85 | const RecordRemovedCallback& record_removed_callback); |
| 86 | |
| 87 | // Returns a time less than or equal to the next time a record will expire. |
| 88 | // Is updated when CleanupRecords or UpdateDnsRecord are called. Returns |
| 89 | // base::Time when the cache is empty. |
| 90 | base::Time next_expiration() const { return next_expiration_; } |
| 91 | |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 92 | // Remove a record from the cache. Returns a scoped version of the pointer |
| 93 | // passed in if it was removed, scoped null otherwise. |
| 94 | scoped_ptr<const RecordParsed> RemoveRecord(const RecordParsed* record); |
| 95 | |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 96 | void Clear(); |
| 97 | |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 98 | private: |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 99 | typedef std::map<Key, const RecordParsed*> RecordMap; |
| 100 | |
| 101 | // Get the effective expiration of a cache entry, based on its creation time |
| 102 | // and TTL. Does adjustments so entries with a TTL of zero will have a |
| 103 | // nonzero TTL, as explained in RFC 6762 Section 10.1. |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 104 | static base::Time GetEffectiveExpiration(const RecordParsed* entry); |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 105 | |
| 106 | // Get optional part of the DNS key for shared records. For example, in PTR |
| 107 | // records this is the pointed domain, since multiple PTR records may exist |
| 108 | // for the same name. |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 109 | static std::string GetOptionalFieldForRecord( |
| 110 | const RecordParsed* record); |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 111 | |
| 112 | RecordMap mdns_cache_; |
| 113 | |
| 114 | base::Time next_expiration_; |
| 115 | |
| 116 | DISALLOW_COPY_AND_ASSIGN(MDnsCache); |
| 117 | }; |
| 118 | |
| 119 | } // namespace net |
| 120 | |
| 121 | #endif // NET_DNS_MDNS_CACHE_H_ |