1 // Copyright 2015 The Weave 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 LIBWEAVE_INCLUDE_WEAVE_PROVIDER_DNS_SERVICE_DISCOVERY_H_ 6 #define LIBWEAVE_INCLUDE_WEAVE_PROVIDER_DNS_SERVICE_DISCOVERY_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include <base/callback.h> 12 13 namespace weave { 14 namespace provider { 15 16 // This interface should be implemented by the user of libweave and 17 // provided during device creation in Device::Create(...) 18 // libweave will use this interface to start/stop mDNS service discovery. 19 // 20 // Implementation of the PublishService(...) method should publish mDNS 21 // service. Publishing service should be done according to RFC 6762 (mDNS) 22 // and RFC 6763 (DNS Service discovery). 23 // 24 // service_type will contain name of the service before .local. 25 // For example, "_privet._tcp". 26 // port will have a port number where Weave HTTP server is running. 27 // For example, 80. 28 // txt will contain a list of strings for mDNS TXT records. 29 // For example, "txtver=3", "name=MyDevice" 30 // 31 // The following mDNS records should be published in the above examples: 32 // _privet._tcp.local PTR <service_name>._privet._tcp.local 33 // <service_name>._privet._tcp.local SRV <local_domain> 80 34 // <service_name>._privet._tcp.local TXT "txtver=3" "name=MyDevice" 35 // <local_domain> A <IPv4 address> 36 // <local_domain> AAAA <IPv6 address> 37 // 38 // In the list above, it is implementer's responsibility to choose 39 // <service_name> and <local_domain>. 40 // If device only supports IPv4 or IPv6, then only the corresponding mDNS 41 // records should be published. <IPv4 address> and <IPv6 address> should 42 // be the addresses of the network interface this record is advertised on. 43 // 44 // Implementation of PublishService(...) may use existing libraries or 45 // services to implement mDNS service discovery. For example, Avahi or 46 // Bonjour. Such implementation may require IPC or similar async 47 // communication mechanisms. PublishService(...) implementation may 48 // just start the process and return quickly (non-blocking) while the 49 // full mDNS implementation is started in the background. In such case 50 // PublishService(...) implementation should remember all input parameters 51 // so it can restart service publishing in case of failures. 52 // From libweave perspective, discovery is started after 53 // PublishService(...) returns and libweave may not call this method again. 54 // 55 // Implementation of the StopPublishing(...) method should stop advertising 56 // specified service type on the mDNS. This should be done according to mDNS 57 // (RFC 6762) and DNS-SD (RFC 6763) specifications, which require announcing 58 // DNS records that will be going away with TTL=1. 59 // 60 // Since this interface allows multiple service types to be published, proper 61 // implementation should maintain list of service types and stop advertising 62 // only the type specified in this request. Other service types, as well as 63 // records necessary for other services, like A, AAAA may still be available 64 // over mDNS. 65 // 66 // In case a device has multiple networking interfaces, the device developer 67 // needs to make a decision where mDNS advertising is necessary and where it is 68 // not. For example, there should be no mDNS advertising on cellular (LTE) or 69 // WAN (for routers) network interfaces. In some cases, there might be more 70 // then one network interface where advertising makes sense. For example, 71 // a device may have both WiFi and Ethernet connections. In such case, 72 // PublishService(...) should make service available on both interface. 73 // 74 // From libweave perspective, it always looks like there is only one network 75 // interface (for both service discovery and web server). It is 76 // the job of this interface implementation to hide network complexity from 77 // the libweave and to bring webserver up on the same port on both interfaces, 78 // as well as publish an mDNS service (uses webserver port). 79 // 80 // See libweave/examples/provider/avahi_client.cc for complete example 81 // using Avahi for DNS service discovery. 82 83 class DnsServiceDiscovery { 84 public: 85 // Publishes new service using DNS-SD or updates existing one. 86 virtual void PublishService(const std::string& service_type, 87 uint16_t port, 88 const std::vector<std::string>& txt) = 0; 89 90 // Stops publishing service. 91 virtual void StopPublishing(const std::string& service_type) = 0; 92 93 protected: ~DnsServiceDiscovery()94 virtual ~DnsServiceDiscovery() {} 95 }; 96 97 } // namespace provider 98 } // namespace weave 99 100 #endif // LIBWEAVE_INCLUDE_WEAVE_PROVIDER_DNS_SERVICE_DISCOVERY_H_ 101