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