Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1 | /* |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2 | * Copyright (C) 2021 The Android Open Source Project |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.server; |
| 18 | |
Yuyang Huang | d5896e7 | 2023-11-28 13:23:59 +0900 | [diff] [blame] | 19 | import static android.Manifest.permission.DEVICE_POWER; |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 20 | import static android.Manifest.permission.NETWORK_SETTINGS; |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 21 | import static android.Manifest.permission.NETWORK_STACK; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.NETID_UNSET; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 23 | import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
| 24 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI; |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 25 | import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK; |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 26 | import static android.net.nsd.NsdManager.MDNS_DISCOVERY_MANAGER_EVENT; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 27 | import static android.net.nsd.NsdManager.MDNS_SERVICE_EVENT; |
Remi NGUYEN VAN | 2f82fcd | 2023-05-10 13:24:53 +0900 | [diff] [blame] | 28 | import static android.net.nsd.NsdManager.RESOLVE_SERVICE_SUCCEEDED; |
Remi NGUYEN VAN | 629234c | 2024-02-08 18:27:45 +0900 | [diff] [blame^] | 29 | import static android.net.nsd.NsdManager.SUBTYPE_LABEL_REGEX; |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 30 | import static android.net.nsd.NsdManager.TYPE_REGEX; |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 31 | import static android.provider.DeviceConfig.NAMESPACE_TETHERING; |
Motomu Utsumi | ed4e7ec | 2023-09-13 14:58:32 +0900 | [diff] [blame] | 32 | |
Remi NGUYEN VAN | beb03f1 | 2023-03-08 19:03:27 +0900 | [diff] [blame] | 33 | import static com.android.modules.utils.build.SdkLevel.isAtLeastU; |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 34 | import static com.android.networkstack.apishim.ConstantsShim.REGISTER_NSD_OFFLOAD_ENGINE; |
Paul Hu | 043bcd4 | 2023-07-14 16:38:25 +0800 | [diff] [blame] | 35 | import static com.android.server.connectivity.mdns.MdnsAdvertiser.AdvertiserMetrics; |
| 36 | import static com.android.server.connectivity.mdns.MdnsConstants.NO_PACKET; |
Yuyang Huang | de802c8 | 2023-05-02 17:14:22 +0900 | [diff] [blame] | 37 | import static com.android.server.connectivity.mdns.MdnsRecord.MAX_LABEL_LENGTH; |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 38 | import static com.android.server.connectivity.mdns.MdnsSearchOptions.AGGRESSIVE_QUERY_MODE; |
Paul Hu | 05086bd | 2024-01-26 03:48:51 +0000 | [diff] [blame] | 39 | import static com.android.server.connectivity.mdns.MdnsSearchOptions.PASSIVE_QUERY_MODE; |
Paul Hu | cdef353 | 2023-06-18 14:47:35 +0000 | [diff] [blame] | 40 | import static com.android.server.connectivity.mdns.util.MdnsUtils.Clock; |
Remi NGUYEN VAN | beb03f1 | 2023-03-08 19:03:27 +0900 | [diff] [blame] | 41 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 42 | import android.annotation.NonNull; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 43 | import android.annotation.Nullable; |
Yuyang Huang | fc83170 | 2023-08-21 17:48:48 +0900 | [diff] [blame] | 44 | import android.annotation.RequiresApi; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 45 | import android.app.ActivityManager; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 46 | import android.content.Context; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 47 | import android.content.Intent; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 48 | import android.net.ConnectivityManager; |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 49 | import android.net.INetd; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 50 | import android.net.InetAddresses; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 51 | import android.net.LinkProperties; |
| 52 | import android.net.Network; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 53 | import android.net.mdns.aidl.DiscoveryInfo; |
| 54 | import android.net.mdns.aidl.GetAddressInfo; |
| 55 | import android.net.mdns.aidl.IMDnsEventListener; |
| 56 | import android.net.mdns.aidl.RegistrationInfo; |
| 57 | import android.net.mdns.aidl.ResolutionInfo; |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 58 | import android.net.nsd.AdvertisingRequest; |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 59 | import android.net.nsd.DiscoveryRequest; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 60 | import android.net.nsd.INsdManager; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 61 | import android.net.nsd.INsdManagerCallback; |
| 62 | import android.net.nsd.INsdServiceConnector; |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 63 | import android.net.nsd.IOffloadEngine; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 64 | import android.net.nsd.MDnsManager; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 65 | import android.net.nsd.NsdManager; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 66 | import android.net.nsd.NsdServiceInfo; |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 67 | import android.net.nsd.OffloadEngine; |
| 68 | import android.net.nsd.OffloadServiceInfo; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 69 | import android.net.wifi.WifiManager; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 70 | import android.os.Binder; |
Yuyang Huang | fc83170 | 2023-08-21 17:48:48 +0900 | [diff] [blame] | 71 | import android.os.Build; |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 72 | import android.os.Handler; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 73 | import android.os.HandlerThread; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 74 | import android.os.IBinder; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 75 | import android.os.Looper; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 76 | import android.os.Message; |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 77 | import android.os.RemoteCallbackList; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 78 | import android.os.RemoteException; |
Dianne Hackborn | 692107e | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 79 | import android.os.UserHandle; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 80 | import android.provider.DeviceConfig; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 81 | import android.text.TextUtils; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 82 | import android.util.ArraySet; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 83 | import android.util.Log; |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 84 | import android.util.Pair; |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 85 | import android.util.SparseArray; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 86 | |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 87 | import com.android.internal.annotations.VisibleForTesting; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 88 | import com.android.internal.util.IndentingPrintWriter; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 89 | import com.android.internal.util.State; |
| 90 | import com.android.internal.util.StateMachine; |
Paul Hu | cdef353 | 2023-06-18 14:47:35 +0000 | [diff] [blame] | 91 | import com.android.metrics.NetworkNsdReportedMetrics; |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 92 | import com.android.modules.utils.build.SdkLevel; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 93 | import com.android.net.module.util.CollectionUtils; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 94 | import com.android.net.module.util.DeviceConfigUtils; |
Yuyang Huang | a6a6ff9 | 2023-04-24 13:33:34 +0900 | [diff] [blame] | 95 | import com.android.net.module.util.InetAddressUtils; |
paulhu | 3ffffe7 | 2021-09-16 10:15:22 +0800 | [diff] [blame] | 96 | import com.android.net.module.util.PermissionUtils; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 97 | import com.android.net.module.util.SharedLog; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 98 | import com.android.server.connectivity.mdns.ExecutorProvider; |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 99 | import com.android.server.connectivity.mdns.MdnsAdvertiser; |
Yuyang Huang | e5cba9c | 2023-11-02 18:05:47 +0900 | [diff] [blame] | 100 | import com.android.server.connectivity.mdns.MdnsAdvertisingOptions; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 101 | import com.android.server.connectivity.mdns.MdnsDiscoveryManager; |
Yuyang Huang | b96a071 | 2023-09-07 15:13:15 +0900 | [diff] [blame] | 102 | import com.android.server.connectivity.mdns.MdnsFeatureFlags; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 103 | import com.android.server.connectivity.mdns.MdnsInterfaceSocket; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 104 | import com.android.server.connectivity.mdns.MdnsMultinetworkSocketClient; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 105 | import com.android.server.connectivity.mdns.MdnsSearchOptions; |
| 106 | import com.android.server.connectivity.mdns.MdnsServiceBrowserListener; |
| 107 | import com.android.server.connectivity.mdns.MdnsServiceInfo; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 108 | import com.android.server.connectivity.mdns.MdnsSocketProvider; |
Yuyang Huang | de802c8 | 2023-05-02 17:14:22 +0900 | [diff] [blame] | 109 | import com.android.server.connectivity.mdns.util.MdnsUtils; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 110 | |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 111 | import java.io.FileDescriptor; |
| 112 | import java.io.PrintWriter; |
Yuyang Huang | aa0e960 | 2023-03-17 12:43:09 +0900 | [diff] [blame] | 113 | import java.net.Inet6Address; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 114 | import java.net.InetAddress; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 115 | import java.net.NetworkInterface; |
| 116 | import java.net.SocketException; |
| 117 | import java.net.UnknownHostException; |
Paul Hu | 2b86591 | 2023-03-06 14:27:53 +0800 | [diff] [blame] | 118 | import java.util.ArrayList; |
Remi NGUYEN VAN | 2f82fcd | 2023-05-10 13:24:53 +0900 | [diff] [blame] | 119 | import java.util.Arrays; |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 120 | import java.util.Collection; |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 121 | import java.util.Collections; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 122 | import java.util.HashMap; |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 123 | import java.util.LinkedHashMap; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 124 | import java.util.List; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 125 | import java.util.Map; |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 126 | import java.util.Objects; |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 127 | import java.util.Set; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 128 | import java.util.regex.Matcher; |
| 129 | import java.util.regex.Pattern; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 130 | |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Network Service Discovery Service handles remote service discovery operation requests by |
| 133 | * implementing the INsdManager interface. |
| 134 | * |
| 135 | * @hide |
| 136 | */ |
Yuyang Huang | fc83170 | 2023-08-21 17:48:48 +0900 | [diff] [blame] | 137 | @RequiresApi(Build.VERSION_CODES.TIRAMISU) |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 138 | public class NsdService extends INsdManager.Stub { |
| 139 | private static final String TAG = "NsdService"; |
| 140 | private static final String MDNS_TAG = "mDnsConnector"; |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 141 | /** |
| 142 | * Enable discovery using the Java DiscoveryManager, instead of the legacy mdnsresponder |
| 143 | * implementation. |
| 144 | */ |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 145 | private static final String MDNS_DISCOVERY_MANAGER_VERSION = "mdns_discovery_manager_version"; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 146 | private static final String LOCAL_DOMAIN_NAME = "local"; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 147 | |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 148 | /** |
| 149 | * Enable advertising using the Java MdnsAdvertiser, instead of the legacy mdnsresponder |
| 150 | * implementation. |
| 151 | */ |
| 152 | private static final String MDNS_ADVERTISER_VERSION = "mdns_advertiser_version"; |
| 153 | |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 154 | /** |
| 155 | * Comma-separated list of type:flag mappings indicating the flags to use to allowlist |
| 156 | * discovery/advertising using MdnsDiscoveryManager / MdnsAdvertiser for a given type. |
| 157 | * |
| 158 | * For example _mytype._tcp.local and _othertype._tcp.local would be configured with: |
| 159 | * _mytype._tcp:mytype,_othertype._tcp.local:othertype |
| 160 | * |
| 161 | * In which case the flags: |
| 162 | * "mdns_discovery_manager_allowlist_mytype_version", |
| 163 | * "mdns_advertiser_allowlist_mytype_version", |
| 164 | * "mdns_discovery_manager_allowlist_othertype_version", |
| 165 | * "mdns_advertiser_allowlist_othertype_version" |
| 166 | * would be used to toggle MdnsDiscoveryManager / MdnsAdvertiser for each type. The flags will |
| 167 | * be read with |
Motomu Utsumi | 624aeb4 | 2023-08-15 15:52:27 +0900 | [diff] [blame] | 168 | * {@link DeviceConfigUtils#isTetheringFeatureEnabled} |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 169 | * |
| 170 | * @see #MDNS_DISCOVERY_MANAGER_ALLOWLIST_FLAG_PREFIX |
| 171 | * @see #MDNS_ADVERTISER_ALLOWLIST_FLAG_PREFIX |
| 172 | * @see #MDNS_ALLOWLIST_FLAG_SUFFIX |
| 173 | */ |
| 174 | private static final String MDNS_TYPE_ALLOWLIST_FLAGS = "mdns_type_allowlist_flags"; |
| 175 | |
| 176 | private static final String MDNS_DISCOVERY_MANAGER_ALLOWLIST_FLAG_PREFIX = |
| 177 | "mdns_discovery_manager_allowlist_"; |
| 178 | private static final String MDNS_ADVERTISER_ALLOWLIST_FLAG_PREFIX = |
| 179 | "mdns_advertiser_allowlist_"; |
| 180 | private static final String MDNS_ALLOWLIST_FLAG_SUFFIX = "_version"; |
| 181 | |
Remi NGUYEN VAN | 0ca094b | 2023-09-13 16:27:12 +0900 | [diff] [blame] | 182 | private static final String FORCE_ENABLE_FLAG_FOR_TEST_PREFIX = "test_"; |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 183 | |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 184 | @VisibleForTesting |
| 185 | static final String MDNS_CONFIG_RUNNING_APP_ACTIVE_IMPORTANCE_CUTOFF = |
| 186 | "mdns_config_running_app_active_importance_cutoff"; |
| 187 | @VisibleForTesting |
| 188 | static final int DEFAULT_RUNNING_APP_ACTIVE_IMPORTANCE_CUTOFF = |
| 189 | ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 190 | private final int mRunningAppActiveImportanceCutoff; |
| 191 | |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 192 | public static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 193 | private static final long CLEANUP_DELAY_MS = 10000; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 194 | private static final int IFACE_IDX_ANY = 0; |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 195 | private static final int MAX_SERVICES_COUNT_METRIC_PER_CLIENT = 100; |
| 196 | @VisibleForTesting |
| 197 | static final int NO_TRANSACTION = -1; |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 198 | private static final int NO_SENT_QUERY_COUNT = 0; |
| 199 | private static final int DISCOVERY_QUERY_SENT_CALLBACK = 1000; |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 200 | private static final int MAX_SUBTYPE_COUNT = 100; |
Paul Hu | 14667de | 2023-04-17 22:42:47 +0800 | [diff] [blame] | 201 | private static final SharedLog LOGGER = new SharedLog("serviceDiscovery"); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 202 | |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 203 | private final Context mContext; |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 204 | private final NsdStateMachine mNsdStateMachine; |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 205 | // It can be null on V+ device since mdns native service provided by netd is removed. |
| 206 | private final @Nullable MDnsManager mMDnsManager; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 207 | private final MDnsEventCallback mMDnsEventCallback; |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 208 | @NonNull |
| 209 | private final Dependencies mDeps; |
| 210 | @NonNull |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 211 | private final MdnsMultinetworkSocketClient mMdnsSocketClient; |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 212 | @NonNull |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 213 | private final MdnsDiscoveryManager mMdnsDiscoveryManager; |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 214 | @NonNull |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 215 | private final MdnsSocketProvider mMdnsSocketProvider; |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 216 | @NonNull |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 217 | private final MdnsAdvertiser mAdvertiser; |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 218 | @NonNull |
| 219 | private final Clock mClock; |
Paul Hu | 14667de | 2023-04-17 22:42:47 +0800 | [diff] [blame] | 220 | private final SharedLog mServiceLogs = LOGGER.forSubComponent(TAG); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 221 | // WARNING : Accessing these values in any thread is not safe, it must only be changed in the |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 222 | // state machine thread. If change this outside state machine, it will need to introduce |
| 223 | // synchronization. |
| 224 | private boolean mIsDaemonStarted = false; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 225 | private boolean mIsMonitoringSocketsStarted = false; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 226 | |
| 227 | /** |
| 228 | * Clients receiving asynchronous messages |
| 229 | */ |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 230 | private final HashMap<NsdServiceConnector, ClientInfo> mClients = new HashMap<>(); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 231 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 232 | /* A map from transaction(unique) id to client info */ |
| 233 | private final SparseArray<ClientInfo> mTransactionIdToClientInfoMap = new SparseArray<>(); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 234 | |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 235 | // Note this is not final to avoid depending on the Wi-Fi service starting before NsdService |
| 236 | @Nullable |
| 237 | private WifiManager.MulticastLock mHeldMulticastLock; |
| 238 | // Fulfilled network requests that require the Wi-Fi lock: key is the obtained Network |
| 239 | // (non-null), value is the requested Network (nullable) |
| 240 | @NonNull |
| 241 | private final ArraySet<Network> mWifiLockRequiredNetworks = new ArraySet<>(); |
| 242 | @NonNull |
| 243 | private final ArraySet<Integer> mRunningAppActiveUids = new ArraySet<>(); |
| 244 | |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 245 | private final long mCleanupDelayMs; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 246 | |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 247 | private static final int INVALID_ID = 0; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 248 | private int mUniqueId = 1; |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 249 | // The count of the connected legacy clients. |
| 250 | private int mLegacyClientCount = 0; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 251 | // The number of client that ever connected. |
| 252 | private int mClientNumberId = 1; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 253 | |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 254 | private final RemoteCallbackList<IOffloadEngine> mOffloadEngines = |
| 255 | new RemoteCallbackList<>(); |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 256 | @NonNull |
| 257 | private final MdnsFeatureFlags mMdnsFeatureFlags; |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 258 | |
| 259 | private static class OffloadEngineInfo { |
| 260 | @NonNull final String mInterfaceName; |
| 261 | final long mOffloadCapabilities; |
| 262 | final long mOffloadType; |
| 263 | @NonNull final IOffloadEngine mOffloadEngine; |
| 264 | |
| 265 | OffloadEngineInfo(@NonNull IOffloadEngine offloadEngine, |
| 266 | @NonNull String interfaceName, long capabilities, long offloadType) { |
| 267 | this.mOffloadEngine = offloadEngine; |
| 268 | this.mInterfaceName = interfaceName; |
| 269 | this.mOffloadCapabilities = capabilities; |
| 270 | this.mOffloadType = offloadType; |
| 271 | } |
| 272 | } |
| 273 | |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 274 | @VisibleForTesting |
| 275 | static class MdnsListener implements MdnsServiceBrowserListener { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 276 | protected final int mClientRequestId; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 277 | protected final int mTransactionId; |
| 278 | @NonNull |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 279 | protected final String mListenedServiceType; |
| 280 | |
Kangping Dong | 97b2adc | 2024-01-11 16:00:37 +0800 | [diff] [blame] | 281 | MdnsListener(int clientRequestId, int transactionId, @NonNull String listenedServiceType) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 282 | mClientRequestId = clientRequestId; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 283 | mTransactionId = transactionId; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 284 | mListenedServiceType = listenedServiceType; |
| 285 | } |
| 286 | |
| 287 | @NonNull |
| 288 | public String getListenedServiceType() { |
| 289 | return mListenedServiceType; |
| 290 | } |
| 291 | |
| 292 | @Override |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 293 | public void onServiceFound(@NonNull MdnsServiceInfo serviceInfo, |
| 294 | boolean isServiceFromCache) { } |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 295 | |
| 296 | @Override |
| 297 | public void onServiceUpdated(@NonNull MdnsServiceInfo serviceInfo) { } |
| 298 | |
| 299 | @Override |
| 300 | public void onServiceRemoved(@NonNull MdnsServiceInfo serviceInfo) { } |
| 301 | |
| 302 | @Override |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 303 | public void onServiceNameDiscovered(@NonNull MdnsServiceInfo serviceInfo, |
| 304 | boolean isServiceFromCache) { } |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 305 | |
| 306 | @Override |
| 307 | public void onServiceNameRemoved(@NonNull MdnsServiceInfo serviceInfo) { } |
| 308 | |
| 309 | @Override |
| 310 | public void onSearchStoppedWithError(int error) { } |
| 311 | |
| 312 | @Override |
| 313 | public void onSearchFailedToStart() { } |
| 314 | |
| 315 | @Override |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 316 | public void onDiscoveryQuerySent(@NonNull List<String> subtypes, |
| 317 | int sentQueryTransactionId) { } |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 318 | |
| 319 | @Override |
| 320 | public void onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode) { } |
| 321 | } |
| 322 | |
| 323 | private class DiscoveryListener extends MdnsListener { |
| 324 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 325 | DiscoveryListener(int clientRequestId, int transactionId, |
Kangping Dong | 97b2adc | 2024-01-11 16:00:37 +0800 | [diff] [blame] | 326 | @NonNull String listenServiceType) { |
| 327 | super(clientRequestId, transactionId, listenServiceType); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 331 | public void onServiceNameDiscovered(@NonNull MdnsServiceInfo serviceInfo, |
| 332 | boolean isServiceFromCache) { |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 333 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 334 | NsdManager.SERVICE_FOUND, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 335 | new MdnsEvent(mClientRequestId, serviceInfo, isServiceFromCache)); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void onServiceNameRemoved(@NonNull MdnsServiceInfo serviceInfo) { |
Paul Hu | 319751a | 2023-01-13 23:56:34 +0800 | [diff] [blame] | 340 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 341 | NsdManager.SERVICE_LOST, |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 342 | new MdnsEvent(mClientRequestId, serviceInfo)); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 343 | } |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 344 | |
| 345 | @Override |
| 346 | public void onDiscoveryQuerySent(@NonNull List<String> subtypes, |
| 347 | int sentQueryTransactionId) { |
| 348 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 349 | DISCOVERY_QUERY_SENT_CALLBACK, new MdnsEvent(mClientRequestId)); |
| 350 | } |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 351 | } |
| 352 | |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 353 | private class ResolutionListener extends MdnsListener { |
| 354 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 355 | ResolutionListener(int clientRequestId, int transactionId, |
Kangping Dong | 97b2adc | 2024-01-11 16:00:37 +0800 | [diff] [blame] | 356 | @NonNull String listenServiceType) { |
| 357 | super(clientRequestId, transactionId, listenServiceType); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Override |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 361 | public void onServiceFound(MdnsServiceInfo serviceInfo, boolean isServiceFromCache) { |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 362 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 363 | NsdManager.RESOLVE_SERVICE_SUCCEEDED, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 364 | new MdnsEvent(mClientRequestId, serviceInfo, isServiceFromCache)); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 365 | } |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 366 | |
| 367 | @Override |
| 368 | public void onDiscoveryQuerySent(@NonNull List<String> subtypes, |
| 369 | int sentQueryTransactionId) { |
| 370 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 371 | DISCOVERY_QUERY_SENT_CALLBACK, new MdnsEvent(mClientRequestId)); |
| 372 | } |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 373 | } |
| 374 | |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 375 | private class ServiceInfoListener extends MdnsListener { |
| 376 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 377 | ServiceInfoListener(int clientRequestId, int transactionId, |
Kangping Dong | 97b2adc | 2024-01-11 16:00:37 +0800 | [diff] [blame] | 378 | @NonNull String listenServiceType) { |
| 379 | super(clientRequestId, transactionId, listenServiceType); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | @Override |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 383 | public void onServiceFound(@NonNull MdnsServiceInfo serviceInfo, |
| 384 | boolean isServiceFromCache) { |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 385 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 386 | NsdManager.SERVICE_UPDATED, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 387 | new MdnsEvent(mClientRequestId, serviceInfo, isServiceFromCache)); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public void onServiceUpdated(@NonNull MdnsServiceInfo serviceInfo) { |
| 392 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 393 | NsdManager.SERVICE_UPDATED, |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 394 | new MdnsEvent(mClientRequestId, serviceInfo)); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public void onServiceRemoved(@NonNull MdnsServiceInfo serviceInfo) { |
| 399 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 400 | NsdManager.SERVICE_UPDATED_LOST, |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 401 | new MdnsEvent(mClientRequestId, serviceInfo)); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 402 | } |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 403 | |
| 404 | @Override |
| 405 | public void onDiscoveryQuerySent(@NonNull List<String> subtypes, |
| 406 | int sentQueryTransactionId) { |
| 407 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 408 | DISCOVERY_QUERY_SENT_CALLBACK, new MdnsEvent(mClientRequestId)); |
| 409 | } |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 412 | private class SocketRequestMonitor implements MdnsSocketProvider.SocketRequestMonitor { |
| 413 | @Override |
| 414 | public void onSocketRequestFulfilled(@Nullable Network socketNetwork, |
| 415 | @NonNull MdnsInterfaceSocket socket, @NonNull int[] transports) { |
| 416 | // The network may be null for Wi-Fi SoftAp interfaces (tethering), but there is no APF |
| 417 | // filtering on such interfaces, so taking the multicast lock is not necessary to |
| 418 | // disable APF filtering of multicast. |
| 419 | if (socketNetwork == null |
| 420 | || !CollectionUtils.contains(transports, TRANSPORT_WIFI) |
| 421 | || CollectionUtils.contains(transports, TRANSPORT_VPN)) { |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (mWifiLockRequiredNetworks.add(socketNetwork)) { |
| 426 | updateMulticastLock(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void onSocketDestroyed(@Nullable Network socketNetwork, |
| 432 | @NonNull MdnsInterfaceSocket socket) { |
| 433 | if (mWifiLockRequiredNetworks.remove(socketNetwork)) { |
| 434 | updateMulticastLock(); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | private class UidImportanceListener implements ActivityManager.OnUidImportanceListener { |
| 440 | private final Handler mHandler; |
| 441 | |
| 442 | private UidImportanceListener(Handler handler) { |
| 443 | mHandler = handler; |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public void onUidImportance(int uid, int importance) { |
| 448 | mHandler.post(() -> handleUidImportanceChanged(uid, importance)); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | private void handleUidImportanceChanged(int uid, int importance) { |
| 453 | // Lower importance values are more "important" |
| 454 | final boolean modified = importance <= mRunningAppActiveImportanceCutoff |
| 455 | ? mRunningAppActiveUids.add(uid) |
| 456 | : mRunningAppActiveUids.remove(uid); |
| 457 | if (modified) { |
| 458 | updateMulticastLock(); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Take or release the lock based on updated internal state. |
| 464 | * |
| 465 | * This determines whether the lock needs to be held based on |
| 466 | * {@link #mWifiLockRequiredNetworks}, {@link #mRunningAppActiveUids} and |
| 467 | * {@link ClientInfo#mClientRequests}, so it must be called after any of the these have been |
| 468 | * updated. |
| 469 | */ |
| 470 | private void updateMulticastLock() { |
| 471 | final int needsLockUid = getMulticastLockNeededUid(); |
| 472 | if (needsLockUid >= 0 && mHeldMulticastLock == null) { |
| 473 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 474 | if (wm == null) { |
| 475 | Log.wtf(TAG, "Got a TRANSPORT_WIFI network without WifiManager"); |
| 476 | return; |
| 477 | } |
| 478 | mHeldMulticastLock = wm.createMulticastLock(TAG); |
| 479 | mHeldMulticastLock.acquire(); |
| 480 | mServiceLogs.log("Taking multicast lock for uid " + needsLockUid); |
| 481 | } else if (needsLockUid < 0 && mHeldMulticastLock != null) { |
| 482 | mHeldMulticastLock.release(); |
| 483 | mHeldMulticastLock = null; |
| 484 | mServiceLogs.log("Released multicast lock"); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * @return The UID of an app requiring the multicast lock, or -1 if none. |
| 490 | */ |
| 491 | private int getMulticastLockNeededUid() { |
| 492 | if (mWifiLockRequiredNetworks.size() == 0) { |
| 493 | // Return early if NSD is not active, or not on any relevant network |
| 494 | return -1; |
| 495 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 496 | for (int i = 0; i < mTransactionIdToClientInfoMap.size(); i++) { |
| 497 | final ClientInfo clientInfo = mTransactionIdToClientInfoMap.valueAt(i); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 498 | if (!mRunningAppActiveUids.contains(clientInfo.mUid)) { |
| 499 | // Ignore non-active UIDs |
| 500 | continue; |
| 501 | } |
| 502 | |
| 503 | if (clientInfo.hasAnyJavaBackendRequestForNetworks(mWifiLockRequiredNetworks)) { |
| 504 | return clientInfo.mUid; |
| 505 | } |
| 506 | } |
| 507 | return -1; |
| 508 | } |
| 509 | |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 510 | /** |
| 511 | * Data class of mdns service callback information. |
| 512 | */ |
| 513 | private static class MdnsEvent { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 514 | final int mClientRequestId; |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 515 | @Nullable |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 516 | final MdnsServiceInfo mMdnsServiceInfo; |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 517 | final boolean mIsServiceFromCache; |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 518 | |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 519 | MdnsEvent(int clientRequestId) { |
| 520 | this(clientRequestId, null /* mdnsServiceInfo */, false /* isServiceFromCache */); |
| 521 | } |
| 522 | |
| 523 | MdnsEvent(int clientRequestId, @Nullable MdnsServiceInfo mdnsServiceInfo) { |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 524 | this(clientRequestId, mdnsServiceInfo, false /* isServiceFromCache */); |
| 525 | } |
| 526 | |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 527 | MdnsEvent(int clientRequestId, @Nullable MdnsServiceInfo mdnsServiceInfo, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 528 | boolean isServiceFromCache) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 529 | mClientRequestId = clientRequestId; |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 530 | mMdnsServiceInfo = mdnsServiceInfo; |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 531 | mIsServiceFromCache = isServiceFromCache; |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Paul Hu | 77c1118 | 2023-10-23 16:17:32 +0800 | [diff] [blame] | 535 | // TODO: Use a Handler instead of a StateMachine since there are no state changes. |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 536 | private class NsdStateMachine extends StateMachine { |
| 537 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 538 | private final EnabledState mEnabledState = new EnabledState(); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 539 | |
| 540 | @Override |
Wink Saville | 358f5d4 | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 541 | protected String getWhatToString(int what) { |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 542 | return NsdManager.nameOf(what); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 545 | private void maybeStartDaemon() { |
Paul Hu | 4a87ed4 | 2024-01-10 08:58:30 +0000 | [diff] [blame] | 546 | if (mIsDaemonStarted) { |
| 547 | if (DBG) Log.d(TAG, "Daemon is already started."); |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 548 | return; |
| 549 | } |
| 550 | |
Paul Hu | 4a87ed4 | 2024-01-10 08:58:30 +0000 | [diff] [blame] | 551 | if (mMDnsManager == null) { |
| 552 | Log.wtf(TAG, "maybeStartDaemon: mMDnsManager is null"); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | mMDnsManager.registerEventListener(mMDnsEventCallback); |
| 556 | mMDnsManager.startDaemon(); |
| 557 | mIsDaemonStarted = true; |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 558 | maybeScheduleStop(); |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 559 | mServiceLogs.log("Start mdns_responder daemon"); |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 560 | } |
| 561 | |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 562 | private void maybeStopDaemon() { |
Paul Hu | 4a87ed4 | 2024-01-10 08:58:30 +0000 | [diff] [blame] | 563 | if (!mIsDaemonStarted) { |
| 564 | if (DBG) Log.d(TAG, "Daemon has not been started."); |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
Paul Hu | 4a87ed4 | 2024-01-10 08:58:30 +0000 | [diff] [blame] | 568 | if (mMDnsManager == null) { |
| 569 | Log.wtf(TAG, "maybeStopDaemon: mMDnsManager is null"); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 570 | return; |
| 571 | } |
| 572 | mMDnsManager.unregisterEventListener(mMDnsEventCallback); |
| 573 | mMDnsManager.stopDaemon(); |
| 574 | mIsDaemonStarted = false; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 575 | mServiceLogs.log("Stop mdns_responder daemon"); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 576 | } |
| 577 | |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 578 | private boolean isAnyRequestActive() { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 579 | return mTransactionIdToClientInfoMap.size() != 0; |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | private void scheduleStop() { |
| 583 | sendMessageDelayed(NsdManager.DAEMON_CLEANUP, mCleanupDelayMs); |
| 584 | } |
| 585 | private void maybeScheduleStop() { |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 586 | // The native daemon should stay alive and can't be cleanup |
| 587 | // if any legacy client connected. |
| 588 | if (!isAnyRequestActive() && mLegacyClientCount == 0) { |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 589 | scheduleStop(); |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 593 | private void cancelStop() { |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 594 | this.removeMessages(NsdManager.DAEMON_CLEANUP); |
| 595 | } |
| 596 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 597 | private void maybeStartMonitoringSockets() { |
| 598 | if (mIsMonitoringSocketsStarted) { |
| 599 | if (DBG) Log.d(TAG, "Socket monitoring is already started."); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | mMdnsSocketProvider.startMonitoringSockets(); |
| 604 | mIsMonitoringSocketsStarted = true; |
| 605 | } |
| 606 | |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 607 | private void maybeStopMonitoringSocketsIfNoActiveRequest() { |
| 608 | if (!mIsMonitoringSocketsStarted) return; |
| 609 | if (isAnyRequestActive()) return; |
| 610 | |
Paul Hu | 58f2060 | 2023-02-18 11:41:07 +0800 | [diff] [blame] | 611 | mMdnsSocketProvider.requestStopWhenInactive(); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 612 | mIsMonitoringSocketsStarted = false; |
| 613 | } |
| 614 | |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 615 | NsdStateMachine(String name, Handler handler) { |
| 616 | super(name, handler); |
Paul Hu | 77c1118 | 2023-10-23 16:17:32 +0800 | [diff] [blame] | 617 | addState(mEnabledState); |
paulhu | 5568f45 | 2021-11-30 13:31:29 +0800 | [diff] [blame] | 618 | State initialState = mEnabledState; |
Hugo Benichi | 912db99 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 619 | setInitialState(initialState); |
Wink Saville | 358f5d4 | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 620 | setLogRecSize(25); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 623 | class EnabledState extends State { |
| 624 | @Override |
| 625 | public void enter() { |
| 626 | sendNsdStateChangeBroadcast(true); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | @Override |
| 630 | public void exit() { |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 631 | // TODO: it is incorrect to stop the daemon without expunging all requests |
| 632 | // and sending error callbacks to clients. |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 633 | scheduleStop(); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 636 | private boolean requestLimitReached(ClientInfo clientInfo) { |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 637 | if (clientInfo.mClientRequests.size() >= ClientInfo.MAX_LIMIT) { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 638 | if (DBG) Log.d(TAG, "Exceeded max outstanding requests " + clientInfo); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 639 | return true; |
| 640 | } |
| 641 | return false; |
| 642 | } |
| 643 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 644 | private ClientRequest storeLegacyRequestMap(int clientRequestId, int transactionId, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 645 | ClientInfo clientInfo, int what, long startTimeMs) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 646 | final LegacyClientRequest request = |
| 647 | new LegacyClientRequest(transactionId, what, startTimeMs); |
| 648 | clientInfo.mClientRequests.put(clientRequestId, request); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 649 | mTransactionIdToClientInfoMap.put(transactionId, clientInfo); |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 650 | // Remove the cleanup event because here comes a new request. |
| 651 | cancelStop(); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 652 | return request; |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 655 | private void storeAdvertiserRequestMap(int clientRequestId, int transactionId, |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 656 | ClientInfo clientInfo, @Nullable Network requestedNetwork) { |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 657 | clientInfo.mClientRequests.put(clientRequestId, new AdvertiserClientRequest( |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 658 | transactionId, requestedNetwork, mClock.elapsedRealtime())); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 659 | mTransactionIdToClientInfoMap.put(transactionId, clientInfo); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 660 | updateMulticastLock(); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 661 | } |
| 662 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 663 | private void removeRequestMap( |
| 664 | int clientRequestId, int transactionId, ClientInfo clientInfo) { |
| 665 | final ClientRequest existing = clientInfo.mClientRequests.get(clientRequestId); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 666 | if (existing == null) return; |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 667 | clientInfo.mClientRequests.remove(clientRequestId); |
| 668 | mTransactionIdToClientInfoMap.remove(transactionId); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 669 | |
| 670 | if (existing instanceof LegacyClientRequest) { |
| 671 | maybeScheduleStop(); |
| 672 | } else { |
| 673 | maybeStopMonitoringSocketsIfNoActiveRequest(); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 674 | updateMulticastLock(); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 678 | private ClientRequest storeDiscoveryManagerRequestMap(int clientRequestId, |
| 679 | int transactionId, MdnsListener listener, ClientInfo clientInfo, |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 680 | @Nullable Network requestedNetwork) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 681 | final DiscoveryManagerRequest request = new DiscoveryManagerRequest(transactionId, |
| 682 | listener, requestedNetwork, mClock.elapsedRealtime()); |
| 683 | clientInfo.mClientRequests.put(clientRequestId, request); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 684 | mTransactionIdToClientInfoMap.put(transactionId, clientInfo); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 685 | updateMulticastLock(); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 686 | return request; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 687 | } |
| 688 | |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 689 | /** |
| 690 | * Truncate a service name to up to 63 UTF-8 bytes. |
| 691 | * |
| 692 | * See RFC6763 4.1.1: service instance names are UTF-8 and up to 63 bytes. Truncating |
| 693 | * names used in registerService follows historical behavior (see mdnsresponder |
| 694 | * handle_regservice_request). |
| 695 | */ |
| 696 | @NonNull |
| 697 | private String truncateServiceName(@NonNull String originalName) { |
Yuyang Huang | de802c8 | 2023-05-02 17:14:22 +0900 | [diff] [blame] | 698 | return MdnsUtils.truncateServiceName(originalName, MAX_LABEL_LENGTH); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 699 | } |
| 700 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 701 | private void stopDiscoveryManagerRequest(ClientRequest request, int clientRequestId, |
| 702 | int transactionId, ClientInfo clientInfo) { |
Paul Hu | e4f5f25 | 2023-02-16 21:13:47 +0800 | [diff] [blame] | 703 | clientInfo.unregisterMdnsListenerFromRequest(request); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 704 | removeRequestMap(clientRequestId, transactionId, clientInfo); |
Paul Hu | e4f5f25 | 2023-02-16 21:13:47 +0800 | [diff] [blame] | 705 | } |
| 706 | |
Paul Hu | 77c1118 | 2023-10-23 16:17:32 +0800 | [diff] [blame] | 707 | private ClientInfo getClientInfoForReply(Message msg) { |
| 708 | final ListenerArgs args = (ListenerArgs) msg.obj; |
| 709 | return mClients.get(args.connector); |
| 710 | } |
| 711 | |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 712 | /** |
| 713 | * Returns {@code false} if {@code subtypes} exceeds the maximum number limit or |
| 714 | * contains invalid subtype label. |
| 715 | */ |
| 716 | private boolean checkSubtypeLabels(Set<String> subtypes) { |
| 717 | if (subtypes.size() > MAX_SUBTYPE_COUNT) { |
| 718 | mServiceLogs.e( |
| 719 | "Too many subtypes: " + subtypes.size() + " (max = " |
| 720 | + MAX_SUBTYPE_COUNT + ")"); |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | for (String subtype : subtypes) { |
| 725 | if (!checkSubtypeLabel(subtype)) { |
| 726 | mServiceLogs.e("Subtype " + subtype + " is invalid"); |
| 727 | return false; |
| 728 | } |
| 729 | } |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | private Set<String> dedupSubtypeLabels(Collection<String> subtypes) { |
| 734 | final Map<String, String> subtypeMap = new LinkedHashMap<>(subtypes.size()); |
| 735 | for (String subtype : subtypes) { |
| 736 | subtypeMap.put(MdnsUtils.toDnsLowerCase(subtype), subtype); |
| 737 | } |
| 738 | return new ArraySet<>(subtypeMap.values()); |
| 739 | } |
| 740 | |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 741 | @Override |
| 742 | public boolean processMessage(Message msg) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 743 | final ClientInfo clientInfo; |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 744 | final int transactionId; |
| 745 | final int clientRequestId = msg.arg2; |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 746 | final OffloadEngineInfo offloadEngineInfo; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 747 | switch (msg.what) { |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 748 | case NsdManager.DISCOVER_SERVICES: { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 749 | if (DBG) Log.d(TAG, "Discover services"); |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 750 | final DiscoveryArgs discoveryArgs = (DiscoveryArgs) msg.obj; |
| 751 | clientInfo = mClients.get(discoveryArgs.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 752 | // If the binder death notification for a INsdManagerCallback was received |
| 753 | // before any calls are received by NsdService, the clientInfo would be |
| 754 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 755 | if (clientInfo == null) { |
| 756 | Log.e(TAG, "Unknown connector in discovery"); |
| 757 | break; |
| 758 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 759 | |
| 760 | if (requestLimitReached(clientInfo)) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 761 | clientInfo.onDiscoverServicesFailedImmediately(clientRequestId, |
| 762 | NsdManager.FAILURE_MAX_LIMIT, true /* isLegacy */); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 763 | break; |
| 764 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 765 | |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 766 | final DiscoveryRequest discoveryRequest = discoveryArgs.discoveryRequest; |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 767 | transactionId = getUniqueId(); |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 768 | final Pair<String, List<String>> typeAndSubtype = |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 769 | parseTypeAndSubtype(discoveryRequest.getServiceType()); |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 770 | final String serviceType = typeAndSubtype == null |
| 771 | ? null : typeAndSubtype.first; |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 772 | if (clientInfo.mUseJavaBackend |
| 773 | || mDeps.isMdnsDiscoveryManagerEnabled(mContext) |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 774 | || useDiscoveryManagerForType(serviceType)) { |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 775 | if (serviceType == null || typeAndSubtype.second.size() > 1) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 776 | clientInfo.onDiscoverServicesFailedImmediately(clientRequestId, |
| 777 | NsdManager.FAILURE_INTERNAL_ERROR, false /* isLegacy */); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 778 | break; |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 779 | } |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 780 | |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 781 | String subtype = discoveryRequest.getSubtype(); |
| 782 | if (subtype == null && !typeAndSubtype.second.isEmpty()) { |
| 783 | subtype = typeAndSubtype.second.get(0); |
| 784 | } |
| 785 | |
| 786 | if (subtype != null && !checkSubtypeLabel(subtype)) { |
| 787 | clientInfo.onDiscoverServicesFailedImmediately(clientRequestId, |
| 788 | NsdManager.FAILURE_BAD_PARAMETERS, false /* isLegacy */); |
| 789 | break; |
| 790 | } |
| 791 | |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 792 | final String listenServiceType = serviceType + ".local"; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 793 | maybeStartMonitoringSockets(); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 794 | final MdnsListener listener = new DiscoveryListener(clientRequestId, |
Kangping Dong | 97b2adc | 2024-01-11 16:00:37 +0800 | [diff] [blame] | 795 | transactionId, listenServiceType); |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 796 | final MdnsSearchOptions.Builder optionsBuilder = |
| 797 | MdnsSearchOptions.newBuilder() |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 798 | .setNetwork(discoveryRequest.getNetwork()) |
Yuyang Huang | ff96322 | 2023-06-01 18:42:42 +0900 | [diff] [blame] | 799 | .setRemoveExpiredService(true) |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 800 | .setQueryMode( |
| 801 | mMdnsFeatureFlags.isAggressiveQueryModeEnabled() |
| 802 | ? AGGRESSIVE_QUERY_MODE |
| 803 | : PASSIVE_QUERY_MODE); |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 804 | if (subtype != null) { |
| 805 | // checkSubtypeLabels() ensures that subtypes start with '_' but |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 806 | // MdnsSearchOptions expects the underscore to not be present. |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 807 | optionsBuilder.addSubtype(subtype.substring(1)); |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 808 | } |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 809 | mMdnsDiscoveryManager.registerListener( |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 810 | listenServiceType, listener, optionsBuilder.build()); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 811 | final ClientRequest request = storeDiscoveryManagerRequestMap( |
| 812 | clientRequestId, transactionId, listener, clientInfo, |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 813 | discoveryRequest.getNetwork()); |
| 814 | clientInfo.onDiscoverServicesStarted( |
| 815 | clientRequestId, discoveryRequest, request); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 816 | clientInfo.log("Register a DiscoveryListener " + transactionId |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 817 | + " for service type:" + listenServiceType); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 818 | } else { |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 819 | maybeStartDaemon(); |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 820 | if (discoverServices(transactionId, discoveryRequest)) { |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 821 | if (DBG) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 822 | Log.d(TAG, "Discover " + msg.arg2 + " " + transactionId |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 823 | + discoveryRequest.getServiceType()); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 824 | } |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 825 | final ClientRequest request = storeLegacyRequestMap(clientRequestId, |
| 826 | transactionId, clientInfo, msg.what, |
| 827 | mClock.elapsedRealtime()); |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 828 | clientInfo.onDiscoverServicesStarted( |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 829 | clientRequestId, discoveryRequest, request); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 830 | } else { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 831 | stopServiceDiscovery(transactionId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 832 | clientInfo.onDiscoverServicesFailedImmediately(clientRequestId, |
| 833 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 834 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 835 | } |
| 836 | break; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 837 | } |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 838 | case NsdManager.STOP_DISCOVERY: { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 839 | if (DBG) Log.d(TAG, "Stop service discovery"); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 840 | final ListenerArgs args = (ListenerArgs) msg.obj; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 841 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 842 | // If the binder death notification for a INsdManagerCallback was received |
| 843 | // before any calls are received by NsdService, the clientInfo would be |
| 844 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 845 | if (clientInfo == null) { |
| 846 | Log.e(TAG, "Unknown connector in stop discovery"); |
| 847 | break; |
| 848 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 849 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 850 | final ClientRequest request = |
| 851 | clientInfo.mClientRequests.get(clientRequestId); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 852 | if (request == null) { |
| 853 | Log.e(TAG, "Unknown client request in STOP_DISCOVERY"); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 854 | break; |
| 855 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 856 | transactionId = request.mTransactionId; |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 857 | // Note isMdnsDiscoveryManagerEnabled may have changed to false at this |
| 858 | // point, so this needs to check the type of the original request to |
| 859 | // unregister instead of looking at the flag value. |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 860 | if (request instanceof DiscoveryManagerRequest) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 861 | stopDiscoveryManagerRequest( |
| 862 | request, clientRequestId, transactionId, clientInfo); |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 863 | clientInfo.onStopDiscoverySucceeded(clientRequestId, request); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 864 | clientInfo.log("Unregister the DiscoveryListener " + transactionId); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 865 | } else { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 866 | removeRequestMap(clientRequestId, transactionId, clientInfo); |
| 867 | if (stopServiceDiscovery(transactionId)) { |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 868 | clientInfo.onStopDiscoverySucceeded(clientRequestId, request); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 869 | } else { |
| 870 | clientInfo.onStopDiscoveryFailed( |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 871 | clientRequestId, NsdManager.FAILURE_INTERNAL_ERROR); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 872 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 873 | } |
| 874 | break; |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 875 | } |
| 876 | case NsdManager.REGISTER_SERVICE: { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 877 | if (DBG) Log.d(TAG, "Register service"); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 878 | final AdvertisingArgs args = (AdvertisingArgs) msg.obj; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 879 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 880 | // If the binder death notification for a INsdManagerCallback was received |
| 881 | // before any calls are received by NsdService, the clientInfo would be |
| 882 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 883 | if (clientInfo == null) { |
| 884 | Log.e(TAG, "Unknown connector in registration"); |
| 885 | break; |
| 886 | } |
| 887 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 888 | if (requestLimitReached(clientInfo)) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 889 | clientInfo.onRegisterServiceFailedImmediately(clientRequestId, |
| 890 | NsdManager.FAILURE_MAX_LIMIT, true /* isLegacy */); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 891 | break; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 892 | } |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 893 | final AdvertisingRequest advertisingRequest = args.advertisingRequest; |
| 894 | if (advertisingRequest == null) { |
| 895 | Log.e(TAG, "Unknown advertisingRequest in registration"); |
| 896 | break; |
| 897 | } |
| 898 | final NsdServiceInfo serviceInfo = advertisingRequest.getServiceInfo(); |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 899 | final String serviceType = serviceInfo.getServiceType(); |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 900 | final Pair<String, List<String>> typeSubtype = parseTypeAndSubtype( |
| 901 | serviceType); |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 902 | final String registerServiceType = typeSubtype == null |
| 903 | ? null : typeSubtype.first; |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 904 | final String hostname = serviceInfo.getHostname(); |
| 905 | // Keep compatible with the legacy behavior: It's allowed to set host |
| 906 | // addresses for a service registration although the host addresses |
| 907 | // won't be registered. To register the addresses for a host, the |
| 908 | // hostname must be specified. |
| 909 | if (hostname == null) { |
| 910 | serviceInfo.setHostAddresses(Collections.emptyList()); |
| 911 | } |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 912 | if (clientInfo.mUseJavaBackend |
| 913 | || mDeps.isMdnsAdvertiserEnabled(mContext) |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 914 | || useAdvertiserForType(registerServiceType)) { |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 915 | if (serviceType != null && registerServiceType == null) { |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 916 | Log.e(TAG, "Invalid service type: " + serviceType); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 917 | clientInfo.onRegisterServiceFailedImmediately(clientRequestId, |
| 918 | NsdManager.FAILURE_INTERNAL_ERROR, false /* isLegacy */); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 919 | break; |
| 920 | } |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 921 | boolean isUpdateOnly = (advertisingRequest.getAdvertisingConfig() |
| 922 | & AdvertisingRequest.NSD_ADVERTISING_UPDATE_ONLY) > 0; |
| 923 | // If it is an update request, then reuse the old transactionId |
| 924 | if (isUpdateOnly) { |
| 925 | final ClientRequest existingClientRequest = |
| 926 | clientInfo.mClientRequests.get(clientRequestId); |
| 927 | if (existingClientRequest == null) { |
| 928 | Log.e(TAG, "Invalid update on requestId: " + clientRequestId); |
| 929 | clientInfo.onRegisterServiceFailedImmediately(clientRequestId, |
| 930 | NsdManager.FAILURE_INTERNAL_ERROR, |
| 931 | false /* isLegacy */); |
| 932 | break; |
| 933 | } |
| 934 | transactionId = existingClientRequest.mTransactionId; |
| 935 | } else { |
| 936 | transactionId = getUniqueId(); |
| 937 | } |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 938 | |
| 939 | if (registerServiceType != null) { |
| 940 | serviceInfo.setServiceType(registerServiceType); |
| 941 | serviceInfo.setServiceName( |
| 942 | truncateServiceName(serviceInfo.getServiceName())); |
| 943 | } |
| 944 | |
| 945 | if (!checkHostname(hostname)) { |
| 946 | clientInfo.onRegisterServiceFailedImmediately(clientRequestId, |
| 947 | NsdManager.FAILURE_BAD_PARAMETERS, false /* isLegacy */); |
| 948 | break; |
| 949 | } |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 950 | |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 951 | Set<String> subtypes = new ArraySet<>(serviceInfo.getSubtypes()); |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 952 | if (typeSubtype != null && typeSubtype.second != null) { |
| 953 | for (String subType : typeSubtype.second) { |
| 954 | if (!TextUtils.isEmpty(subType)) { |
| 955 | subtypes.add(subType); |
| 956 | } |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 957 | } |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 958 | } |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 959 | subtypes = dedupSubtypeLabels(subtypes); |
| 960 | |
| 961 | if (!checkSubtypeLabels(subtypes)) { |
| 962 | clientInfo.onRegisterServiceFailedImmediately(clientRequestId, |
| 963 | NsdManager.FAILURE_BAD_PARAMETERS, false /* isLegacy */); |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | serviceInfo.setSubtypes(subtypes); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 968 | maybeStartMonitoringSockets(); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 969 | final MdnsAdvertisingOptions mdnsAdvertisingOptions = |
| 970 | MdnsAdvertisingOptions.newBuilder().setIsOnlyUpdate( |
| 971 | isUpdateOnly).build(); |
Yuyang Huang | e5cba9c | 2023-11-02 18:05:47 +0900 | [diff] [blame] | 972 | mAdvertiser.addOrUpdateService(transactionId, serviceInfo, |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 973 | mdnsAdvertisingOptions, clientInfo.mUid); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 974 | storeAdvertiserRequestMap(clientRequestId, transactionId, clientInfo, |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 975 | serviceInfo.getNetwork()); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 976 | } else { |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 977 | maybeStartDaemon(); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 978 | transactionId = getUniqueId(); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 979 | if (registerService(transactionId, serviceInfo)) { |
| 980 | if (DBG) { |
| 981 | Log.d(TAG, "Register " + clientRequestId |
| 982 | + " " + transactionId); |
| 983 | } |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 984 | storeLegacyRequestMap(clientRequestId, transactionId, clientInfo, |
| 985 | msg.what, mClock.elapsedRealtime()); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 986 | // Return success after mDns reports success |
| 987 | } else { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 988 | unregisterService(transactionId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 989 | clientInfo.onRegisterServiceFailedImmediately(clientRequestId, |
| 990 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 991 | } |
| 992 | |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 993 | } |
| 994 | break; |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 995 | } |
| 996 | case NsdManager.UNREGISTER_SERVICE: { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 997 | if (DBG) Log.d(TAG, "unregister service"); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 998 | final ListenerArgs args = (ListenerArgs) msg.obj; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 999 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 1000 | // If the binder death notification for a INsdManagerCallback was received |
| 1001 | // before any calls are received by NsdService, the clientInfo would be |
| 1002 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 1003 | if (clientInfo == null) { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 1004 | Log.e(TAG, "Unknown connector in unregistration"); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1005 | break; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1006 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1007 | final ClientRequest request = |
| 1008 | clientInfo.mClientRequests.get(clientRequestId); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1009 | if (request == null) { |
| 1010 | Log.e(TAG, "Unknown client request in UNREGISTER_SERVICE"); |
| 1011 | break; |
| 1012 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1013 | transactionId = request.mTransactionId; |
| 1014 | removeRequestMap(clientRequestId, transactionId, clientInfo); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1015 | |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 1016 | // Note isMdnsAdvertiserEnabled may have changed to false at this point, |
| 1017 | // so this needs to check the type of the original request to unregister |
| 1018 | // instead of looking at the flag value. |
| 1019 | if (request instanceof AdvertiserClientRequest) { |
Paul Hu | 043bcd4 | 2023-07-14 16:38:25 +0800 | [diff] [blame] | 1020 | final AdvertiserMetrics metrics = |
| 1021 | mAdvertiser.getAdvertiserMetrics(transactionId); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1022 | mAdvertiser.removeService(transactionId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1023 | clientInfo.onUnregisterServiceSucceeded( |
| 1024 | clientRequestId, request, metrics); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1025 | } else { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1026 | if (unregisterService(transactionId)) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1027 | clientInfo.onUnregisterServiceSucceeded(clientRequestId, request, |
Paul Hu | 043bcd4 | 2023-07-14 16:38:25 +0800 | [diff] [blame] | 1028 | new AdvertiserMetrics(NO_PACKET /* repliedRequestsCount */, |
| 1029 | NO_PACKET /* sentPacketCount */, |
| 1030 | 0 /* conflictDuringProbingCount */, |
| 1031 | 0 /* conflictAfterProbingCount */)); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1032 | } else { |
| 1033 | clientInfo.onUnregisterServiceFailed( |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1034 | clientRequestId, NsdManager.FAILURE_INTERNAL_ERROR); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1035 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1036 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1037 | break; |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1038 | } |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1039 | case NsdManager.RESOLVE_SERVICE: { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 1040 | if (DBG) Log.d(TAG, "Resolve service"); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 1041 | final ListenerArgs args = (ListenerArgs) msg.obj; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 1042 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 1043 | // If the binder death notification for a INsdManagerCallback was received |
| 1044 | // before any calls are received by NsdService, the clientInfo would be |
| 1045 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 1046 | if (clientInfo == null) { |
| 1047 | Log.e(TAG, "Unknown connector in resolution"); |
| 1048 | break; |
| 1049 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1050 | |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1051 | final NsdServiceInfo info = args.serviceInfo; |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1052 | transactionId = getUniqueId(); |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 1053 | final Pair<String, List<String>> typeSubtype = |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 1054 | parseTypeAndSubtype(info.getServiceType()); |
| 1055 | final String serviceType = typeSubtype == null |
| 1056 | ? null : typeSubtype.first; |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 1057 | if (clientInfo.mUseJavaBackend |
| 1058 | || mDeps.isMdnsDiscoveryManagerEnabled(mContext) |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 1059 | || useDiscoveryManagerForType(serviceType)) { |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1060 | if (serviceType == null) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1061 | clientInfo.onResolveServiceFailedImmediately(clientRequestId, |
| 1062 | NsdManager.FAILURE_INTERNAL_ERROR, false /* isLegacy */); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1063 | break; |
| 1064 | } |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1065 | final String resolveServiceType = serviceType + ".local"; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1066 | |
| 1067 | maybeStartMonitoringSockets(); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1068 | final MdnsListener listener = new ResolutionListener(clientRequestId, |
Kangping Dong | 97b2adc | 2024-01-11 16:00:37 +0800 | [diff] [blame] | 1069 | transactionId, resolveServiceType); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1070 | final MdnsSearchOptions options = MdnsSearchOptions.newBuilder() |
| 1071 | .setNetwork(info.getNetwork()) |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 1072 | .setQueryMode(mMdnsFeatureFlags.isAggressiveQueryModeEnabled() |
| 1073 | ? AGGRESSIVE_QUERY_MODE |
| 1074 | : PASSIVE_QUERY_MODE) |
Remi NGUYEN VAN | bb62b1d | 2023-02-27 12:18:27 +0900 | [diff] [blame] | 1075 | .setResolveInstanceName(info.getServiceName()) |
Yuyang Huang | ff96322 | 2023-06-01 18:42:42 +0900 | [diff] [blame] | 1076 | .setRemoveExpiredService(true) |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1077 | .build(); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1078 | mMdnsDiscoveryManager.registerListener( |
| 1079 | resolveServiceType, listener, options); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1080 | storeDiscoveryManagerRequestMap(clientRequestId, transactionId, |
| 1081 | listener, clientInfo, info.getNetwork()); |
| 1082 | clientInfo.log("Register a ResolutionListener " + transactionId |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 1083 | + " for service type:" + resolveServiceType); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1084 | } else { |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1085 | if (clientInfo.mResolvedService != null) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1086 | clientInfo.onResolveServiceFailedImmediately(clientRequestId, |
| 1087 | NsdManager.FAILURE_ALREADY_ACTIVE, true /* isLegacy */); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1088 | break; |
| 1089 | } |
| 1090 | |
| 1091 | maybeStartDaemon(); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1092 | if (resolveService(transactionId, info)) { |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1093 | clientInfo.mResolvedService = new NsdServiceInfo(); |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1094 | storeLegacyRequestMap(clientRequestId, transactionId, clientInfo, |
| 1095 | msg.what, mClock.elapsedRealtime()); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1096 | } else { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1097 | clientInfo.onResolveServiceFailedImmediately(clientRequestId, |
| 1098 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1099 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1100 | } |
| 1101 | break; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1102 | } |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1103 | case NsdManager.STOP_RESOLUTION: { |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 1104 | if (DBG) Log.d(TAG, "Stop service resolution"); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 1105 | final ListenerArgs args = (ListenerArgs) msg.obj; |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 1106 | clientInfo = mClients.get(args.connector); |
| 1107 | // If the binder death notification for a INsdManagerCallback was received |
| 1108 | // before any calls are received by NsdService, the clientInfo would be |
| 1109 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 1110 | if (clientInfo == null) { |
| 1111 | Log.e(TAG, "Unknown connector in stop resolution"); |
| 1112 | break; |
| 1113 | } |
| 1114 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1115 | final ClientRequest request = |
| 1116 | clientInfo.mClientRequests.get(clientRequestId); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1117 | if (request == null) { |
| 1118 | Log.e(TAG, "Unknown client request in STOP_RESOLUTION"); |
| 1119 | break; |
| 1120 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1121 | transactionId = request.mTransactionId; |
Paul Hu | e4f5f25 | 2023-02-16 21:13:47 +0800 | [diff] [blame] | 1122 | // Note isMdnsDiscoveryManagerEnabled may have changed to false at this |
| 1123 | // point, so this needs to check the type of the original request to |
| 1124 | // unregister instead of looking at the flag value. |
| 1125 | if (request instanceof DiscoveryManagerRequest) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1126 | stopDiscoveryManagerRequest( |
| 1127 | request, clientRequestId, transactionId, clientInfo); |
Paul Hu | 6014905 | 2023-07-31 14:26:08 +0800 | [diff] [blame] | 1128 | clientInfo.onStopResolutionSucceeded(clientRequestId, request); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1129 | clientInfo.log("Unregister the ResolutionListener " + transactionId); |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 1130 | } else { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1131 | removeRequestMap(clientRequestId, transactionId, clientInfo); |
| 1132 | if (stopResolveService(transactionId)) { |
Paul Hu | 6014905 | 2023-07-31 14:26:08 +0800 | [diff] [blame] | 1133 | clientInfo.onStopResolutionSucceeded(clientRequestId, request); |
Paul Hu | e4f5f25 | 2023-02-16 21:13:47 +0800 | [diff] [blame] | 1134 | } else { |
| 1135 | clientInfo.onStopResolutionFailed( |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1136 | clientRequestId, NsdManager.FAILURE_OPERATION_NOT_RUNNING); |
Paul Hu | e4f5f25 | 2023-02-16 21:13:47 +0800 | [diff] [blame] | 1137 | } |
| 1138 | clientInfo.mResolvedService = null; |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 1139 | } |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 1140 | break; |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1141 | } |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1142 | case NsdManager.REGISTER_SERVICE_CALLBACK: { |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1143 | if (DBG) Log.d(TAG, "Register a service callback"); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 1144 | final ListenerArgs args = (ListenerArgs) msg.obj; |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1145 | clientInfo = mClients.get(args.connector); |
| 1146 | // If the binder death notification for a INsdManagerCallback was received |
| 1147 | // before any calls are received by NsdService, the clientInfo would be |
| 1148 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 1149 | if (clientInfo == null) { |
| 1150 | Log.e(TAG, "Unknown connector in callback registration"); |
| 1151 | break; |
| 1152 | } |
| 1153 | |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1154 | final NsdServiceInfo info = args.serviceInfo; |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1155 | transactionId = getUniqueId(); |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 1156 | final Pair<String, List<String>> typeAndSubtype = |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 1157 | parseTypeAndSubtype(info.getServiceType()); |
| 1158 | final String serviceType = typeAndSubtype == null |
| 1159 | ? null : typeAndSubtype.first; |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1160 | if (serviceType == null) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1161 | clientInfo.onServiceInfoCallbackRegistrationFailed(clientRequestId, |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1162 | NsdManager.FAILURE_BAD_PARAMETERS); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | } |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1165 | final String resolveServiceType = serviceType + ".local"; |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1166 | |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1167 | maybeStartMonitoringSockets(); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1168 | final MdnsListener listener = new ServiceInfoListener(clientRequestId, |
Kangping Dong | 97b2adc | 2024-01-11 16:00:37 +0800 | [diff] [blame] | 1169 | transactionId, resolveServiceType); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1170 | final MdnsSearchOptions options = MdnsSearchOptions.newBuilder() |
| 1171 | .setNetwork(info.getNetwork()) |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 1172 | .setQueryMode(mMdnsFeatureFlags.isAggressiveQueryModeEnabled() |
| 1173 | ? AGGRESSIVE_QUERY_MODE |
| 1174 | : PASSIVE_QUERY_MODE) |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1175 | .setResolveInstanceName(info.getServiceName()) |
Yuyang Huang | ff96322 | 2023-06-01 18:42:42 +0900 | [diff] [blame] | 1176 | .setRemoveExpiredService(true) |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1177 | .build(); |
| 1178 | mMdnsDiscoveryManager.registerListener( |
| 1179 | resolveServiceType, listener, options); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1180 | storeDiscoveryManagerRequestMap(clientRequestId, transactionId, listener, |
| 1181 | clientInfo, info.getNetwork()); |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 1182 | clientInfo.onServiceInfoCallbackRegistered(transactionId); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1183 | clientInfo.log("Register a ServiceInfoListener " + transactionId |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 1184 | + " for service type:" + resolveServiceType); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1185 | break; |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1186 | } |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1187 | case NsdManager.UNREGISTER_SERVICE_CALLBACK: { |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1188 | if (DBG) Log.d(TAG, "Unregister a service callback"); |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 1189 | final ListenerArgs args = (ListenerArgs) msg.obj; |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1190 | clientInfo = mClients.get(args.connector); |
| 1191 | // If the binder death notification for a INsdManagerCallback was received |
| 1192 | // before any calls are received by NsdService, the clientInfo would be |
| 1193 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 1194 | if (clientInfo == null) { |
| 1195 | Log.e(TAG, "Unknown connector in callback unregistration"); |
| 1196 | break; |
| 1197 | } |
| 1198 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1199 | final ClientRequest request = |
| 1200 | clientInfo.mClientRequests.get(clientRequestId); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1201 | if (request == null) { |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1202 | Log.e(TAG, "Unknown client request in UNREGISTER_SERVICE_CALLBACK"); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1203 | break; |
| 1204 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1205 | transactionId = request.mTransactionId; |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1206 | if (request instanceof DiscoveryManagerRequest) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1207 | stopDiscoveryManagerRequest( |
| 1208 | request, clientRequestId, transactionId, clientInfo); |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 1209 | clientInfo.onServiceInfoCallbackUnregistered(clientRequestId, request); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1210 | clientInfo.log("Unregister the ServiceInfoListener " + transactionId); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1211 | } else { |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1212 | loge("Unregister failed with non-DiscoveryManagerRequest."); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1213 | } |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1214 | break; |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1215 | } |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1216 | case MDNS_SERVICE_EVENT: |
| 1217 | if (!handleMDnsServiceEvent(msg.arg1, msg.arg2, msg.obj)) { |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 1218 | return NOT_HANDLED; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1219 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1220 | break; |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1221 | case MDNS_DISCOVERY_MANAGER_EVENT: |
| 1222 | if (!handleMdnsDiscoveryManagerEvent(msg.arg1, msg.arg2, msg.obj)) { |
| 1223 | return NOT_HANDLED; |
| 1224 | } |
| 1225 | break; |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 1226 | case NsdManager.REGISTER_OFFLOAD_ENGINE: |
| 1227 | offloadEngineInfo = (OffloadEngineInfo) msg.obj; |
| 1228 | // TODO: Limits the number of registrations created by a given class. |
| 1229 | mOffloadEngines.register(offloadEngineInfo.mOffloadEngine, |
| 1230 | offloadEngineInfo); |
Yuyang Huang | c275a9e | 2023-08-25 18:03:22 +0900 | [diff] [blame] | 1231 | sendAllOffloadServiceInfos(offloadEngineInfo); |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 1232 | break; |
| 1233 | case NsdManager.UNREGISTER_OFFLOAD_ENGINE: |
| 1234 | mOffloadEngines.unregister((IOffloadEngine) msg.obj); |
| 1235 | break; |
Paul Hu | 77c1118 | 2023-10-23 16:17:32 +0800 | [diff] [blame] | 1236 | case NsdManager.REGISTER_CLIENT: |
| 1237 | final ConnectorArgs arg = (ConnectorArgs) msg.obj; |
| 1238 | final INsdManagerCallback cb = arg.callback; |
| 1239 | try { |
| 1240 | cb.asBinder().linkToDeath(arg.connector, 0); |
| 1241 | final String tag = "Client" + arg.uid + "-" + mClientNumberId++; |
| 1242 | final NetworkNsdReportedMetrics metrics = |
| 1243 | mDeps.makeNetworkNsdReportedMetrics( |
| 1244 | (int) mClock.elapsedRealtime()); |
| 1245 | clientInfo = new ClientInfo(cb, arg.uid, arg.useJavaBackend, |
| 1246 | mServiceLogs.forSubComponent(tag), metrics); |
| 1247 | mClients.put(arg.connector, clientInfo); |
| 1248 | } catch (RemoteException e) { |
| 1249 | Log.w(TAG, "Client request id " + clientRequestId |
| 1250 | + " has already died"); |
| 1251 | } |
| 1252 | break; |
| 1253 | case NsdManager.UNREGISTER_CLIENT: |
| 1254 | final NsdServiceConnector connector = (NsdServiceConnector) msg.obj; |
| 1255 | clientInfo = mClients.remove(connector); |
| 1256 | if (clientInfo != null) { |
| 1257 | clientInfo.expungeAllRequests(); |
| 1258 | if (clientInfo.isPreSClient()) { |
| 1259 | mLegacyClientCount -= 1; |
| 1260 | } |
| 1261 | } |
| 1262 | maybeStopMonitoringSocketsIfNoActiveRequest(); |
| 1263 | maybeScheduleStop(); |
| 1264 | break; |
| 1265 | case NsdManager.DAEMON_CLEANUP: |
| 1266 | maybeStopDaemon(); |
| 1267 | break; |
| 1268 | // This event should be only sent by the legacy (target SDK < S) clients. |
| 1269 | // Mark the sending client as legacy. |
| 1270 | case NsdManager.DAEMON_STARTUP: |
| 1271 | clientInfo = getClientInfoForReply(msg); |
| 1272 | if (clientInfo != null) { |
| 1273 | cancelStop(); |
| 1274 | clientInfo.setPreSClient(); |
| 1275 | mLegacyClientCount += 1; |
| 1276 | maybeStartDaemon(); |
| 1277 | } |
| 1278 | break; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1279 | default: |
Paul Hu | 77c1118 | 2023-10-23 16:17:32 +0800 | [diff] [blame] | 1280 | Log.wtf(TAG, "Unhandled " + msg); |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 1281 | return NOT_HANDLED; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1282 | } |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 1283 | return HANDLED; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1284 | } |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1285 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1286 | private boolean handleMDnsServiceEvent(int code, int transactionId, Object obj) { |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1287 | NsdServiceInfo servInfo; |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1288 | ClientInfo clientInfo = mTransactionIdToClientInfoMap.get(transactionId); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1289 | if (clientInfo == null) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1290 | Log.e(TAG, String.format( |
| 1291 | "transactionId %d for %d has no client mapping", transactionId, code)); |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 1292 | return false; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /* This goes in response as msg.arg2 */ |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1296 | int clientRequestId = clientInfo.getClientRequestId(transactionId); |
| 1297 | if (clientRequestId < 0) { |
Vinit Deshapnde | 930a851 | 2013-06-25 19:45:03 -0700 | [diff] [blame] | 1298 | // This can happen because of race conditions. For example, |
| 1299 | // SERVICE_FOUND may race with STOP_SERVICE_DISCOVERY, |
| 1300 | // and we may get in this situation. |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1301 | Log.d(TAG, String.format("%d for transactionId %d that is no longer active", |
| 1302 | code, transactionId)); |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 1303 | return false; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1304 | } |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 1305 | final ClientRequest request = clientInfo.mClientRequests.get(clientRequestId); |
| 1306 | if (request == null) { |
| 1307 | Log.e(TAG, "Unknown client request. clientRequestId=" + clientRequestId); |
| 1308 | return false; |
| 1309 | } |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 1310 | if (DBG) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1311 | Log.d(TAG, String.format( |
| 1312 | "MDns service event code:%d transactionId=%d", code, transactionId)); |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 1313 | } |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1314 | switch (code) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1315 | case IMDnsEventListener.SERVICE_FOUND: { |
| 1316 | final DiscoveryInfo info = (DiscoveryInfo) obj; |
| 1317 | final String name = info.serviceName; |
| 1318 | final String type = info.registrationType; |
| 1319 | servInfo = new NsdServiceInfo(name, type); |
| 1320 | final int foundNetId = info.netId; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1321 | if (foundNetId == 0L) { |
| 1322 | // Ignore services that do not have a Network: they are not usable |
| 1323 | // by apps, as they would need privileged permissions to use |
| 1324 | // interfaces that do not have an associated Network. |
| 1325 | break; |
| 1326 | } |
Remi NGUYEN VAN | 643edb6 | 2023-01-23 19:14:57 +0900 | [diff] [blame] | 1327 | if (foundNetId == INetd.DUMMY_NET_ID) { |
| 1328 | // Ignore services on the dummy0 interface: they are only seen when |
| 1329 | // discovering locally advertised services, and are not reachable |
| 1330 | // through that interface. |
| 1331 | break; |
| 1332 | } |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 1333 | setServiceNetworkForCallback(servInfo, info.netId, info.interfaceIdx); |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 1334 | |
| 1335 | clientInfo.onServiceFound(clientRequestId, servInfo, request); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1336 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1337 | } |
| 1338 | case IMDnsEventListener.SERVICE_LOST: { |
| 1339 | final DiscoveryInfo info = (DiscoveryInfo) obj; |
| 1340 | final String name = info.serviceName; |
| 1341 | final String type = info.registrationType; |
| 1342 | final int lostNetId = info.netId; |
| 1343 | servInfo = new NsdServiceInfo(name, type); |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 1344 | // The network could be set to null (netId 0) if it was torn down when the |
| 1345 | // service is lost |
| 1346 | // TODO: avoid returning null in that case, possibly by remembering |
| 1347 | // found services on the same interface index and their network at the time |
| 1348 | setServiceNetworkForCallback(servInfo, lostNetId, info.interfaceIdx); |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 1349 | clientInfo.onServiceLost(clientRequestId, servInfo, request); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1350 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1351 | } |
| 1352 | case IMDnsEventListener.SERVICE_DISCOVERY_FAILED: |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 1353 | clientInfo.onDiscoverServicesFailed(clientRequestId, |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1354 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */, |
| 1355 | transactionId, |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 1356 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1357 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1358 | case IMDnsEventListener.SERVICE_REGISTERED: { |
| 1359 | final RegistrationInfo info = (RegistrationInfo) obj; |
| 1360 | final String name = info.serviceName; |
| 1361 | servInfo = new NsdServiceInfo(name, null /* serviceType */); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1362 | clientInfo.onRegisterServiceSucceeded(clientRequestId, servInfo, request); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1363 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1364 | } |
| 1365 | case IMDnsEventListener.SERVICE_REGISTRATION_FAILED: |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 1366 | clientInfo.onRegisterServiceFailed(clientRequestId, |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1367 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */, |
| 1368 | transactionId, |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 1369 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1370 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1371 | case IMDnsEventListener.SERVICE_RESOLVED: { |
| 1372 | final ResolutionInfo info = (ResolutionInfo) obj; |
Sreeram Ramachandran | a53dd7f | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 1373 | int index = 0; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1374 | final String fullName = info.serviceFullName; |
| 1375 | while (index < fullName.length() && fullName.charAt(index) != '.') { |
| 1376 | if (fullName.charAt(index) == '\\') { |
Sreeram Ramachandran | a53dd7f | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 1377 | ++index; |
| 1378 | } |
| 1379 | ++index; |
| 1380 | } |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1381 | if (index >= fullName.length()) { |
| 1382 | Log.e(TAG, "Invalid service found " + fullName); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1383 | break; |
| 1384 | } |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1385 | |
paulhu | be18660 | 2022-04-12 07:18:23 +0000 | [diff] [blame] | 1386 | String name = unescape(fullName.substring(0, index)); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1387 | String rest = fullName.substring(index); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1388 | String type = rest.replace(".local.", ""); |
| 1389 | |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1390 | final NsdServiceInfo serviceInfo = clientInfo.mResolvedService; |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 1391 | serviceInfo.setServiceName(name); |
| 1392 | serviceInfo.setServiceType(type); |
| 1393 | serviceInfo.setPort(info.port); |
| 1394 | serviceInfo.setTxtRecords(info.txtRecord); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1395 | // Network will be added after SERVICE_GET_ADDR_SUCCESS |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1396 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1397 | stopResolveService(transactionId); |
| 1398 | removeRequestMap(clientRequestId, transactionId, clientInfo); |
Vinit Deshapnde | 4429e87 | 2013-11-12 15:36:37 -0800 | [diff] [blame] | 1399 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1400 | final int transactionId2 = getUniqueId(); |
| 1401 | if (getAddrInfo(transactionId2, info.hostname, info.interfaceIdx)) { |
| 1402 | storeLegacyRequestMap(clientRequestId, transactionId2, clientInfo, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1403 | NsdManager.RESOLVE_SERVICE, request.mStartTimeMs); |
Vinit Deshapnde | 4429e87 | 2013-11-12 15:36:37 -0800 | [diff] [blame] | 1404 | } else { |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1405 | clientInfo.onResolveServiceFailed(clientRequestId, |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1406 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */, |
| 1407 | transactionId, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1408 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1409 | clientInfo.mResolvedService = null; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1410 | } |
| 1411 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1412 | } |
| 1413 | case IMDnsEventListener.SERVICE_RESOLUTION_FAILED: |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1414 | /* NNN resolveId errorCode */ |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1415 | stopResolveService(transactionId); |
| 1416 | removeRequestMap(clientRequestId, transactionId, clientInfo); |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1417 | clientInfo.onResolveServiceFailed(clientRequestId, |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1418 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */, |
| 1419 | transactionId, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1420 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1421 | clientInfo.mResolvedService = null; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1422 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1423 | case IMDnsEventListener.SERVICE_GET_ADDR_FAILED: |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1424 | /* NNN resolveId errorCode */ |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1425 | stopGetAddrInfo(transactionId); |
| 1426 | removeRequestMap(clientRequestId, transactionId, clientInfo); |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1427 | clientInfo.onResolveServiceFailed(clientRequestId, |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1428 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */, |
| 1429 | transactionId, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1430 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1431 | clientInfo.mResolvedService = null; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1432 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1433 | case IMDnsEventListener.SERVICE_GET_ADDR_SUCCESS: { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1434 | /* NNN resolveId hostname ttl addr interfaceIdx netId */ |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1435 | final GetAddressInfo info = (GetAddressInfo) obj; |
| 1436 | final String address = info.address; |
| 1437 | final int netId = info.netId; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1438 | InetAddress serviceHost = null; |
| 1439 | try { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1440 | serviceHost = InetAddress.getByName(address); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1441 | } catch (UnknownHostException e) { |
| 1442 | Log.wtf(TAG, "Invalid host in GET_ADDR_SUCCESS", e); |
| 1443 | } |
| 1444 | |
| 1445 | // If the resolved service is on an interface without a network, consider it |
| 1446 | // as a failure: it would not be usable by apps as they would need |
| 1447 | // privileged permissions. |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1448 | if (netId != NETID_UNSET && serviceHost != null) { |
| 1449 | clientInfo.mResolvedService.setHost(serviceHost); |
| 1450 | setServiceNetworkForCallback(clientInfo.mResolvedService, |
| 1451 | netId, info.interfaceIdx); |
| 1452 | clientInfo.onResolveServiceSucceeded( |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1453 | clientRequestId, clientInfo.mResolvedService, request); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1454 | } else { |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1455 | clientInfo.onResolveServiceFailed(clientRequestId, |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1456 | NsdManager.FAILURE_INTERNAL_ERROR, true /* isLegacy */, |
| 1457 | transactionId, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1458 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1459 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1460 | stopGetAddrInfo(transactionId); |
| 1461 | removeRequestMap(clientRequestId, transactionId, clientInfo); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1462 | clientInfo.mResolvedService = null; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1463 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1464 | } |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1465 | default: |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 1466 | return false; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1467 | } |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 1468 | return true; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 1469 | } |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1470 | |
Remi NGUYEN VAN | 2f82fcd | 2023-05-10 13:24:53 +0900 | [diff] [blame] | 1471 | @Nullable |
| 1472 | private NsdServiceInfo buildNsdServiceInfoFromMdnsEvent( |
| 1473 | final MdnsEvent event, int code) { |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1474 | final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo; |
Remi NGUYEN VAN | 2f82fcd | 2023-05-10 13:24:53 +0900 | [diff] [blame] | 1475 | final String[] typeArray = serviceInfo.getServiceType(); |
| 1476 | final String joinedType; |
| 1477 | if (typeArray.length == 0 |
| 1478 | || !typeArray[typeArray.length - 1].equals(LOCAL_DOMAIN_NAME)) { |
| 1479 | Log.wtf(TAG, "MdnsServiceInfo type does not end in .local: " |
| 1480 | + Arrays.toString(typeArray)); |
| 1481 | return null; |
| 1482 | } else { |
| 1483 | joinedType = TextUtils.join(".", |
| 1484 | Arrays.copyOfRange(typeArray, 0, typeArray.length - 1)); |
| 1485 | } |
| 1486 | final String serviceType; |
| 1487 | switch (code) { |
| 1488 | case NsdManager.SERVICE_FOUND: |
| 1489 | case NsdManager.SERVICE_LOST: |
| 1490 | // For consistency with historical behavior, discovered service types have |
| 1491 | // a dot at the end. |
| 1492 | serviceType = joinedType + "."; |
| 1493 | break; |
| 1494 | case RESOLVE_SERVICE_SUCCEEDED: |
| 1495 | // For consistency with historical behavior, resolved service types have |
| 1496 | // a dot at the beginning. |
| 1497 | serviceType = "." + joinedType; |
| 1498 | break; |
| 1499 | default: |
| 1500 | serviceType = joinedType; |
| 1501 | break; |
| 1502 | } |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1503 | final String serviceName = serviceInfo.getServiceInstanceName(); |
| 1504 | final NsdServiceInfo servInfo = new NsdServiceInfo(serviceName, serviceType); |
| 1505 | final Network network = serviceInfo.getNetwork(); |
Yuyang Huang | 3bee9d4 | 2023-04-04 13:00:54 +0900 | [diff] [blame] | 1506 | // In MdnsDiscoveryManagerEvent, the Network can be null which means it is a |
| 1507 | // network for Tethering interface. In other words, the network == null means the |
| 1508 | // network has netId = INetd.LOCAL_NET_ID. |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1509 | setServiceNetworkForCallback( |
| 1510 | servInfo, |
Yuyang Huang | 3bee9d4 | 2023-04-04 13:00:54 +0900 | [diff] [blame] | 1511 | network == null ? INetd.LOCAL_NET_ID : network.netId, |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1512 | serviceInfo.getInterfaceIndex()); |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 1513 | servInfo.setSubtypes(dedupSubtypeLabels(serviceInfo.getSubtypes())); |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1514 | return servInfo; |
| 1515 | } |
| 1516 | |
| 1517 | private boolean handleMdnsDiscoveryManagerEvent( |
| 1518 | int transactionId, int code, Object obj) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1519 | final ClientInfo clientInfo = mTransactionIdToClientInfoMap.get(transactionId); |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1520 | if (clientInfo == null) { |
| 1521 | Log.e(TAG, String.format( |
| 1522 | "id %d for %d has no client mapping", transactionId, code)); |
| 1523 | return false; |
| 1524 | } |
| 1525 | |
| 1526 | final MdnsEvent event = (MdnsEvent) obj; |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1527 | final int clientRequestId = event.mClientRequestId; |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 1528 | final ClientRequest request = clientInfo.mClientRequests.get(clientRequestId); |
| 1529 | if (request == null) { |
| 1530 | Log.e(TAG, "Unknown client request. clientRequestId=" + clientRequestId); |
| 1531 | return false; |
| 1532 | } |
| 1533 | |
| 1534 | // Deal with the discovery sent callback |
| 1535 | if (code == DISCOVERY_QUERY_SENT_CALLBACK) { |
| 1536 | request.onQuerySent(); |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
| 1540 | // Deal with other callbacks. |
Remi NGUYEN VAN | 2f82fcd | 2023-05-10 13:24:53 +0900 | [diff] [blame] | 1541 | final NsdServiceInfo info = buildNsdServiceInfoFromMdnsEvent(event, code); |
| 1542 | // Errors are already logged if null |
| 1543 | if (info == null) return false; |
Paul Hu | 83ec7f4 | 2023-06-07 18:04:09 +0800 | [diff] [blame] | 1544 | mServiceLogs.log(String.format( |
| 1545 | "MdnsDiscoveryManager event code=%s transactionId=%d", |
| 1546 | NsdManager.nameOf(code), transactionId)); |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1547 | switch (code) { |
| 1548 | case NsdManager.SERVICE_FOUND: |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 1549 | clientInfo.onServiceFound(clientRequestId, info, request); |
Paul Hu | 319751a | 2023-01-13 23:56:34 +0800 | [diff] [blame] | 1550 | break; |
| 1551 | case NsdManager.SERVICE_LOST: |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 1552 | clientInfo.onServiceLost(clientRequestId, info, request); |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1553 | break; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1554 | case NsdManager.RESOLVE_SERVICE_SUCCEEDED: { |
| 1555 | final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1556 | info.setPort(serviceInfo.getPort()); |
| 1557 | |
| 1558 | Map<String, String> attrs = serviceInfo.getAttributes(); |
| 1559 | for (Map.Entry<String, String> kv : attrs.entrySet()) { |
| 1560 | final String key = kv.getKey(); |
| 1561 | try { |
| 1562 | info.setAttribute(key, serviceInfo.getAttributeAsBytes(key)); |
| 1563 | } catch (IllegalArgumentException e) { |
| 1564 | Log.e(TAG, "Invalid attribute", e); |
| 1565 | } |
| 1566 | } |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 1567 | info.setHostname(getHostname(serviceInfo)); |
Yuyang Huang | aa0e960 | 2023-03-17 12:43:09 +0900 | [diff] [blame] | 1568 | final List<InetAddress> addresses = getInetAddresses(serviceInfo); |
Paul Hu | 2b86591 | 2023-03-06 14:27:53 +0800 | [diff] [blame] | 1569 | if (addresses.size() != 0) { |
| 1570 | info.setHostAddresses(addresses); |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1571 | request.setServiceFromCache(event.mIsServiceFromCache); |
| 1572 | clientInfo.onResolveServiceSucceeded(clientRequestId, info, request); |
Paul Hu | 2b86591 | 2023-03-06 14:27:53 +0800 | [diff] [blame] | 1573 | } else { |
| 1574 | // No address. Notify resolution failure. |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1575 | clientInfo.onResolveServiceFailed(clientRequestId, |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1576 | NsdManager.FAILURE_INTERNAL_ERROR, false /* isLegacy */, |
| 1577 | transactionId, |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 1578 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | // Unregister the listener immediately like IMDnsEventListener design |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 1582 | if (!(request instanceof DiscoveryManagerRequest)) { |
| 1583 | Log.wtf(TAG, "non-DiscoveryManager request in DiscoveryManager event"); |
| 1584 | break; |
| 1585 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 1586 | stopDiscoveryManagerRequest( |
| 1587 | request, clientRequestId, transactionId, clientInfo); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame] | 1588 | break; |
| 1589 | } |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1590 | case NsdManager.SERVICE_UPDATED: { |
| 1591 | final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo; |
| 1592 | info.setPort(serviceInfo.getPort()); |
| 1593 | |
| 1594 | Map<String, String> attrs = serviceInfo.getAttributes(); |
| 1595 | for (Map.Entry<String, String> kv : attrs.entrySet()) { |
| 1596 | final String key = kv.getKey(); |
| 1597 | try { |
| 1598 | info.setAttribute(key, serviceInfo.getAttributeAsBytes(key)); |
| 1599 | } catch (IllegalArgumentException e) { |
| 1600 | Log.e(TAG, "Invalid attribute", e); |
| 1601 | } |
| 1602 | } |
| 1603 | |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 1604 | info.setHostname(getHostname(serviceInfo)); |
Yuyang Huang | aa0e960 | 2023-03-17 12:43:09 +0900 | [diff] [blame] | 1605 | final List<InetAddress> addresses = getInetAddresses(serviceInfo); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1606 | info.setHostAddresses(addresses); |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 1607 | clientInfo.onServiceUpdated(clientRequestId, info, request); |
| 1608 | // Set the ServiceFromCache flag only if the service is actually being |
| 1609 | // retrieved from the cache. This flag should not be overridden by later |
| 1610 | // service updates, which may not be cached. |
| 1611 | if (event.mIsServiceFromCache) { |
| 1612 | request.setServiceFromCache(true); |
| 1613 | } |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1614 | break; |
| 1615 | } |
| 1616 | case NsdManager.SERVICE_UPDATED_LOST: |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 1617 | clientInfo.onServiceUpdatedLost(clientRequestId, request); |
Paul Hu | 30bd70d | 2023-02-07 13:20:56 +0000 | [diff] [blame] | 1618 | break; |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 1619 | default: |
| 1620 | return false; |
| 1621 | } |
| 1622 | return true; |
| 1623 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1624 | } |
| 1625 | } |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1626 | |
Yuyang Huang | aa0e960 | 2023-03-17 12:43:09 +0900 | [diff] [blame] | 1627 | @NonNull |
| 1628 | private static List<InetAddress> getInetAddresses(@NonNull MdnsServiceInfo serviceInfo) { |
| 1629 | final List<String> v4Addrs = serviceInfo.getIpv4Addresses(); |
| 1630 | final List<String> v6Addrs = serviceInfo.getIpv6Addresses(); |
| 1631 | final List<InetAddress> addresses = new ArrayList<>(v4Addrs.size() + v6Addrs.size()); |
| 1632 | for (String ipv4Address : v4Addrs) { |
| 1633 | try { |
| 1634 | addresses.add(InetAddresses.parseNumericAddress(ipv4Address)); |
| 1635 | } catch (IllegalArgumentException e) { |
| 1636 | Log.wtf(TAG, "Invalid ipv4 address", e); |
| 1637 | } |
| 1638 | } |
| 1639 | for (String ipv6Address : v6Addrs) { |
| 1640 | try { |
Yuyang Huang | a6a6ff9 | 2023-04-24 13:33:34 +0900 | [diff] [blame] | 1641 | final Inet6Address addr = (Inet6Address) InetAddresses.parseNumericAddress( |
| 1642 | ipv6Address); |
| 1643 | addresses.add(InetAddressUtils.withScopeId(addr, serviceInfo.getInterfaceIndex())); |
| 1644 | } catch (IllegalArgumentException e) { |
Yuyang Huang | aa0e960 | 2023-03-17 12:43:09 +0900 | [diff] [blame] | 1645 | Log.wtf(TAG, "Invalid ipv6 address", e); |
| 1646 | } |
| 1647 | } |
| 1648 | return addresses; |
| 1649 | } |
| 1650 | |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 1651 | @NonNull |
| 1652 | private static String getHostname(@NonNull MdnsServiceInfo serviceInfo) { |
| 1653 | String[] hostname = serviceInfo.getHostName(); |
| 1654 | // Strip the "local" top-level domain. |
| 1655 | if (hostname.length >= 2 && hostname[hostname.length - 1].equals("local")) { |
| 1656 | hostname = Arrays.copyOf(hostname, hostname.length - 1); |
| 1657 | } |
| 1658 | return String.join(".", hostname); |
| 1659 | } |
| 1660 | |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 1661 | private static void setServiceNetworkForCallback(NsdServiceInfo info, int netId, int ifaceIdx) { |
| 1662 | switch (netId) { |
| 1663 | case NETID_UNSET: |
| 1664 | info.setNetwork(null); |
| 1665 | break; |
| 1666 | case INetd.LOCAL_NET_ID: |
| 1667 | // Special case for LOCAL_NET_ID: Networks on netId 99 are not generally |
| 1668 | // visible / usable for apps, so do not return it. Store the interface |
| 1669 | // index instead, so at least if the client tries to resolve the service |
| 1670 | // with that NsdServiceInfo, it will be done on the same interface. |
| 1671 | // If they recreate the NsdServiceInfo themselves, resolution would be |
| 1672 | // done on all interfaces as before T, which should also work. |
| 1673 | info.setNetwork(null); |
| 1674 | info.setInterfaceIndex(ifaceIdx); |
| 1675 | break; |
| 1676 | default: |
| 1677 | info.setNetwork(new Network(netId)); |
| 1678 | } |
| 1679 | } |
| 1680 | |
paulhu | be18660 | 2022-04-12 07:18:23 +0000 | [diff] [blame] | 1681 | // The full service name is escaped from standard DNS rules on mdnsresponder, making it suitable |
| 1682 | // for passing to standard system DNS APIs such as res_query() . Thus, make the service name |
| 1683 | // unescape for getting right service address. See "Notes on DNS Name Escaping" on |
| 1684 | // external/mdnsresponder/mDNSShared/dns_sd.h for more details. |
| 1685 | private String unescape(String s) { |
| 1686 | StringBuilder sb = new StringBuilder(s.length()); |
| 1687 | for (int i = 0; i < s.length(); ++i) { |
| 1688 | char c = s.charAt(i); |
| 1689 | if (c == '\\') { |
| 1690 | if (++i >= s.length()) { |
| 1691 | Log.e(TAG, "Unexpected end of escape sequence in: " + s); |
| 1692 | break; |
| 1693 | } |
| 1694 | c = s.charAt(i); |
| 1695 | if (c != '.' && c != '\\') { |
| 1696 | if (i + 2 >= s.length()) { |
| 1697 | Log.e(TAG, "Unexpected end of escape sequence in: " + s); |
| 1698 | break; |
| 1699 | } |
| 1700 | c = (char) ((c - '0') * 100 + (s.charAt(i + 1) - '0') * 10 |
| 1701 | + (s.charAt(i + 2) - '0')); |
| 1702 | i += 2; |
| 1703 | } |
| 1704 | } |
| 1705 | sb.append(c); |
| 1706 | } |
| 1707 | return sb.toString(); |
| 1708 | } |
| 1709 | |
Paul Hu | 7445e3d | 2023-03-03 15:14:00 +0800 | [diff] [blame] | 1710 | /** |
| 1711 | * Check the given service type is valid and construct it to a service type |
| 1712 | * which can use for discovery / resolution service. |
| 1713 | * |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 1714 | * <p>The valid service type should be 2 labels, or 3 labels if the query is for a |
Paul Hu | 7445e3d | 2023-03-03 15:14:00 +0800 | [diff] [blame] | 1715 | * subtype (see RFC6763 7.1). Each label is up to 63 characters and must start with an |
| 1716 | * underscore; they are alphanumerical characters or dashes or underscore, except the |
| 1717 | * last one that is just alphanumerical. The last label must be _tcp or _udp. |
| 1718 | * |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 1719 | * <p>The subtypes may also be specified with a comma after the service type, for example |
| 1720 | * _type._tcp,_subtype1,_subtype2 |
Remi NGUYEN VAN | f2d0641 | 2023-05-11 19:18:44 +0900 | [diff] [blame] | 1721 | * |
Paul Hu | 7445e3d | 2023-03-03 15:14:00 +0800 | [diff] [blame] | 1722 | * @param serviceType the request service type for discovery / resolution service |
| 1723 | * @return constructed service type or null if the given service type is invalid. |
| 1724 | */ |
| 1725 | @Nullable |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 1726 | public static Pair<String, List<String>> parseTypeAndSubtype(String serviceType) { |
Paul Hu | 7445e3d | 2023-03-03 15:14:00 +0800 | [diff] [blame] | 1727 | if (TextUtils.isEmpty(serviceType)) return null; |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 1728 | final Pattern serviceTypePattern = Pattern.compile(TYPE_REGEX); |
Paul Hu | 7445e3d | 2023-03-03 15:14:00 +0800 | [diff] [blame] | 1729 | final Matcher matcher = serviceTypePattern.matcher(serviceType); |
| 1730 | if (!matcher.matches()) return null; |
Yuyang Huang | 170d42f | 2023-12-09 15:26:16 +0900 | [diff] [blame] | 1731 | final String queryType = matcher.group(2); |
| 1732 | // Use the subtype at the beginning |
| 1733 | if (matcher.group(1) != null) { |
| 1734 | return new Pair<>(queryType, List.of(matcher.group(1))); |
| 1735 | } |
| 1736 | // Use the subtypes at the end |
| 1737 | final String subTypesStr = matcher.group(3); |
| 1738 | if (subTypesStr != null && !subTypesStr.isEmpty()) { |
| 1739 | final String[] subTypes = subTypesStr.substring(1).split(","); |
| 1740 | return new Pair<>(queryType, List.of(subTypes)); |
| 1741 | } |
| 1742 | |
| 1743 | return new Pair<>(queryType, Collections.emptyList()); |
Paul Hu | 7445e3d | 2023-03-03 15:14:00 +0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 1746 | /** |
| 1747 | * Checks if the hostname is valid. |
| 1748 | * |
| 1749 | * <p>For now NsdService only allows single-label hostnames conforming to RFC 1035. In other |
| 1750 | * words, the hostname should be at most 63 characters long and it only contains letters, digits |
| 1751 | * and hyphens. |
| 1752 | */ |
| 1753 | public static boolean checkHostname(@Nullable String hostname) { |
| 1754 | if (hostname == null) { |
| 1755 | return true; |
| 1756 | } |
| 1757 | String HOSTNAME_REGEX = "^[a-zA-Z]([a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?$"; |
| 1758 | return Pattern.compile(HOSTNAME_REGEX).matcher(hostname).matches(); |
| 1759 | } |
| 1760 | |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 1761 | /** Returns {@code true} if {@code subtype} is a valid DNS-SD subtype label. */ |
| 1762 | private static boolean checkSubtypeLabel(String subtype) { |
Remi NGUYEN VAN | 629234c | 2024-02-08 18:27:45 +0900 | [diff] [blame^] | 1763 | return Pattern.compile("^" + SUBTYPE_LABEL_REGEX + "$").matcher(subtype).matches(); |
Kangping Dong | 5af24b6 | 2023-12-10 21:41:16 +0800 | [diff] [blame] | 1764 | } |
| 1765 | |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1766 | @VisibleForTesting |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1767 | NsdService(Context ctx, Handler handler, long cleanupDelayMs) { |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1768 | this(ctx, handler, cleanupDelayMs, new Dependencies()); |
| 1769 | } |
| 1770 | |
| 1771 | @VisibleForTesting |
| 1772 | NsdService(Context ctx, Handler handler, long cleanupDelayMs, Dependencies deps) { |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 1773 | mCleanupDelayMs = cleanupDelayMs; |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1774 | mContext = ctx; |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1775 | mNsdStateMachine = new NsdStateMachine(TAG, handler); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1776 | mNsdStateMachine.start(); |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 1777 | // It can fail on V+ device since mdns native service provided by netd is removed. |
| 1778 | mMDnsManager = SdkLevel.isAtLeastV() ? null : ctx.getSystemService(MDnsManager.class); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1779 | mMDnsEventCallback = new MDnsEventCallback(mNsdStateMachine); |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 1780 | mDeps = deps; |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1781 | |
Paul Hu | 14667de | 2023-04-17 22:42:47 +0800 | [diff] [blame] | 1782 | mMdnsSocketProvider = deps.makeMdnsSocketProvider(ctx, handler.getLooper(), |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 1783 | LOGGER.forSubComponent("MdnsSocketProvider"), new SocketRequestMonitor()); |
Yuyang Huang | 700778b | 2023-03-08 16:17:05 +0900 | [diff] [blame] | 1784 | // Netlink monitor starts on boot, and intentionally never stopped, to ensure that all |
Yuyang Huang | fca402a | 2023-05-24 14:45:59 +0900 | [diff] [blame] | 1785 | // address events are received. When the netlink monitor starts, any IP addresses already |
| 1786 | // on the interfaces will not be seen. In practice, the network will not connect at boot |
| 1787 | // time As a result, all the netlink message should be observed if the netlink monitor |
| 1788 | // starts here. |
Yuyang Huang | 700778b | 2023-03-08 16:17:05 +0900 | [diff] [blame] | 1789 | handler.post(mMdnsSocketProvider::startNetLinkMonitor); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 1790 | |
| 1791 | // NsdService is started after ActivityManager (startOtherServices in SystemServer, vs. |
| 1792 | // startBootstrapServices). |
| 1793 | mRunningAppActiveImportanceCutoff = mDeps.getDeviceConfigInt( |
| 1794 | MDNS_CONFIG_RUNNING_APP_ACTIVE_IMPORTANCE_CUTOFF, |
| 1795 | DEFAULT_RUNNING_APP_ACTIVE_IMPORTANCE_CUTOFF); |
| 1796 | final ActivityManager am = ctx.getSystemService(ActivityManager.class); |
| 1797 | am.addOnUidImportanceListener(new UidImportanceListener(handler), |
| 1798 | mRunningAppActiveImportanceCutoff); |
| 1799 | |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 1800 | mMdnsFeatureFlags = new MdnsFeatureFlags.Builder() |
Paul Hu | f3fe333 | 2023-10-16 17:13:25 +0800 | [diff] [blame] | 1801 | .setIsMdnsOffloadFeatureEnabled(mDeps.isTetheringFeatureNotChickenedOut( |
| 1802 | mContext, MdnsFeatureFlags.NSD_FORCE_DISABLE_MDNS_OFFLOAD)) |
| 1803 | .setIncludeInetAddressRecordsInProbing(mDeps.isFeatureEnabled( |
| 1804 | mContext, MdnsFeatureFlags.INCLUDE_INET_ADDRESS_RECORDS_IN_PROBING)) |
Paul Hu | 596a500 | 2023-10-18 17:07:31 +0800 | [diff] [blame] | 1805 | .setIsExpiredServicesRemovalEnabled(mDeps.isFeatureEnabled( |
| 1806 | mContext, MdnsFeatureFlags.NSD_EXPIRED_SERVICES_REMOVAL)) |
Paul Hu | fd357ef | 2023-11-01 16:32:45 +0800 | [diff] [blame] | 1807 | .setIsLabelCountLimitEnabled(mDeps.isTetheringFeatureNotChickenedOut( |
| 1808 | mContext, MdnsFeatureFlags.NSD_LIMIT_LABEL_COUNT)) |
Paul Hu | 01f243f | 2023-11-22 17:26:36 +0800 | [diff] [blame] | 1809 | .setIsKnownAnswerSuppressionEnabled(mDeps.isFeatureEnabled( |
| 1810 | mContext, MdnsFeatureFlags.NSD_KNOWN_ANSWER_SUPPRESSION)) |
Remi NGUYEN VAN | 0ca094b | 2023-09-13 16:27:12 +0900 | [diff] [blame] | 1811 | .setIsUnicastReplyEnabled(mDeps.isFeatureEnabled( |
| 1812 | mContext, MdnsFeatureFlags.NSD_UNICAST_REPLY_ENABLED)) |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 1813 | .setIsAggressiveQueryModeEnabled(mDeps.isFeatureEnabled( |
| 1814 | mContext, MdnsFeatureFlags.NSD_AGGRESSIVE_QUERY_MODE)) |
Remi NGUYEN VAN | 0ca094b | 2023-09-13 16:27:12 +0900 | [diff] [blame] | 1815 | .setOverrideProvider(flag -> mDeps.isFeatureEnabled( |
| 1816 | mContext, FORCE_ENABLE_FLAG_FOR_TEST_PREFIX + flag)) |
Paul Hu | f3fe333 | 2023-10-16 17:13:25 +0800 | [diff] [blame] | 1817 | .build(); |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 1818 | mMdnsSocketClient = |
Yuyang Huang | 7ddf293 | 2023-08-01 18:16:30 +0900 | [diff] [blame] | 1819 | new MdnsMultinetworkSocketClient(handler.getLooper(), mMdnsSocketProvider, |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 1820 | LOGGER.forSubComponent("MdnsMultinetworkSocketClient"), mMdnsFeatureFlags); |
Paul Hu | 14667de | 2023-04-17 22:42:47 +0800 | [diff] [blame] | 1821 | mMdnsDiscoveryManager = deps.makeMdnsDiscoveryManager(new ExecutorProvider(), |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 1822 | mMdnsSocketClient, LOGGER.forSubComponent("MdnsDiscoveryManager"), |
| 1823 | mMdnsFeatureFlags); |
Remi NGUYEN VAN | a8a777b | 2023-01-18 18:57:41 +0900 | [diff] [blame] | 1824 | handler.post(() -> mMdnsSocketClient.setCallback(mMdnsDiscoveryManager)); |
| 1825 | mAdvertiser = deps.makeMdnsAdvertiser(handler.getLooper(), mMdnsSocketProvider, |
Paul Hu | 11a883d | 2023-12-14 07:36:44 +0000 | [diff] [blame] | 1826 | new AdvertiserCallback(), LOGGER.forSubComponent("MdnsAdvertiser"), |
| 1827 | mMdnsFeatureFlags, mContext); |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 1828 | mClock = deps.makeClock(); |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | /** |
| 1832 | * Dependencies of NsdService, for injection in tests. |
| 1833 | */ |
| 1834 | @VisibleForTesting |
| 1835 | public static class Dependencies { |
| 1836 | /** |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1837 | * Check whether the MdnsDiscoveryManager feature is enabled. |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1838 | * |
| 1839 | * @param context The global context information about an app environment. |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1840 | * @return true if the MdnsDiscoveryManager feature is enabled. |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1841 | */ |
| 1842 | public boolean isMdnsDiscoveryManagerEnabled(Context context) { |
Motomu Utsumi | 624aeb4 | 2023-08-15 15:52:27 +0900 | [diff] [blame] | 1843 | return isAtLeastU() || DeviceConfigUtils.isTetheringFeatureEnabled(context, |
Motomu Utsumi | 3e0be39 | 2023-08-15 16:32:44 +0900 | [diff] [blame] | 1844 | MDNS_DISCOVERY_MANAGER_VERSION); |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | /** |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1848 | * Check whether the MdnsAdvertiser feature is enabled. |
| 1849 | * |
| 1850 | * @param context The global context information about an app environment. |
| 1851 | * @return true if the MdnsAdvertiser feature is enabled. |
| 1852 | */ |
| 1853 | public boolean isMdnsAdvertiserEnabled(Context context) { |
Motomu Utsumi | 624aeb4 | 2023-08-15 15:52:27 +0900 | [diff] [blame] | 1854 | return isAtLeastU() || DeviceConfigUtils.isTetheringFeatureEnabled(context, |
Motomu Utsumi | 3e0be39 | 2023-08-15 16:32:44 +0900 | [diff] [blame] | 1855 | MDNS_ADVERTISER_VERSION); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | /** |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 1859 | * Get the type allowlist flag value. |
| 1860 | * @see #MDNS_TYPE_ALLOWLIST_FLAGS |
| 1861 | */ |
| 1862 | @Nullable |
| 1863 | public String getTypeAllowlistFlags() { |
| 1864 | return DeviceConfigUtils.getDeviceConfigProperty(NAMESPACE_TETHERING, |
| 1865 | MDNS_TYPE_ALLOWLIST_FLAGS, null); |
| 1866 | } |
| 1867 | |
| 1868 | /** |
Motomu Utsumi | 624aeb4 | 2023-08-15 15:52:27 +0900 | [diff] [blame] | 1869 | * @see DeviceConfigUtils#isTetheringFeatureEnabled |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 1870 | */ |
| 1871 | public boolean isFeatureEnabled(Context context, String feature) { |
Motomu Utsumi | 3e0be39 | 2023-08-15 16:32:44 +0900 | [diff] [blame] | 1872 | return DeviceConfigUtils.isTetheringFeatureEnabled(context, feature); |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | /** |
Yuyang Huang | b96a071 | 2023-09-07 15:13:15 +0900 | [diff] [blame] | 1876 | * @see DeviceConfigUtils#isTetheringFeatureNotChickenedOut |
| 1877 | */ |
Motomu Utsumi | ed4e7ec | 2023-09-13 14:58:32 +0900 | [diff] [blame] | 1878 | public boolean isTetheringFeatureNotChickenedOut(Context context, String feature) { |
| 1879 | return DeviceConfigUtils.isTetheringFeatureNotChickenedOut(context, feature); |
Yuyang Huang | b96a071 | 2023-09-07 15:13:15 +0900 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | /** |
Paul Hu | f3fe333 | 2023-10-16 17:13:25 +0800 | [diff] [blame] | 1883 | * @see DeviceConfigUtils#isTrunkStableFeatureEnabled |
| 1884 | */ |
| 1885 | public boolean isTrunkStableFeatureEnabled(String feature) { |
| 1886 | return DeviceConfigUtils.isTrunkStableFeatureEnabled(feature); |
| 1887 | } |
| 1888 | |
| 1889 | /** |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1890 | * @see MdnsDiscoveryManager |
| 1891 | */ |
| 1892 | public MdnsDiscoveryManager makeMdnsDiscoveryManager( |
Paul Hu | 14667de | 2023-04-17 22:42:47 +0800 | [diff] [blame] | 1893 | @NonNull ExecutorProvider executorProvider, |
Paul Hu | f3fe333 | 2023-10-16 17:13:25 +0800 | [diff] [blame] | 1894 | @NonNull MdnsMultinetworkSocketClient socketClient, @NonNull SharedLog sharedLog, |
| 1895 | @NonNull MdnsFeatureFlags featureFlags) { |
| 1896 | return new MdnsDiscoveryManager( |
| 1897 | executorProvider, socketClient, sharedLog, featureFlags); |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | /** |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1901 | * @see MdnsAdvertiser |
| 1902 | */ |
| 1903 | public MdnsAdvertiser makeMdnsAdvertiser( |
| 1904 | @NonNull Looper looper, @NonNull MdnsSocketProvider socketProvider, |
Yuyang Huang | b96a071 | 2023-09-07 15:13:15 +0900 | [diff] [blame] | 1905 | @NonNull MdnsAdvertiser.AdvertiserCallback cb, @NonNull SharedLog sharedLog, |
Remi NGUYEN VAN | 5c9d6cb | 2024-01-23 17:14:55 +0900 | [diff] [blame] | 1906 | MdnsFeatureFlags featureFlags, Context context) { |
| 1907 | return new MdnsAdvertiser(looper, socketProvider, cb, sharedLog, featureFlags, context); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /** |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1911 | * @see MdnsSocketProvider |
| 1912 | */ |
Paul Hu | 14667de | 2023-04-17 22:42:47 +0800 | [diff] [blame] | 1913 | public MdnsSocketProvider makeMdnsSocketProvider(@NonNull Context context, |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 1914 | @NonNull Looper looper, @NonNull SharedLog sharedLog, |
| 1915 | @NonNull MdnsSocketProvider.SocketRequestMonitor socketCreationCallback) { |
| 1916 | return new MdnsSocketProvider(context, looper, sharedLog, socketCreationCallback); |
| 1917 | } |
| 1918 | |
| 1919 | /** |
| 1920 | * @see DeviceConfig#getInt(String, String, int) |
| 1921 | */ |
| 1922 | public int getDeviceConfigInt(@NonNull String config, int defaultValue) { |
| 1923 | return DeviceConfig.getInt(NAMESPACE_TETHERING, config, defaultValue); |
| 1924 | } |
| 1925 | |
| 1926 | /** |
| 1927 | * @see Binder#getCallingUid() |
| 1928 | */ |
| 1929 | public int getCallingUid() { |
| 1930 | return Binder.getCallingUid(); |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1931 | } |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 1932 | |
| 1933 | /** |
| 1934 | * @see NetworkNsdReportedMetrics |
| 1935 | */ |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 1936 | public NetworkNsdReportedMetrics makeNetworkNsdReportedMetrics(int clientId) { |
| 1937 | return new NetworkNsdReportedMetrics(clientId); |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | /** |
| 1941 | * @see MdnsUtils.Clock |
| 1942 | */ |
| 1943 | public Clock makeClock() { |
| 1944 | return new Clock(); |
| 1945 | } |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1946 | } |
| 1947 | |
Remi NGUYEN VAN | 151d0a5 | 2023-03-03 17:50:50 +0900 | [diff] [blame] | 1948 | /** |
| 1949 | * Return whether a type is allowlisted to use the Java backend. |
| 1950 | * @param type The service type |
| 1951 | * @param flagPrefix One of {@link #MDNS_ADVERTISER_ALLOWLIST_FLAG_PREFIX} or |
| 1952 | * {@link #MDNS_DISCOVERY_MANAGER_ALLOWLIST_FLAG_PREFIX}. |
| 1953 | */ |
| 1954 | private boolean isTypeAllowlistedForJavaBackend(@Nullable String type, |
| 1955 | @NonNull String flagPrefix) { |
| 1956 | if (type == null) return false; |
| 1957 | final String typesConfig = mDeps.getTypeAllowlistFlags(); |
| 1958 | if (TextUtils.isEmpty(typesConfig)) return false; |
| 1959 | |
| 1960 | final String mappingPrefix = type + ":"; |
| 1961 | String mappedFlag = null; |
| 1962 | for (String mapping : TextUtils.split(typesConfig, ",")) { |
| 1963 | if (mapping.startsWith(mappingPrefix)) { |
| 1964 | mappedFlag = mapping.substring(mappingPrefix.length()); |
| 1965 | break; |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | if (mappedFlag == null) return false; |
| 1970 | |
| 1971 | return mDeps.isFeatureEnabled(mContext, |
| 1972 | flagPrefix + mappedFlag + MDNS_ALLOWLIST_FLAG_SUFFIX); |
| 1973 | } |
| 1974 | |
| 1975 | private boolean useDiscoveryManagerForType(@Nullable String type) { |
| 1976 | return isTypeAllowlistedForJavaBackend(type, MDNS_DISCOVERY_MANAGER_ALLOWLIST_FLAG_PREFIX); |
| 1977 | } |
| 1978 | |
| 1979 | private boolean useAdvertiserForType(@Nullable String type) { |
| 1980 | return isTypeAllowlistedForJavaBackend(type, MDNS_ADVERTISER_ALLOWLIST_FLAG_PREFIX); |
| 1981 | } |
| 1982 | |
paulhu | 1b35e82 | 2022-04-08 14:48:41 +0800 | [diff] [blame] | 1983 | public static NsdService create(Context context) { |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1984 | HandlerThread thread = new HandlerThread(TAG); |
| 1985 | thread.start(); |
| 1986 | Handler handler = new Handler(thread.getLooper()); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1987 | NsdService service = new NsdService(context, handler, CLEANUP_DELAY_MS); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1988 | return service; |
| 1989 | } |
| 1990 | |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1991 | private static class MDnsEventCallback extends IMDnsEventListener.Stub { |
| 1992 | private final StateMachine mStateMachine; |
| 1993 | |
| 1994 | MDnsEventCallback(StateMachine sm) { |
| 1995 | mStateMachine = sm; |
| 1996 | } |
| 1997 | |
| 1998 | @Override |
| 1999 | public void onServiceRegistrationStatus(final RegistrationInfo status) { |
| 2000 | mStateMachine.sendMessage( |
| 2001 | MDNS_SERVICE_EVENT, status.result, status.id, status); |
| 2002 | } |
| 2003 | |
| 2004 | @Override |
| 2005 | public void onServiceDiscoveryStatus(final DiscoveryInfo status) { |
| 2006 | mStateMachine.sendMessage( |
| 2007 | MDNS_SERVICE_EVENT, status.result, status.id, status); |
| 2008 | } |
| 2009 | |
| 2010 | @Override |
| 2011 | public void onServiceResolutionStatus(final ResolutionInfo status) { |
| 2012 | mStateMachine.sendMessage( |
| 2013 | MDNS_SERVICE_EVENT, status.result, status.id, status); |
| 2014 | } |
| 2015 | |
| 2016 | @Override |
| 2017 | public void onGettingServiceAddressStatus(final GetAddressInfo status) { |
| 2018 | mStateMachine.sendMessage( |
| 2019 | MDNS_SERVICE_EVENT, status.result, status.id, status); |
| 2020 | } |
| 2021 | |
| 2022 | @Override |
| 2023 | public int getInterfaceVersion() throws RemoteException { |
| 2024 | return this.VERSION; |
| 2025 | } |
| 2026 | |
| 2027 | @Override |
| 2028 | public String getInterfaceHash() throws RemoteException { |
| 2029 | return this.HASH; |
| 2030 | } |
| 2031 | } |
| 2032 | |
Yuyang Huang | c275a9e | 2023-08-25 18:03:22 +0900 | [diff] [blame] | 2033 | private void sendAllOffloadServiceInfos(@NonNull OffloadEngineInfo offloadEngineInfo) { |
| 2034 | final String targetInterface = offloadEngineInfo.mInterfaceName; |
| 2035 | final IOffloadEngine offloadEngine = offloadEngineInfo.mOffloadEngine; |
| 2036 | final List<MdnsAdvertiser.OffloadServiceInfoWrapper> offloadWrappers = |
| 2037 | mAdvertiser.getAllInterfaceOffloadServiceInfos(targetInterface); |
| 2038 | for (MdnsAdvertiser.OffloadServiceInfoWrapper wrapper : offloadWrappers) { |
| 2039 | try { |
| 2040 | offloadEngine.onOffloadServiceUpdated(wrapper.mOffloadServiceInfo); |
| 2041 | } catch (RemoteException e) { |
| 2042 | // Can happen in regular cases, do not log a stacktrace |
| 2043 | Log.i(TAG, "Failed to send offload callback, remote died: " + e.getMessage()); |
| 2044 | } |
| 2045 | } |
| 2046 | } |
| 2047 | |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2048 | private void sendOffloadServiceInfosUpdate(@NonNull String targetInterfaceName, |
| 2049 | @NonNull OffloadServiceInfo offloadServiceInfo, boolean isRemove) { |
| 2050 | final int count = mOffloadEngines.beginBroadcast(); |
| 2051 | try { |
| 2052 | for (int i = 0; i < count; i++) { |
| 2053 | final OffloadEngineInfo offloadEngineInfo = |
| 2054 | (OffloadEngineInfo) mOffloadEngines.getBroadcastCookie(i); |
| 2055 | final String interfaceName = offloadEngineInfo.mInterfaceName; |
| 2056 | if (!targetInterfaceName.equals(interfaceName) |
| 2057 | || ((offloadEngineInfo.mOffloadType |
| 2058 | & offloadServiceInfo.getOffloadType()) == 0)) { |
| 2059 | continue; |
| 2060 | } |
| 2061 | try { |
| 2062 | if (isRemove) { |
| 2063 | mOffloadEngines.getBroadcastItem(i).onOffloadServiceRemoved( |
| 2064 | offloadServiceInfo); |
| 2065 | } else { |
| 2066 | mOffloadEngines.getBroadcastItem(i).onOffloadServiceUpdated( |
| 2067 | offloadServiceInfo); |
| 2068 | } |
| 2069 | } catch (RemoteException e) { |
| 2070 | // Can happen in regular cases, do not log a stacktrace |
Yuyang Huang | c275a9e | 2023-08-25 18:03:22 +0900 | [diff] [blame] | 2071 | Log.i(TAG, "Failed to send offload callback, remote died: " + e.getMessage()); |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2072 | } |
| 2073 | } |
| 2074 | } finally { |
| 2075 | mOffloadEngines.finishBroadcast(); |
| 2076 | } |
| 2077 | } |
| 2078 | |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2079 | private class AdvertiserCallback implements MdnsAdvertiser.AdvertiserCallback { |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2080 | // TODO: add a callback to notify when a service is being added on each interface (as soon |
| 2081 | // as probing starts), and call mOffloadCallbacks. This callback is for |
| 2082 | // OFFLOAD_CAPABILITY_FILTER_REPLIES offload type. |
| 2083 | |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2084 | @Override |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2085 | public void onRegisterServiceSucceeded(int transactionId, NsdServiceInfo registeredInfo) { |
| 2086 | mServiceLogs.log("onRegisterServiceSucceeded: transactionId " + transactionId); |
| 2087 | final ClientInfo clientInfo = getClientInfoOrLog(transactionId); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2088 | if (clientInfo == null) return; |
| 2089 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2090 | final int clientRequestId = getClientRequestIdOrLog(clientInfo, transactionId); |
| 2091 | if (clientRequestId < 0) return; |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2092 | |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 2093 | // onRegisterServiceSucceeded only has the service name and hostname in its info. This |
| 2094 | // aligns with historical behavior. |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2095 | final NsdServiceInfo cbInfo = new NsdServiceInfo(registeredInfo.getServiceName(), null); |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 2096 | cbInfo.setHostname(registeredInfo.getHostname()); |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 2097 | final ClientRequest request = clientInfo.mClientRequests.get(clientRequestId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2098 | clientInfo.onRegisterServiceSucceeded(clientRequestId, cbInfo, request); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | @Override |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2102 | public void onRegisterServiceFailed(int transactionId, int errorCode) { |
| 2103 | final ClientInfo clientInfo = getClientInfoOrLog(transactionId); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2104 | if (clientInfo == null) return; |
| 2105 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2106 | final int clientRequestId = getClientRequestIdOrLog(clientInfo, transactionId); |
| 2107 | if (clientRequestId < 0) return; |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 2108 | final ClientRequest request = clientInfo.mClientRequests.get(clientRequestId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2109 | clientInfo.onRegisterServiceFailed(clientRequestId, errorCode, false /* isLegacy */, |
| 2110 | transactionId, request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2111 | } |
| 2112 | |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2113 | @Override |
| 2114 | public void onOffloadStartOrUpdate(@NonNull String interfaceName, |
| 2115 | @NonNull OffloadServiceInfo offloadServiceInfo) { |
| 2116 | sendOffloadServiceInfosUpdate(interfaceName, offloadServiceInfo, false /* isRemove */); |
| 2117 | } |
| 2118 | |
| 2119 | @Override |
| 2120 | public void onOffloadStop(@NonNull String interfaceName, |
| 2121 | @NonNull OffloadServiceInfo offloadServiceInfo) { |
| 2122 | sendOffloadServiceInfosUpdate(interfaceName, offloadServiceInfo, true /* isRemove */); |
| 2123 | } |
| 2124 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2125 | private ClientInfo getClientInfoOrLog(int transactionId) { |
| 2126 | final ClientInfo clientInfo = mTransactionIdToClientInfoMap.get(transactionId); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2127 | if (clientInfo == null) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2128 | Log.e(TAG, String.format("Callback for service %d has no client", transactionId)); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2129 | } |
| 2130 | return clientInfo; |
| 2131 | } |
| 2132 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2133 | private int getClientRequestIdOrLog(@NonNull ClientInfo info, int transactionId) { |
| 2134 | final int clientRequestId = info.getClientRequestId(transactionId); |
| 2135 | if (clientRequestId < 0) { |
| 2136 | Log.e(TAG, String.format( |
| 2137 | "Client request ID not found for service %d", transactionId)); |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2138 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2139 | return clientRequestId; |
Remi NGUYEN VAN | 5b9074c | 2023-01-18 15:58:03 +0900 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 2143 | private static class ConnectorArgs { |
| 2144 | @NonNull public final NsdServiceConnector connector; |
| 2145 | @NonNull public final INsdManagerCallback callback; |
| 2146 | public final boolean useJavaBackend; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2147 | public final int uid; |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 2148 | |
| 2149 | ConnectorArgs(@NonNull NsdServiceConnector connector, @NonNull INsdManagerCallback callback, |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2150 | boolean useJavaBackend, int uid) { |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 2151 | this.connector = connector; |
| 2152 | this.callback = callback; |
| 2153 | this.useJavaBackend = useJavaBackend; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2154 | this.uid = uid; |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2158 | @Override |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 2159 | public INsdServiceConnector connect(INsdManagerCallback cb, boolean useJavaBackend) { |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 2160 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INTERNET, "NsdService"); |
Paul Hu | 101dbf5 | 2023-08-09 16:05:20 +0800 | [diff] [blame] | 2161 | final int uid = mDeps.getCallingUid(); |
| 2162 | if (cb == null) { |
| 2163 | throw new IllegalArgumentException("Unknown client callback from uid=" + uid); |
| 2164 | } |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 2165 | if (DBG) Log.d(TAG, "New client connect. useJavaBackend=" + useJavaBackend); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2166 | final INsdServiceConnector connector = new NsdServiceConnector(); |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2167 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(NsdManager.REGISTER_CLIENT, |
Paul Hu | 101dbf5 | 2023-08-09 16:05:20 +0800 | [diff] [blame] | 2168 | new ConnectorArgs((NsdServiceConnector) connector, cb, useJavaBackend, uid))); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2169 | return connector; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 2170 | } |
| 2171 | |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2172 | private static class ListenerArgs { |
| 2173 | public final NsdServiceConnector connector; |
| 2174 | public final NsdServiceInfo serviceInfo; |
| 2175 | ListenerArgs(NsdServiceConnector connector, NsdServiceInfo serviceInfo) { |
| 2176 | this.connector = connector; |
| 2177 | this.serviceInfo = serviceInfo; |
| 2178 | } |
| 2179 | } |
| 2180 | |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2181 | private static class AdvertisingArgs { |
| 2182 | public final NsdServiceConnector connector; |
| 2183 | public final AdvertisingRequest advertisingRequest; |
| 2184 | |
| 2185 | AdvertisingArgs(NsdServiceConnector connector, AdvertisingRequest advertisingRequest) { |
| 2186 | this.connector = connector; |
| 2187 | this.advertisingRequest = advertisingRequest; |
| 2188 | } |
| 2189 | } |
| 2190 | |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2191 | private static final class DiscoveryArgs { |
| 2192 | public final NsdServiceConnector connector; |
| 2193 | public final DiscoveryRequest discoveryRequest; |
| 2194 | DiscoveryArgs(NsdServiceConnector connector, DiscoveryRequest discoveryRequest) { |
| 2195 | this.connector = connector; |
| 2196 | this.discoveryRequest = discoveryRequest; |
| 2197 | } |
| 2198 | } |
| 2199 | |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2200 | private class NsdServiceConnector extends INsdServiceConnector.Stub |
| 2201 | implements IBinder.DeathRecipient { |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2202 | |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2203 | @Override |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2204 | public void registerService(int listenerKey, AdvertisingRequest advertisingRequest) |
| 2205 | throws RemoteException { |
Handa Wang | 096e32e | 2024-01-14 08:21:28 +0000 | [diff] [blame] | 2206 | NsdManager.checkServiceInfoForRegistration(advertisingRequest.getServiceInfo()); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2207 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 2208 | NsdManager.REGISTER_SERVICE, 0, listenerKey, |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2209 | new AdvertisingArgs(this, advertisingRequest) |
| 2210 | )); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | @Override |
| 2214 | public void unregisterService(int listenerKey) { |
| 2215 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 2216 | NsdManager.UNREGISTER_SERVICE, 0, listenerKey, |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2217 | new ListenerArgs(this, (NsdServiceInfo) null))); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | @Override |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2221 | public void discoverServices(int listenerKey, DiscoveryRequest discoveryRequest) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2222 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 2223 | NsdManager.DISCOVER_SERVICES, 0, listenerKey, |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2224 | new DiscoveryArgs(this, discoveryRequest))); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | @Override |
| 2228 | public void stopDiscovery(int listenerKey) { |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2229 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(NsdManager.STOP_DISCOVERY, |
| 2230 | 0, listenerKey, new ListenerArgs(this, (NsdServiceInfo) null))); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | @Override |
| 2234 | public void resolveService(int listenerKey, NsdServiceInfo serviceInfo) { |
| 2235 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 2236 | NsdManager.RESOLVE_SERVICE, 0, listenerKey, |
| 2237 | new ListenerArgs(this, serviceInfo))); |
| 2238 | } |
| 2239 | |
| 2240 | @Override |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 2241 | public void stopResolution(int listenerKey) { |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2242 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(NsdManager.STOP_RESOLUTION, |
| 2243 | 0, listenerKey, new ListenerArgs(this, (NsdServiceInfo) null))); |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | @Override |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 2247 | public void registerServiceInfoCallback(int listenerKey, NsdServiceInfo serviceInfo) { |
| 2248 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 2249 | NsdManager.REGISTER_SERVICE_CALLBACK, 0, listenerKey, |
| 2250 | new ListenerArgs(this, serviceInfo))); |
| 2251 | } |
| 2252 | |
| 2253 | @Override |
| 2254 | public void unregisterServiceInfoCallback(int listenerKey) { |
| 2255 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 2256 | NsdManager.UNREGISTER_SERVICE_CALLBACK, 0, listenerKey, |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2257 | new ListenerArgs(this, (NsdServiceInfo) null))); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | @Override |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2261 | public void startDaemon() { |
Yuyang Huang | 86d083f | 2023-12-12 19:56:41 +0900 | [diff] [blame] | 2262 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(NsdManager.DAEMON_STARTUP, |
| 2263 | new ListenerArgs(this, (NsdServiceInfo) null))); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | @Override |
| 2267 | public void binderDied() { |
| 2268 | mNsdStateMachine.sendMessage( |
| 2269 | mNsdStateMachine.obtainMessage(NsdManager.UNREGISTER_CLIENT, this)); |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2270 | |
| 2271 | } |
| 2272 | |
| 2273 | @Override |
| 2274 | public void registerOffloadEngine(String ifaceName, IOffloadEngine cb, |
| 2275 | @OffloadEngine.OffloadCapability long offloadCapabilities, |
| 2276 | @OffloadEngine.OffloadType long offloadTypes) { |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 2277 | checkOffloadEnginePermission(mContext); |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2278 | Objects.requireNonNull(ifaceName); |
| 2279 | Objects.requireNonNull(cb); |
| 2280 | mNsdStateMachine.sendMessage( |
| 2281 | mNsdStateMachine.obtainMessage(NsdManager.REGISTER_OFFLOAD_ENGINE, |
| 2282 | new OffloadEngineInfo(cb, ifaceName, offloadCapabilities, |
| 2283 | offloadTypes))); |
| 2284 | } |
| 2285 | |
| 2286 | @Override |
| 2287 | public void unregisterOffloadEngine(IOffloadEngine cb) { |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 2288 | checkOffloadEnginePermission(mContext); |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2289 | Objects.requireNonNull(cb); |
| 2290 | mNsdStateMachine.sendMessage( |
| 2291 | mNsdStateMachine.obtainMessage(NsdManager.UNREGISTER_OFFLOAD_ENGINE, cb)); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2292 | } |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 2293 | |
| 2294 | private static void checkOffloadEnginePermission(Context context) { |
| 2295 | if (!SdkLevel.isAtLeastT()) { |
| 2296 | throw new SecurityException("API is not available in before API level 33"); |
| 2297 | } |
Yuyang Huang | d5896e7 | 2023-11-28 13:23:59 +0900 | [diff] [blame] | 2298 | |
Ken Chen | a7bae55 | 2023-12-27 11:40:57 +0800 | [diff] [blame] | 2299 | final ArrayList<String> permissionsList = new ArrayList<>(Arrays.asList(NETWORK_STACK, |
| 2300 | PERMISSION_MAINLINE_NETWORK_STACK, NETWORK_SETTINGS)); |
| 2301 | |
| 2302 | if (SdkLevel.isAtLeastV()) { |
| 2303 | // REGISTER_NSD_OFFLOAD_ENGINE was only added to the SDK in V. |
| 2304 | permissionsList.add(REGISTER_NSD_OFFLOAD_ENGINE); |
| 2305 | } else if (SdkLevel.isAtLeastU()) { |
| 2306 | // REGISTER_NSD_OFFLOAD_ENGINE cannot be backport to U. In U, check the DEVICE_POWER |
| 2307 | // permission instead. |
| 2308 | permissionsList.add(DEVICE_POWER); |
Yuyang Huang | d5896e7 | 2023-11-28 13:23:59 +0900 | [diff] [blame] | 2309 | } |
| 2310 | |
Junyu Lai | 71b5153 | 2024-02-01 10:39:01 +0800 | [diff] [blame] | 2311 | if (PermissionUtils.hasAnyPermissionOf(context, |
Ken Chen | a7bae55 | 2023-12-27 11:40:57 +0800 | [diff] [blame] | 2312 | permissionsList.toArray(new String[0]))) { |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 2313 | return; |
| 2314 | } |
| 2315 | throw new SecurityException("Requires one of the following permissions: " |
Ken Chen | a7bae55 | 2023-12-27 11:40:57 +0800 | [diff] [blame] | 2316 | + String.join(", ", permissionsList) + "."); |
Yuyang Huang | 8e6fbc8 | 2023-08-07 17:46:19 +0900 | [diff] [blame] | 2317 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
Hugo Benichi | 912db99 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 2320 | private void sendNsdStateChangeBroadcast(boolean isEnabled) { |
Irfan Sheriff | 52fc83a | 2012-04-19 10:26:34 -0700 | [diff] [blame] | 2321 | final Intent intent = new Intent(NsdManager.ACTION_NSD_STATE_CHANGED); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 2322 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Hugo Benichi | 912db99 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 2323 | int nsdState = isEnabled ? NsdManager.NSD_STATE_ENABLED : NsdManager.NSD_STATE_DISABLED; |
| 2324 | intent.putExtra(NsdManager.EXTRA_NSD_STATE, nsdState); |
Dianne Hackborn | 692107e | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2325 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 2328 | private int getUniqueId() { |
| 2329 | if (++mUniqueId == INVALID_ID) return ++mUniqueId; |
| 2330 | return mUniqueId; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2331 | } |
| 2332 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2333 | private boolean registerService(int transactionId, NsdServiceInfo service) { |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 2334 | if (mMDnsManager == null) { |
| 2335 | Log.wtf(TAG, "registerService: mMDnsManager is null"); |
| 2336 | return false; |
| 2337 | } |
| 2338 | |
Hugo Benichi | 6d70644 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 2339 | if (DBG) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2340 | Log.d(TAG, "registerService: " + transactionId + " " + service); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2341 | } |
Hugo Benichi | 6d70644 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 2342 | String name = service.getServiceName(); |
| 2343 | String type = service.getServiceType(); |
| 2344 | int port = service.getPort(); |
| 2345 | byte[] textRecord = service.getTxtRecord(); |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 2346 | final int registerInterface = getNetworkInterfaceIndex(service); |
| 2347 | if (service.getNetwork() != null && registerInterface == IFACE_IDX_ANY) { |
Paul Hu | 360a8e9 | 2022-04-26 11:14:14 +0800 | [diff] [blame] | 2348 | Log.e(TAG, "Interface to register service on not found"); |
| 2349 | return false; |
| 2350 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2351 | return mMDnsManager.registerService( |
| 2352 | transactionId, name, type, port, textRecord, registerInterface); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2355 | private boolean unregisterService(int transactionId) { |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 2356 | if (mMDnsManager == null) { |
| 2357 | Log.wtf(TAG, "unregisterService: mMDnsManager is null"); |
| 2358 | return false; |
| 2359 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2360 | return mMDnsManager.stopOperation(transactionId); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2363 | private boolean discoverServices(int transactionId, DiscoveryRequest discoveryRequest) { |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 2364 | if (mMDnsManager == null) { |
| 2365 | Log.wtf(TAG, "discoverServices: mMDnsManager is null"); |
| 2366 | return false; |
| 2367 | } |
| 2368 | |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2369 | final String type = discoveryRequest.getServiceType(); |
| 2370 | final int discoverInterface = getNetworkInterfaceIndex(discoveryRequest); |
| 2371 | if (discoveryRequest.getNetwork() != null && discoverInterface == IFACE_IDX_ANY) { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2372 | Log.e(TAG, "Interface to discover service on not found"); |
| 2373 | return false; |
| 2374 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2375 | return mMDnsManager.discover(transactionId, type, discoverInterface); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2376 | } |
| 2377 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2378 | private boolean stopServiceDiscovery(int transactionId) { |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 2379 | if (mMDnsManager == null) { |
| 2380 | Log.wtf(TAG, "stopServiceDiscovery: mMDnsManager is null"); |
| 2381 | return false; |
| 2382 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2383 | return mMDnsManager.stopOperation(transactionId); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2384 | } |
| 2385 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2386 | private boolean resolveService(int transactionId, NsdServiceInfo service) { |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 2387 | if (mMDnsManager == null) { |
| 2388 | Log.wtf(TAG, "resolveService: mMDnsManager is null"); |
| 2389 | return false; |
| 2390 | } |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2391 | final String name = service.getServiceName(); |
| 2392 | final String type = service.getServiceType(); |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 2393 | final int resolveInterface = getNetworkInterfaceIndex(service); |
| 2394 | if (service.getNetwork() != null && resolveInterface == IFACE_IDX_ANY) { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2395 | Log.e(TAG, "Interface to resolve service on not found"); |
| 2396 | return false; |
| 2397 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2398 | return mMDnsManager.resolve(transactionId, name, type, "local.", resolveInterface); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | /** |
| 2402 | * Guess the interface to use to resolve or discover a service on a specific network. |
| 2403 | * |
| 2404 | * This is an imperfect guess, as for example the network may be gone or not yet fully |
| 2405 | * registered. This is fine as failing is correct if the network is gone, and a client |
| 2406 | * attempting to resolve/discover on a network not yet setup would have a bad time anyway; also |
| 2407 | * this is to support the legacy mdnsresponder implementation, which historically resolved |
| 2408 | * services on an unspecified network. |
| 2409 | */ |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 2410 | private int getNetworkInterfaceIndex(NsdServiceInfo serviceInfo) { |
| 2411 | final Network network = serviceInfo.getNetwork(); |
| 2412 | if (network == null) { |
| 2413 | // Fallback to getInterfaceIndex if present (typically if the NsdServiceInfo was |
| 2414 | // provided by NsdService from discovery results, and the service was found on an |
| 2415 | // interface that has no app-usable Network). |
| 2416 | if (serviceInfo.getInterfaceIndex() != 0) { |
| 2417 | return serviceInfo.getInterfaceIndex(); |
| 2418 | } |
| 2419 | return IFACE_IDX_ANY; |
| 2420 | } |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2421 | return getNetworkInterfaceIndex(network); |
| 2422 | } |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2423 | |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2424 | /** |
| 2425 | * Returns the interface to use to discover a service on a specific network, or {@link |
| 2426 | * IFACE_IDX_ANY} if no network is specified. |
| 2427 | */ |
| 2428 | private int getNetworkInterfaceIndex(DiscoveryRequest discoveryRequest) { |
| 2429 | final Network network = discoveryRequest.getNetwork(); |
| 2430 | if (network == null) { |
| 2431 | return IFACE_IDX_ANY; |
| 2432 | } |
| 2433 | return getNetworkInterfaceIndex(network); |
| 2434 | } |
| 2435 | |
| 2436 | /** |
| 2437 | * Returns the interface of a specific network, or {@link IFACE_IDX_ANY} if no interface is |
| 2438 | * associated with {@code network}. |
| 2439 | */ |
| 2440 | private int getNetworkInterfaceIndex(@NonNull Network network) { |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2441 | String interfaceName = getNetworkInterfaceName(network); |
| 2442 | if (interfaceName == null) { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2443 | return IFACE_IDX_ANY; |
| 2444 | } |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2445 | return getNetworkInterfaceIndexByName(interfaceName); |
| 2446 | } |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2447 | |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2448 | private String getNetworkInterfaceName(@Nullable Network network) { |
| 2449 | if (network == null) { |
| 2450 | return null; |
| 2451 | } |
| 2452 | final ConnectivityManager cm = mContext.getSystemService(ConnectivityManager.class); |
| 2453 | if (cm == null) { |
| 2454 | Log.wtf(TAG, "No ConnectivityManager"); |
| 2455 | return null; |
| 2456 | } |
| 2457 | final LinkProperties lp = cm.getLinkProperties(network); |
| 2458 | if (lp == null) { |
| 2459 | return null; |
| 2460 | } |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2461 | // Only resolve on non-stacked interfaces |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2462 | return lp.getInterfaceName(); |
| 2463 | } |
| 2464 | |
| 2465 | private int getNetworkInterfaceIndexByName(final String ifaceName) { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2466 | final NetworkInterface iface; |
| 2467 | try { |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2468 | iface = NetworkInterface.getByName(ifaceName); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2469 | } catch (SocketException e) { |
| 2470 | Log.e(TAG, "Error querying interface", e); |
| 2471 | return IFACE_IDX_ANY; |
| 2472 | } |
| 2473 | |
| 2474 | if (iface == null) { |
Yuyang Huang | 33fa4d2 | 2023-02-14 22:59:37 +0900 | [diff] [blame] | 2475 | Log.e(TAG, "Interface not found: " + ifaceName); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 2476 | return IFACE_IDX_ANY; |
| 2477 | } |
| 2478 | |
| 2479 | return iface.getIndex(); |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 2480 | } |
| 2481 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2482 | private boolean stopResolveService(int transactionId) { |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 2483 | if (mMDnsManager == null) { |
| 2484 | Log.wtf(TAG, "stopResolveService: mMDnsManager is null"); |
| 2485 | return false; |
| 2486 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2487 | return mMDnsManager.stopOperation(transactionId); |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2490 | private boolean getAddrInfo(int transactionId, String hostname, int interfaceIdx) { |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 2491 | if (mMDnsManager == null) { |
| 2492 | Log.wtf(TAG, "getAddrInfo: mMDnsManager is null"); |
| 2493 | return false; |
| 2494 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2495 | return mMDnsManager.getServiceAddress(transactionId, hostname, interfaceIdx); |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 2496 | } |
| 2497 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2498 | private boolean stopGetAddrInfo(int transactionId) { |
Ken Chen | 80c9f6f | 2023-11-15 18:24:54 +0800 | [diff] [blame] | 2499 | if (mMDnsManager == null) { |
| 2500 | Log.wtf(TAG, "stopGetAddrInfo: mMDnsManager is null"); |
| 2501 | return false; |
| 2502 | } |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2503 | return mMDnsManager.stopOperation(transactionId); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | @Override |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2507 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Junyu Lai | 71b5153 | 2024-02-01 10:39:01 +0800 | [diff] [blame] | 2508 | if (!PermissionUtils.hasDumpPermission(mContext, TAG, writer)) return; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2509 | |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2510 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
| 2511 | // Dump state machine logs |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 2512 | mNsdStateMachine.dump(fd, pw, args); |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2513 | |
| 2514 | // Dump service and clients logs |
| 2515 | pw.println(); |
Paul Hu | 14667de | 2023-04-17 22:42:47 +0800 | [diff] [blame] | 2516 | pw.println("Logs:"); |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2517 | pw.increaseIndent(); |
| 2518 | mServiceLogs.reverseDump(pw); |
| 2519 | pw.decreaseIndent(); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 2520 | } |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 2521 | |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2522 | private abstract static class ClientRequest { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2523 | private final int mTransactionId; |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 2524 | private final long mStartTimeMs; |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2525 | private int mFoundServiceCount = 0; |
| 2526 | private int mLostServiceCount = 0; |
| 2527 | private final Set<String> mServices = new ArraySet<>(); |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 2528 | private boolean mIsServiceFromCache = false; |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 2529 | private int mSentQueryCount = NO_SENT_QUERY_COUNT; |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2530 | |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2531 | private ClientRequest(int transactionId, long startTimeMs) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2532 | mTransactionId = transactionId; |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 2533 | mStartTimeMs = startTimeMs; |
| 2534 | } |
| 2535 | |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2536 | public long calculateRequestDurationMs(long stopTimeMs) { |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 2537 | return stopTimeMs - mStartTimeMs; |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2538 | } |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2539 | |
| 2540 | public void onServiceFound(String serviceName) { |
| 2541 | mFoundServiceCount++; |
| 2542 | if (mServices.size() <= MAX_SERVICES_COUNT_METRIC_PER_CLIENT) { |
| 2543 | mServices.add(serviceName); |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | public void onServiceLost() { |
| 2548 | mLostServiceCount++; |
| 2549 | } |
| 2550 | |
| 2551 | public int getFoundServiceCount() { |
| 2552 | return mFoundServiceCount; |
| 2553 | } |
| 2554 | |
| 2555 | public int getLostServiceCount() { |
| 2556 | return mLostServiceCount; |
| 2557 | } |
| 2558 | |
| 2559 | public int getServicesCount() { |
| 2560 | return mServices.size(); |
| 2561 | } |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 2562 | |
| 2563 | public void setServiceFromCache(boolean isServiceFromCache) { |
| 2564 | mIsServiceFromCache = isServiceFromCache; |
| 2565 | } |
| 2566 | |
| 2567 | public boolean isServiceFromCache() { |
| 2568 | return mIsServiceFromCache; |
| 2569 | } |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 2570 | |
| 2571 | public void onQuerySent() { |
| 2572 | mSentQueryCount++; |
| 2573 | } |
| 2574 | |
| 2575 | public int getSentQueryCount() { |
| 2576 | return mSentQueryCount; |
| 2577 | } |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | private static class LegacyClientRequest extends ClientRequest { |
| 2581 | private final int mRequestCode; |
| 2582 | |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2583 | private LegacyClientRequest(int transactionId, int requestCode, long startTimeMs) { |
| 2584 | super(transactionId, startTimeMs); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2585 | mRequestCode = requestCode; |
| 2586 | } |
| 2587 | } |
| 2588 | |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2589 | private abstract static class JavaBackendClientRequest extends ClientRequest { |
| 2590 | @Nullable |
| 2591 | private final Network mRequestedNetwork; |
| 2592 | |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 2593 | private JavaBackendClientRequest(int transactionId, @Nullable Network requestedNetwork, |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2594 | long startTimeMs) { |
| 2595 | super(transactionId, startTimeMs); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2596 | mRequestedNetwork = requestedNetwork; |
| 2597 | } |
| 2598 | |
| 2599 | @Nullable |
| 2600 | public Network getRequestedNetwork() { |
| 2601 | return mRequestedNetwork; |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2605 | private static class AdvertiserClientRequest extends JavaBackendClientRequest { |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 2606 | private AdvertiserClientRequest(int transactionId, @Nullable Network requestedNetwork, |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2607 | long startTimeMs) { |
| 2608 | super(transactionId, requestedNetwork, startTimeMs); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | private static class DiscoveryManagerRequest extends JavaBackendClientRequest { |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2613 | @NonNull |
| 2614 | private final MdnsListener mListener; |
| 2615 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2616 | private DiscoveryManagerRequest(int transactionId, @NonNull MdnsListener listener, |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2617 | @Nullable Network requestedNetwork, long startTimeMs) { |
| 2618 | super(transactionId, requestedNetwork, startTimeMs); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2619 | mListener = listener; |
| 2620 | } |
| 2621 | } |
| 2622 | |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 2623 | /* Information tracked per client */ |
| 2624 | private class ClientInfo { |
| 2625 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 2626 | private static final int MAX_LIMIT = 10; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2627 | private final INsdManagerCallback mCb; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 2628 | /* Remembers a resolved service until getaddrinfo completes */ |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 2629 | private NsdServiceInfo mResolvedService; |
| 2630 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2631 | /* A map from client request ID (listenerKey) to the request */ |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2632 | private final SparseArray<ClientRequest> mClientRequests = new SparseArray<>(); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 2633 | |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 2634 | // The target SDK of this client < Build.VERSION_CODES.S |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2635 | private boolean mIsPreSClient = false; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2636 | private final int mUid; |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 2637 | // The flag of using java backend if the client's target SDK >= U |
| 2638 | private final boolean mUseJavaBackend; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2639 | // Store client logs |
| 2640 | private final SharedLog mClientLogs; |
Paul Hu | cdef353 | 2023-06-18 14:47:35 +0000 | [diff] [blame] | 2641 | // Report the nsd metrics data |
| 2642 | private final NetworkNsdReportedMetrics mMetrics; |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 2643 | |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2644 | private ClientInfo(INsdManagerCallback cb, int uid, boolean useJavaBackend, |
Paul Hu | cdef353 | 2023-06-18 14:47:35 +0000 | [diff] [blame] | 2645 | SharedLog sharedLog, NetworkNsdReportedMetrics metrics) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2646 | mCb = cb; |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2647 | mUid = uid; |
Paul Hu | 2e0a88c | 2023-03-09 16:05:01 +0800 | [diff] [blame] | 2648 | mUseJavaBackend = useJavaBackend; |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2649 | mClientLogs = sharedLog; |
| 2650 | mClientLogs.log("New client. useJavaBackend=" + useJavaBackend); |
Paul Hu | cdef353 | 2023-06-18 14:47:35 +0000 | [diff] [blame] | 2651 | mMetrics = metrics; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 2652 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 2653 | |
| 2654 | @Override |
| 2655 | public String toString() { |
Jeff Sharkey | 6346538 | 2020-10-17 21:20:13 -0600 | [diff] [blame] | 2656 | StringBuilder sb = new StringBuilder(); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 2657 | sb.append("mResolvedService ").append(mResolvedService).append("\n"); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2658 | sb.append("mIsLegacy ").append(mIsPreSClient).append("\n"); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2659 | sb.append("mUseJavaBackend ").append(mUseJavaBackend).append("\n"); |
| 2660 | sb.append("mUid ").append(mUid).append("\n"); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2661 | for (int i = 0; i < mClientRequests.size(); i++) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2662 | int clientRequestId = mClientRequests.keyAt(i); |
| 2663 | sb.append("clientRequestId ") |
| 2664 | .append(clientRequestId) |
| 2665 | .append(" transactionId ").append(mClientRequests.valueAt(i).mTransactionId) |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2666 | .append(" type ").append( |
| 2667 | mClientRequests.valueAt(i).getClass().getSimpleName()) |
| 2668 | .append("\n"); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 2669 | } |
| 2670 | return sb.toString(); |
| 2671 | } |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 2672 | |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2673 | private boolean isPreSClient() { |
| 2674 | return mIsPreSClient; |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 2675 | } |
| 2676 | |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2677 | private void setPreSClient() { |
| 2678 | mIsPreSClient = true; |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 2679 | } |
| 2680 | |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2681 | private MdnsListener unregisterMdnsListenerFromRequest(ClientRequest request) { |
Paul Hu | e4f5f25 | 2023-02-16 21:13:47 +0800 | [diff] [blame] | 2682 | final MdnsListener listener = |
| 2683 | ((DiscoveryManagerRequest) request).mListener; |
| 2684 | mMdnsDiscoveryManager.unregisterListener( |
| 2685 | listener.getListenedServiceType(), listener); |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2686 | return listener; |
Paul Hu | e4f5f25 | 2023-02-16 21:13:47 +0800 | [diff] [blame] | 2687 | } |
| 2688 | |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 2689 | // Remove any pending requests from the global map when we get rid of a client, |
| 2690 | // and send cancellations to the daemon. |
| 2691 | private void expungeAllRequests() { |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2692 | mClientLogs.log("Client unregistered. expungeAllRequests!"); |
Hugo Benichi | d2552ae | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 2693 | // TODO: to keep handler responsive, do not clean all requests for that client at once. |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2694 | for (int i = 0; i < mClientRequests.size(); i++) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2695 | final int clientRequestId = mClientRequests.keyAt(i); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2696 | final ClientRequest request = mClientRequests.valueAt(i); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2697 | final int transactionId = request.mTransactionId; |
| 2698 | mTransactionIdToClientInfoMap.remove(transactionId); |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 2699 | if (DBG) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2700 | Log.d(TAG, "Terminating clientRequestId " + clientRequestId |
| 2701 | + " transactionId " + transactionId |
| 2702 | + " type " + mClientRequests.get(clientRequestId)); |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 2703 | } |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2704 | |
| 2705 | if (request instanceof DiscoveryManagerRequest) { |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2706 | final MdnsListener listener = unregisterMdnsListenerFromRequest(request); |
| 2707 | if (listener instanceof DiscoveryListener) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2708 | mMetrics.reportServiceDiscoveryStop(false /* isLegacy */, transactionId, |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2709 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
| 2710 | request.getFoundServiceCount(), |
| 2711 | request.getLostServiceCount(), |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 2712 | request.getServicesCount(), |
| 2713 | request.getSentQueryCount()); |
Paul Hu | 6014905 | 2023-07-31 14:26:08 +0800 | [diff] [blame] | 2714 | } else if (listener instanceof ResolutionListener) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2715 | mMetrics.reportServiceResolutionStop(false /* isLegacy */, transactionId, |
Paul Hu | 6014905 | 2023-07-31 14:26:08 +0800 | [diff] [blame] | 2716 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 2717 | } else if (listener instanceof ServiceInfoListener) { |
| 2718 | mMetrics.reportServiceInfoCallbackUnregistered(transactionId, |
| 2719 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
| 2720 | request.getFoundServiceCount(), |
| 2721 | request.getLostServiceCount(), |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 2722 | request.isServiceFromCache(), |
| 2723 | request.getSentQueryCount()); |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2724 | } |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2725 | continue; |
| 2726 | } |
| 2727 | |
| 2728 | if (request instanceof AdvertiserClientRequest) { |
Paul Hu | 043bcd4 | 2023-07-14 16:38:25 +0800 | [diff] [blame] | 2729 | final AdvertiserMetrics metrics = |
| 2730 | mAdvertiser.getAdvertiserMetrics(transactionId); |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2731 | mAdvertiser.removeService(transactionId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2732 | mMetrics.reportServiceUnregistration(false /* isLegacy */, transactionId, |
Paul Hu | 043bcd4 | 2023-07-14 16:38:25 +0800 | [diff] [blame] | 2733 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
| 2734 | metrics.mRepliedRequestsCount, metrics.mSentPacketCount, |
| 2735 | metrics.mConflictDuringProbingCount, |
| 2736 | metrics.mConflictAfterProbingCount); |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2737 | continue; |
| 2738 | } |
| 2739 | |
| 2740 | if (!(request instanceof LegacyClientRequest)) { |
| 2741 | throw new IllegalStateException("Unknown request type: " + request.getClass()); |
| 2742 | } |
| 2743 | |
| 2744 | switch (((LegacyClientRequest) request).mRequestCode) { |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 2745 | case NsdManager.DISCOVER_SERVICES: |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2746 | stopServiceDiscovery(transactionId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2747 | mMetrics.reportServiceDiscoveryStop(true /* isLegacy */, transactionId, |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2748 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
| 2749 | request.getFoundServiceCount(), |
| 2750 | request.getLostServiceCount(), |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 2751 | request.getServicesCount(), |
| 2752 | NO_SENT_QUERY_COUNT); |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 2753 | break; |
| 2754 | case NsdManager.RESOLVE_SERVICE: |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2755 | stopResolveService(transactionId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2756 | mMetrics.reportServiceResolutionStop(true /* isLegacy */, transactionId, |
Paul Hu | 6014905 | 2023-07-31 14:26:08 +0800 | [diff] [blame] | 2757 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 2758 | break; |
| 2759 | case NsdManager.REGISTER_SERVICE: |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2760 | unregisterService(transactionId); |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2761 | mMetrics.reportServiceUnregistration(true /* isLegacy */, transactionId, |
Paul Hu | 043bcd4 | 2023-07-14 16:38:25 +0800 | [diff] [blame] | 2762 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
| 2763 | NO_PACKET /* repliedRequestsCount */, |
| 2764 | NO_PACKET /* sentPacketCount */, |
| 2765 | 0 /* conflictDuringProbingCount */, |
| 2766 | 0 /* conflictAfterProbingCount */); |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 2767 | break; |
| 2768 | default: |
| 2769 | break; |
| 2770 | } |
| 2771 | } |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 2772 | mClientRequests.clear(); |
Remi NGUYEN VAN | a8efbe0 | 2023-05-26 11:43:20 +0900 | [diff] [blame] | 2773 | updateMulticastLock(); |
| 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * Returns true if this client has any Java backend request that requests one of the given |
| 2778 | * networks. |
| 2779 | */ |
| 2780 | boolean hasAnyJavaBackendRequestForNetworks(@NonNull ArraySet<Network> networks) { |
| 2781 | for (int i = 0; i < mClientRequests.size(); i++) { |
| 2782 | final ClientRequest req = mClientRequests.valueAt(i); |
| 2783 | if (!(req instanceof JavaBackendClientRequest)) { |
| 2784 | continue; |
| 2785 | } |
| 2786 | final Network reqNetwork = ((JavaBackendClientRequest) mClientRequests.valueAt(i)) |
| 2787 | .getRequestedNetwork(); |
| 2788 | if (MdnsUtils.isAnyNetworkMatched(reqNetwork, networks)) { |
| 2789 | return true; |
| 2790 | } |
| 2791 | } |
| 2792 | return false; |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 2793 | } |
| 2794 | |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2795 | // mClientRequests is a sparse array of client request id -> ClientRequest. For a given |
| 2796 | // transaction id, return the corresponding client request id. |
| 2797 | private int getClientRequestId(final int transactionId) { |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2798 | for (int i = 0; i < mClientRequests.size(); i++) { |
Paul Hu | d44e1b7 | 2023-06-16 02:07:42 +0000 | [diff] [blame] | 2799 | if (mClientRequests.valueAt(i).mTransactionId == transactionId) { |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2800 | return mClientRequests.keyAt(i); |
| 2801 | } |
Christopher Lane | 7441122 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 2802 | } |
Remi NGUYEN VAN | 8f453b9 | 2023-01-18 17:44:36 +0900 | [diff] [blame] | 2803 | return -1; |
Christopher Lane | 7441122 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 2804 | } |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2805 | |
Paul Hu | b2e67d3 | 2023-04-18 05:50:14 +0000 | [diff] [blame] | 2806 | private void log(String message) { |
| 2807 | mClientLogs.log(message); |
| 2808 | } |
| 2809 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2810 | private static boolean isLegacyClientRequest(@NonNull ClientRequest request) { |
| 2811 | return !(request instanceof DiscoveryManagerRequest) |
| 2812 | && !(request instanceof AdvertiserClientRequest); |
| 2813 | } |
| 2814 | |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2815 | void onDiscoverServicesStarted(int listenerKey, DiscoveryRequest discoveryRequest, |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2816 | ClientRequest request) { |
| 2817 | mMetrics.reportServiceDiscoveryStarted( |
| 2818 | isLegacyClientRequest(request), request.mTransactionId); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2819 | try { |
Kangping Dong | 1f1a379 | 2023-12-10 22:05:04 +0800 | [diff] [blame] | 2820 | mCb.onDiscoverServicesStarted(listenerKey, discoveryRequest); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2821 | } catch (RemoteException e) { |
| 2822 | Log.e(TAG, "Error calling onDiscoverServicesStarted", e); |
| 2823 | } |
| 2824 | } |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2825 | void onDiscoverServicesFailedImmediately(int listenerKey, int error, boolean isLegacy) { |
| 2826 | onDiscoverServicesFailed(listenerKey, error, isLegacy, NO_TRANSACTION, |
| 2827 | 0L /* durationMs */); |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2828 | } |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2829 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2830 | void onDiscoverServicesFailed(int listenerKey, int error, boolean isLegacy, |
| 2831 | int transactionId, long durationMs) { |
| 2832 | mMetrics.reportServiceDiscoveryFailed(isLegacy, transactionId, durationMs); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2833 | try { |
| 2834 | mCb.onDiscoverServicesFailed(listenerKey, error); |
| 2835 | } catch (RemoteException e) { |
| 2836 | Log.e(TAG, "Error calling onDiscoverServicesFailed", e); |
| 2837 | } |
| 2838 | } |
| 2839 | |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2840 | void onServiceFound(int listenerKey, NsdServiceInfo info, ClientRequest request) { |
| 2841 | request.onServiceFound(info.getServiceName()); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2842 | try { |
| 2843 | mCb.onServiceFound(listenerKey, info); |
| 2844 | } catch (RemoteException e) { |
| 2845 | Log.e(TAG, "Error calling onServiceFound(", e); |
| 2846 | } |
| 2847 | } |
| 2848 | |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2849 | void onServiceLost(int listenerKey, NsdServiceInfo info, ClientRequest request) { |
| 2850 | request.onServiceLost(); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2851 | try { |
| 2852 | mCb.onServiceLost(listenerKey, info); |
| 2853 | } catch (RemoteException e) { |
| 2854 | Log.e(TAG, "Error calling onServiceLost(", e); |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | void onStopDiscoveryFailed(int listenerKey, int error) { |
| 2859 | try { |
| 2860 | mCb.onStopDiscoveryFailed(listenerKey, error); |
| 2861 | } catch (RemoteException e) { |
| 2862 | Log.e(TAG, "Error calling onStopDiscoveryFailed", e); |
| 2863 | } |
| 2864 | } |
| 2865 | |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2866 | void onStopDiscoverySucceeded(int listenerKey, ClientRequest request) { |
| 2867 | mMetrics.reportServiceDiscoveryStop( |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2868 | isLegacyClientRequest(request), |
Paul Hu | 812e921 | 2023-06-20 06:24:53 +0000 | [diff] [blame] | 2869 | request.mTransactionId, |
| 2870 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
| 2871 | request.getFoundServiceCount(), |
| 2872 | request.getLostServiceCount(), |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 2873 | request.getServicesCount(), |
| 2874 | request.getSentQueryCount()); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2875 | try { |
| 2876 | mCb.onStopDiscoverySucceeded(listenerKey); |
| 2877 | } catch (RemoteException e) { |
| 2878 | Log.e(TAG, "Error calling onStopDiscoverySucceeded", e); |
| 2879 | } |
| 2880 | } |
| 2881 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2882 | void onRegisterServiceFailedImmediately(int listenerKey, int error, boolean isLegacy) { |
| 2883 | onRegisterServiceFailed(listenerKey, error, isLegacy, NO_TRANSACTION, |
| 2884 | 0L /* durationMs */); |
Paul Hu | 777ed05 | 2023-06-19 13:35:15 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2887 | void onRegisterServiceFailed(int listenerKey, int error, boolean isLegacy, |
| 2888 | int transactionId, long durationMs) { |
| 2889 | mMetrics.reportServiceRegistrationFailed(isLegacy, transactionId, durationMs); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2890 | try { |
| 2891 | mCb.onRegisterServiceFailed(listenerKey, error); |
| 2892 | } catch (RemoteException e) { |
| 2893 | Log.e(TAG, "Error calling onRegisterServiceFailed", e); |
| 2894 | } |
| 2895 | } |
| 2896 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2897 | void onRegisterServiceSucceeded(int listenerKey, NsdServiceInfo info, |
| 2898 | ClientRequest request) { |
| 2899 | mMetrics.reportServiceRegistrationSucceeded(isLegacyClientRequest(request), |
| 2900 | request.mTransactionId, |
| 2901 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2902 | try { |
| 2903 | mCb.onRegisterServiceSucceeded(listenerKey, info); |
| 2904 | } catch (RemoteException e) { |
| 2905 | Log.e(TAG, "Error calling onRegisterServiceSucceeded", e); |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | void onUnregisterServiceFailed(int listenerKey, int error) { |
| 2910 | try { |
| 2911 | mCb.onUnregisterServiceFailed(listenerKey, error); |
| 2912 | } catch (RemoteException e) { |
| 2913 | Log.e(TAG, "Error calling onUnregisterServiceFailed", e); |
| 2914 | } |
| 2915 | } |
| 2916 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2917 | void onUnregisterServiceSucceeded(int listenerKey, ClientRequest request, |
Paul Hu | 043bcd4 | 2023-07-14 16:38:25 +0800 | [diff] [blame] | 2918 | AdvertiserMetrics metrics) { |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2919 | mMetrics.reportServiceUnregistration(isLegacyClientRequest(request), |
| 2920 | request.mTransactionId, |
| 2921 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
Paul Hu | 043bcd4 | 2023-07-14 16:38:25 +0800 | [diff] [blame] | 2922 | metrics.mRepliedRequestsCount, metrics.mSentPacketCount, |
| 2923 | metrics.mConflictDuringProbingCount, metrics.mConflictAfterProbingCount); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2924 | try { |
| 2925 | mCb.onUnregisterServiceSucceeded(listenerKey); |
| 2926 | } catch (RemoteException e) { |
| 2927 | Log.e(TAG, "Error calling onUnregisterServiceSucceeded", e); |
| 2928 | } |
| 2929 | } |
| 2930 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2931 | void onResolveServiceFailedImmediately(int listenerKey, int error, boolean isLegacy) { |
| 2932 | onResolveServiceFailed(listenerKey, error, isLegacy, NO_TRANSACTION, |
| 2933 | 0L /* durationMs */); |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2936 | void onResolveServiceFailed(int listenerKey, int error, boolean isLegacy, |
| 2937 | int transactionId, long durationMs) { |
| 2938 | mMetrics.reportServiceResolutionFailed(isLegacy, transactionId, durationMs); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2939 | try { |
| 2940 | mCb.onResolveServiceFailed(listenerKey, error); |
| 2941 | } catch (RemoteException e) { |
| 2942 | Log.e(TAG, "Error calling onResolveServiceFailed", e); |
| 2943 | } |
| 2944 | } |
| 2945 | |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 2946 | void onResolveServiceSucceeded(int listenerKey, NsdServiceInfo info, |
| 2947 | ClientRequest request) { |
| 2948 | mMetrics.reportServiceResolved( |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2949 | isLegacyClientRequest(request), |
Paul Hu | a6bc463 | 2023-06-26 01:18:29 +0000 | [diff] [blame] | 2950 | request.mTransactionId, |
| 2951 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 2952 | request.isServiceFromCache(), |
| 2953 | request.getSentQueryCount()); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 2954 | try { |
| 2955 | mCb.onResolveServiceSucceeded(listenerKey, info); |
| 2956 | } catch (RemoteException e) { |
| 2957 | Log.e(TAG, "Error calling onResolveServiceSucceeded", e); |
| 2958 | } |
| 2959 | } |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 2960 | |
| 2961 | void onStopResolutionFailed(int listenerKey, int error) { |
| 2962 | try { |
| 2963 | mCb.onStopResolutionFailed(listenerKey, error); |
| 2964 | } catch (RemoteException e) { |
| 2965 | Log.e(TAG, "Error calling onStopResolutionFailed", e); |
| 2966 | } |
| 2967 | } |
| 2968 | |
Paul Hu | 6014905 | 2023-07-31 14:26:08 +0800 | [diff] [blame] | 2969 | void onStopResolutionSucceeded(int listenerKey, ClientRequest request) { |
| 2970 | mMetrics.reportServiceResolutionStop( |
Paul Hu | 508a012 | 2023-09-11 15:31:33 +0800 | [diff] [blame] | 2971 | isLegacyClientRequest(request), |
Paul Hu | 6014905 | 2023-07-31 14:26:08 +0800 | [diff] [blame] | 2972 | request.mTransactionId, |
| 2973 | request.calculateRequestDurationMs(mClock.elapsedRealtime())); |
Paul Hu | b58deb7 | 2022-12-26 09:24:42 +0000 | [diff] [blame] | 2974 | try { |
| 2975 | mCb.onStopResolutionSucceeded(listenerKey); |
| 2976 | } catch (RemoteException e) { |
| 2977 | Log.e(TAG, "Error calling onStopResolutionSucceeded", e); |
| 2978 | } |
| 2979 | } |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 2980 | |
| 2981 | void onServiceInfoCallbackRegistrationFailed(int listenerKey, int error) { |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 2982 | mMetrics.reportServiceInfoCallbackRegistrationFailed(NO_TRANSACTION); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 2983 | try { |
| 2984 | mCb.onServiceInfoCallbackRegistrationFailed(listenerKey, error); |
| 2985 | } catch (RemoteException e) { |
| 2986 | Log.e(TAG, "Error calling onServiceInfoCallbackRegistrationFailed", e); |
| 2987 | } |
| 2988 | } |
| 2989 | |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 2990 | void onServiceInfoCallbackRegistered(int transactionId) { |
| 2991 | mMetrics.reportServiceInfoCallbackRegistered(transactionId); |
| 2992 | } |
| 2993 | |
| 2994 | void onServiceUpdated(int listenerKey, NsdServiceInfo info, ClientRequest request) { |
| 2995 | request.onServiceFound(info.getServiceName()); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 2996 | try { |
| 2997 | mCb.onServiceUpdated(listenerKey, info); |
| 2998 | } catch (RemoteException e) { |
| 2999 | Log.e(TAG, "Error calling onServiceUpdated", e); |
| 3000 | } |
| 3001 | } |
| 3002 | |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 3003 | void onServiceUpdatedLost(int listenerKey, ClientRequest request) { |
| 3004 | request.onServiceLost(); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 3005 | try { |
| 3006 | mCb.onServiceUpdatedLost(listenerKey); |
| 3007 | } catch (RemoteException e) { |
| 3008 | Log.e(TAG, "Error calling onServiceUpdatedLost", e); |
| 3009 | } |
| 3010 | } |
| 3011 | |
Paul Hu | ddce591 | 2023-08-01 10:26:49 +0800 | [diff] [blame] | 3012 | void onServiceInfoCallbackUnregistered(int listenerKey, ClientRequest request) { |
| 3013 | mMetrics.reportServiceInfoCallbackUnregistered( |
| 3014 | request.mTransactionId, |
| 3015 | request.calculateRequestDurationMs(mClock.elapsedRealtime()), |
| 3016 | request.getFoundServiceCount(), |
| 3017 | request.getLostServiceCount(), |
Paul Hu | bad6fe9 | 2023-07-24 21:25:22 +0800 | [diff] [blame] | 3018 | request.isServiceFromCache(), |
| 3019 | request.getSentQueryCount()); |
Paul Hu | 18aeccc | 2022-12-27 08:48:48 +0000 | [diff] [blame] | 3020 | try { |
| 3021 | mCb.onServiceInfoCallbackUnregistered(listenerKey); |
| 3022 | } catch (RemoteException e) { |
| 3023 | Log.e(TAG, "Error calling onServiceInfoCallbackUnregistered", e); |
| 3024 | } |
| 3025 | } |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 3026 | } |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 3027 | } |