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