Home | History | Annotate | Download | only in dns
      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 
      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"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/time/time.h"
     15 #include "net/base/net_export.h"
     16 
     17 namespace net {
     18 
     19 class ParsedDnsRecord;
     20 class RecordParsed;
     21 
     22 // mDNS Cache
     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
     25 // record expiration.
     26 class NET_EXPORT_PRIVATE MDnsCache {
     27  public:
     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.
     32   class Key {
     33    public:
     34     Key(unsigned type, const std::string& name, const std::string& optional);
     35     Key(const Key&);
     36     Key& operator=(const Key&);
     37     ~Key();
     38     bool operator<(const Key& key) const;
     39     bool operator==(const Key& key) const;
     40 
     41     unsigned type() const { return type_; }
     42     const std::string& name() const  { return name_; }
     43     const std::string& optional() const { return optional_; }
     44 
     45     // Create the cache key corresponding to |record|.
     46     static Key CreateFor(const RecordParsed* record);
     47    private:
     48     unsigned type_;
     49     std::string name_;
     50     std::string optional_;
     51   };
     52 
     53   typedef base::Callback<void(const RecordParsed*)> RecordRemovedCallback;
     54 
     55   enum UpdateType {
     56     RecordAdded,
     57     RecordChanged,
     58     RecordRemoved,
     59     NoChange
     60   };
     61 
     62   MDnsCache();
     63   ~MDnsCache();
     64 
     65   // Return value indicates whether the record was added, changed
     66   // (existed previously with different value) or not changed (existed
     67   // previously with same value).
     68   UpdateType UpdateDnsRecord(scoped_ptr<const RecordParsed> record);
     69 
     70   // Check cache for record with key |key|. Return the record if it exists, or
     71   // NULL if it doesn't.
     72   const RecordParsed* LookupKey(const Key& key);
     73 
     74   // Return records with type |type| and name |name|. Expired records will not
     75   // be returned. If |type| is zero, return all records with name |name|.
     76   void FindDnsRecords(unsigned type,
     77                       const std::string& name,
     78                       std::vector<const RecordParsed*>* records,
     79                       base::Time now) const;
     80 
     81   // Remove expired records, call |record_removed_callback| for every removed
     82   // record.
     83   void CleanupRecords(base::Time now,
     84                       const RecordRemovedCallback& record_removed_callback);
     85 
     86   // Returns a time less than or equal to the next time a record will expire.
     87   // Is updated when CleanupRecords or UpdateDnsRecord are called. Returns
     88   // base::Time when the cache is empty.
     89   base::Time next_expiration() const { return next_expiration_; }
     90 
     91   // Remove a record from the cache.  Returns a scoped version of the pointer
     92   // passed in if it was removed, scoped null otherwise.
     93   scoped_ptr<const RecordParsed> RemoveRecord(const RecordParsed* record);
     94 
     95   void Clear();
     96 
     97  private:
     98   typedef std::map<Key, const RecordParsed*> RecordMap;
     99 
    100   // Get the effective expiration of a cache entry, based on its creation time
    101   // and TTL. Does adjustments so entries with a TTL of zero will have a
    102   // nonzero TTL, as explained in RFC 6762 Section 10.1.
    103   static base::Time GetEffectiveExpiration(const RecordParsed* entry);
    104 
    105   // Get optional part of the DNS key for shared records. For example, in PTR
    106   // records this is the pointed domain, since multiple PTR records may exist
    107   // for the same name.
    108   static std::string GetOptionalFieldForRecord(
    109       const RecordParsed* record);
    110 
    111   RecordMap mdns_cache_;
    112 
    113   base::Time next_expiration_;
    114 
    115   DISALLOW_COPY_AND_ASSIGN(MDnsCache);
    116 };
    117 
    118 }  // namespace net
    119 
    120 #endif  // NET_DNS_MDNS_CACHE_H_
    121