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