Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 17 | #pragma once |
Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 18 | |
Ryan Prichard | d0ce71b | 2023-07-21 17:35:35 -0700 | [diff] [blame] | 19 | #include <functional> |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 20 | #include <optional> |
| 21 | #include <string> |
| 22 | #include <string_view> |
Lingfeng Yang | f44871a | 2018-11-16 22:47:31 -0800 | [diff] [blame] | 23 | |
Joshua Duong | 77b8ff3 | 2020-04-16 15:58:19 -0700 | [diff] [blame] | 24 | // The rules for Service Names [RFC6335] state that they may be no more |
| 25 | // than fifteen characters long (not counting the mandatory underscore), |
| 26 | // consisting of only letters, digits, and hyphens, must begin and end |
| 27 | // with a letter or digit, must not contain consecutive hyphens, and |
| 28 | // must contain at least one letter. |
| 29 | #define ADB_MDNS_SERVICE_TYPE "adb" |
| 30 | #define ADB_MDNS_TLS_PAIRING_TYPE "adb-tls-pairing" |
| 31 | #define ADB_MDNS_TLS_CONNECT_TYPE "adb-tls-connect" |
Lingfeng Yang | f44871a | 2018-11-16 22:47:31 -0800 | [diff] [blame] | 32 | |
Lingfeng Yang | 48bdec7 | 2019-10-24 22:30:11 -0700 | [diff] [blame] | 33 | // Client/service versions are initially defined to be matching, |
| 34 | // but may go out of sync as different clients and services |
| 35 | // try to talk to each other. |
| 36 | #define ADB_SECURE_SERVICE_VERSION 1 |
| 37 | #define ADB_SECURE_CLIENT_VERSION ADB_SECURE_SERVICE_VERSION |
| 38 | |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 39 | constexpr int kADBTransportServiceRefIndex = 0; |
| 40 | constexpr int kADBSecurePairingServiceRefIndex = 1; |
| 41 | constexpr int kADBSecureConnectServiceRefIndex = 2; |
| 42 | constexpr int kNumADBDNSServices = 3; |
Lingfeng Yang | 48bdec7 | 2019-10-24 22:30:11 -0700 | [diff] [blame] | 43 | |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 44 | extern const char* _Nonnull kADBSecurePairingServiceTxtRecord; |
| 45 | extern const char* _Nonnull kADBSecureConnectServiceTxtRecord; |
Lingfeng Yang | f44871a | 2018-11-16 22:47:31 -0800 | [diff] [blame] | 46 | |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 47 | extern const char* _Nonnull kADBDNSServices[kNumADBDNSServices]; |
| 48 | extern const char* _Nonnull kADBDNSServiceTxtRecords[kNumADBDNSServices]; |
| 49 | |
| 50 | #if ADB_HOST |
| 51 | // ADB Secure DNS service interface. Used to query what ADB Secure DNS services have been |
| 52 | // resolved, and to run some kind of callback for each one. |
| 53 | using adb_secure_foreach_service_callback = |
| 54 | std::function<void(const std::string& service_name, const std::string& reg_type, |
| 55 | const std::string& ip_address, uint16_t port)>; |
| 56 | |
| 57 | // Tries to connect to a |service_name| if found. Returns true if found and |
| 58 | // connected, false otherwise. |
| 59 | bool adb_secure_connect_by_service_name(const std::string& instance_name); |
| 60 | |
| 61 | // Returns the index in kADBDNSServices array if |reg_type| matches a service name, otherwise |
| 62 | // std::nullopt. |
| 63 | std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type); |
| 64 | // Returns true if auto-connect is allowed for |service_name| and |instance_name|. |
| 65 | // See ADB_MDNS_AUTO_CONNECT environment variable for more info. |
| 66 | bool adb_DNSServiceShouldAutoConnect(std::string_view service_name, std::string_view instance_name); |
| 67 | |
Elliott Hughes | a8ab5ce | 2024-06-28 12:10:09 +0000 | [diff] [blame] | 68 | void mdns_cleanup(); |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 69 | std::string mdns_check(); |
| 70 | std::string mdns_list_discovered_services(); |
| 71 | |
| 72 | struct MdnsInfo { |
| 73 | std::string service_name; |
| 74 | std::string service_type; |
| 75 | std::string addr; |
| 76 | uint16_t port = 0; |
| 77 | |
| 78 | MdnsInfo(std::string_view name, std::string_view type, std::string_view addr, uint16_t port) |
| 79 | : service_name(name), service_type(type), addr(addr), port(port) {} |
Lingfeng Yang | 48bdec7 | 2019-10-24 22:30:11 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 82 | std::optional<MdnsInfo> mdns_get_connect_service_info(const std::string& name); |
| 83 | std::optional<MdnsInfo> mdns_get_pairing_service_info(const std::string& name); |
Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 84 | |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 85 | // TODO: Remove once openscreen has support for bonjour client APIs. |
| 86 | struct AdbMdnsResponderFuncs { |
Elliott Hughes | a8ab5ce | 2024-06-28 12:10:09 +0000 | [diff] [blame] | 87 | std::string (*_Nonnull mdns_check)(); |
| 88 | std::string (*_Nonnull mdns_list_discovered_services)(); |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 89 | std::optional<MdnsInfo> (*_Nonnull mdns_get_connect_service_info)(const std::string&); |
| 90 | std::optional<MdnsInfo> (*_Nonnull mdns_get_pairing_service_info)(const std::string&); |
Elliott Hughes | a8ab5ce | 2024-06-28 12:10:09 +0000 | [diff] [blame] | 91 | void (*_Nonnull mdns_cleanup)(); |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 92 | bool (*_Nonnull adb_secure_connect_by_service_name)(const std::string&); |
| 93 | }; // AdbBonjourCallbacks |
| 94 | |
| 95 | // TODO: Remove once openscreen has support for bonjour client APIs. |
| 96 | // Start mdns discovery using MdnsResponder backend. Fills in AdbMdnsResponderFuncs for adb mdns |
| 97 | // related functions. |
| 98 | AdbMdnsResponderFuncs StartMdnsResponderDiscovery(); |
| 99 | #endif // ADB_HOST |