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