blob: e8f9153f0094933424d04d36554d116f4cb8e3b4 [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>
9#include <string>
10#include <vector>
11
12#include "base/callback.h"
Avi Drissman13fc8932015-12-20 04:40:4613#include "base/macros.h"
[email protected]f6a9add2013-05-23 00:56:3614#include "base/memory/scoped_ptr.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
20class ParsedDnsRecord;
21class 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.
27class NET_EXPORT_PRIVATE MDnsCache {
[email protected]21df16962013-07-01 17:39:5328 public:
[email protected]5e6f42732013-06-19 03:43:3129 // 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]f6a9add2013-05-23 00:56:3654 typedef base::Callback<void(const RecordParsed*)> RecordRemovedCallback;
55
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();
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]5e6f42732013-06-19 03:43:3171 // 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]f6a9add2013-05-23 00:56:3675 // Return records with type |type| and name |name|. Expired records will not
[email protected]21df16962013-07-01 17:39:5376 // be returned. If |type| is zero, return all records with name |name|.
[email protected]f6a9add2013-05-23 00:56:3677 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]21df16962013-07-01 17:39:5392 // 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]f6a9add2013-05-23 00:56:3696 void Clear();
97
[email protected]21df16962013-07-01 17:39:5398 private:
[email protected]f6a9add2013-05-23 00:56:3699 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]5e6f42732013-06-19 03:43:31104 static base::Time GetEffectiveExpiration(const RecordParsed* entry);
[email protected]f6a9add2013-05-23 00:56:36105
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]5e6f42732013-06-19 03:43:31109 static std::string GetOptionalFieldForRecord(
110 const RecordParsed* record);
[email protected]f6a9add2013-05-23 00:56:36111
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_