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