Openscreen discovery integration for adb client.
The openscreen discovery library will allow adb client on all three
platforms to discover mdns services without relying on having Bonjour on
the host machine.
If the host does have bonjour running, we will fallback to using the
MdnsResponder client.
The openscreen discovery code path will be disabled by default for now.
You can enable it by setting ADB_MDNS_OPENSCREEN=1. Check `adb
host-features` string for 'openscreen_mdns' feature.
Test: test_adb.py
Bug: 152884934
Change-Id: Ief153afe7f080cb21134897d8eaf00d90c6983d8
diff --git a/adb_mdns.h b/adb_mdns.h
index 3111248..471ec25 100644
--- a/adb_mdns.h
+++ b/adb_mdns.h
@@ -14,10 +14,11 @@
* limitations under the License.
*/
-#ifndef _ADB_MDNS_H_
-#define _ADB_MDNS_H_
+#pragma once
-#include <android-base/macros.h>
+#include <optional>
+#include <string>
+#include <string_view>
// The rules for Service Names [RFC6335] state that they may be no more
// than fifteen characters long (not counting the mandatory underscore),
@@ -28,65 +29,70 @@
#define ADB_MDNS_TLS_PAIRING_TYPE "adb-tls-pairing"
#define ADB_MDNS_TLS_CONNECT_TYPE "adb-tls-connect"
-const int kADBTransportServiceRefIndex = 0;
-const int kADBSecurePairingServiceRefIndex = 1;
-const int kADBSecureConnectServiceRefIndex = 2;
-
-// Each ADB Secure service advertises with a TXT record indicating the version
-// using a key/value pair per RFC 6763 (https://tools.ietf.org/html/rfc6763).
-//
-// The first key/value pair is always the version of the protocol.
-// There may be more key/value pairs added after.
-//
-// The version is purposely represented as the single letter "v" due to the
-// need to minimize DNS traffic. The version starts at 1. With each breaking
-// protocol change, the version is incremented by 1.
-//
-// Newer adb clients/daemons need to recognize and either reject
-// or be backward-compatible with older verseions if there is a mismatch.
-//
-// Relevant sections:
-//
-// """
-// 6.4. Rules for Keys in DNS-SD Key/Value Pairs
-//
-// The key MUST be at least one character. DNS-SD TXT record strings
-// beginning with an '=' character (i.e., the key is missing) MUST be
-// silently ignored.
-//
-// ...
-//
-// 6.5. Rules for Values in DNS-SD Key/Value Pairs
-//
-// If there is an '=' in a DNS-SD TXT record string, then everything
-// after the first '=' to the end of the string is the value. The value
-// can contain any eight-bit values including '='.
-// """
-
-#define ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ver) ("v=" #ver)
-
// Client/service versions are initially defined to be matching,
// but may go out of sync as different clients and services
// try to talk to each other.
#define ADB_SECURE_SERVICE_VERSION 1
#define ADB_SECURE_CLIENT_VERSION ADB_SECURE_SERVICE_VERSION
-const char* kADBSecurePairingServiceTxtRecord =
- ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ADB_SECURE_SERVICE_VERSION);
-const char* kADBSecureConnectServiceTxtRecord =
- ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ADB_SECURE_SERVICE_VERSION);
+constexpr int kADBTransportServiceRefIndex = 0;
+constexpr int kADBSecurePairingServiceRefIndex = 1;
+constexpr int kADBSecureConnectServiceRefIndex = 2;
+constexpr int kNumADBDNSServices = 3;
-#define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "._tcp")
-const char* kADBDNSServices[] = {ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_SERVICE_TYPE),
- ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_PAIRING_TYPE),
- ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_CONNECT_TYPE)};
+extern const char* _Nonnull kADBSecurePairingServiceTxtRecord;
+extern const char* _Nonnull kADBSecureConnectServiceTxtRecord;
-const char* kADBDNSServiceTxtRecords[] = {
- nullptr,
- kADBSecurePairingServiceTxtRecord,
- kADBSecureConnectServiceTxtRecord,
+extern const char* _Nonnull kADBDNSServices[kNumADBDNSServices];
+extern const char* _Nonnull kADBDNSServiceTxtRecords[kNumADBDNSServices];
+
+#if ADB_HOST
+// ADB Secure DNS service interface. Used to query what ADB Secure DNS services have been
+// resolved, and to run some kind of callback for each one.
+using adb_secure_foreach_service_callback =
+ std::function<void(const std::string& service_name, const std::string& reg_type,
+ const std::string& ip_address, uint16_t port)>;
+
+// Tries to connect to a |service_name| if found. Returns true if found and
+// connected, false otherwise.
+bool adb_secure_connect_by_service_name(const std::string& instance_name);
+
+// Returns the index in kADBDNSServices array if |reg_type| matches a service name, otherwise
+// std::nullopt.
+std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type);
+// Returns true if auto-connect is allowed for |service_name| and |instance_name|.
+// See ADB_MDNS_AUTO_CONNECT environment variable for more info.
+bool adb_DNSServiceShouldAutoConnect(std::string_view service_name, std::string_view instance_name);
+
+void mdns_cleanup(void);
+std::string mdns_check();
+std::string mdns_list_discovered_services();
+
+struct MdnsInfo {
+ std::string service_name;
+ std::string service_type;
+ std::string addr;
+ uint16_t port = 0;
+
+ MdnsInfo(std::string_view name, std::string_view type, std::string_view addr, uint16_t port)
+ : service_name(name), service_type(type), addr(addr), port(port) {}
};
-const int kNumADBDNSServices = arraysize(kADBDNSServices);
+std::optional<MdnsInfo> mdns_get_connect_service_info(const std::string& name);
+std::optional<MdnsInfo> mdns_get_pairing_service_info(const std::string& name);
-#endif
+// TODO: Remove once openscreen has support for bonjour client APIs.
+struct AdbMdnsResponderFuncs {
+ std::string (*_Nonnull mdns_check)(void);
+ std::string (*_Nonnull mdns_list_discovered_services)(void);
+ std::optional<MdnsInfo> (*_Nonnull mdns_get_connect_service_info)(const std::string&);
+ std::optional<MdnsInfo> (*_Nonnull mdns_get_pairing_service_info)(const std::string&);
+ void (*_Nonnull mdns_cleanup)(void);
+ bool (*_Nonnull adb_secure_connect_by_service_name)(const std::string&);
+}; // AdbBonjourCallbacks
+
+// TODO: Remove once openscreen has support for bonjour client APIs.
+// Start mdns discovery using MdnsResponder backend. Fills in AdbMdnsResponderFuncs for adb mdns
+// related functions.
+AdbMdnsResponderFuncs StartMdnsResponderDiscovery();
+#endif // ADB_HOST