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