mdns_cache.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.
  4. #ifndef NET_DNS_MDNS_CACHE_H_
  5. #define NET_DNS_MDNS_CACHE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/time/time.h"
  12. #include "net/base/net_export.h"
  13. namespace net {
  14. class RecordParsed;
  15. // mDNS Cache
  16. // This is a cache of mDNS records. It keeps track of expiration times and is
  17. // guaranteed not to return expired records. It also has facilities for timely
  18. // record expiration.
  19. class NET_EXPORT_PRIVATE MDnsCache {
  20. public:
  21. // Key type for the record map. It is a 3-tuple of type, name and optional
  22. // value ordered by type, then name, then optional value. This allows us to
  23. // query for all records of a certain type and name, while also allowing us
  24. // to set records of a certain type, name and optionally value as unique.
  25. class Key {
  26. public:
  27. Key(unsigned type, const std::string& name, const std::string& optional);
  28. Key(const Key&);
  29. Key& operator=(const Key&);
  30. ~Key();
  31. bool operator<(const Key& key) const;
  32. bool operator==(const Key& key) const;
  33. unsigned type() const { return type_; }
  34. const std::string& name_lowercase() const { return name_lowercase_; }
  35. const std::string& optional() const { return optional_; }
  36. // Create the cache key corresponding to |record|.
  37. static Key CreateFor(const RecordParsed* record);
  38. private:
  39. unsigned type_;
  40. std::string name_lowercase_;
  41. std::string optional_;
  42. };
  43. typedef base::RepeatingCallback<void(const RecordParsed*)>
  44. RecordRemovedCallback;
  45. enum UpdateType {
  46. RecordAdded,
  47. RecordChanged,
  48. RecordRemoved,
  49. NoChange
  50. };
  51. MDnsCache();
  52. MDnsCache(const MDnsCache&) = delete;
  53. MDnsCache& operator=(const MDnsCache&) = delete;
  54. ~MDnsCache();
  55. // Return value indicates whether the record was added, changed
  56. // (existed previously with different value) or not changed (existed
  57. // previously with same value).
  58. UpdateType UpdateDnsRecord(std::unique_ptr<const RecordParsed> record);
  59. // Check cache for record with key |key|. Return the record if it exists, or
  60. // NULL if it doesn't.
  61. const RecordParsed* LookupKey(const Key& key);
  62. // Return records with type |type| and name |name|. Expired records will not
  63. // be returned. If |type| is zero, return all records with name |name|.
  64. void FindDnsRecords(unsigned type,
  65. const std::string& name,
  66. std::vector<const RecordParsed*>* records,
  67. base::Time now) const;
  68. // Remove expired records, call |record_removed_callback| for every removed
  69. // record.
  70. void CleanupRecords(base::Time now,
  71. const RecordRemovedCallback& record_removed_callback);
  72. // Returns a time less than or equal to the next time a record will expire.
  73. // Is updated when CleanupRecords or UpdateDnsRecord are called. Returns
  74. // base::Time when the cache is empty.
  75. base::Time next_expiration() const { return next_expiration_; }
  76. // Remove a record from the cache. Returns a scoped version of the pointer
  77. // passed in if it was removed, scoped null otherwise.
  78. std::unique_ptr<const RecordParsed> RemoveRecord(const RecordParsed* record);
  79. bool IsCacheOverfilled() const;
  80. void set_entry_limit_for_testing(size_t entry_limit) {
  81. entry_limit_ = entry_limit;
  82. }
  83. private:
  84. typedef std::map<Key, std::unique_ptr<const RecordParsed>> RecordMap;
  85. // Get the effective expiration of a cache entry, based on its creation time
  86. // and TTL. Does adjustments so entries with a TTL of zero will have a
  87. // nonzero TTL, as explained in RFC 6762 Section 10.1.
  88. static base::Time GetEffectiveExpiration(const RecordParsed* entry);
  89. // Get optional part of the DNS key for shared records. For example, in PTR
  90. // records this is the pointed domain, since multiple PTR records may exist
  91. // for the same name.
  92. static std::string GetOptionalFieldForRecord(const RecordParsed* record);
  93. RecordMap mdns_cache_;
  94. base::Time next_expiration_;
  95. size_t entry_limit_;
  96. };
  97. } // namespace net
  98. #endif // NET_DNS_MDNS_CACHE_H_