xref: /aosp_15_r20/external/cronet/net/dns/mdns_cache.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
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 <memory>
10 #include <string>
11 #include <vector>
12 
13 #include "base/functional/callback.h"
14 #include "base/time/time.h"
15 #include "net/base/net_export.h"
16 
17 namespace net {
18 
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 {
26  public:
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 
type()40     unsigned type() const { return type_; }
name_lowercase()41     const std::string& name_lowercase() const { return name_lowercase_; }
optional()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_;
48     std::string name_lowercase_;
49     std::string optional_;
50   };
51 
52   typedef base::RepeatingCallback<void(const RecordParsed*)>
53       RecordRemovedCallback;
54 
55   enum UpdateType {
56     RecordAdded,
57     RecordChanged,
58     RecordRemoved,
59     NoChange
60   };
61 
62   MDnsCache();
63 
64   MDnsCache(const MDnsCache&) = delete;
65   MDnsCache& operator=(const MDnsCache&) = delete;
66 
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).
72   UpdateType UpdateDnsRecord(std::unique_ptr<const RecordParsed> record);
73 
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 
78   // Return records with type |type| and name |name|. Expired records will not
79   // be returned. If |type| is zero, return all records with name |name|.
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.
next_expiration()93   base::Time next_expiration() const { return next_expiration_; }
94 
95   // Remove a record from the cache.  Returns a scoped version of the pointer
96   // passed in if it was removed, scoped null otherwise.
97   std::unique_ptr<const RecordParsed> RemoveRecord(const RecordParsed* record);
98 
99   bool IsCacheOverfilled() const;
100 
set_entry_limit_for_testing(size_t entry_limit)101   void set_entry_limit_for_testing(size_t entry_limit) {
102     entry_limit_ = entry_limit;
103   }
104 
105  private:
106   typedef std::map<Key, std::unique_ptr<const RecordParsed>> RecordMap;
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.
111   static base::Time GetEffectiveExpiration(const RecordParsed* entry);
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.
116   static std::string GetOptionalFieldForRecord(const RecordParsed* record);
117 
118   RecordMap mdns_cache_;
119 
120   base::Time next_expiration_;
121   size_t entry_limit_;
122 };
123 
124 }  // namespace net
125 
126 #endif  // NET_DNS_MDNS_CACHE_H_
127