1 // Copyright 2019 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 DISCOVERY_MDNS_PUBLIC_MDNS_SERVICE_H_ 6 #define DISCOVERY_MDNS_PUBLIC_MDNS_SERVICE_H_ 7 8 #include <functional> 9 #include <memory> 10 11 #include "discovery/common/config.h" 12 #include "discovery/mdns/public/mdns_constants.h" 13 #include "platform/base/error.h" 14 #include "platform/base/interface_info.h" 15 #include "platform/base/ip_address.h" 16 17 namespace openscreen { 18 19 class TaskRunner; 20 21 namespace discovery { 22 23 class DomainName; 24 class MdnsDomainConfirmedProvider; 25 class MdnsRecord; 26 class MdnsRecordChangedCallback; 27 class ReportingClient; 28 29 class MdnsService { 30 public: 31 MdnsService(); 32 virtual ~MdnsService(); 33 34 // Creates a new MdnsService instance, to be owned by the caller. On failure, 35 // returns nullptr. |task_runner|, |reporting_client|, and |config| must exist 36 // for the duration of the resulting instance's life. 37 static std::unique_ptr<MdnsService> Create( 38 TaskRunner* task_runner, 39 ReportingClient* reporting_client, 40 const Config& config, 41 const Config::NetworkInfo& network_info); 42 43 // Starts an mDNS query with the given properties. Updated records are passed 44 // to |callback|. The caller must ensure |callback| remains alive while it is 45 // registered with a query. 46 virtual void StartQuery(const DomainName& name, 47 DnsType dns_type, 48 DnsClass dns_class, 49 MdnsRecordChangedCallback* callback) = 0; 50 51 // Stops an mDNS query with the given properties. |callback| must be the same 52 // callback pointer that was previously passed to StartQuery. 53 virtual void StopQuery(const DomainName& name, 54 DnsType dns_type, 55 DnsClass dns_class, 56 MdnsRecordChangedCallback* callback) = 0; 57 58 // Re-initializes the process of service discovery for the provided domain 59 // name. All ongoing queries for this domain are restarted and any previously 60 // received query results are discarded. 61 virtual void ReinitializeQueries(const DomainName& name) = 0; 62 63 // Starts probing for a valid domain name based on the given one. |callback| 64 // will be called once a valid domain is found, and the instance must persist 65 // until that call is received. 66 virtual Error StartProbe(MdnsDomainConfirmedProvider* callback, 67 DomainName requested_name, 68 IPAddress address) = 0; 69 70 // Registers a new mDNS record for advertisement by this service. For A, AAAA, 71 // SRV, and TXT records, the domain name must have already been claimed by the 72 // ClaimExclusiveOwnership() method and for PTR records the name being pointed 73 // to must have been claimed in the same fashion, but the domain name in the 74 // top-level MdnsRecord entity does not. 75 virtual Error RegisterRecord(const MdnsRecord& record) = 0; 76 77 // Updates the existing record with name matching the name of the new record. 78 // NOTE: This method is not valid for PTR records. 79 virtual Error UpdateRegisteredRecord(const MdnsRecord& old_record, 80 const MdnsRecord& new_record) = 0; 81 82 // Stops advertising the provided record. If no more records with the provided 83 // name are bing advertised after this call's completion, then ownership of 84 // the name is released. 85 virtual Error UnregisterRecord(const MdnsRecord& record) = 0; 86 }; 87 88 } // namespace discovery 89 } // namespace openscreen 90 91 #endif // DISCOVERY_MDNS_PUBLIC_MDNS_SERVICE_H_ 92