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