OLD | NEW |
(Empty) | |
| 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_CLIENT_H_ |
| 6 #define NET_DNS_MDNS_CLIENT_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "net/dns/dns_query.h" |
| 13 #include "net/dns/dns_response.h" |
| 14 #include "net/dns/record_parsed.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 class RecordParsed; |
| 19 |
| 20 // Used in the MDnsListener delegate to signify what type of change has been |
| 21 // made to a record. |
| 22 enum MDnsUpdateType { |
| 23 kMDnsRecordAdded, |
| 24 kMDnsRecordChanged, |
| 25 kMDnsRecordRemoved |
| 26 }; |
| 27 |
| 28 // Used in the MDnsTransaction to signify what type of result the transaction |
| 29 // has recieved. |
| 30 enum MDnsTransactionResult { |
| 31 // Passed whenever a record is found. |
| 32 kMDnsTransactionRecord, |
| 33 // The transaction is done. Applies to non-single-valued transactions. Is |
| 34 // called when the transaction has finished (this is the last call to the |
| 35 // callback). |
| 36 kMDnsTransactionDone, |
| 37 // No results have been found. Applies to single-valued transactions. Is |
| 38 // called when the transaction has finished without finding any results. |
| 39 // For transactions that use the network, this happens when a timeout occurs, |
| 40 // for transactions that are cache-only, this happens when no results are in |
| 41 // the cache. |
| 42 kMDnsTransactionNoResults, |
| 43 // Called when an NSec record is read for this transaction's query. This means |
| 44 // there cannot possibly be a record of the type and name for this |
| 45 // transaction. |
| 46 kMDnsTransactionNSec |
| 47 }; |
| 48 |
| 49 // Used when creating an MDnsTransaction. |
| 50 enum MDnsTransactionFlags { |
| 51 // Transaction should return only one result, and stop listening after it. |
| 52 // Note that single result transactions will signal when their timeout is |
| 53 // reached, whereas multi-result transactions will not. |
| 54 kMDnsTransactionSingleResult = 1 << 0, |
| 55 // Query the cache or the network. May both be used. One must be present. |
| 56 kMDnsTransactionQueryCache = 1 << 1, |
| 57 kMDnsTransactionQueryNetwork = 1 << 2, |
| 58 // TODO(noamsml): Add flag for flushing cache when feature is implemented |
| 59 // Mask of all possible flags on MDnsTransaction. |
| 60 kMDnsTransactionFlagMask = (1 << 3) - 1, |
| 61 }; |
| 62 |
| 63 // Represents a one-time record lookup. A transaction takes one |
| 64 // associated callback (see |MDnsClient::CreateTransaction|) and calls it |
| 65 // whenever a matching record has been found, either from the cache or |
| 66 // by querying the network (it may choose to query either or both based on its |
| 67 // creation flags, see MDnsTransactionFlags). Network-based transactions will |
| 68 // time out after a reasonable number of seconds. |
| 69 class MDnsTransaction { |
| 70 public: |
| 71 typedef base::Callback<void(MDnsTransactionResult, const RecordParsed*)> |
| 72 ResultCallback; |
| 73 |
| 74 // Destroying the transaction cancels it. |
| 75 virtual ~MDnsTransaction() {} |
| 76 |
| 77 // Start the transaction. Return true on success. |
| 78 virtual bool Start() = 0; |
| 79 |
| 80 // Get the host or service name for the transaction. |
| 81 virtual const std::string& GetName() const = 0; |
| 82 |
| 83 // Get the type for this transaction (SRV, TXT, A, AAA, etc) |
| 84 virtual uint16 GetType() const = 0; |
| 85 }; |
| 86 |
| 87 // A listener listens for updates regarding a specific record or set of records. |
| 88 class MDnsListener { |
| 89 public: |
| 90 class Delegate { |
| 91 public: |
| 92 virtual ~Delegate() {} |
| 93 |
| 94 // Called when a record is added, removed or updated. |
| 95 virtual void OnRecordUpdate(MDnsUpdateType update, |
| 96 const RecordParsed* record) = 0; |
| 97 |
| 98 // Called when a record is marked nonexistent by an NSEC record. |
| 99 virtual void OnNsecRecord(const std::string& name, unsigned type) = 0; |
| 100 |
| 101 // Called when the cache is purged (due, for example, ot the network |
| 102 // disconnecting). |
| 103 virtual void OnCachePurged() = 0; |
| 104 }; |
| 105 |
| 106 // Destroying the listener stops listening. |
| 107 virtual ~MDnsListener() {} |
| 108 |
| 109 // Start the listener. Return true on success. |
| 110 virtual bool Start() = 0; |
| 111 |
| 112 // Get the host or service name for this query. |
| 113 // Return an empty string for no name. |
| 114 virtual const std::string& GetName() const = 0; |
| 115 |
| 116 // Get the type for this query (SRV, TXT, A, AAA, etc) |
| 117 virtual uint16 GetType() const = 0; |
| 118 }; |
| 119 |
| 120 // Listens for Multicast DNS on the local network. You can access |
| 121 // information regarding multicast DNS either by creating an |MDnsListener| to |
| 122 // be notified of new records, or by creating an |MDnsTransaction| to look up |
| 123 // the value of a specific records. |
| 124 class MDnsClient { |
| 125 public: |
| 126 virtual ~MDnsClient() {} |
| 127 |
| 128 // Create listener object for RRType |rrtype| and name |name|. |
| 129 // If |name| is an empty string, listen to all notification of type |
| 130 // |rrtype|. |
| 131 virtual scoped_ptr<MDnsListener> CreateListener( |
| 132 uint16 rrtype, |
| 133 const std::string& name, |
| 134 MDnsListener::Delegate* delegate) = 0; |
| 135 |
| 136 // Create a transaction that can be used to query either the MDns cache, the |
| 137 // network, or both. |
| 138 virtual scoped_ptr<MDnsTransaction> CreateTransaction( |
| 139 uint16 rrtype, |
| 140 const std::string& name, |
| 141 int flags, // See MDnsTransactionFlags |
| 142 const MDnsTransaction::ResultCallback& callback) = 0; |
| 143 |
| 144 // Lazily create and return static instance for MDnsClient. |
| 145 static MDnsClient* GetInstance(); |
| 146 |
| 147 // Set the global instance (for testing). MUST be called before the first call |
| 148 // to GetInstance. |
| 149 static void SetInstance(MDnsClient* instance); |
| 150 }; |
| 151 |
| 152 } // net |
| 153 #endif // NET_DNS_MDNS_CLIENT_H_ |
OLD | NEW |