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