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