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