OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_DNS_MDNS_CLIENT_H_ | 5 #ifndef NET_DNS_MDNS_CLIENT_H_ |
6 #define NET_DNS_MDNS_CLIENT_H_ | 6 #define NET_DNS_MDNS_CLIENT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "net/dns/dns_query.h" | 12 #include "net/dns/dns_query.h" |
13 #include "net/dns/dns_response.h" | 13 #include "net/dns/dns_response.h" |
14 #include "net/dns/record_parsed.h" | 14 #include "net/dns/record_parsed.h" |
15 | 15 |
16 namespace net { | 16 namespace net { |
17 | 17 |
18 class RecordParsed; | 18 class RecordParsed; |
19 | 19 |
20 // Used in the MDnsListener delegate to signify what type of change has been | |
21 // made to a record. | |
20 enum MDnsUpdateType { | 22 enum MDnsUpdateType { |
21 kMDnsRecordAdded, | 23 kMDnsRecordAdded, |
22 kMDnsRecordChanged, | 24 kMDnsRecordChanged, |
23 kMDnsRecordRemoved | 25 kMDnsRecordRemoved |
24 }; | 26 }; |
25 | 27 |
28 // Used in the MDnsTransaction to signify what type of result the transaction | |
29 // has recieved. | |
26 enum MDnsTransactionResult { | 30 enum MDnsTransactionResult { |
27 kMDnsTransactionSuccess, | 31 // Passed whenever a record is found. |
28 kMDnsTransactionTimeout, | 32 kMDnsTransactionRecord, |
29 kMDnsTransactionNsec | 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 | |
30 }; | 47 }; |
31 | 48 |
32 // A class representing a one-time unique-record lookup. A transaction takes one | 49 // Used when creating an MDnsTransaction. |
33 // associated callback (see |MDnsClient::CreateTransaction|) and calls it when | 50 enum MDnsTransactionFlags { |
34 // a matching record has been found, either from the cache or by querying the | 51 // Transaction should return only one result, and stop listening after it. |
35 // network. Transactions will time out after a reasonable number of seconds. | 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. | |
36 class MDnsTransaction { | 69 class MDnsTransaction { |
37 public: | 70 public: |
38 typedef base::Callback<void(MDnsTransactionResult, const RecordParsed*)> | 71 typedef base::Callback<void(MDnsTransactionResult, const RecordParsed*)> |
39 ResultCallback; | 72 ResultCallback; |
40 | 73 |
41 // Destroying the transaction cancels it. | 74 // Destroying the transaction cancels it. |
42 virtual ~MDnsTransaction() {} | 75 virtual ~MDnsTransaction() {} |
43 | 76 |
44 // Start the transaction. | 77 // Start the transaction. Return true on success. |
45 virtual bool Start() = 0; | 78 virtual bool Start() = 0; |
46 | 79 |
47 // Get the host or service name for the transaction. | 80 // Get the host or service name for the transaction. |
48 virtual const std::string& GetName() const = 0; | 81 virtual const std::string& GetName() const = 0; |
49 | 82 |
50 // Get the type for this transaction (SRV, TXT, A, AAA, etc) | 83 // Get the type for this transaction (SRV, TXT, A, AAA, etc) |
51 virtual uint16 GetType() const = 0; | 84 virtual uint16 GetType() const = 0; |
52 }; | 85 }; |
53 | 86 |
54 // A listener listens for updates regarding a specific record or set of records. | 87 // A listener listens for updates regarding a specific record or set of records. |
55 // If a listener is active (determined at its creation) it will send queries | |
56 // when appropriate. | |
57 class MDnsListener { | 88 class MDnsListener { |
58 public: | 89 public: |
59 class Delegate { | 90 class Delegate { |
60 public: | 91 public: |
61 virtual ~Delegate() {} | 92 virtual ~Delegate() {} |
62 | 93 |
63 // Called when a record is added, removed or updated. | 94 // Called when a record is added, removed or updated. |
64 virtual void OnRecordUpdate(MDnsUpdateType update, | 95 virtual void OnRecordUpdate(MDnsUpdateType update, |
65 const RecordParsed* record) = 0; | 96 const RecordParsed* record) = 0; |
66 | 97 |
67 // Called when a record is marked nonexistent by an NSEC record. | 98 // Called when a record is marked nonexistent by an NSEC record. |
68 virtual void OnNsecRecord(const std::string& name, unsigned type) = 0; | 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; | |
69 }; | 104 }; |
70 | 105 |
71 // Destroying the listener stops listening. | 106 // Destroying the listener stops listening. |
72 virtual ~MDnsListener() {} | 107 virtual ~MDnsListener() {} |
73 | 108 |
74 // Start the listener. | 109 // Start the listener. Return true on success. |
75 virtual bool Start() = 0; | 110 virtual bool Start() = 0; |
76 | 111 |
77 // Get the host or service name for this query. | 112 // Get the host or service name for this query. |
78 // Return an empty string for no name. | 113 // Return an empty string for no name. |
79 virtual const std::string& GetName() const = 0; | 114 virtual const std::string& GetName() const = 0; |
80 | 115 |
81 // Get the type for this query (SRV, TXT, A, AAA, etc) | 116 // Get the type for this query (SRV, TXT, A, AAA, etc) |
82 virtual uint16 GetType() const = 0; | 117 virtual uint16 GetType() const = 0; |
83 | |
84 // Whether this listener is active (true) or passive (false) | |
85 virtual bool IsActive() const = 0; | |
86 | |
87 // Applies only to listeners with names. Will send out a query for new | |
88 // information. |force_refresh_cache| will force a refresh of all cached | |
89 // entities. | |
90 virtual bool SendQuery(bool force_refresh_cache) = 0; | |
91 | |
92 // Applies only to listeners with names. Query mDNS cache synchronously for | |
93 // either single- or multi- valued records. | |
94 virtual bool QueryCache(std::vector<const RecordParsed*>* records) const = 0; | |
95 }; | 118 }; |
96 | 119 |
97 // This class listens for Multicast DNS on the local network. You can access | 120 // Listens for Multicast DNS on the local network. You can access |
98 // information regarding multicast DNS either by creating an |MDnsListener| to | 121 // information regarding multicast DNS either by creating an |MDnsListener| to |
99 // be notified of new records, or by creating an |MDnsTransaction| to look up | 122 // be notified of new records, or by creating an |MDnsTransaction| to look up |
100 // the value of a specific records. | 123 // the value of a specific records. |
101 class MDnsClient { | 124 class MDnsClient { |
102 public: | 125 public: |
103 virtual ~MDnsClient() {} | 126 virtual ~MDnsClient() {} |
104 | 127 |
105 // Create listener object for RRType |rrtype| and name |name|. | 128 // Create listener object for RRType |rrtype| and name |name|. |
106 // If |name| is an empty string, listen to all notification of type | 129 // If |name| is an empty string, listen to all notification of type |
107 // |rrtype|. If a listener is |active|, it will send queries when the client | 130 // |rrtype|. |
108 // thinks they are needed. If |alert_existing_records| is true, a listener's | |
109 // delegate will be alerted for records already in the cache when it is | |
110 // created. | |
111 virtual scoped_ptr<MDnsListener> CreateListener( | 131 virtual scoped_ptr<MDnsListener> CreateListener( |
112 uint16 rrtype, | 132 uint16 rrtype, |
113 const std::string& name, | 133 const std::string& name, |
114 bool active, | |
115 bool alert_existing_records, | |
116 MDnsListener::Delegate* delegate) = 0; | 134 MDnsListener::Delegate* delegate) = 0; |
117 | 135 |
118 // Create a transaction to Query MDNS for a single-value query | 136 // Create a transaction that can be used to query either the MDns cache, the |
119 // (A, AAAA, TXT, and SRV) asynchronously. May defer to cache. | 137 // network, or both. |
120 virtual scoped_ptr<MDnsTransaction> CreateTransaction( | 138 virtual scoped_ptr<MDnsTransaction> CreateTransaction( |
121 uint16 rrtype, | 139 uint16 rrtype, |
122 const std::string& name, | 140 const std::string& name, |
141 int flags, // See MDnsTransactionFlags | |
szym
2013/06/10 21:58:28
Move this to the comment above.
Noam Samuel
2013/06/11 20:35:03
Done.
| |
123 const MDnsTransaction::ResultCallback& callback) = 0; | 142 const MDnsTransaction::ResultCallback& callback) = 0; |
124 | 143 |
125 // Lazily create and return static instance for MDnsClient. | 144 // Lazily create and return static instance for MDnsClient. |
126 static MDnsClient* GetInstance(); | 145 static MDnsClient* GetInstance(); |
127 | 146 |
128 // Set the global instance (for testing). MUST be called before the first call | 147 // Set the global instance (for testing). MUST be called before the first call |
129 // to GetInstance. | 148 // to GetInstance. |
130 static void SetInstance(MDnsClient* instance); | 149 static void SetInstance(MDnsClient* instance); |
131 }; | 150 }; |
132 | 151 |
133 } // net | 152 } // net |
134 #endif // NET_DNS_MDNS_CLIENT_H_ | 153 #endif // NET_DNS_MDNS_CLIENT_H_ |
OLD | NEW |