blob: 471ec2501ceef27d9088eb048687d0112707c384 [file] [log] [blame]
Casey Dahlin3122cdf2016-06-23 14:19:37 -07001/*
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 Duongf4ba8d72021-01-13 12:18:15 -080017#pragma once
Casey Dahlin3122cdf2016-06-23 14:19:37 -070018
Joshua Duongf4ba8d72021-01-13 12:18:15 -080019#include <optional>
20#include <string>
21#include <string_view>
Lingfeng Yangf44871a2018-11-16 22:47:31 -080022
Joshua Duong77b8ff32020-04-16 15:58:19 -070023// The rules for Service Names [RFC6335] state that they may be no more
24// than fifteen characters long (not counting the mandatory underscore),
25// consisting of only letters, digits, and hyphens, must begin and end
26// with a letter or digit, must not contain consecutive hyphens, and
27// must contain at least one letter.
28#define ADB_MDNS_SERVICE_TYPE "adb"
29#define ADB_MDNS_TLS_PAIRING_TYPE "adb-tls-pairing"
30#define ADB_MDNS_TLS_CONNECT_TYPE "adb-tls-connect"
Lingfeng Yangf44871a2018-11-16 22:47:31 -080031
Lingfeng Yang48bdec72019-10-24 22:30:11 -070032// Client/service versions are initially defined to be matching,
33// but may go out of sync as different clients and services
34// try to talk to each other.
35#define ADB_SECURE_SERVICE_VERSION 1
36#define ADB_SECURE_CLIENT_VERSION ADB_SECURE_SERVICE_VERSION
37
Joshua Duongf4ba8d72021-01-13 12:18:15 -080038constexpr int kADBTransportServiceRefIndex = 0;
39constexpr int kADBSecurePairingServiceRefIndex = 1;
40constexpr int kADBSecureConnectServiceRefIndex = 2;
41constexpr int kNumADBDNSServices = 3;
Lingfeng Yang48bdec72019-10-24 22:30:11 -070042
Joshua Duongf4ba8d72021-01-13 12:18:15 -080043extern const char* _Nonnull kADBSecurePairingServiceTxtRecord;
44extern const char* _Nonnull kADBSecureConnectServiceTxtRecord;
Lingfeng Yangf44871a2018-11-16 22:47:31 -080045
Joshua Duongf4ba8d72021-01-13 12:18:15 -080046extern const char* _Nonnull kADBDNSServices[kNumADBDNSServices];
47extern 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.
52using 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)>;
55
56// Tries to connect to a |service_name| if found. Returns true if found and
57// connected, false otherwise.
58bool adb_secure_connect_by_service_name(const std::string& instance_name);
59
60// Returns the index in kADBDNSServices array if |reg_type| matches a service name, otherwise
61// std::nullopt.
62std::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.
65bool adb_DNSServiceShouldAutoConnect(std::string_view service_name, std::string_view instance_name);
66
67void mdns_cleanup(void);
68std::string mdns_check();
69std::string mdns_list_discovered_services();
70
71struct 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) {}
Lingfeng Yang48bdec72019-10-24 22:30:11 -070079};
80
Joshua Duongf4ba8d72021-01-13 12:18:15 -080081std::optional<MdnsInfo> mdns_get_connect_service_info(const std::string& name);
82std::optional<MdnsInfo> mdns_get_pairing_service_info(const std::string& name);
Casey Dahlin3122cdf2016-06-23 14:19:37 -070083
Joshua Duongf4ba8d72021-01-13 12:18:15 -080084// TODO: Remove once openscreen has support for bonjour client APIs.
85struct 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
93
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.
97AdbMdnsResponderFuncs StartMdnsResponderDiscovery();
98#endif // ADB_HOST