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; |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 20 | import static android.net.nsd.NsdManager.MDNS_DISCOVERY_MANAGER_EVENT; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 21 | import static android.net.nsd.NsdManager.MDNS_SERVICE_EVENT; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 22 | import static android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 23 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 24 | import android.annotation.NonNull; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 25 | import android.annotation.Nullable; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 26 | import android.content.Context; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 28 | import android.net.ConnectivityManager; |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 29 | import android.net.INetd; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 30 | import android.net.InetAddresses; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 31 | import android.net.LinkProperties; |
| 32 | import android.net.Network; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 33 | import android.net.mdns.aidl.DiscoveryInfo; |
| 34 | import android.net.mdns.aidl.GetAddressInfo; |
| 35 | import android.net.mdns.aidl.IMDnsEventListener; |
| 36 | import android.net.mdns.aidl.RegistrationInfo; |
| 37 | import android.net.mdns.aidl.ResolutionInfo; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 38 | import android.net.nsd.INsdManager; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 39 | import android.net.nsd.INsdManagerCallback; |
| 40 | import android.net.nsd.INsdServiceConnector; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 41 | import android.net.nsd.MDnsManager; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 42 | import android.net.nsd.NsdManager; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 43 | import android.net.nsd.NsdServiceInfo; |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 44 | import android.os.Handler; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 45 | import android.os.HandlerThread; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 46 | import android.os.IBinder; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 47 | import android.os.Looper; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 48 | import android.os.Message; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 49 | import android.os.RemoteException; |
Dianne Hackborn | 692107e | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 50 | import android.os.UserHandle; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 51 | import android.text.TextUtils; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 52 | import android.util.Log; |
| 53 | import android.util.Pair; |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 54 | import android.util.SparseArray; |
Hugo Benichi | d2552ae | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 55 | import android.util.SparseIntArray; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 56 | |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 57 | import com.android.internal.annotations.VisibleForTesting; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 58 | import com.android.internal.util.State; |
| 59 | import com.android.internal.util.StateMachine; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 60 | import com.android.net.module.util.DeviceConfigUtils; |
paulhu | 3ffffe7 | 2021-09-16 10:15:22 +0800 | [diff] [blame] | 61 | import com.android.net.module.util.PermissionUtils; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 62 | import com.android.server.connectivity.mdns.ExecutorProvider; |
| 63 | import com.android.server.connectivity.mdns.MdnsDiscoveryManager; |
| 64 | import com.android.server.connectivity.mdns.MdnsMultinetworkSocketClient; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 65 | import com.android.server.connectivity.mdns.MdnsSearchOptions; |
| 66 | import com.android.server.connectivity.mdns.MdnsServiceBrowserListener; |
| 67 | import com.android.server.connectivity.mdns.MdnsServiceInfo; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 68 | import com.android.server.connectivity.mdns.MdnsSocketClientBase; |
| 69 | import com.android.server.connectivity.mdns.MdnsSocketProvider; |
paulhu | a262cc1 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 70 | |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 71 | import java.io.FileDescriptor; |
| 72 | import java.io.PrintWriter; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 73 | import java.net.InetAddress; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 74 | import java.net.NetworkInterface; |
| 75 | import java.net.SocketException; |
| 76 | import java.net.UnknownHostException; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 77 | import java.util.HashMap; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 78 | import java.util.List; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 79 | import java.util.Map; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 80 | import java.util.regex.Matcher; |
| 81 | import java.util.regex.Pattern; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 82 | |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Network Service Discovery Service handles remote service discovery operation requests by |
| 85 | * implementing the INsdManager interface. |
| 86 | * |
| 87 | * @hide |
| 88 | */ |
| 89 | public class NsdService extends INsdManager.Stub { |
| 90 | private static final String TAG = "NsdService"; |
| 91 | private static final String MDNS_TAG = "mDnsConnector"; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 92 | private static final String MDNS_DISCOVERY_MANAGER_VERSION = "mdns_discovery_manager_version"; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 93 | private static final String LOCAL_DOMAIN_NAME = "local"; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 94 | |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 95 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 96 | private static final long CLEANUP_DELAY_MS = 10000; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 97 | private static final int IFACE_IDX_ANY = 0; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 98 | |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 99 | private final Context mContext; |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 100 | private final NsdStateMachine mNsdStateMachine; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 101 | private final MDnsManager mMDnsManager; |
| 102 | private final MDnsEventCallback mMDnsEventCallback; |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 103 | @Nullable |
| 104 | private final MdnsMultinetworkSocketClient mMdnsSocketClient; |
| 105 | @Nullable |
| 106 | private final MdnsDiscoveryManager mMdnsDiscoveryManager; |
| 107 | @Nullable |
| 108 | private final MdnsSocketProvider mMdnsSocketProvider; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 109 | // 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] | 110 | // state machine thread. If change this outside state machine, it will need to introduce |
| 111 | // synchronization. |
| 112 | private boolean mIsDaemonStarted = false; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 113 | private boolean mIsMonitoringSocketsStarted = false; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Clients receiving asynchronous messages |
| 117 | */ |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 118 | private final HashMap<NsdServiceConnector, ClientInfo> mClients = new HashMap<>(); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 119 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 120 | /* A map from unique id to client info */ |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 121 | private final SparseArray<ClientInfo> mIdToClientInfoMap= new SparseArray<>(); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 122 | |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 123 | private final long mCleanupDelayMs; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 124 | |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 125 | private static final int INVALID_ID = 0; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 126 | private int mUniqueId = 1; |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 127 | // The count of the connected legacy clients. |
| 128 | private int mLegacyClientCount = 0; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 129 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 130 | private static class MdnsListener implements MdnsServiceBrowserListener { |
| 131 | protected final int mClientId; |
| 132 | protected final int mTransactionId; |
| 133 | @NonNull |
| 134 | protected final NsdServiceInfo mReqServiceInfo; |
| 135 | @NonNull |
| 136 | protected final String mListenedServiceType; |
| 137 | |
| 138 | MdnsListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo, |
| 139 | @NonNull String listenedServiceType) { |
| 140 | mClientId = clientId; |
| 141 | mTransactionId = transactionId; |
| 142 | mReqServiceInfo = reqServiceInfo; |
| 143 | mListenedServiceType = listenedServiceType; |
| 144 | } |
| 145 | |
| 146 | @NonNull |
| 147 | public String getListenedServiceType() { |
| 148 | return mListenedServiceType; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onServiceFound(@NonNull MdnsServiceInfo serviceInfo) { } |
| 153 | |
| 154 | @Override |
| 155 | public void onServiceUpdated(@NonNull MdnsServiceInfo serviceInfo) { } |
| 156 | |
| 157 | @Override |
| 158 | public void onServiceRemoved(@NonNull MdnsServiceInfo serviceInfo) { } |
| 159 | |
| 160 | @Override |
| 161 | public void onServiceNameDiscovered(@NonNull MdnsServiceInfo serviceInfo) { } |
| 162 | |
| 163 | @Override |
| 164 | public void onServiceNameRemoved(@NonNull MdnsServiceInfo serviceInfo) { } |
| 165 | |
| 166 | @Override |
| 167 | public void onSearchStoppedWithError(int error) { } |
| 168 | |
| 169 | @Override |
| 170 | public void onSearchFailedToStart() { } |
| 171 | |
| 172 | @Override |
| 173 | public void onDiscoveryQuerySent(@NonNull List<String> subtypes, int transactionId) { } |
| 174 | |
| 175 | @Override |
| 176 | public void onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode) { } |
| 177 | } |
| 178 | |
| 179 | private class DiscoveryListener extends MdnsListener { |
| 180 | |
| 181 | DiscoveryListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo, |
| 182 | @NonNull String listenServiceType) { |
| 183 | super(clientId, transactionId, reqServiceInfo, listenServiceType); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void onServiceNameDiscovered(@NonNull MdnsServiceInfo serviceInfo) { |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 188 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 189 | NsdManager.SERVICE_FOUND, |
| 190 | new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo)); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void onServiceNameRemoved(@NonNull MdnsServiceInfo serviceInfo) { |
Paul Hu | 319751a | 2023-01-13 23:56:34 +0800 | [diff] [blame] | 195 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 196 | NsdManager.SERVICE_LOST, |
| 197 | new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo)); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 201 | private class ResolutionListener extends MdnsListener { |
| 202 | |
| 203 | ResolutionListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo, |
| 204 | @NonNull String listenServiceType) { |
| 205 | super(clientId, transactionId, reqServiceInfo, listenServiceType); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void onServiceFound(MdnsServiceInfo serviceInfo) { |
| 210 | mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId, |
| 211 | NsdManager.RESOLVE_SERVICE_SUCCEEDED, |
| 212 | new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo)); |
| 213 | } |
| 214 | } |
| 215 | |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 216 | /** |
| 217 | * Data class of mdns service callback information. |
| 218 | */ |
| 219 | private static class MdnsEvent { |
| 220 | final int mClientId; |
| 221 | @NonNull |
| 222 | final String mRequestedServiceType; |
| 223 | @NonNull |
| 224 | final MdnsServiceInfo mMdnsServiceInfo; |
| 225 | |
| 226 | MdnsEvent(int clientId, @NonNull String requestedServiceType, |
| 227 | @NonNull MdnsServiceInfo mdnsServiceInfo) { |
| 228 | mClientId = clientId; |
| 229 | mRequestedServiceType = requestedServiceType; |
| 230 | mMdnsServiceInfo = mdnsServiceInfo; |
| 231 | } |
| 232 | } |
| 233 | |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 234 | private class NsdStateMachine extends StateMachine { |
| 235 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 236 | private final DefaultState mDefaultState = new DefaultState(); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 237 | private final EnabledState mEnabledState = new EnabledState(); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 238 | |
| 239 | @Override |
Wink Saville | 358f5d4 | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 240 | protected String getWhatToString(int what) { |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 241 | return NsdManager.nameOf(what); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 244 | private void maybeStartDaemon() { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 245 | if (mIsDaemonStarted) { |
| 246 | if (DBG) Log.d(TAG, "Daemon is already started."); |
| 247 | return; |
| 248 | } |
| 249 | mMDnsManager.registerEventListener(mMDnsEventCallback); |
| 250 | mMDnsManager.startDaemon(); |
| 251 | mIsDaemonStarted = true; |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 252 | maybeScheduleStop(); |
| 253 | } |
| 254 | |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 255 | private void maybeStopDaemon() { |
| 256 | if (!mIsDaemonStarted) { |
| 257 | if (DBG) Log.d(TAG, "Daemon has not been started."); |
| 258 | return; |
| 259 | } |
| 260 | mMDnsManager.unregisterEventListener(mMDnsEventCallback); |
| 261 | mMDnsManager.stopDaemon(); |
| 262 | mIsDaemonStarted = false; |
| 263 | } |
| 264 | |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 265 | private boolean isAnyRequestActive() { |
| 266 | return mIdToClientInfoMap.size() != 0; |
| 267 | } |
| 268 | |
| 269 | private void scheduleStop() { |
| 270 | sendMessageDelayed(NsdManager.DAEMON_CLEANUP, mCleanupDelayMs); |
| 271 | } |
| 272 | private void maybeScheduleStop() { |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 273 | // The native daemon should stay alive and can't be cleanup |
| 274 | // if any legacy client connected. |
| 275 | if (!isAnyRequestActive() && mLegacyClientCount == 0) { |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 276 | scheduleStop(); |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 280 | private void cancelStop() { |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 281 | this.removeMessages(NsdManager.DAEMON_CLEANUP); |
| 282 | } |
| 283 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 284 | private void maybeStartMonitoringSockets() { |
| 285 | if (mIsMonitoringSocketsStarted) { |
| 286 | if (DBG) Log.d(TAG, "Socket monitoring is already started."); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | mMdnsSocketProvider.startMonitoringSockets(); |
| 291 | mIsMonitoringSocketsStarted = true; |
| 292 | } |
| 293 | |
| 294 | private void maybeStopMonitoringSockets() { |
| 295 | if (!mIsMonitoringSocketsStarted) { |
| 296 | if (DBG) Log.d(TAG, "Socket monitoring has not been started."); |
| 297 | return; |
| 298 | } |
| 299 | mMdnsSocketProvider.stopMonitoringSockets(); |
| 300 | mIsMonitoringSocketsStarted = false; |
| 301 | } |
| 302 | |
| 303 | private void maybeStopMonitoringSocketsIfNoActiveRequest() { |
| 304 | if (!isAnyRequestActive()) { |
| 305 | maybeStopMonitoringSockets(); |
| 306 | } |
| 307 | } |
| 308 | |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 309 | NsdStateMachine(String name, Handler handler) { |
| 310 | super(name, handler); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 311 | addState(mDefaultState); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 312 | addState(mEnabledState, mDefaultState); |
paulhu | 5568f45 | 2021-11-30 13:31:29 +0800 | [diff] [blame] | 313 | State initialState = mEnabledState; |
Hugo Benichi | 912db99 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 314 | setInitialState(initialState); |
Wink Saville | 358f5d4 | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 315 | setLogRecSize(25); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | class DefaultState extends State { |
| 319 | @Override |
| 320 | public boolean processMessage(Message msg) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 321 | final ClientInfo cInfo; |
| 322 | final int clientId = msg.arg2; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 323 | switch (msg.what) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 324 | case NsdManager.REGISTER_CLIENT: |
| 325 | final Pair<NsdServiceConnector, INsdManagerCallback> arg = |
| 326 | (Pair<NsdServiceConnector, INsdManagerCallback>) msg.obj; |
| 327 | final INsdManagerCallback cb = arg.second; |
| 328 | try { |
| 329 | cb.asBinder().linkToDeath(arg.first, 0); |
| 330 | cInfo = new ClientInfo(cb); |
| 331 | mClients.put(arg.first, cInfo); |
| 332 | } catch (RemoteException e) { |
| 333 | Log.w(TAG, "Client " + clientId + " has already died"); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 334 | } |
| 335 | break; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 336 | case NsdManager.UNREGISTER_CLIENT: |
| 337 | final NsdServiceConnector connector = (NsdServiceConnector) msg.obj; |
| 338 | cInfo = mClients.remove(connector); |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 339 | if (cInfo != null) { |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 340 | if (mMdnsDiscoveryManager != null) { |
| 341 | cInfo.unregisterAllListeners(); |
| 342 | } |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 343 | cInfo.expungeAllRequests(); |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 344 | if (cInfo.isLegacy()) { |
| 345 | mLegacyClientCount -= 1; |
| 346 | } |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 347 | } |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 348 | if (mMdnsDiscoveryManager != null) { |
| 349 | maybeStopMonitoringSocketsIfNoActiveRequest(); |
| 350 | } |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 351 | maybeScheduleStop(); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 352 | break; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 353 | case NsdManager.DISCOVER_SERVICES: |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 354 | cInfo = getClientInfoForReply(msg); |
| 355 | if (cInfo != null) { |
| 356 | cInfo.onDiscoverServicesFailed( |
| 357 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 358 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 359 | break; |
| 360 | case NsdManager.STOP_DISCOVERY: |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 361 | cInfo = getClientInfoForReply(msg); |
| 362 | if (cInfo != null) { |
| 363 | cInfo.onStopDiscoveryFailed( |
| 364 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 365 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 366 | break; |
| 367 | case NsdManager.REGISTER_SERVICE: |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 368 | cInfo = getClientInfoForReply(msg); |
| 369 | if (cInfo != null) { |
| 370 | cInfo.onRegisterServiceFailed( |
| 371 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 372 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 373 | break; |
| 374 | case NsdManager.UNREGISTER_SERVICE: |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 375 | cInfo = getClientInfoForReply(msg); |
| 376 | if (cInfo != null) { |
| 377 | cInfo.onUnregisterServiceFailed( |
| 378 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 379 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 380 | break; |
| 381 | case NsdManager.RESOLVE_SERVICE: |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 382 | cInfo = getClientInfoForReply(msg); |
| 383 | if (cInfo != null) { |
| 384 | cInfo.onResolveServiceFailed( |
| 385 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 386 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 387 | break; |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 388 | case NsdManager.DAEMON_CLEANUP: |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 389 | maybeStopDaemon(); |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 390 | break; |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 391 | // This event should be only sent by the legacy (target SDK < S) clients. |
| 392 | // Mark the sending client as legacy. |
| 393 | case NsdManager.DAEMON_STARTUP: |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 394 | cInfo = getClientInfoForReply(msg); |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 395 | if (cInfo != null) { |
| 396 | cancelStop(); |
| 397 | cInfo.setLegacy(); |
| 398 | mLegacyClientCount += 1; |
| 399 | maybeStartDaemon(); |
| 400 | } |
| 401 | break; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 402 | default: |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 403 | Log.e(TAG, "Unhandled " + msg); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 404 | return NOT_HANDLED; |
| 405 | } |
| 406 | return HANDLED; |
| 407 | } |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 408 | |
| 409 | private ClientInfo getClientInfoForReply(Message msg) { |
| 410 | final ListenerArgs args = (ListenerArgs) msg.obj; |
| 411 | return mClients.get(args.connector); |
| 412 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 415 | class EnabledState extends State { |
| 416 | @Override |
| 417 | public void enter() { |
| 418 | sendNsdStateChangeBroadcast(true); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void exit() { |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 423 | // TODO: it is incorrect to stop the daemon without expunging all requests |
| 424 | // and sending error callbacks to clients. |
Luke Huang | 92860f9 | 2021-06-23 06:29:30 +0000 | [diff] [blame] | 425 | scheduleStop(); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 428 | private boolean requestLimitReached(ClientInfo clientInfo) { |
| 429 | if (clientInfo.mClientIds.size() >= ClientInfo.MAX_LIMIT) { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 430 | if (DBG) Log.d(TAG, "Exceeded max outstanding requests " + clientInfo); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 431 | return true; |
| 432 | } |
| 433 | return false; |
| 434 | } |
| 435 | |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 436 | private void storeRequestMap(int clientId, int globalId, ClientInfo clientInfo, int what) { |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 437 | clientInfo.mClientIds.put(clientId, globalId); |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 438 | clientInfo.mClientRequests.put(clientId, what); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 439 | mIdToClientInfoMap.put(globalId, clientInfo); |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 440 | // Remove the cleanup event because here comes a new request. |
| 441 | cancelStop(); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) { |
Hugo Benichi | d2552ae | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 445 | clientInfo.mClientIds.delete(clientId); |
| 446 | clientInfo.mClientRequests.delete(clientId); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 447 | mIdToClientInfoMap.remove(globalId); |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 448 | maybeScheduleStop(); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 451 | private void storeListenerMap(int clientId, int transactionId, MdnsListener listener, |
| 452 | ClientInfo clientInfo) { |
| 453 | clientInfo.mClientIds.put(clientId, transactionId); |
| 454 | clientInfo.mListeners.put(clientId, listener); |
| 455 | mIdToClientInfoMap.put(transactionId, clientInfo); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | private void removeListenerMap(int clientId, int transactionId, ClientInfo clientInfo) { |
| 459 | clientInfo.mClientIds.delete(clientId); |
| 460 | clientInfo.mListeners.delete(clientId); |
| 461 | mIdToClientInfoMap.remove(transactionId); |
| 462 | maybeStopMonitoringSocketsIfNoActiveRequest(); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Check the given service type is valid and construct it to a service type |
| 467 | * which can use for discovery / resolution service. |
| 468 | * |
| 469 | * <p> The valid service type should be 2 labels, or 3 labels if the query is for a |
| 470 | * subtype (see RFC6763 7.1). Each label is up to 63 characters and must start with an |
| 471 | * underscore; they are alphanumerical characters or dashes or underscore, except the |
| 472 | * last one that is just alphanumerical. The last label must be _tcp or _udp. |
| 473 | * |
| 474 | * @param serviceType the request service type for discovery / resolution service |
| 475 | * @return constructed service type or null if the given service type is invalid. |
| 476 | */ |
| 477 | @Nullable |
| 478 | private String constructServiceType(String serviceType) { |
| 479 | if (TextUtils.isEmpty(serviceType)) return null; |
| 480 | |
| 481 | final Pattern serviceTypePattern = Pattern.compile( |
| 482 | "^(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\.)?" |
| 483 | + "(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\._(?:tcp|udp))$"); |
| 484 | final Matcher matcher = serviceTypePattern.matcher(serviceType); |
| 485 | if (!matcher.matches()) return null; |
| 486 | return matcher.group(1) == null |
| 487 | ? serviceType + ".local" |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 488 | : matcher.group(1) + "_sub." + matcher.group(2) + ".local"; |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 489 | } |
| 490 | |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 491 | @Override |
| 492 | public boolean processMessage(Message msg) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 493 | final ClientInfo clientInfo; |
| 494 | final int id; |
| 495 | final int clientId = msg.arg2; |
| 496 | final ListenerArgs args; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 497 | switch (msg.what) { |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 498 | case NsdManager.DISCOVER_SERVICES: { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 499 | if (DBG) Log.d(TAG, "Discover services"); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 500 | args = (ListenerArgs) msg.obj; |
| 501 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 502 | // If the binder death notification for a INsdManagerCallback was received |
| 503 | // before any calls are received by NsdService, the clientInfo would be |
| 504 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 505 | if (clientInfo == null) { |
| 506 | Log.e(TAG, "Unknown connector in discovery"); |
| 507 | break; |
| 508 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 509 | |
| 510 | if (requestLimitReached(clientInfo)) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 511 | clientInfo.onDiscoverServicesFailed( |
| 512 | clientId, NsdManager.FAILURE_MAX_LIMIT); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 513 | break; |
| 514 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 515 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 516 | final NsdServiceInfo info = args.serviceInfo; |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 517 | id = getUniqueId(); |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 518 | if (mMdnsDiscoveryManager != null) { |
| 519 | final String serviceType = constructServiceType(info.getServiceType()); |
| 520 | if (serviceType == null) { |
| 521 | clientInfo.onDiscoverServicesFailed(clientId, |
| 522 | NsdManager.FAILURE_INTERNAL_ERROR); |
| 523 | break; |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 524 | } |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 525 | |
| 526 | maybeStartMonitoringSockets(); |
| 527 | final MdnsListener listener = |
| 528 | new DiscoveryListener(clientId, id, info, serviceType); |
| 529 | final MdnsSearchOptions options = MdnsSearchOptions.newBuilder() |
| 530 | .setNetwork(info.getNetwork()) |
| 531 | .setIsPassiveMode(true) |
| 532 | .build(); |
| 533 | mMdnsDiscoveryManager.registerListener(serviceType, listener, options); |
| 534 | storeListenerMap(clientId, id, listener, clientInfo); |
| 535 | clientInfo.onDiscoverServicesStarted(clientId, info); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 536 | } else { |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 537 | maybeStartDaemon(); |
| 538 | if (discoverServices(id, info)) { |
| 539 | if (DBG) { |
| 540 | Log.d(TAG, "Discover " + msg.arg2 + " " + id |
| 541 | + info.getServiceType()); |
| 542 | } |
| 543 | storeRequestMap(clientId, id, clientInfo, msg.what); |
| 544 | clientInfo.onDiscoverServicesStarted(clientId, info); |
| 545 | } else { |
| 546 | stopServiceDiscovery(id); |
| 547 | clientInfo.onDiscoverServicesFailed(clientId, |
| 548 | NsdManager.FAILURE_INTERNAL_ERROR); |
| 549 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 550 | } |
| 551 | break; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 552 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 553 | case NsdManager.STOP_DISCOVERY: |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 554 | if (DBG) Log.d(TAG, "Stop service discovery"); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 555 | args = (ListenerArgs) msg.obj; |
| 556 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 557 | // If the binder death notification for a INsdManagerCallback was received |
| 558 | // before any calls are received by NsdService, the clientInfo would be |
| 559 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 560 | if (clientInfo == null) { |
| 561 | Log.e(TAG, "Unknown connector in stop discovery"); |
| 562 | break; |
| 563 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 564 | |
| 565 | try { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 566 | id = clientInfo.mClientIds.get(clientId); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 567 | } catch (NullPointerException e) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 568 | clientInfo.onStopDiscoveryFailed( |
| 569 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 570 | break; |
| 571 | } |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 572 | if (mMdnsDiscoveryManager != null) { |
| 573 | final MdnsListener listener = clientInfo.mListeners.get(clientId); |
| 574 | if (listener == null) { |
| 575 | clientInfo.onStopDiscoveryFailed( |
| 576 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 577 | break; |
| 578 | } |
| 579 | mMdnsDiscoveryManager.unregisterListener( |
| 580 | listener.getListenedServiceType(), listener); |
| 581 | removeListenerMap(clientId, id, clientInfo); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 582 | clientInfo.onStopDiscoverySucceeded(clientId); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 583 | } else { |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 584 | removeRequestMap(clientId, id, clientInfo); |
| 585 | if (stopServiceDiscovery(id)) { |
| 586 | clientInfo.onStopDiscoverySucceeded(clientId); |
| 587 | } else { |
| 588 | clientInfo.onStopDiscoveryFailed( |
| 589 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 590 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 591 | } |
| 592 | break; |
| 593 | case NsdManager.REGISTER_SERVICE: |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 594 | if (DBG) Log.d(TAG, "Register service"); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 595 | args = (ListenerArgs) msg.obj; |
| 596 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 597 | // If the binder death notification for a INsdManagerCallback was received |
| 598 | // before any calls are received by NsdService, the clientInfo would be |
| 599 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 600 | if (clientInfo == null) { |
| 601 | Log.e(TAG, "Unknown connector in registration"); |
| 602 | break; |
| 603 | } |
| 604 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 605 | if (requestLimitReached(clientInfo)) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 606 | clientInfo.onRegisterServiceFailed( |
| 607 | clientId, NsdManager.FAILURE_MAX_LIMIT); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 608 | break; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 611 | maybeStartDaemon(); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 612 | id = getUniqueId(); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 613 | if (registerService(id, args.serviceInfo)) { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 614 | if (DBG) Log.d(TAG, "Register " + clientId + " " + id); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 615 | storeRequestMap(clientId, id, clientInfo, msg.what); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 616 | // Return success after mDns reports success |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 617 | } else { |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 618 | unregisterService(id); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 619 | clientInfo.onRegisterServiceFailed( |
| 620 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 621 | } |
| 622 | break; |
| 623 | case NsdManager.UNREGISTER_SERVICE: |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 624 | if (DBG) Log.d(TAG, "unregister service"); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 625 | args = (ListenerArgs) msg.obj; |
| 626 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 627 | // If the binder death notification for a INsdManagerCallback was received |
| 628 | // before any calls are received by NsdService, the clientInfo would be |
| 629 | // 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] | 630 | if (clientInfo == null) { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 631 | Log.e(TAG, "Unknown connector in unregistration"); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 632 | break; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 633 | } |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 634 | id = clientInfo.mClientIds.get(clientId); |
| 635 | removeRequestMap(clientId, id, clientInfo); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 636 | if (unregisterService(id)) { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 637 | clientInfo.onUnregisterServiceSucceeded(clientId); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 638 | } else { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 639 | clientInfo.onUnregisterServiceFailed( |
| 640 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 641 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 642 | break; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 643 | case NsdManager.RESOLVE_SERVICE: { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 644 | if (DBG) Log.d(TAG, "Resolve service"); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 645 | args = (ListenerArgs) msg.obj; |
| 646 | clientInfo = mClients.get(args.connector); |
Paul Hu | 116b4c0 | 2022-08-16 07:21:55 +0000 | [diff] [blame] | 647 | // If the binder death notification for a INsdManagerCallback was received |
| 648 | // before any calls are received by NsdService, the clientInfo would be |
| 649 | // cleared and cause NPE. Add a null check here to prevent this corner case. |
| 650 | if (clientInfo == null) { |
| 651 | Log.e(TAG, "Unknown connector in resolution"); |
| 652 | break; |
| 653 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 654 | |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 655 | final NsdServiceInfo info = args.serviceInfo; |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 656 | id = getUniqueId(); |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 657 | if (mMdnsDiscoveryManager != null) { |
| 658 | final String serviceType = constructServiceType(info.getServiceType()); |
| 659 | if (serviceType == null) { |
| 660 | clientInfo.onResolveServiceFailed(clientId, |
| 661 | NsdManager.FAILURE_INTERNAL_ERROR); |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | maybeStartMonitoringSockets(); |
| 666 | final MdnsListener listener = |
| 667 | new ResolutionListener(clientId, id, info, serviceType); |
| 668 | final MdnsSearchOptions options = MdnsSearchOptions.newBuilder() |
| 669 | .setNetwork(info.getNetwork()) |
| 670 | .setIsPassiveMode(true) |
| 671 | .build(); |
| 672 | mMdnsDiscoveryManager.registerListener(serviceType, listener, options); |
| 673 | storeListenerMap(clientId, id, listener, clientInfo); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 674 | } else { |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 675 | if (clientInfo.mResolvedService != null) { |
| 676 | clientInfo.onResolveServiceFailed( |
| 677 | clientId, NsdManager.FAILURE_ALREADY_ACTIVE); |
| 678 | break; |
| 679 | } |
| 680 | |
| 681 | maybeStartDaemon(); |
| 682 | if (resolveService(id, args.serviceInfo)) { |
| 683 | clientInfo.mResolvedService = new NsdServiceInfo(); |
| 684 | storeRequestMap(clientId, id, clientInfo, msg.what); |
| 685 | } else { |
| 686 | clientInfo.onResolveServiceFailed( |
| 687 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 688 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 689 | } |
| 690 | break; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 691 | } |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 692 | case MDNS_SERVICE_EVENT: |
| 693 | if (!handleMDnsServiceEvent(msg.arg1, msg.arg2, msg.obj)) { |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 694 | return NOT_HANDLED; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 695 | } |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 696 | break; |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 697 | case MDNS_DISCOVERY_MANAGER_EVENT: |
| 698 | if (!handleMdnsDiscoveryManagerEvent(msg.arg1, msg.arg2, msg.obj)) { |
| 699 | return NOT_HANDLED; |
| 700 | } |
| 701 | break; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 702 | default: |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 703 | return NOT_HANDLED; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 704 | } |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 705 | return HANDLED; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 706 | } |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 707 | |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 708 | private boolean handleMDnsServiceEvent(int code, int id, Object obj) { |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 709 | NsdServiceInfo servInfo; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 710 | ClientInfo clientInfo = mIdToClientInfoMap.get(id); |
| 711 | if (clientInfo == null) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 712 | Log.e(TAG, String.format("id %d for %d has no client mapping", id, code)); |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 713 | return false; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* This goes in response as msg.arg2 */ |
Christopher Lane | 7441122 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 717 | int clientId = clientInfo.getClientId(id); |
| 718 | if (clientId < 0) { |
Vinit Deshapnde | 930a851 | 2013-06-25 19:45:03 -0700 | [diff] [blame] | 719 | // This can happen because of race conditions. For example, |
| 720 | // SERVICE_FOUND may race with STOP_SERVICE_DISCOVERY, |
| 721 | // and we may get in this situation. |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 722 | Log.d(TAG, String.format("%d for listener id %d that is no longer active", |
| 723 | code, id)); |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 724 | return false; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 725 | } |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 726 | if (DBG) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 727 | Log.d(TAG, String.format("MDns service event code:%d id=%d", code, id)); |
Hugo Benichi | 32be63d | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 728 | } |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 729 | switch (code) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 730 | case IMDnsEventListener.SERVICE_FOUND: { |
| 731 | final DiscoveryInfo info = (DiscoveryInfo) obj; |
| 732 | final String name = info.serviceName; |
| 733 | final String type = info.registrationType; |
| 734 | servInfo = new NsdServiceInfo(name, type); |
| 735 | final int foundNetId = info.netId; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 736 | if (foundNetId == 0L) { |
| 737 | // Ignore services that do not have a Network: they are not usable |
| 738 | // by apps, as they would need privileged permissions to use |
| 739 | // interfaces that do not have an associated Network. |
| 740 | break; |
| 741 | } |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 742 | setServiceNetworkForCallback(servInfo, info.netId, info.interfaceIdx); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 743 | clientInfo.onServiceFound(clientId, servInfo); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 744 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 745 | } |
| 746 | case IMDnsEventListener.SERVICE_LOST: { |
| 747 | final DiscoveryInfo info = (DiscoveryInfo) obj; |
| 748 | final String name = info.serviceName; |
| 749 | final String type = info.registrationType; |
| 750 | final int lostNetId = info.netId; |
| 751 | servInfo = new NsdServiceInfo(name, type); |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 752 | // The network could be set to null (netId 0) if it was torn down when the |
| 753 | // service is lost |
| 754 | // TODO: avoid returning null in that case, possibly by remembering |
| 755 | // found services on the same interface index and their network at the time |
| 756 | setServiceNetworkForCallback(servInfo, lostNetId, info.interfaceIdx); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 757 | clientInfo.onServiceLost(clientId, servInfo); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 758 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 759 | } |
| 760 | case IMDnsEventListener.SERVICE_DISCOVERY_FAILED: |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 761 | clientInfo.onDiscoverServicesFailed( |
| 762 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 763 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 764 | case IMDnsEventListener.SERVICE_REGISTERED: { |
| 765 | final RegistrationInfo info = (RegistrationInfo) obj; |
| 766 | final String name = info.serviceName; |
| 767 | servInfo = new NsdServiceInfo(name, null /* serviceType */); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 768 | clientInfo.onRegisterServiceSucceeded(clientId, servInfo); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 769 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 770 | } |
| 771 | case IMDnsEventListener.SERVICE_REGISTRATION_FAILED: |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 772 | clientInfo.onRegisterServiceFailed( |
| 773 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 774 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 775 | case IMDnsEventListener.SERVICE_RESOLVED: { |
| 776 | final ResolutionInfo info = (ResolutionInfo) obj; |
Sreeram Ramachandran | a53dd7f | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 777 | int index = 0; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 778 | final String fullName = info.serviceFullName; |
| 779 | while (index < fullName.length() && fullName.charAt(index) != '.') { |
| 780 | if (fullName.charAt(index) == '\\') { |
Sreeram Ramachandran | a53dd7f | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 781 | ++index; |
| 782 | } |
| 783 | ++index; |
| 784 | } |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 785 | if (index >= fullName.length()) { |
| 786 | Log.e(TAG, "Invalid service found " + fullName); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 787 | break; |
| 788 | } |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 789 | |
paulhu | be18660 | 2022-04-12 07:18:23 +0000 | [diff] [blame] | 790 | String name = unescape(fullName.substring(0, index)); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 791 | String rest = fullName.substring(index); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 792 | String type = rest.replace(".local.", ""); |
| 793 | |
| 794 | clientInfo.mResolvedService.setServiceName(name); |
| 795 | clientInfo.mResolvedService.setServiceType(type); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 796 | clientInfo.mResolvedService.setPort(info.port); |
| 797 | clientInfo.mResolvedService.setTxtRecords(info.txtRecord); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 798 | // Network will be added after SERVICE_GET_ADDR_SUCCESS |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 799 | |
| 800 | stopResolveService(id); |
Vinit Deshapnde | 4429e87 | 2013-11-12 15:36:37 -0800 | [diff] [blame] | 801 | removeRequestMap(clientId, id, clientInfo); |
| 802 | |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 803 | final int id2 = getUniqueId(); |
| 804 | if (getAddrInfo(id2, info.hostname, info.interfaceIdx)) { |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 805 | storeRequestMap(clientId, id2, clientInfo, NsdManager.RESOLVE_SERVICE); |
Vinit Deshapnde | 4429e87 | 2013-11-12 15:36:37 -0800 | [diff] [blame] | 806 | } else { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 807 | clientInfo.onResolveServiceFailed( |
| 808 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 809 | clientInfo.mResolvedService = null; |
| 810 | } |
| 811 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 812 | } |
| 813 | case IMDnsEventListener.SERVICE_RESOLUTION_FAILED: |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 814 | /* NNN resolveId errorCode */ |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 815 | stopResolveService(id); |
| 816 | removeRequestMap(clientId, id, clientInfo); |
| 817 | clientInfo.mResolvedService = null; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 818 | clientInfo.onResolveServiceFailed( |
| 819 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 820 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 821 | case IMDnsEventListener.SERVICE_GET_ADDR_FAILED: |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 822 | /* NNN resolveId errorCode */ |
| 823 | stopGetAddrInfo(id); |
| 824 | removeRequestMap(clientId, id, clientInfo); |
| 825 | clientInfo.mResolvedService = null; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 826 | clientInfo.onResolveServiceFailed( |
| 827 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 828 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 829 | case IMDnsEventListener.SERVICE_GET_ADDR_SUCCESS: { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 830 | /* NNN resolveId hostname ttl addr interfaceIdx netId */ |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 831 | final GetAddressInfo info = (GetAddressInfo) obj; |
| 832 | final String address = info.address; |
| 833 | final int netId = info.netId; |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 834 | InetAddress serviceHost = null; |
| 835 | try { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 836 | serviceHost = InetAddress.getByName(address); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 837 | } catch (UnknownHostException e) { |
| 838 | Log.wtf(TAG, "Invalid host in GET_ADDR_SUCCESS", e); |
| 839 | } |
| 840 | |
| 841 | // If the resolved service is on an interface without a network, consider it |
| 842 | // as a failure: it would not be usable by apps as they would need |
| 843 | // privileged permissions. |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 844 | if (netId != NETID_UNSET && serviceHost != null) { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 845 | clientInfo.mResolvedService.setHost(serviceHost); |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 846 | setServiceNetworkForCallback(clientInfo.mResolvedService, |
| 847 | netId, info.interfaceIdx); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 848 | clientInfo.onResolveServiceSucceeded( |
| 849 | clientId, clientInfo.mResolvedService); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 850 | } else { |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 851 | clientInfo.onResolveServiceFailed( |
| 852 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 853 | } |
| 854 | stopGetAddrInfo(id); |
| 855 | removeRequestMap(clientId, id, clientInfo); |
| 856 | clientInfo.mResolvedService = null; |
| 857 | break; |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 858 | } |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 859 | default: |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 860 | return false; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 861 | } |
Hugo Benichi | f0c8409 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 862 | return true; |
Vairavan Srinivasan | 6ce4818 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 863 | } |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 864 | |
| 865 | private NsdServiceInfo buildNsdServiceInfoFromMdnsEvent(final MdnsEvent event) { |
| 866 | final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo; |
| 867 | final String serviceType = event.mRequestedServiceType; |
| 868 | final String serviceName = serviceInfo.getServiceInstanceName(); |
| 869 | final NsdServiceInfo servInfo = new NsdServiceInfo(serviceName, serviceType); |
| 870 | final Network network = serviceInfo.getNetwork(); |
| 871 | setServiceNetworkForCallback( |
| 872 | servInfo, |
| 873 | network == null ? NETID_UNSET : network.netId, |
| 874 | serviceInfo.getInterfaceIndex()); |
| 875 | return servInfo; |
| 876 | } |
| 877 | |
| 878 | private boolean handleMdnsDiscoveryManagerEvent( |
| 879 | int transactionId, int code, Object obj) { |
| 880 | final ClientInfo clientInfo = mIdToClientInfoMap.get(transactionId); |
| 881 | if (clientInfo == null) { |
| 882 | Log.e(TAG, String.format( |
| 883 | "id %d for %d has no client mapping", transactionId, code)); |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | final MdnsEvent event = (MdnsEvent) obj; |
| 888 | final int clientId = event.mClientId; |
Paul Hu | 319751a | 2023-01-13 23:56:34 +0800 | [diff] [blame] | 889 | final NsdServiceInfo info = buildNsdServiceInfoFromMdnsEvent(event); |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 890 | if (DBG) { |
| 891 | Log.d(TAG, String.format("MdnsDiscoveryManager event code=%s transactionId=%d", |
| 892 | NsdManager.nameOf(code), transactionId)); |
| 893 | } |
| 894 | switch (code) { |
| 895 | case NsdManager.SERVICE_FOUND: |
Paul Hu | 319751a | 2023-01-13 23:56:34 +0800 | [diff] [blame] | 896 | clientInfo.onServiceFound(clientId, info); |
| 897 | break; |
| 898 | case NsdManager.SERVICE_LOST: |
| 899 | clientInfo.onServiceLost(clientId, info); |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 900 | break; |
Paul Hu | 75069ed | 2023-01-14 00:31:09 +0800 | [diff] [blame^] | 901 | case NsdManager.RESOLVE_SERVICE_SUCCEEDED: { |
| 902 | final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo; |
| 903 | // Add '.' in front of the service type that aligns with historical behavior |
| 904 | info.setServiceType("." + event.mRequestedServiceType); |
| 905 | info.setPort(serviceInfo.getPort()); |
| 906 | |
| 907 | Map<String, String> attrs = serviceInfo.getAttributes(); |
| 908 | for (Map.Entry<String, String> kv : attrs.entrySet()) { |
| 909 | final String key = kv.getKey(); |
| 910 | try { |
| 911 | info.setAttribute(key, serviceInfo.getAttributeAsBytes(key)); |
| 912 | } catch (IllegalArgumentException e) { |
| 913 | Log.e(TAG, "Invalid attribute", e); |
| 914 | } |
| 915 | } |
| 916 | try { |
| 917 | if (serviceInfo.getIpv4Address() != null) { |
| 918 | info.setHost(InetAddresses.parseNumericAddress( |
| 919 | serviceInfo.getIpv4Address())); |
| 920 | } else { |
| 921 | info.setHost(InetAddresses.parseNumericAddress( |
| 922 | serviceInfo.getIpv6Address())); |
| 923 | } |
| 924 | clientInfo.onResolveServiceSucceeded(clientId, info); |
| 925 | } catch (IllegalArgumentException e) { |
| 926 | Log.wtf(TAG, "Invalid address in RESOLVE_SERVICE_SUCCEEDED", e); |
| 927 | clientInfo.onResolveServiceFailed( |
| 928 | clientId, NsdManager.FAILURE_INTERNAL_ERROR); |
| 929 | } |
| 930 | |
| 931 | // Unregister the listener immediately like IMDnsEventListener design |
| 932 | final MdnsListener listener = clientInfo.mListeners.get(clientId); |
| 933 | mMdnsDiscoveryManager.unregisterListener( |
| 934 | listener.getListenedServiceType(), listener); |
| 935 | removeListenerMap(clientId, transactionId, clientInfo); |
| 936 | break; |
| 937 | } |
Paul Hu | 019621e | 2023-01-13 23:26:49 +0800 | [diff] [blame] | 938 | default: |
| 939 | return false; |
| 940 | } |
| 941 | return true; |
| 942 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 945 | |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 946 | private static void setServiceNetworkForCallback(NsdServiceInfo info, int netId, int ifaceIdx) { |
| 947 | switch (netId) { |
| 948 | case NETID_UNSET: |
| 949 | info.setNetwork(null); |
| 950 | break; |
| 951 | case INetd.LOCAL_NET_ID: |
| 952 | // Special case for LOCAL_NET_ID: Networks on netId 99 are not generally |
| 953 | // visible / usable for apps, so do not return it. Store the interface |
| 954 | // index instead, so at least if the client tries to resolve the service |
| 955 | // with that NsdServiceInfo, it will be done on the same interface. |
| 956 | // If they recreate the NsdServiceInfo themselves, resolution would be |
| 957 | // done on all interfaces as before T, which should also work. |
| 958 | info.setNetwork(null); |
| 959 | info.setInterfaceIndex(ifaceIdx); |
| 960 | break; |
| 961 | default: |
| 962 | info.setNetwork(new Network(netId)); |
| 963 | } |
| 964 | } |
| 965 | |
paulhu | be18660 | 2022-04-12 07:18:23 +0000 | [diff] [blame] | 966 | // The full service name is escaped from standard DNS rules on mdnsresponder, making it suitable |
| 967 | // for passing to standard system DNS APIs such as res_query() . Thus, make the service name |
| 968 | // unescape for getting right service address. See "Notes on DNS Name Escaping" on |
| 969 | // external/mdnsresponder/mDNSShared/dns_sd.h for more details. |
| 970 | private String unescape(String s) { |
| 971 | StringBuilder sb = new StringBuilder(s.length()); |
| 972 | for (int i = 0; i < s.length(); ++i) { |
| 973 | char c = s.charAt(i); |
| 974 | if (c == '\\') { |
| 975 | if (++i >= s.length()) { |
| 976 | Log.e(TAG, "Unexpected end of escape sequence in: " + s); |
| 977 | break; |
| 978 | } |
| 979 | c = s.charAt(i); |
| 980 | if (c != '.' && c != '\\') { |
| 981 | if (i + 2 >= s.length()) { |
| 982 | Log.e(TAG, "Unexpected end of escape sequence in: " + s); |
| 983 | break; |
| 984 | } |
| 985 | c = (char) ((c - '0') * 100 + (s.charAt(i + 1) - '0') * 10 |
| 986 | + (s.charAt(i + 2) - '0')); |
| 987 | i += 2; |
| 988 | } |
| 989 | } |
| 990 | sb.append(c); |
| 991 | } |
| 992 | return sb.toString(); |
| 993 | } |
| 994 | |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 995 | @VisibleForTesting |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 996 | NsdService(Context ctx, Handler handler, long cleanupDelayMs) { |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 997 | this(ctx, handler, cleanupDelayMs, new Dependencies()); |
| 998 | } |
| 999 | |
| 1000 | @VisibleForTesting |
| 1001 | NsdService(Context ctx, Handler handler, long cleanupDelayMs, Dependencies deps) { |
Luke Huang | 0529858 | 2021-06-13 16:52:05 +0000 | [diff] [blame] | 1002 | mCleanupDelayMs = cleanupDelayMs; |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1003 | mContext = ctx; |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1004 | mNsdStateMachine = new NsdStateMachine(TAG, handler); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1005 | mNsdStateMachine.start(); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1006 | mMDnsManager = ctx.getSystemService(MDnsManager.class); |
| 1007 | mMDnsEventCallback = new MDnsEventCallback(mNsdStateMachine); |
Paul Hu | 4bd98ef | 2023-01-12 13:42:07 +0800 | [diff] [blame] | 1008 | if (deps.isMdnsDiscoveryManagerEnabled(ctx)) { |
| 1009 | mMdnsSocketProvider = deps.makeMdnsSocketProvider(ctx, handler.getLooper()); |
| 1010 | mMdnsSocketClient = |
| 1011 | new MdnsMultinetworkSocketClient(handler.getLooper(), mMdnsSocketProvider); |
| 1012 | mMdnsDiscoveryManager = |
| 1013 | deps.makeMdnsDiscoveryManager(new ExecutorProvider(), mMdnsSocketClient); |
| 1014 | handler.post(() -> mMdnsSocketClient.setCallback(mMdnsDiscoveryManager)); |
| 1015 | } else { |
| 1016 | mMdnsSocketProvider = null; |
| 1017 | mMdnsSocketClient = null; |
| 1018 | mMdnsDiscoveryManager = null; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * Dependencies of NsdService, for injection in tests. |
| 1024 | */ |
| 1025 | @VisibleForTesting |
| 1026 | public static class Dependencies { |
| 1027 | /** |
| 1028 | * Check whether or not MdnsDiscoveryManager feature is enabled. |
| 1029 | * |
| 1030 | * @param context The global context information about an app environment. |
| 1031 | * @return true if MdnsDiscoveryManager feature is enabled. |
| 1032 | */ |
| 1033 | public boolean isMdnsDiscoveryManagerEnabled(Context context) { |
| 1034 | return DeviceConfigUtils.isFeatureEnabled(context, NAMESPACE_CONNECTIVITY, |
| 1035 | MDNS_DISCOVERY_MANAGER_VERSION, false /* defaultEnabled */); |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * @see MdnsDiscoveryManager |
| 1040 | */ |
| 1041 | public MdnsDiscoveryManager makeMdnsDiscoveryManager( |
| 1042 | ExecutorProvider executorProvider, MdnsSocketClientBase socketClient) { |
| 1043 | return new MdnsDiscoveryManager(executorProvider, socketClient); |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * @see MdnsSocketProvider |
| 1048 | */ |
| 1049 | public MdnsSocketProvider makeMdnsSocketProvider(Context context, Looper looper) { |
| 1050 | return new MdnsSocketProvider(context, looper); |
| 1051 | } |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
paulhu | 1b35e82 | 2022-04-08 14:48:41 +0800 | [diff] [blame] | 1054 | public static NsdService create(Context context) { |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1055 | HandlerThread thread = new HandlerThread(TAG); |
| 1056 | thread.start(); |
| 1057 | Handler handler = new Handler(thread.getLooper()); |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1058 | NsdService service = new NsdService(context, handler, CLEANUP_DELAY_MS); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1059 | return service; |
| 1060 | } |
| 1061 | |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1062 | private static class MDnsEventCallback extends IMDnsEventListener.Stub { |
| 1063 | private final StateMachine mStateMachine; |
| 1064 | |
| 1065 | MDnsEventCallback(StateMachine sm) { |
| 1066 | mStateMachine = sm; |
| 1067 | } |
| 1068 | |
| 1069 | @Override |
| 1070 | public void onServiceRegistrationStatus(final RegistrationInfo status) { |
| 1071 | mStateMachine.sendMessage( |
| 1072 | MDNS_SERVICE_EVENT, status.result, status.id, status); |
| 1073 | } |
| 1074 | |
| 1075 | @Override |
| 1076 | public void onServiceDiscoveryStatus(final DiscoveryInfo status) { |
| 1077 | mStateMachine.sendMessage( |
| 1078 | MDNS_SERVICE_EVENT, status.result, status.id, status); |
| 1079 | } |
| 1080 | |
| 1081 | @Override |
| 1082 | public void onServiceResolutionStatus(final ResolutionInfo status) { |
| 1083 | mStateMachine.sendMessage( |
| 1084 | MDNS_SERVICE_EVENT, status.result, status.id, status); |
| 1085 | } |
| 1086 | |
| 1087 | @Override |
| 1088 | public void onGettingServiceAddressStatus(final GetAddressInfo status) { |
| 1089 | mStateMachine.sendMessage( |
| 1090 | MDNS_SERVICE_EVENT, status.result, status.id, status); |
| 1091 | } |
| 1092 | |
| 1093 | @Override |
| 1094 | public int getInterfaceVersion() throws RemoteException { |
| 1095 | return this.VERSION; |
| 1096 | } |
| 1097 | |
| 1098 | @Override |
| 1099 | public String getInterfaceHash() throws RemoteException { |
| 1100 | return this.HASH; |
| 1101 | } |
| 1102 | } |
| 1103 | |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 1104 | @Override |
| 1105 | public INsdServiceConnector connect(INsdManagerCallback cb) { |
Hugo Benichi | 803a2f0 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1106 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INTERNET, "NsdService"); |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 1107 | final INsdServiceConnector connector = new NsdServiceConnector(); |
| 1108 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 1109 | NsdManager.REGISTER_CLIENT, new Pair<>(connector, cb))); |
| 1110 | return connector; |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 1113 | private static class ListenerArgs { |
| 1114 | public final NsdServiceConnector connector; |
| 1115 | public final NsdServiceInfo serviceInfo; |
| 1116 | ListenerArgs(NsdServiceConnector connector, NsdServiceInfo serviceInfo) { |
| 1117 | this.connector = connector; |
| 1118 | this.serviceInfo = serviceInfo; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | private class NsdServiceConnector extends INsdServiceConnector.Stub |
| 1123 | implements IBinder.DeathRecipient { |
| 1124 | @Override |
| 1125 | public void registerService(int listenerKey, NsdServiceInfo serviceInfo) { |
| 1126 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 1127 | NsdManager.REGISTER_SERVICE, 0, listenerKey, |
| 1128 | new ListenerArgs(this, serviceInfo))); |
| 1129 | } |
| 1130 | |
| 1131 | @Override |
| 1132 | public void unregisterService(int listenerKey) { |
| 1133 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 1134 | NsdManager.UNREGISTER_SERVICE, 0, listenerKey, |
| 1135 | new ListenerArgs(this, null))); |
| 1136 | } |
| 1137 | |
| 1138 | @Override |
| 1139 | public void discoverServices(int listenerKey, NsdServiceInfo serviceInfo) { |
| 1140 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 1141 | NsdManager.DISCOVER_SERVICES, 0, listenerKey, |
| 1142 | new ListenerArgs(this, serviceInfo))); |
| 1143 | } |
| 1144 | |
| 1145 | @Override |
| 1146 | public void stopDiscovery(int listenerKey) { |
| 1147 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 1148 | NsdManager.STOP_DISCOVERY, 0, listenerKey, new ListenerArgs(this, null))); |
| 1149 | } |
| 1150 | |
| 1151 | @Override |
| 1152 | public void resolveService(int listenerKey, NsdServiceInfo serviceInfo) { |
| 1153 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 1154 | NsdManager.RESOLVE_SERVICE, 0, listenerKey, |
| 1155 | new ListenerArgs(this, serviceInfo))); |
| 1156 | } |
| 1157 | |
| 1158 | @Override |
| 1159 | public void startDaemon() { |
| 1160 | mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage( |
| 1161 | NsdManager.DAEMON_STARTUP, new ListenerArgs(this, null))); |
| 1162 | } |
| 1163 | |
| 1164 | @Override |
| 1165 | public void binderDied() { |
| 1166 | mNsdStateMachine.sendMessage( |
| 1167 | mNsdStateMachine.obtainMessage(NsdManager.UNREGISTER_CLIENT, this)); |
| 1168 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Hugo Benichi | 912db99 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 1171 | private void sendNsdStateChangeBroadcast(boolean isEnabled) { |
Irfan Sheriff | 52fc83a | 2012-04-19 10:26:34 -0700 | [diff] [blame] | 1172 | final Intent intent = new Intent(NsdManager.ACTION_NSD_STATE_CHANGED); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1173 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Hugo Benichi | 912db99 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 1174 | int nsdState = isEnabled ? NsdManager.NSD_STATE_ENABLED : NsdManager.NSD_STATE_DISABLED; |
| 1175 | intent.putExtra(NsdManager.EXTRA_NSD_STATE, nsdState); |
Dianne Hackborn | 692107e | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1176 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1179 | private int getUniqueId() { |
| 1180 | if (++mUniqueId == INVALID_ID) return ++mUniqueId; |
| 1181 | return mUniqueId; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1184 | private boolean registerService(int regId, NsdServiceInfo service) { |
Hugo Benichi | 6d70644 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 1185 | if (DBG) { |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 1186 | Log.d(TAG, "registerService: " + regId + " " + service); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1187 | } |
Hugo Benichi | 6d70644 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 1188 | String name = service.getServiceName(); |
| 1189 | String type = service.getServiceType(); |
| 1190 | int port = service.getPort(); |
| 1191 | byte[] textRecord = service.getTxtRecord(); |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 1192 | final int registerInterface = getNetworkInterfaceIndex(service); |
| 1193 | if (service.getNetwork() != null && registerInterface == IFACE_IDX_ANY) { |
Paul Hu | 360a8e9 | 2022-04-26 11:14:14 +0800 | [diff] [blame] | 1194 | Log.e(TAG, "Interface to register service on not found"); |
| 1195 | return false; |
| 1196 | } |
| 1197 | return mMDnsManager.registerService(regId, name, type, port, textRecord, registerInterface); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1200 | private boolean unregisterService(int regId) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1201 | return mMDnsManager.stopOperation(regId); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1204 | private boolean discoverServices(int discoveryId, NsdServiceInfo serviceInfo) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1205 | final String type = serviceInfo.getServiceType(); |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 1206 | final int discoverInterface = getNetworkInterfaceIndex(serviceInfo); |
| 1207 | if (serviceInfo.getNetwork() != null && discoverInterface == IFACE_IDX_ANY) { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1208 | Log.e(TAG, "Interface to discover service on not found"); |
| 1209 | return false; |
| 1210 | } |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1211 | return mMDnsManager.discover(discoveryId, type, discoverInterface); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1214 | private boolean stopServiceDiscovery(int discoveryId) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1215 | return mMDnsManager.stopOperation(discoveryId); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1218 | private boolean resolveService(int resolveId, NsdServiceInfo service) { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1219 | final String name = service.getServiceName(); |
| 1220 | final String type = service.getServiceType(); |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 1221 | final int resolveInterface = getNetworkInterfaceIndex(service); |
| 1222 | if (service.getNetwork() != null && resolveInterface == IFACE_IDX_ANY) { |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1223 | Log.e(TAG, "Interface to resolve service on not found"); |
| 1224 | return false; |
| 1225 | } |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1226 | return mMDnsManager.resolve(resolveId, name, type, "local.", resolveInterface); |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * Guess the interface to use to resolve or discover a service on a specific network. |
| 1231 | * |
| 1232 | * This is an imperfect guess, as for example the network may be gone or not yet fully |
| 1233 | * registered. This is fine as failing is correct if the network is gone, and a client |
| 1234 | * attempting to resolve/discover on a network not yet setup would have a bad time anyway; also |
| 1235 | * this is to support the legacy mdnsresponder implementation, which historically resolved |
| 1236 | * services on an unspecified network. |
| 1237 | */ |
Remi NGUYEN VAN | 1a8ee10 | 2022-05-30 12:42:24 +0900 | [diff] [blame] | 1238 | private int getNetworkInterfaceIndex(NsdServiceInfo serviceInfo) { |
| 1239 | final Network network = serviceInfo.getNetwork(); |
| 1240 | if (network == null) { |
| 1241 | // Fallback to getInterfaceIndex if present (typically if the NsdServiceInfo was |
| 1242 | // provided by NsdService from discovery results, and the service was found on an |
| 1243 | // interface that has no app-usable Network). |
| 1244 | if (serviceInfo.getInterfaceIndex() != 0) { |
| 1245 | return serviceInfo.getInterfaceIndex(); |
| 1246 | } |
| 1247 | return IFACE_IDX_ANY; |
| 1248 | } |
Remi NGUYEN VAN | 2365130 | 2021-12-16 15:31:16 +0900 | [diff] [blame] | 1249 | |
| 1250 | final ConnectivityManager cm = mContext.getSystemService(ConnectivityManager.class); |
| 1251 | if (cm == null) { |
| 1252 | Log.wtf(TAG, "No ConnectivityManager for resolveService"); |
| 1253 | return IFACE_IDX_ANY; |
| 1254 | } |
| 1255 | final LinkProperties lp = cm.getLinkProperties(network); |
| 1256 | if (lp == null) return IFACE_IDX_ANY; |
| 1257 | |
| 1258 | // Only resolve on non-stacked interfaces |
| 1259 | final NetworkInterface iface; |
| 1260 | try { |
| 1261 | iface = NetworkInterface.getByName(lp.getInterfaceName()); |
| 1262 | } catch (SocketException e) { |
| 1263 | Log.e(TAG, "Error querying interface", e); |
| 1264 | return IFACE_IDX_ANY; |
| 1265 | } |
| 1266 | |
| 1267 | if (iface == null) { |
| 1268 | Log.e(TAG, "Interface not found: " + lp.getInterfaceName()); |
| 1269 | return IFACE_IDX_ANY; |
| 1270 | } |
| 1271 | |
| 1272 | return iface.getIndex(); |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | private boolean stopResolveService(int resolveId) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1276 | return mMDnsManager.stopOperation(resolveId); |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1279 | private boolean getAddrInfo(int resolveId, String hostname, int interfaceIdx) { |
| 1280 | return mMDnsManager.getServiceAddress(resolveId, hostname, interfaceIdx); |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | private boolean stopGetAddrInfo(int resolveId) { |
paulhu | 2b9ed95 | 2022-02-10 21:58:32 +0800 | [diff] [blame] | 1284 | return mMDnsManager.stopOperation(resolveId); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | @Override |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1288 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
paulhu | 3ffffe7 | 2021-09-16 10:15:22 +0800 | [diff] [blame] | 1289 | if (!PermissionUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1290 | |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1291 | for (ClientInfo client : mClients.values()) { |
| 1292 | pw.println("Client Info"); |
| 1293 | pw.println(client); |
| 1294 | } |
| 1295 | |
| 1296 | mNsdStateMachine.dump(fd, pw, args); |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1297 | } |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1298 | |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1299 | /* Information tracked per client */ |
| 1300 | private class ClientInfo { |
| 1301 | |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1302 | private static final int MAX_LIMIT = 10; |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 1303 | private final INsdManagerCallback mCb; |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1304 | /* Remembers a resolved service until getaddrinfo completes */ |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1305 | private NsdServiceInfo mResolvedService; |
| 1306 | |
| 1307 | /* A map from client id to unique id sent to mDns */ |
Hugo Benichi | d2552ae | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 1308 | private final SparseIntArray mClientIds = new SparseIntArray(); |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1309 | |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 1310 | /* A map from client id to the type of the request we had received */ |
Hugo Benichi | d2552ae | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 1311 | private final SparseIntArray mClientRequests = new SparseIntArray(); |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 1312 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 1313 | /* A map from client id to the MdnsListener */ |
| 1314 | private final SparseArray<MdnsListener> mListeners = new SparseArray<>(); |
| 1315 | |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 1316 | // The target SDK of this client < Build.VERSION_CODES.S |
| 1317 | private boolean mIsLegacy = false; |
| 1318 | |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 1319 | private ClientInfo(INsdManagerCallback cb) { |
| 1320 | mCb = cb; |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 1321 | if (DBG) Log.d(TAG, "New client"); |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1322 | } |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1323 | |
| 1324 | @Override |
| 1325 | public String toString() { |
Jeff Sharkey | 6346538 | 2020-10-17 21:20:13 -0600 | [diff] [blame] | 1326 | StringBuilder sb = new StringBuilder(); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1327 | sb.append("mResolvedService ").append(mResolvedService).append("\n"); |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 1328 | sb.append("mIsLegacy ").append(mIsLegacy).append("\n"); |
Irfan Sheriff | e4c42f4 | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 1329 | for(int i = 0; i< mClientIds.size(); i++) { |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 1330 | int clientID = mClientIds.keyAt(i); |
| 1331 | sb.append("clientId ").append(clientID). |
| 1332 | append(" mDnsId ").append(mClientIds.valueAt(i)). |
| 1333 | append(" type ").append(mClientRequests.get(clientID)).append("\n"); |
Irfan Sheriff | 7500665 | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 1334 | } |
| 1335 | return sb.toString(); |
| 1336 | } |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 1337 | |
Luke Huang | f7277ed | 2021-07-12 21:15:10 +0800 | [diff] [blame] | 1338 | private boolean isLegacy() { |
| 1339 | return mIsLegacy; |
| 1340 | } |
| 1341 | |
| 1342 | private void setLegacy() { |
| 1343 | mIsLegacy = true; |
| 1344 | } |
| 1345 | |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 1346 | // Remove any pending requests from the global map when we get rid of a client, |
| 1347 | // and send cancellations to the daemon. |
| 1348 | private void expungeAllRequests() { |
| 1349 | int globalId, clientId, i; |
Hugo Benichi | d2552ae | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 1350 | // TODO: to keep handler responsive, do not clean all requests for that client at once. |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 1351 | for (i = 0; i < mClientIds.size(); i++) { |
| 1352 | clientId = mClientIds.keyAt(i); |
| 1353 | globalId = mClientIds.valueAt(i); |
| 1354 | mIdToClientInfoMap.remove(globalId); |
paulhu | b222570 | 2021-11-17 09:35:33 +0800 | [diff] [blame] | 1355 | if (DBG) { |
| 1356 | Log.d(TAG, "Terminating client-ID " + clientId |
| 1357 | + " global-ID " + globalId + " type " + mClientRequests.get(clientId)); |
| 1358 | } |
Dave Platt | feff2af | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 1359 | switch (mClientRequests.get(clientId)) { |
| 1360 | case NsdManager.DISCOVER_SERVICES: |
| 1361 | stopServiceDiscovery(globalId); |
| 1362 | break; |
| 1363 | case NsdManager.RESOLVE_SERVICE: |
| 1364 | stopResolveService(globalId); |
| 1365 | break; |
| 1366 | case NsdManager.REGISTER_SERVICE: |
| 1367 | unregisterService(globalId); |
| 1368 | break; |
| 1369 | default: |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | mClientIds.clear(); |
| 1374 | mClientRequests.clear(); |
| 1375 | } |
| 1376 | |
Paul Hu | 23fa202 | 2023-01-13 22:57:24 +0800 | [diff] [blame] | 1377 | void unregisterAllListeners() { |
| 1378 | for (int i = 0; i < mListeners.size(); i++) { |
| 1379 | final MdnsListener listener = mListeners.valueAt(i); |
| 1380 | mMdnsDiscoveryManager.unregisterListener( |
| 1381 | listener.getListenedServiceType(), listener); |
| 1382 | } |
| 1383 | mListeners.clear(); |
| 1384 | } |
| 1385 | |
Christopher Lane | 7441122 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 1386 | // mClientIds is a sparse array of listener id -> mDnsClient id. For a given mDnsClient id, |
| 1387 | // return the corresponding listener id. mDnsClient id is also called a global id. |
| 1388 | private int getClientId(final int globalId) { |
Hugo Benichi | d2552ae | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 1389 | int idx = mClientIds.indexOfValue(globalId); |
| 1390 | if (idx < 0) { |
| 1391 | return idx; |
Christopher Lane | 7441122 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 1392 | } |
Hugo Benichi | d2552ae | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 1393 | return mClientIds.keyAt(idx); |
Christopher Lane | 7441122 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 1394 | } |
Remi NGUYEN VAN | 62eb76e | 2021-09-09 17:39:05 +0900 | [diff] [blame] | 1395 | |
| 1396 | void onDiscoverServicesStarted(int listenerKey, NsdServiceInfo info) { |
| 1397 | try { |
| 1398 | mCb.onDiscoverServicesStarted(listenerKey, info); |
| 1399 | } catch (RemoteException e) { |
| 1400 | Log.e(TAG, "Error calling onDiscoverServicesStarted", e); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | void onDiscoverServicesFailed(int listenerKey, int error) { |
| 1405 | try { |
| 1406 | mCb.onDiscoverServicesFailed(listenerKey, error); |
| 1407 | } catch (RemoteException e) { |
| 1408 | Log.e(TAG, "Error calling onDiscoverServicesFailed", e); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | void onServiceFound(int listenerKey, NsdServiceInfo info) { |
| 1413 | try { |
| 1414 | mCb.onServiceFound(listenerKey, info); |
| 1415 | } catch (RemoteException e) { |
| 1416 | Log.e(TAG, "Error calling onServiceFound(", e); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | void onServiceLost(int listenerKey, NsdServiceInfo info) { |
| 1421 | try { |
| 1422 | mCb.onServiceLost(listenerKey, info); |
| 1423 | } catch (RemoteException e) { |
| 1424 | Log.e(TAG, "Error calling onServiceLost(", e); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | void onStopDiscoveryFailed(int listenerKey, int error) { |
| 1429 | try { |
| 1430 | mCb.onStopDiscoveryFailed(listenerKey, error); |
| 1431 | } catch (RemoteException e) { |
| 1432 | Log.e(TAG, "Error calling onStopDiscoveryFailed", e); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | void onStopDiscoverySucceeded(int listenerKey) { |
| 1437 | try { |
| 1438 | mCb.onStopDiscoverySucceeded(listenerKey); |
| 1439 | } catch (RemoteException e) { |
| 1440 | Log.e(TAG, "Error calling onStopDiscoverySucceeded", e); |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | void onRegisterServiceFailed(int listenerKey, int error) { |
| 1445 | try { |
| 1446 | mCb.onRegisterServiceFailed(listenerKey, error); |
| 1447 | } catch (RemoteException e) { |
| 1448 | Log.e(TAG, "Error calling onRegisterServiceFailed", e); |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | void onRegisterServiceSucceeded(int listenerKey, NsdServiceInfo info) { |
| 1453 | try { |
| 1454 | mCb.onRegisterServiceSucceeded(listenerKey, info); |
| 1455 | } catch (RemoteException e) { |
| 1456 | Log.e(TAG, "Error calling onRegisterServiceSucceeded", e); |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | void onUnregisterServiceFailed(int listenerKey, int error) { |
| 1461 | try { |
| 1462 | mCb.onUnregisterServiceFailed(listenerKey, error); |
| 1463 | } catch (RemoteException e) { |
| 1464 | Log.e(TAG, "Error calling onUnregisterServiceFailed", e); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | void onUnregisterServiceSucceeded(int listenerKey) { |
| 1469 | try { |
| 1470 | mCb.onUnregisterServiceSucceeded(listenerKey); |
| 1471 | } catch (RemoteException e) { |
| 1472 | Log.e(TAG, "Error calling onUnregisterServiceSucceeded", e); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | void onResolveServiceFailed(int listenerKey, int error) { |
| 1477 | try { |
| 1478 | mCb.onResolveServiceFailed(listenerKey, error); |
| 1479 | } catch (RemoteException e) { |
| 1480 | Log.e(TAG, "Error calling onResolveServiceFailed", e); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | void onResolveServiceSucceeded(int listenerKey, NsdServiceInfo info) { |
| 1485 | try { |
| 1486 | mCb.onResolveServiceSucceeded(listenerKey, info); |
| 1487 | } catch (RemoteException e) { |
| 1488 | Log.e(TAG, "Error calling onResolveServiceSucceeded", e); |
| 1489 | } |
| 1490 | } |
Irfan Sheriff | e8de246 | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 1491 | } |
Irfan Sheriff | 77ec558 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1492 | } |