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