blob: c041e54694be759224b7850cd3ba092e8c37581f [file] [log] [blame]
[email protected]f6a9add2013-05-23 00:56:361// 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>
danakj22f90e72016-04-16 01:55:409#include <memory>
[email protected]f6a9add2013-05-23 00:56:3610#include <string>
11#include <vector>
12
13#include "base/callback.h"
Avi Drissman13fc8932015-12-20 04:40:4614#include "base/macros.h"
[email protected]66e96c42013-06-28 15:20:3115#include "base/time/time.h"
[email protected]f6a9add2013-05-23 00:56:3616#include "net/base/net_export.h"
17
18namespace net {
19
[email protected]f6a9add2013-05-23 00:56:3620class RecordParsed;
21
22// mDNS Cache
23// This is a cache of mDNS records. It keeps track of expiration times and is
24// guaranteed not to return expired records. It also has facilities for timely
25// record expiration.
26class NET_EXPORT_PRIVATE MDnsCache {
[email protected]21df16962013-07-01 17:39:5327 public:
[email protected]5e6f42732013-06-19 03:43:3128 // Key type for the record map. It is a 3-tuple of type, name and optional
29 // value ordered by type, then name, then optional value. This allows us to
30 // query for all records of a certain type and name, while also allowing us
31 // to set records of a certain type, name and optionally value as unique.
32 class Key {
33 public:
34 Key(unsigned type, const std::string& name, const std::string& optional);
35 Key(const Key&);
36 Key& operator=(const Key&);
37 ~Key();
38 bool operator<(const Key& key) const;
39 bool operator==(const Key& key) const;
40
41 unsigned type() const { return type_; }
Piotr Pawliczek3318cc92020-07-25 05:10:1342 const std::string& name_lowercase() const { return name_lowercase_; }
[email protected]5e6f42732013-06-19 03:43:3143 const std::string& optional() const { return optional_; }
44
45 // Create the cache key corresponding to |record|.
46 static Key CreateFor(const RecordParsed* record);
47 private:
48 unsigned type_;
Piotr Pawliczek3318cc92020-07-25 05:10:1349 std::string name_lowercase_;
[email protected]5e6f42732013-06-19 03:43:3150 std::string optional_;
51 };
52
Anna Malova9b2095b2020-03-04 15:41:1853 typedef base::RepeatingCallback<void(const RecordParsed*)>
54 RecordRemovedCallback;
[email protected]f6a9add2013-05-23 00:56:3655
56 enum UpdateType {
57 RecordAdded,
58 RecordChanged,
[email protected]bdd6c3d2014-01-30 08:07:4259 RecordRemoved,
[email protected]f6a9add2013-05-23 00:56:3660 NoChange
61 };
62
63 MDnsCache();
Peter Boström407869b2021-10-07 04:42:4864
65 MDnsCache(const MDnsCache&) = delete;
66 MDnsCache& operator=(const MDnsCache&) = delete;
67
[email protected]f6a9add2013-05-23 00:56:3668 ~MDnsCache();
69
70 // Return value indicates whether the record was added, changed
71 // (existed previously with different value) or not changed (existed
72 // previously with same value).
danakj22f90e72016-04-16 01:55:4073 UpdateType UpdateDnsRecord(std::unique_ptr<const RecordParsed> record);
[email protected]f6a9add2013-05-23 00:56:3674
[email protected]5e6f42732013-06-19 03:43:3175 // Check cache for record with key |key|. Return the record if it exists, or
76 // NULL if it doesn't.
77 const RecordParsed* LookupKey(const Key& key);
78
[email protected]f6a9add2013-05-23 00:56:3679 // Return records with type |type| and name |name|. Expired records will not
[email protected]21df16962013-07-01 17:39:5380 // be returned. If |type| is zero, return all records with name |name|.
[email protected]f6a9add2013-05-23 00:56:3681 void FindDnsRecords(unsigned type,
82 const std::string& name,
83 std::vector<const RecordParsed*>* records,
84 base::Time now) const;
85
86 // Remove expired records, call |record_removed_callback| for every removed
87 // record.
88 void CleanupRecords(base::Time now,
89 const RecordRemovedCallback& record_removed_callback);
90
91 // Returns a time less than or equal to the next time a record will expire.
92 // Is updated when CleanupRecords or UpdateDnsRecord are called. Returns
93 // base::Time when the cache is empty.
94 base::Time next_expiration() const { return next_expiration_; }
95
[email protected]21df16962013-07-01 17:39:5396 // Remove a record from the cache. Returns a scoped version of the pointer
97 // passed in if it was removed, scoped null otherwise.
danakj22f90e72016-04-16 01:55:4098 std::unique_ptr<const RecordParsed> RemoveRecord(const RecordParsed* record);
[email protected]21df16962013-07-01 17:39:5399
Eric Orthacdd8ae2019-03-29 22:07:19100 bool IsCacheOverfilled() const;
101
102 void set_entry_limit_for_testing(size_t entry_limit) {
103 entry_limit_ = entry_limit;
104 }
105
[email protected]21df16962013-07-01 17:39:53106 private:
danakj22f90e72016-04-16 01:55:40107 typedef std::map<Key, std::unique_ptr<const RecordParsed>> RecordMap;
[email protected]f6a9add2013-05-23 00:56:36108
109 // Get the effective expiration of a cache entry, based on its creation time
110 // and TTL. Does adjustments so entries with a TTL of zero will have a
111 // nonzero TTL, as explained in RFC 6762 Section 10.1.
[email protected]5e6f42732013-06-19 03:43:31112 static base::Time GetEffectiveExpiration(const RecordParsed* entry);
[email protected]f6a9add2013-05-23 00:56:36113
114 // Get optional part of the DNS key for shared records. For example, in PTR
115 // records this is the pointed domain, since multiple PTR records may exist
116 // for the same name.
vitalybukaa97cddb2016-01-06 05:25:17117 static std::string GetOptionalFieldForRecord(const RecordParsed* record);
[email protected]f6a9add2013-05-23 00:56:36118
119 RecordMap mdns_cache_;
120
121 base::Time next_expiration_;
Eric Orthacdd8ae2019-03-29 22:07:19122 size_t entry_limit_;
[email protected]f6a9add2013-05-23 00:56:36123};
124
125} // namespace net
126
127#endif // NET_DNS_MDNS_CACHE_H_