blob: 0909c531c095b62b36415e7a2fa01dd1f11b4b19 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2013 The Chromium Authors
[email protected]f6a9add2013-05-23 00:56:362// 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
Avi Drissman41c4a412023-01-11 22:45:3713#include "base/functional/callback.h"
[email protected]66e96c42013-06-28 15:20:3114#include "base/time/time.h"
[email protected]f6a9add2013-05-23 00:56:3615#include "net/base/net_export.h"
16
17namespace net {
18
[email protected]f6a9add2013-05-23 00:56:3619class 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.
25class NET_EXPORT_PRIVATE MDnsCache {
[email protected]21df16962013-07-01 17:39:5326 public:
[email protected]5e6f42732013-06-19 03:43:3127 // 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 Pawliczek3318cc92020-07-25 05:10:1341 const std::string& name_lowercase() const { return name_lowercase_; }
[email protected]5e6f42732013-06-19 03:43:3142 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 Pawliczek3318cc92020-07-25 05:10:1348 std::string name_lowercase_;
[email protected]5e6f42732013-06-19 03:43:3149 std::string optional_;
50 };
51
Anna Malova9b2095b2020-03-04 15:41:1852 typedef base::RepeatingCallback<void(const RecordParsed*)>
53 RecordRemovedCallback;
[email protected]f6a9add2013-05-23 00:56:3654
55 enum UpdateType {
56 RecordAdded,
57 RecordChanged,
[email protected]bdd6c3d2014-01-30 08:07:4258 RecordRemoved,
[email protected]f6a9add2013-05-23 00:56:3659 NoChange
60 };
61
62 MDnsCache();
Peter Boström407869b2021-10-07 04:42:4863
64 MDnsCache(const MDnsCache&) = delete;
65 MDnsCache& operator=(const MDnsCache&) = delete;
66
[email protected]f6a9add2013-05-23 00:56:3667 ~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).
danakj22f90e72016-04-16 01:55:4072 UpdateType UpdateDnsRecord(std::unique_ptr<const RecordParsed> record);
[email protected]f6a9add2013-05-23 00:56:3673
[email protected]5e6f42732013-06-19 03:43:3174 // 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]f6a9add2013-05-23 00:56:3678 // Return records with type |type| and name |name|. Expired records will not
[email protected]21df16962013-07-01 17:39:5379 // be returned. If |type| is zero, return all records with name |name|.
[email protected]f6a9add2013-05-23 00:56:3680 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]21df16962013-07-01 17:39:5395 // Remove a record from the cache. Returns a scoped version of the pointer
96 // passed in if it was removed, scoped null otherwise.
danakj22f90e72016-04-16 01:55:4097 std::unique_ptr<const RecordParsed> RemoveRecord(const RecordParsed* record);
[email protected]21df16962013-07-01 17:39:5398
Eric Orthacdd8ae2019-03-29 22:07:1999 bool IsCacheOverfilled() const;
100
101 void set_entry_limit_for_testing(size_t entry_limit) {
102 entry_limit_ = entry_limit;
103 }
104
[email protected]21df16962013-07-01 17:39:53105 private:
danakj22f90e72016-04-16 01:55:40106 typedef std::map<Key, std::unique_ptr<const RecordParsed>> RecordMap;
[email protected]f6a9add2013-05-23 00:56:36107
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]5e6f42732013-06-19 03:43:31111 static base::Time GetEffectiveExpiration(const RecordParsed* entry);
[email protected]f6a9add2013-05-23 00:56:36112
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.
vitalybukaa97cddb2016-01-06 05:25:17116 static std::string GetOptionalFieldForRecord(const RecordParsed* record);
[email protected]f6a9add2013-05-23 00:56:36117
118 RecordMap mdns_cache_;
119
120 base::Time next_expiration_;
Eric Orthacdd8ae2019-03-29 22:07:19121 size_t entry_limit_;
[email protected]f6a9add2013-05-23 00:56:36122};
123
124} // namespace net
125
126#endif // NET_DNS_MDNS_CACHE_H_