1 // 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.
5 #ifndef NET_DNS_MDNS_CACHE_H_
6 #define NET_DNS_MDNS_CACHE_H_
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time.h"
15 #include "net/base/net_export.h"
19 class ParsedDnsRecord
;
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
26 class NET_EXPORT_PRIVATE MDnsCache
{
28 // 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.
34 Key(unsigned type
, const std::string
& name
, const std::string
& optional
);
36 Key
& operator=(const Key
&);
38 bool operator<(const Key
& key
) const;
39 bool operator==(const Key
& key
) const;
41 unsigned type() const { return type_
; }
42 const std::string
& name() const { return name_
; }
43 const std::string
& optional() const { return optional_
; }
45 // Create the cache key corresponding to |record|.
46 static Key
CreateFor(const RecordParsed
* record
);
50 std::string optional_
;
53 typedef base::Callback
<void(const RecordParsed
*)> RecordRemovedCallback
;
64 // Return value indicates whether the record was added, changed
65 // (existed previously with different value) or not changed (existed
66 // previously with same value).
67 UpdateType
UpdateDnsRecord(scoped_ptr
<const RecordParsed
> record
);
69 // Check cache for record with key |key|. Return the record if it exists, or
70 // NULL if it doesn't.
71 const RecordParsed
* LookupKey(const Key
& key
);
73 // Return records with type |type| and name |name|. Expired records will not
74 // be returned. If |name| is empty, return all records with type |type|.
75 void FindDnsRecords(unsigned type
,
76 const std::string
& name
,
77 std::vector
<const RecordParsed
*>* records
,
78 base::Time now
) const;
80 // Remove expired records, call |record_removed_callback| for every removed
82 void CleanupRecords(base::Time now
,
83 const RecordRemovedCallback
& record_removed_callback
);
85 // Returns a time less than or equal to the next time a record will expire.
86 // Is updated when CleanupRecords or UpdateDnsRecord are called. Returns
87 // base::Time when the cache is empty.
88 base::Time
next_expiration() const { return next_expiration_
; }
93 typedef std::map
<Key
, const RecordParsed
*> RecordMap
;
95 // Get the effective expiration of a cache entry, based on its creation time
96 // and TTL. Does adjustments so entries with a TTL of zero will have a
97 // nonzero TTL, as explained in RFC 6762 Section 10.1.
98 static base::Time
GetEffectiveExpiration(const RecordParsed
* entry
);
100 // Get optional part of the DNS key for shared records. For example, in PTR
101 // records this is the pointed domain, since multiple PTR records may exist
102 // for the same name.
103 static std::string
GetOptionalFieldForRecord(
104 const RecordParsed
* record
);
106 RecordMap mdns_cache_
;
108 base::Time next_expiration_
;
110 DISALLOW_COPY_AND_ASSIGN(MDnsCache
);
115 #endif // NET_DNS_MDNS_CACHE_H_