|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -#ifndef _ADB_MDNS_H_ |
18 |
| -#define _ADB_MDNS_H_ |
| 17 | +#pragma once |
19 | 18 |
|
20 |
| -#include <android-base/macros.h> |
| 19 | +#include <optional> |
| 20 | +#include <string> |
| 21 | +#include <string_view> |
21 | 22 |
|
22 | 23 | // The rules for Service Names [RFC6335] state that they may be no more
|
23 | 24 | // than fifteen characters long (not counting the mandatory underscore),
|
|
28 | 29 | #define ADB_MDNS_TLS_PAIRING_TYPE "adb-tls-pairing"
|
29 | 30 | #define ADB_MDNS_TLS_CONNECT_TYPE "adb-tls-connect"
|
30 | 31 |
|
31 |
| -const int kADBTransportServiceRefIndex = 0; |
32 |
| -const int kADBSecurePairingServiceRefIndex = 1; |
33 |
| -const int kADBSecureConnectServiceRefIndex = 2; |
34 |
| - |
35 |
| -// Each ADB Secure service advertises with a TXT record indicating the version |
36 |
| -// using a key/value pair per RFC 6763 (https://tools.ietf.org/html/rfc6763). |
37 |
| -// |
38 |
| -// The first key/value pair is always the version of the protocol. |
39 |
| -// There may be more key/value pairs added after. |
40 |
| -// |
41 |
| -// The version is purposely represented as the single letter "v" due to the |
42 |
| -// need to minimize DNS traffic. The version starts at 1. With each breaking |
43 |
| -// protocol change, the version is incremented by 1. |
44 |
| -// |
45 |
| -// Newer adb clients/daemons need to recognize and either reject |
46 |
| -// or be backward-compatible with older verseions if there is a mismatch. |
47 |
| -// |
48 |
| -// Relevant sections: |
49 |
| -// |
50 |
| -// """ |
51 |
| -// 6.4. Rules for Keys in DNS-SD Key/Value Pairs |
52 |
| -// |
53 |
| -// The key MUST be at least one character. DNS-SD TXT record strings |
54 |
| -// beginning with an '=' character (i.e., the key is missing) MUST be |
55 |
| -// silently ignored. |
56 |
| -// |
57 |
| -// ... |
58 |
| -// |
59 |
| -// 6.5. Rules for Values in DNS-SD Key/Value Pairs |
60 |
| -// |
61 |
| -// If there is an '=' in a DNS-SD TXT record string, then everything |
62 |
| -// after the first '=' to the end of the string is the value. The value |
63 |
| -// can contain any eight-bit values including '='. |
64 |
| -// """ |
65 |
| - |
66 |
| -#define ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ver) ("v=" #ver) |
67 |
| - |
68 | 32 | // Client/service versions are initially defined to be matching,
|
69 | 33 | // but may go out of sync as different clients and services
|
70 | 34 | // try to talk to each other.
|
71 | 35 | #define ADB_SECURE_SERVICE_VERSION 1
|
72 | 36 | #define ADB_SECURE_CLIENT_VERSION ADB_SECURE_SERVICE_VERSION
|
73 | 37 |
|
74 |
| -const char* kADBSecurePairingServiceTxtRecord = |
75 |
| - ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ADB_SECURE_SERVICE_VERSION); |
76 |
| -const char* kADBSecureConnectServiceTxtRecord = |
77 |
| - ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ADB_SECURE_SERVICE_VERSION); |
| 38 | +constexpr int kADBTransportServiceRefIndex = 0; |
| 39 | +constexpr int kADBSecurePairingServiceRefIndex = 1; |
| 40 | +constexpr int kADBSecureConnectServiceRefIndex = 2; |
| 41 | +constexpr int kNumADBDNSServices = 3; |
| 42 | + |
| 43 | +extern const char* _Nonnull kADBSecurePairingServiceTxtRecord; |
| 44 | +extern const char* _Nonnull kADBSecureConnectServiceTxtRecord; |
| 45 | + |
| 46 | +extern const char* _Nonnull kADBDNSServices[kNumADBDNSServices]; |
| 47 | +extern const char* _Nonnull kADBDNSServiceTxtRecords[kNumADBDNSServices]; |
| 48 | + |
| 49 | +#if ADB_HOST |
| 50 | +// ADB Secure DNS service interface. Used to query what ADB Secure DNS services have been |
| 51 | +// resolved, and to run some kind of callback for each one. |
| 52 | +using adb_secure_foreach_service_callback = |
| 53 | + std::function<void(const std::string& service_name, const std::string& reg_type, |
| 54 | + const std::string& ip_address, uint16_t port)>; |
78 | 55 |
|
79 |
| -#define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "._tcp") |
80 |
| -const char* kADBDNSServices[] = {ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_SERVICE_TYPE), |
81 |
| - ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_PAIRING_TYPE), |
82 |
| - ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_CONNECT_TYPE)}; |
| 56 | +// Tries to connect to a |service_name| if found. Returns true if found and |
| 57 | +// connected, false otherwise. |
| 58 | +bool adb_secure_connect_by_service_name(const std::string& instance_name); |
83 | 59 |
|
84 |
| -const char* kADBDNSServiceTxtRecords[] = { |
85 |
| - nullptr, |
86 |
| - kADBSecurePairingServiceTxtRecord, |
87 |
| - kADBSecureConnectServiceTxtRecord, |
| 60 | +// Returns the index in kADBDNSServices array if |reg_type| matches a service name, otherwise |
| 61 | +// std::nullopt. |
| 62 | +std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type); |
| 63 | +// Returns true if auto-connect is allowed for |service_name| and |instance_name|. |
| 64 | +// See ADB_MDNS_AUTO_CONNECT environment variable for more info. |
| 65 | +bool adb_DNSServiceShouldAutoConnect(std::string_view service_name, std::string_view instance_name); |
| 66 | + |
| 67 | +void mdns_cleanup(void); |
| 68 | +std::string mdns_check(); |
| 69 | +std::string mdns_list_discovered_services(); |
| 70 | + |
| 71 | +struct MdnsInfo { |
| 72 | + std::string service_name; |
| 73 | + std::string service_type; |
| 74 | + std::string addr; |
| 75 | + uint16_t port = 0; |
| 76 | + |
| 77 | + MdnsInfo(std::string_view name, std::string_view type, std::string_view addr, uint16_t port) |
| 78 | + : service_name(name), service_type(type), addr(addr), port(port) {} |
88 | 79 | };
|
89 | 80 |
|
90 |
| -const int kNumADBDNSServices = arraysize(kADBDNSServices); |
| 81 | +std::optional<MdnsInfo> mdns_get_connect_service_info(const std::string& name); |
| 82 | +std::optional<MdnsInfo> mdns_get_pairing_service_info(const std::string& name); |
| 83 | + |
| 84 | +// TODO: Remove once openscreen has support for bonjour client APIs. |
| 85 | +struct AdbMdnsResponderFuncs { |
| 86 | + std::string (*_Nonnull mdns_check)(void); |
| 87 | + std::string (*_Nonnull mdns_list_discovered_services)(void); |
| 88 | + std::optional<MdnsInfo> (*_Nonnull mdns_get_connect_service_info)(const std::string&); |
| 89 | + std::optional<MdnsInfo> (*_Nonnull mdns_get_pairing_service_info)(const std::string&); |
| 90 | + void (*_Nonnull mdns_cleanup)(void); |
| 91 | + bool (*_Nonnull adb_secure_connect_by_service_name)(const std::string&); |
| 92 | +}; // AdbBonjourCallbacks |
91 | 93 |
|
92 |
| -#endif |
| 94 | +// TODO: Remove once openscreen has support for bonjour client APIs. |
| 95 | +// Start mdns discovery using MdnsResponder backend. Fills in AdbMdnsResponderFuncs for adb mdns |
| 96 | +// related functions. |
| 97 | +AdbMdnsResponderFuncs StartMdnsResponderDiscovery(); |
| 98 | +#endif // ADB_HOST |
0 commit comments