2012-03-22 17:01:39 -07:00
|
|
|
/*
|
2021-09-09 17:39:05 +09:00
|
|
|
* Copyright (C) 2021 The Android Open Source Project
|
2012-03-22 17:01:39 -07:00
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.server;
|
|
|
|
|
|
2022-02-10 21:58:32 +08:00
|
|
|
import static android.net.ConnectivityManager.NETID_UNSET;
|
2023-01-13 23:26:49 +08:00
|
|
|
import static android.net.nsd.NsdManager.MDNS_DISCOVERY_MANAGER_EVENT;
|
2022-02-10 21:58:32 +08:00
|
|
|
import static android.net.nsd.NsdManager.MDNS_SERVICE_EVENT;
|
2023-01-12 13:42:07 +08:00
|
|
|
import static android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY;
|
2022-02-10 21:58:32 +08:00
|
|
|
|
2023-01-13 22:57:24 +08:00
|
|
|
import android.annotation.NonNull;
|
2023-01-12 13:42:07 +08:00
|
|
|
import android.annotation.Nullable;
|
2019-08-12 16:25:11 +08:00
|
|
|
import android.content.Context;
|
2012-04-17 23:15:29 -07:00
|
|
|
import android.content.Intent;
|
2021-12-16 15:31:16 +09:00
|
|
|
import android.net.ConnectivityManager;
|
2022-05-30 12:42:24 +09:00
|
|
|
import android.net.INetd;
|
2023-01-14 00:31:09 +08:00
|
|
|
import android.net.InetAddresses;
|
2021-12-16 15:31:16 +09:00
|
|
|
import android.net.LinkProperties;
|
|
|
|
|
import android.net.Network;
|
2022-02-10 21:58:32 +08:00
|
|
|
import android.net.mdns.aidl.DiscoveryInfo;
|
|
|
|
|
import android.net.mdns.aidl.GetAddressInfo;
|
|
|
|
|
import android.net.mdns.aidl.IMDnsEventListener;
|
|
|
|
|
import android.net.mdns.aidl.RegistrationInfo;
|
|
|
|
|
import android.net.mdns.aidl.ResolutionInfo;
|
2012-03-22 17:01:39 -07:00
|
|
|
import android.net.nsd.INsdManager;
|
2021-09-09 17:39:05 +09:00
|
|
|
import android.net.nsd.INsdManagerCallback;
|
|
|
|
|
import android.net.nsd.INsdServiceConnector;
|
2022-02-10 21:58:32 +08:00
|
|
|
import android.net.nsd.MDnsManager;
|
2012-03-22 17:01:39 -07:00
|
|
|
import android.net.nsd.NsdManager;
|
2019-08-12 16:25:11 +08:00
|
|
|
import android.net.nsd.NsdServiceInfo;
|
2017-04-24 11:35:06 +09:00
|
|
|
import android.os.Handler;
|
2019-08-12 16:25:11 +08:00
|
|
|
import android.os.HandlerThread;
|
2021-09-09 17:39:05 +09:00
|
|
|
import android.os.IBinder;
|
2023-01-12 13:42:07 +08:00
|
|
|
import android.os.Looper;
|
2012-03-22 17:01:39 -07:00
|
|
|
import android.os.Message;
|
2021-09-09 17:39:05 +09:00
|
|
|
import android.os.RemoteException;
|
2012-08-29 18:32:08 -07:00
|
|
|
import android.os.UserHandle;
|
2023-01-13 22:57:24 +08:00
|
|
|
import android.text.TextUtils;
|
2021-09-09 17:39:05 +09:00
|
|
|
import android.util.Log;
|
|
|
|
|
import android.util.Pair;
|
2012-05-03 16:44:27 -07:00
|
|
|
import android.util.SparseArray;
|
2017-04-11 14:42:47 +09:00
|
|
|
import android.util.SparseIntArray;
|
2012-03-22 17:01:39 -07:00
|
|
|
|
2019-08-12 16:25:11 +08:00
|
|
|
import com.android.internal.annotations.VisibleForTesting;
|
|
|
|
|
import com.android.internal.util.State;
|
|
|
|
|
import com.android.internal.util.StateMachine;
|
2023-01-12 13:42:07 +08:00
|
|
|
import com.android.net.module.util.DeviceConfigUtils;
|
2021-09-16 10:15:22 +08:00
|
|
|
import com.android.net.module.util.PermissionUtils;
|
2023-01-12 13:42:07 +08:00
|
|
|
import com.android.server.connectivity.mdns.ExecutorProvider;
|
2023-01-18 15:58:03 +09:00
|
|
|
import com.android.server.connectivity.mdns.MdnsAdvertiser;
|
2023-01-12 13:42:07 +08:00
|
|
|
import com.android.server.connectivity.mdns.MdnsDiscoveryManager;
|
|
|
|
|
import com.android.server.connectivity.mdns.MdnsMultinetworkSocketClient;
|
2023-01-13 22:57:24 +08:00
|
|
|
import com.android.server.connectivity.mdns.MdnsSearchOptions;
|
|
|
|
|
import com.android.server.connectivity.mdns.MdnsServiceBrowserListener;
|
|
|
|
|
import com.android.server.connectivity.mdns.MdnsServiceInfo;
|
2023-01-12 13:42:07 +08:00
|
|
|
import com.android.server.connectivity.mdns.MdnsSocketClientBase;
|
|
|
|
|
import com.android.server.connectivity.mdns.MdnsSocketProvider;
|
2019-08-12 16:25:11 +08:00
|
|
|
|
2012-03-22 17:01:39 -07:00
|
|
|
import java.io.FileDescriptor;
|
|
|
|
|
import java.io.PrintWriter;
|
2012-04-11 14:52:19 -07:00
|
|
|
import java.net.InetAddress;
|
2021-12-16 15:31:16 +09:00
|
|
|
import java.net.NetworkInterface;
|
|
|
|
|
import java.net.SocketException;
|
|
|
|
|
import java.net.UnknownHostException;
|
2023-01-18 15:58:03 +09:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.nio.CharBuffer;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.CharsetEncoder;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2012-04-11 14:52:19 -07:00
|
|
|
import java.util.HashMap;
|
2023-01-13 22:57:24 +08:00
|
|
|
import java.util.List;
|
2023-01-14 00:31:09 +08:00
|
|
|
import java.util.Map;
|
2022-12-27 08:48:48 +00:00
|
|
|
import java.util.Objects;
|
2023-01-13 22:57:24 +08:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
2012-03-22 17:01:39 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Network Service Discovery Service handles remote service discovery operation requests by
|
|
|
|
|
* implementing the INsdManager interface.
|
|
|
|
|
*
|
|
|
|
|
* @hide
|
|
|
|
|
*/
|
|
|
|
|
public class NsdService extends INsdManager.Stub {
|
|
|
|
|
private static final String TAG = "NsdService";
|
|
|
|
|
private static final String MDNS_TAG = "mDnsConnector";
|
2023-01-18 15:58:03 +09:00
|
|
|
/**
|
|
|
|
|
* Enable discovery using the Java DiscoveryManager, instead of the legacy mdnsresponder
|
|
|
|
|
* implementation.
|
|
|
|
|
*/
|
2023-01-12 13:42:07 +08:00
|
|
|
private static final String MDNS_DISCOVERY_MANAGER_VERSION = "mdns_discovery_manager_version";
|
2023-01-13 22:57:24 +08:00
|
|
|
private static final String LOCAL_DOMAIN_NAME = "local";
|
2023-01-18 15:58:03 +09:00
|
|
|
// Max label length as per RFC 1034/1035
|
|
|
|
|
private static final int MAX_LABEL_LENGTH = 63;
|
2012-03-22 17:01:39 -07:00
|
|
|
|
2023-01-18 15:58:03 +09:00
|
|
|
/**
|
|
|
|
|
* Enable advertising using the Java MdnsAdvertiser, instead of the legacy mdnsresponder
|
|
|
|
|
* implementation.
|
|
|
|
|
*/
|
|
|
|
|
private static final String MDNS_ADVERTISER_VERSION = "mdns_advertiser_version";
|
|
|
|
|
|
|
|
|
|
public static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
|
2021-06-23 06:29:30 +00:00
|
|
|
private static final long CLEANUP_DELAY_MS = 10000;
|
2021-12-16 15:31:16 +09:00
|
|
|
private static final int IFACE_IDX_ANY = 0;
|
2012-03-22 17:01:39 -07:00
|
|
|
|
2017-04-05 14:06:11 +09:00
|
|
|
private final Context mContext;
|
|
|
|
|
private final NsdStateMachine mNsdStateMachine;
|
2022-02-10 21:58:32 +08:00
|
|
|
private final MDnsManager mMDnsManager;
|
|
|
|
|
private final MDnsEventCallback mMDnsEventCallback;
|
2023-01-12 13:42:07 +08:00
|
|
|
@Nullable
|
|
|
|
|
private final MdnsMultinetworkSocketClient mMdnsSocketClient;
|
|
|
|
|
@Nullable
|
|
|
|
|
private final MdnsDiscoveryManager mMdnsDiscoveryManager;
|
|
|
|
|
@Nullable
|
|
|
|
|
private final MdnsSocketProvider mMdnsSocketProvider;
|
2023-01-18 15:58:03 +09:00
|
|
|
@Nullable
|
|
|
|
|
private final MdnsAdvertiser mAdvertiser;
|
2023-01-13 22:57:24 +08:00
|
|
|
// WARNING : Accessing these values in any thread is not safe, it must only be changed in the
|
2022-02-10 21:58:32 +08:00
|
|
|
// state machine thread. If change this outside state machine, it will need to introduce
|
|
|
|
|
// synchronization.
|
|
|
|
|
private boolean mIsDaemonStarted = false;
|
2023-01-13 22:57:24 +08:00
|
|
|
private boolean mIsMonitoringSocketsStarted = false;
|
2012-03-22 17:01:39 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clients receiving asynchronous messages
|
|
|
|
|
*/
|
2021-09-09 17:39:05 +09:00
|
|
|
private final HashMap<NsdServiceConnector, ClientInfo> mClients = new HashMap<>();
|
2012-03-22 17:01:39 -07:00
|
|
|
|
2012-05-03 16:44:27 -07:00
|
|
|
/* A map from unique id to client info */
|
2017-04-05 14:06:11 +09:00
|
|
|
private final SparseArray<ClientInfo> mIdToClientInfoMap= new SparseArray<>();
|
2012-05-03 16:44:27 -07:00
|
|
|
|
2021-06-13 16:52:05 +00:00
|
|
|
private final long mCleanupDelayMs;
|
2012-03-22 17:01:39 -07:00
|
|
|
|
2017-04-05 14:06:11 +09:00
|
|
|
private static final int INVALID_ID = 0;
|
2012-04-11 14:52:19 -07:00
|
|
|
private int mUniqueId = 1;
|
2021-07-12 21:15:10 +08:00
|
|
|
// The count of the connected legacy clients.
|
|
|
|
|
private int mLegacyClientCount = 0;
|
2012-04-11 14:52:19 -07:00
|
|
|
|
2023-01-13 22:57:24 +08:00
|
|
|
private static class MdnsListener implements MdnsServiceBrowserListener {
|
|
|
|
|
protected final int mClientId;
|
|
|
|
|
protected final int mTransactionId;
|
|
|
|
|
@NonNull
|
|
|
|
|
protected final NsdServiceInfo mReqServiceInfo;
|
|
|
|
|
@NonNull
|
|
|
|
|
protected final String mListenedServiceType;
|
|
|
|
|
|
|
|
|
|
MdnsListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo,
|
|
|
|
|
@NonNull String listenedServiceType) {
|
|
|
|
|
mClientId = clientId;
|
|
|
|
|
mTransactionId = transactionId;
|
|
|
|
|
mReqServiceInfo = reqServiceInfo;
|
|
|
|
|
mListenedServiceType = listenedServiceType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
public String getListenedServiceType() {
|
|
|
|
|
return mListenedServiceType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceFound(@NonNull MdnsServiceInfo serviceInfo) { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceUpdated(@NonNull MdnsServiceInfo serviceInfo) { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceRemoved(@NonNull MdnsServiceInfo serviceInfo) { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceNameDiscovered(@NonNull MdnsServiceInfo serviceInfo) { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceNameRemoved(@NonNull MdnsServiceInfo serviceInfo) { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSearchStoppedWithError(int error) { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSearchFailedToStart() { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDiscoveryQuerySent(@NonNull List<String> subtypes, int transactionId) { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class DiscoveryListener extends MdnsListener {
|
|
|
|
|
|
|
|
|
|
DiscoveryListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo,
|
|
|
|
|
@NonNull String listenServiceType) {
|
|
|
|
|
super(clientId, transactionId, reqServiceInfo, listenServiceType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceNameDiscovered(@NonNull MdnsServiceInfo serviceInfo) {
|
2023-01-13 23:26:49 +08:00
|
|
|
mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId,
|
|
|
|
|
NsdManager.SERVICE_FOUND,
|
|
|
|
|
new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo));
|
2023-01-13 22:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceNameRemoved(@NonNull MdnsServiceInfo serviceInfo) {
|
2023-01-13 23:56:34 +08:00
|
|
|
mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId,
|
|
|
|
|
NsdManager.SERVICE_LOST,
|
|
|
|
|
new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo));
|
2023-01-13 22:57:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-14 00:31:09 +08:00
|
|
|
private class ResolutionListener extends MdnsListener {
|
|
|
|
|
|
|
|
|
|
ResolutionListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo,
|
|
|
|
|
@NonNull String listenServiceType) {
|
|
|
|
|
super(clientId, transactionId, reqServiceInfo, listenServiceType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceFound(MdnsServiceInfo serviceInfo) {
|
|
|
|
|
mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId,
|
|
|
|
|
NsdManager.RESOLVE_SERVICE_SUCCEEDED,
|
|
|
|
|
new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 23:26:49 +08:00
|
|
|
/**
|
|
|
|
|
* Data class of mdns service callback information.
|
|
|
|
|
*/
|
|
|
|
|
private static class MdnsEvent {
|
|
|
|
|
final int mClientId;
|
|
|
|
|
@NonNull
|
|
|
|
|
final String mRequestedServiceType;
|
|
|
|
|
@NonNull
|
|
|
|
|
final MdnsServiceInfo mMdnsServiceInfo;
|
|
|
|
|
|
|
|
|
|
MdnsEvent(int clientId, @NonNull String requestedServiceType,
|
|
|
|
|
@NonNull MdnsServiceInfo mdnsServiceInfo) {
|
|
|
|
|
mClientId = clientId;
|
|
|
|
|
mRequestedServiceType = requestedServiceType;
|
|
|
|
|
mMdnsServiceInfo = mdnsServiceInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-17 23:15:29 -07:00
|
|
|
private class NsdStateMachine extends StateMachine {
|
|
|
|
|
|
2012-05-03 16:44:27 -07:00
|
|
|
private final DefaultState mDefaultState = new DefaultState();
|
|
|
|
|
private final EnabledState mEnabledState = new EnabledState();
|
2012-04-17 23:15:29 -07:00
|
|
|
|
|
|
|
|
@Override
|
2012-05-29 12:40:46 -07:00
|
|
|
protected String getWhatToString(int what) {
|
2017-04-05 14:06:11 +09:00
|
|
|
return NsdManager.nameOf(what);
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-23 06:29:30 +00:00
|
|
|
private void maybeStartDaemon() {
|
2022-02-10 21:58:32 +08:00
|
|
|
if (mIsDaemonStarted) {
|
|
|
|
|
if (DBG) Log.d(TAG, "Daemon is already started.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mMDnsManager.registerEventListener(mMDnsEventCallback);
|
|
|
|
|
mMDnsManager.startDaemon();
|
|
|
|
|
mIsDaemonStarted = true;
|
2021-06-13 16:52:05 +00:00
|
|
|
maybeScheduleStop();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:58:32 +08:00
|
|
|
private void maybeStopDaemon() {
|
|
|
|
|
if (!mIsDaemonStarted) {
|
|
|
|
|
if (DBG) Log.d(TAG, "Daemon has not been started.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mMDnsManager.unregisterEventListener(mMDnsEventCallback);
|
|
|
|
|
mMDnsManager.stopDaemon();
|
|
|
|
|
mIsDaemonStarted = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 06:29:30 +00:00
|
|
|
private boolean isAnyRequestActive() {
|
|
|
|
|
return mIdToClientInfoMap.size() != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void scheduleStop() {
|
|
|
|
|
sendMessageDelayed(NsdManager.DAEMON_CLEANUP, mCleanupDelayMs);
|
|
|
|
|
}
|
|
|
|
|
private void maybeScheduleStop() {
|
2021-07-12 21:15:10 +08:00
|
|
|
// The native daemon should stay alive and can't be cleanup
|
|
|
|
|
// if any legacy client connected.
|
|
|
|
|
if (!isAnyRequestActive() && mLegacyClientCount == 0) {
|
2021-06-23 06:29:30 +00:00
|
|
|
scheduleStop();
|
2021-06-13 16:52:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 06:29:30 +00:00
|
|
|
private void cancelStop() {
|
2021-06-13 16:52:05 +00:00
|
|
|
this.removeMessages(NsdManager.DAEMON_CLEANUP);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 22:57:24 +08:00
|
|
|
private void maybeStartMonitoringSockets() {
|
|
|
|
|
if (mIsMonitoringSocketsStarted) {
|
|
|
|
|
if (DBG) Log.d(TAG, "Socket monitoring is already started.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mMdnsSocketProvider.startMonitoringSockets();
|
|
|
|
|
mIsMonitoringSocketsStarted = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void maybeStopMonitoringSockets() {
|
|
|
|
|
if (!mIsMonitoringSocketsStarted) {
|
|
|
|
|
if (DBG) Log.d(TAG, "Socket monitoring has not been started.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mMdnsSocketProvider.stopMonitoringSockets();
|
|
|
|
|
mIsMonitoringSocketsStarted = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void maybeStopMonitoringSocketsIfNoActiveRequest() {
|
|
|
|
|
if (!isAnyRequestActive()) {
|
|
|
|
|
maybeStopMonitoringSockets();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 11:35:06 +09:00
|
|
|
NsdStateMachine(String name, Handler handler) {
|
|
|
|
|
super(name, handler);
|
2012-04-17 23:15:29 -07:00
|
|
|
addState(mDefaultState);
|
|
|
|
|
addState(mEnabledState, mDefaultState);
|
2021-11-30 13:31:29 +08:00
|
|
|
State initialState = mEnabledState;
|
2017-04-24 16:41:03 +09:00
|
|
|
setInitialState(initialState);
|
2012-05-29 12:40:46 -07:00
|
|
|
setLogRecSize(25);
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DefaultState extends State {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean processMessage(Message msg) {
|
2021-09-09 17:39:05 +09:00
|
|
|
final ClientInfo cInfo;
|
|
|
|
|
final int clientId = msg.arg2;
|
2012-04-17 23:15:29 -07:00
|
|
|
switch (msg.what) {
|
2021-09-09 17:39:05 +09:00
|
|
|
case NsdManager.REGISTER_CLIENT:
|
|
|
|
|
final Pair<NsdServiceConnector, INsdManagerCallback> arg =
|
|
|
|
|
(Pair<NsdServiceConnector, INsdManagerCallback>) msg.obj;
|
|
|
|
|
final INsdManagerCallback cb = arg.second;
|
|
|
|
|
try {
|
|
|
|
|
cb.asBinder().linkToDeath(arg.first, 0);
|
|
|
|
|
cInfo = new ClientInfo(cb);
|
|
|
|
|
mClients.put(arg.first, cInfo);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.w(TAG, "Client " + clientId + " has already died");
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
break;
|
2021-09-09 17:39:05 +09:00
|
|
|
case NsdManager.UNREGISTER_CLIENT:
|
|
|
|
|
final NsdServiceConnector connector = (NsdServiceConnector) msg.obj;
|
|
|
|
|
cInfo = mClients.remove(connector);
|
2014-03-07 14:48:22 -08:00
|
|
|
if (cInfo != null) {
|
2023-01-13 22:57:24 +08:00
|
|
|
if (mMdnsDiscoveryManager != null) {
|
|
|
|
|
cInfo.unregisterAllListeners();
|
|
|
|
|
}
|
2014-03-07 14:48:22 -08:00
|
|
|
cInfo.expungeAllRequests();
|
2021-07-12 21:15:10 +08:00
|
|
|
if (cInfo.isLegacy()) {
|
|
|
|
|
mLegacyClientCount -= 1;
|
|
|
|
|
}
|
2014-03-07 14:48:22 -08:00
|
|
|
}
|
2023-01-18 15:58:03 +09:00
|
|
|
if (mMdnsDiscoveryManager != null || mAdvertiser != null) {
|
2023-01-13 22:57:24 +08:00
|
|
|
maybeStopMonitoringSocketsIfNoActiveRequest();
|
|
|
|
|
}
|
2021-07-12 21:15:10 +08:00
|
|
|
maybeScheduleStop();
|
2012-04-17 23:15:29 -07:00
|
|
|
break;
|
|
|
|
|
case NsdManager.DISCOVER_SERVICES:
|
2021-09-09 17:39:05 +09:00
|
|
|
cInfo = getClientInfoForReply(msg);
|
|
|
|
|
if (cInfo != null) {
|
|
|
|
|
cInfo.onDiscoverServicesFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
break;
|
|
|
|
|
case NsdManager.STOP_DISCOVERY:
|
2021-09-09 17:39:05 +09:00
|
|
|
cInfo = getClientInfoForReply(msg);
|
|
|
|
|
if (cInfo != null) {
|
|
|
|
|
cInfo.onStopDiscoveryFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
break;
|
|
|
|
|
case NsdManager.REGISTER_SERVICE:
|
2021-09-09 17:39:05 +09:00
|
|
|
cInfo = getClientInfoForReply(msg);
|
|
|
|
|
if (cInfo != null) {
|
|
|
|
|
cInfo.onRegisterServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
break;
|
|
|
|
|
case NsdManager.UNREGISTER_SERVICE:
|
2021-09-09 17:39:05 +09:00
|
|
|
cInfo = getClientInfoForReply(msg);
|
|
|
|
|
if (cInfo != null) {
|
|
|
|
|
cInfo.onUnregisterServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
break;
|
|
|
|
|
case NsdManager.RESOLVE_SERVICE:
|
2021-09-09 17:39:05 +09:00
|
|
|
cInfo = getClientInfoForReply(msg);
|
|
|
|
|
if (cInfo != null) {
|
|
|
|
|
cInfo.onResolveServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
break;
|
2022-12-26 09:24:42 +00:00
|
|
|
case NsdManager.STOP_RESOLUTION:
|
|
|
|
|
cInfo = getClientInfoForReply(msg);
|
|
|
|
|
if (cInfo != null) {
|
|
|
|
|
cInfo.onStopResolutionFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_OPERATION_NOT_RUNNING);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-12-27 08:48:48 +00:00
|
|
|
case NsdManager.REGISTER_SERVICE_CALLBACK:
|
|
|
|
|
cInfo = getClientInfoForReply(msg);
|
|
|
|
|
if (cInfo != null) {
|
|
|
|
|
cInfo.onServiceInfoCallbackRegistrationFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_BAD_PARAMETERS);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-06-13 16:52:05 +00:00
|
|
|
case NsdManager.DAEMON_CLEANUP:
|
2022-02-10 21:58:32 +08:00
|
|
|
maybeStopDaemon();
|
2021-06-13 16:52:05 +00:00
|
|
|
break;
|
2021-07-12 21:15:10 +08:00
|
|
|
// This event should be only sent by the legacy (target SDK < S) clients.
|
|
|
|
|
// Mark the sending client as legacy.
|
|
|
|
|
case NsdManager.DAEMON_STARTUP:
|
2021-09-09 17:39:05 +09:00
|
|
|
cInfo = getClientInfoForReply(msg);
|
2021-07-12 21:15:10 +08:00
|
|
|
if (cInfo != null) {
|
|
|
|
|
cancelStop();
|
|
|
|
|
cInfo.setLegacy();
|
|
|
|
|
mLegacyClientCount += 1;
|
|
|
|
|
maybeStartDaemon();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2012-04-17 23:15:29 -07:00
|
|
|
default:
|
2021-11-17 09:35:33 +08:00
|
|
|
Log.e(TAG, "Unhandled " + msg);
|
2012-04-17 23:15:29 -07:00
|
|
|
return NOT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
return HANDLED;
|
|
|
|
|
}
|
2021-09-09 17:39:05 +09:00
|
|
|
|
|
|
|
|
private ClientInfo getClientInfoForReply(Message msg) {
|
|
|
|
|
final ListenerArgs args = (ListenerArgs) msg.obj;
|
|
|
|
|
return mClients.get(args.connector);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class EnabledState extends State {
|
|
|
|
|
@Override
|
|
|
|
|
public void enter() {
|
|
|
|
|
sendNsdStateChangeBroadcast(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void exit() {
|
2021-06-13 16:52:05 +00:00
|
|
|
// TODO: it is incorrect to stop the daemon without expunging all requests
|
|
|
|
|
// and sending error callbacks to clients.
|
2021-06-23 06:29:30 +00:00
|
|
|
scheduleStop();
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2012-05-03 16:44:27 -07:00
|
|
|
private boolean requestLimitReached(ClientInfo clientInfo) {
|
|
|
|
|
if (clientInfo.mClientIds.size() >= ClientInfo.MAX_LIMIT) {
|
2021-11-17 09:35:33 +08:00
|
|
|
if (DBG) Log.d(TAG, "Exceeded max outstanding requests " + clientInfo);
|
2012-05-03 16:44:27 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-07 14:48:22 -08:00
|
|
|
private void storeRequestMap(int clientId, int globalId, ClientInfo clientInfo, int what) {
|
2012-05-03 16:44:27 -07:00
|
|
|
clientInfo.mClientIds.put(clientId, globalId);
|
2014-03-07 14:48:22 -08:00
|
|
|
clientInfo.mClientRequests.put(clientId, what);
|
2012-05-03 16:44:27 -07:00
|
|
|
mIdToClientInfoMap.put(globalId, clientInfo);
|
2021-06-13 16:52:05 +00:00
|
|
|
// Remove the cleanup event because here comes a new request.
|
|
|
|
|
cancelStop();
|
2012-05-03 16:44:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) {
|
2017-04-11 14:42:47 +09:00
|
|
|
clientInfo.mClientIds.delete(clientId);
|
|
|
|
|
clientInfo.mClientRequests.delete(clientId);
|
2012-05-03 16:44:27 -07:00
|
|
|
mIdToClientInfoMap.remove(globalId);
|
2021-06-13 16:52:05 +00:00
|
|
|
maybeScheduleStop();
|
2023-01-18 15:58:03 +09:00
|
|
|
maybeStopMonitoringSocketsIfNoActiveRequest();
|
2012-05-03 16:44:27 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-13 22:57:24 +08:00
|
|
|
private void storeListenerMap(int clientId, int transactionId, MdnsListener listener,
|
|
|
|
|
ClientInfo clientInfo) {
|
|
|
|
|
clientInfo.mClientIds.put(clientId, transactionId);
|
|
|
|
|
clientInfo.mListeners.put(clientId, listener);
|
|
|
|
|
mIdToClientInfoMap.put(transactionId, clientInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeListenerMap(int clientId, int transactionId, ClientInfo clientInfo) {
|
|
|
|
|
clientInfo.mClientIds.delete(clientId);
|
|
|
|
|
clientInfo.mListeners.delete(clientId);
|
|
|
|
|
mIdToClientInfoMap.remove(transactionId);
|
|
|
|
|
maybeStopMonitoringSocketsIfNoActiveRequest();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 08:48:48 +00:00
|
|
|
private void clearRegisteredServiceInfo(ClientInfo clientInfo) {
|
|
|
|
|
clientInfo.mRegisteredService = null;
|
|
|
|
|
clientInfo.mClientIdForServiceUpdates = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 22:57:24 +08:00
|
|
|
/**
|
|
|
|
|
* Check the given service type is valid and construct it to a service type
|
|
|
|
|
* which can use for discovery / resolution service.
|
|
|
|
|
*
|
|
|
|
|
* <p> The valid service type should be 2 labels, or 3 labels if the query is for a
|
|
|
|
|
* subtype (see RFC6763 7.1). Each label is up to 63 characters and must start with an
|
|
|
|
|
* underscore; they are alphanumerical characters or dashes or underscore, except the
|
|
|
|
|
* last one that is just alphanumerical. The last label must be _tcp or _udp.
|
|
|
|
|
*
|
|
|
|
|
* @param serviceType the request service type for discovery / resolution service
|
|
|
|
|
* @return constructed service type or null if the given service type is invalid.
|
|
|
|
|
*/
|
|
|
|
|
@Nullable
|
|
|
|
|
private String constructServiceType(String serviceType) {
|
|
|
|
|
if (TextUtils.isEmpty(serviceType)) return null;
|
|
|
|
|
|
|
|
|
|
final Pattern serviceTypePattern = Pattern.compile(
|
|
|
|
|
"^(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\.)?"
|
|
|
|
|
+ "(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\._(?:tcp|udp))$");
|
|
|
|
|
final Matcher matcher = serviceTypePattern.matcher(serviceType);
|
|
|
|
|
if (!matcher.matches()) return null;
|
|
|
|
|
return matcher.group(1) == null
|
2023-01-18 15:58:03 +09:00
|
|
|
? serviceType
|
|
|
|
|
: matcher.group(1) + "_sub." + matcher.group(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Truncate a service name to up to 63 UTF-8 bytes.
|
|
|
|
|
*
|
|
|
|
|
* See RFC6763 4.1.1: service instance names are UTF-8 and up to 63 bytes. Truncating
|
|
|
|
|
* names used in registerService follows historical behavior (see mdnsresponder
|
|
|
|
|
* handle_regservice_request).
|
|
|
|
|
*/
|
|
|
|
|
@NonNull
|
|
|
|
|
private String truncateServiceName(@NonNull String originalName) {
|
|
|
|
|
// UTF-8 is at most 4 bytes per character; return early in the common case where
|
|
|
|
|
// the name can't possibly be over the limit given its string length.
|
|
|
|
|
if (originalName.length() <= MAX_LABEL_LENGTH / 4) return originalName;
|
|
|
|
|
|
|
|
|
|
final Charset utf8 = StandardCharsets.UTF_8;
|
|
|
|
|
final CharsetEncoder encoder = utf8.newEncoder();
|
|
|
|
|
final ByteBuffer out = ByteBuffer.allocate(MAX_LABEL_LENGTH);
|
|
|
|
|
// encode will write as many characters as possible to the out buffer, and just
|
|
|
|
|
// return an overflow code if there were too many characters (no need to check the
|
|
|
|
|
// return code here, this method truncates the name on purpose).
|
|
|
|
|
encoder.encode(CharBuffer.wrap(originalName), out, true /* endOfInput */);
|
|
|
|
|
return new String(out.array(), 0, out.position(), utf8);
|
2023-01-13 22:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
2012-04-17 23:15:29 -07:00
|
|
|
@Override
|
|
|
|
|
public boolean processMessage(Message msg) {
|
2021-09-09 17:39:05 +09:00
|
|
|
final ClientInfo clientInfo;
|
|
|
|
|
final int id;
|
|
|
|
|
final int clientId = msg.arg2;
|
|
|
|
|
final ListenerArgs args;
|
2012-04-17 23:15:29 -07:00
|
|
|
switch (msg.what) {
|
2023-01-14 00:31:09 +08:00
|
|
|
case NsdManager.DISCOVER_SERVICES: {
|
2021-11-17 09:35:33 +08:00
|
|
|
if (DBG) Log.d(TAG, "Discover services");
|
2021-09-09 17:39:05 +09:00
|
|
|
args = (ListenerArgs) msg.obj;
|
|
|
|
|
clientInfo = mClients.get(args.connector);
|
2022-08-16 07:21:55 +00:00
|
|
|
// If the binder death notification for a INsdManagerCallback was received
|
|
|
|
|
// before any calls are received by NsdService, the clientInfo would be
|
|
|
|
|
// cleared and cause NPE. Add a null check here to prevent this corner case.
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, "Unknown connector in discovery");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-05-03 16:44:27 -07:00
|
|
|
|
|
|
|
|
if (requestLimitReached(clientInfo)) {
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onDiscoverServicesFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_MAX_LIMIT);
|
2012-04-17 23:15:29 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2012-05-03 16:44:27 -07:00
|
|
|
|
2023-01-13 22:57:24 +08:00
|
|
|
final NsdServiceInfo info = args.serviceInfo;
|
2012-05-03 16:44:27 -07:00
|
|
|
id = getUniqueId();
|
2023-01-13 22:57:24 +08:00
|
|
|
if (mMdnsDiscoveryManager != null) {
|
|
|
|
|
final String serviceType = constructServiceType(info.getServiceType());
|
|
|
|
|
if (serviceType == null) {
|
|
|
|
|
clientInfo.onDiscoverServicesFailed(clientId,
|
|
|
|
|
NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
break;
|
2012-05-03 16:44:27 -07:00
|
|
|
}
|
2023-01-13 22:57:24 +08:00
|
|
|
|
2023-01-18 15:58:03 +09:00
|
|
|
final String listenServiceType = serviceType + ".local";
|
2023-01-13 22:57:24 +08:00
|
|
|
maybeStartMonitoringSockets();
|
|
|
|
|
final MdnsListener listener =
|
2023-01-18 15:58:03 +09:00
|
|
|
new DiscoveryListener(clientId, id, info, listenServiceType);
|
2023-01-13 22:57:24 +08:00
|
|
|
final MdnsSearchOptions options = MdnsSearchOptions.newBuilder()
|
|
|
|
|
.setNetwork(info.getNetwork())
|
|
|
|
|
.setIsPassiveMode(true)
|
|
|
|
|
.build();
|
2023-01-18 15:58:03 +09:00
|
|
|
mMdnsDiscoveryManager.registerListener(
|
|
|
|
|
listenServiceType, listener, options);
|
2023-01-13 22:57:24 +08:00
|
|
|
storeListenerMap(clientId, id, listener, clientInfo);
|
|
|
|
|
clientInfo.onDiscoverServicesStarted(clientId, info);
|
2012-04-17 23:15:29 -07:00
|
|
|
} else {
|
2023-01-13 22:57:24 +08:00
|
|
|
maybeStartDaemon();
|
|
|
|
|
if (discoverServices(id, info)) {
|
|
|
|
|
if (DBG) {
|
|
|
|
|
Log.d(TAG, "Discover " + msg.arg2 + " " + id
|
|
|
|
|
+ info.getServiceType());
|
|
|
|
|
}
|
|
|
|
|
storeRequestMap(clientId, id, clientInfo, msg.what);
|
|
|
|
|
clientInfo.onDiscoverServicesStarted(clientId, info);
|
|
|
|
|
} else {
|
|
|
|
|
stopServiceDiscovery(id);
|
|
|
|
|
clientInfo.onDiscoverServicesFailed(clientId,
|
|
|
|
|
NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
break;
|
2023-01-14 00:31:09 +08:00
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
case NsdManager.STOP_DISCOVERY:
|
2021-11-17 09:35:33 +08:00
|
|
|
if (DBG) Log.d(TAG, "Stop service discovery");
|
2021-09-09 17:39:05 +09:00
|
|
|
args = (ListenerArgs) msg.obj;
|
|
|
|
|
clientInfo = mClients.get(args.connector);
|
2022-08-16 07:21:55 +00:00
|
|
|
// If the binder death notification for a INsdManagerCallback was received
|
|
|
|
|
// before any calls are received by NsdService, the clientInfo would be
|
|
|
|
|
// cleared and cause NPE. Add a null check here to prevent this corner case.
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, "Unknown connector in stop discovery");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-05-03 16:44:27 -07:00
|
|
|
|
|
|
|
|
try {
|
2021-09-09 17:39:05 +09:00
|
|
|
id = clientInfo.mClientIds.get(clientId);
|
2012-05-03 16:44:27 -07:00
|
|
|
} catch (NullPointerException e) {
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onStopDiscoveryFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
2012-04-17 23:15:29 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2023-01-13 22:57:24 +08:00
|
|
|
if (mMdnsDiscoveryManager != null) {
|
|
|
|
|
final MdnsListener listener = clientInfo.mListeners.get(clientId);
|
|
|
|
|
if (listener == null) {
|
|
|
|
|
clientInfo.onStopDiscoveryFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
mMdnsDiscoveryManager.unregisterListener(
|
|
|
|
|
listener.getListenedServiceType(), listener);
|
|
|
|
|
removeListenerMap(clientId, id, clientInfo);
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onStopDiscoverySucceeded(clientId);
|
2012-04-17 23:15:29 -07:00
|
|
|
} else {
|
2023-01-13 22:57:24 +08:00
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
|
|
|
|
if (stopServiceDiscovery(id)) {
|
|
|
|
|
clientInfo.onStopDiscoverySucceeded(clientId);
|
|
|
|
|
} else {
|
|
|
|
|
clientInfo.onStopDiscoveryFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NsdManager.REGISTER_SERVICE:
|
2021-11-17 09:35:33 +08:00
|
|
|
if (DBG) Log.d(TAG, "Register service");
|
2021-09-09 17:39:05 +09:00
|
|
|
args = (ListenerArgs) msg.obj;
|
|
|
|
|
clientInfo = mClients.get(args.connector);
|
2022-08-16 07:21:55 +00:00
|
|
|
// If the binder death notification for a INsdManagerCallback was received
|
|
|
|
|
// before any calls are received by NsdService, the clientInfo would be
|
|
|
|
|
// cleared and cause NPE. Add a null check here to prevent this corner case.
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, "Unknown connector in registration");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-03 16:44:27 -07:00
|
|
|
if (requestLimitReached(clientInfo)) {
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onRegisterServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_MAX_LIMIT);
|
2012-05-03 16:44:27 -07:00
|
|
|
break;
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2012-05-03 16:44:27 -07:00
|
|
|
id = getUniqueId();
|
2023-01-18 15:58:03 +09:00
|
|
|
if (mAdvertiser != null) {
|
|
|
|
|
final NsdServiceInfo serviceInfo = args.serviceInfo;
|
|
|
|
|
final String serviceType = serviceInfo.getServiceType();
|
|
|
|
|
final String registerServiceType = constructServiceType(serviceType);
|
|
|
|
|
if (registerServiceType == null) {
|
|
|
|
|
Log.e(TAG, "Invalid service type: " + serviceType);
|
|
|
|
|
clientInfo.onRegisterServiceFailed(clientId,
|
|
|
|
|
NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
serviceInfo.setServiceType(registerServiceType);
|
|
|
|
|
serviceInfo.setServiceName(truncateServiceName(
|
|
|
|
|
serviceInfo.getServiceName()));
|
|
|
|
|
|
|
|
|
|
maybeStartMonitoringSockets();
|
|
|
|
|
mAdvertiser.addService(id, serviceInfo);
|
2021-09-09 17:39:05 +09:00
|
|
|
storeRequestMap(clientId, id, clientInfo, msg.what);
|
2012-04-17 23:15:29 -07:00
|
|
|
} else {
|
2023-01-18 15:58:03 +09:00
|
|
|
maybeStartDaemon();
|
|
|
|
|
if (registerService(id, args.serviceInfo)) {
|
|
|
|
|
if (DBG) Log.d(TAG, "Register " + clientId + " " + id);
|
|
|
|
|
storeRequestMap(clientId, id, clientInfo, msg.what);
|
|
|
|
|
// Return success after mDns reports success
|
|
|
|
|
} else {
|
|
|
|
|
unregisterService(id);
|
|
|
|
|
clientInfo.onRegisterServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NsdManager.UNREGISTER_SERVICE:
|
2021-11-17 09:35:33 +08:00
|
|
|
if (DBG) Log.d(TAG, "unregister service");
|
2021-09-09 17:39:05 +09:00
|
|
|
args = (ListenerArgs) msg.obj;
|
|
|
|
|
clientInfo = mClients.get(args.connector);
|
2022-08-16 07:21:55 +00:00
|
|
|
// If the binder death notification for a INsdManagerCallback was received
|
|
|
|
|
// before any calls are received by NsdService, the clientInfo would be
|
|
|
|
|
// cleared and cause NPE. Add a null check here to prevent this corner case.
|
2021-09-09 17:39:05 +09:00
|
|
|
if (clientInfo == null) {
|
2021-11-17 09:35:33 +08:00
|
|
|
Log.e(TAG, "Unknown connector in unregistration");
|
2012-05-03 16:44:27 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2021-09-09 17:39:05 +09:00
|
|
|
id = clientInfo.mClientIds.get(clientId);
|
|
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
2023-01-18 15:58:03 +09:00
|
|
|
|
|
|
|
|
if (mAdvertiser != null) {
|
|
|
|
|
mAdvertiser.removeService(id);
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onUnregisterServiceSucceeded(clientId);
|
2012-04-17 23:15:29 -07:00
|
|
|
} else {
|
2023-01-18 15:58:03 +09:00
|
|
|
if (unregisterService(id)) {
|
|
|
|
|
clientInfo.onUnregisterServiceSucceeded(clientId);
|
|
|
|
|
} else {
|
|
|
|
|
clientInfo.onUnregisterServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
break;
|
2023-01-14 00:31:09 +08:00
|
|
|
case NsdManager.RESOLVE_SERVICE: {
|
2021-11-17 09:35:33 +08:00
|
|
|
if (DBG) Log.d(TAG, "Resolve service");
|
2021-09-09 17:39:05 +09:00
|
|
|
args = (ListenerArgs) msg.obj;
|
|
|
|
|
clientInfo = mClients.get(args.connector);
|
2022-08-16 07:21:55 +00:00
|
|
|
// If the binder death notification for a INsdManagerCallback was received
|
|
|
|
|
// before any calls are received by NsdService, the clientInfo would be
|
|
|
|
|
// cleared and cause NPE. Add a null check here to prevent this corner case.
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, "Unknown connector in resolution");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-05-03 16:44:27 -07:00
|
|
|
|
2023-01-14 00:31:09 +08:00
|
|
|
final NsdServiceInfo info = args.serviceInfo;
|
2012-05-03 16:44:27 -07:00
|
|
|
id = getUniqueId();
|
2023-01-14 00:31:09 +08:00
|
|
|
if (mMdnsDiscoveryManager != null) {
|
|
|
|
|
final String serviceType = constructServiceType(info.getServiceType());
|
|
|
|
|
if (serviceType == null) {
|
|
|
|
|
clientInfo.onResolveServiceFailed(clientId,
|
|
|
|
|
NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-01-18 15:58:03 +09:00
|
|
|
final String resolveServiceType = serviceType + ".local";
|
2023-01-14 00:31:09 +08:00
|
|
|
|
|
|
|
|
maybeStartMonitoringSockets();
|
|
|
|
|
final MdnsListener listener =
|
2023-01-18 15:58:03 +09:00
|
|
|
new ResolutionListener(clientId, id, info, resolveServiceType);
|
2023-01-14 00:31:09 +08:00
|
|
|
final MdnsSearchOptions options = MdnsSearchOptions.newBuilder()
|
|
|
|
|
.setNetwork(info.getNetwork())
|
|
|
|
|
.setIsPassiveMode(true)
|
|
|
|
|
.build();
|
2023-01-18 15:58:03 +09:00
|
|
|
mMdnsDiscoveryManager.registerListener(
|
|
|
|
|
resolveServiceType, listener, options);
|
2023-01-14 00:31:09 +08:00
|
|
|
storeListenerMap(clientId, id, listener, clientInfo);
|
2012-04-17 23:15:29 -07:00
|
|
|
} else {
|
2023-01-14 00:31:09 +08:00
|
|
|
if (clientInfo.mResolvedService != null) {
|
|
|
|
|
clientInfo.onResolveServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_ALREADY_ACTIVE);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maybeStartDaemon();
|
|
|
|
|
if (resolveService(id, args.serviceInfo)) {
|
|
|
|
|
clientInfo.mResolvedService = new NsdServiceInfo();
|
|
|
|
|
storeRequestMap(clientId, id, clientInfo, msg.what);
|
|
|
|
|
} else {
|
|
|
|
|
clientInfo.onResolveServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
break;
|
2023-01-14 00:31:09 +08:00
|
|
|
}
|
2022-12-26 09:24:42 +00:00
|
|
|
case NsdManager.STOP_RESOLUTION:
|
|
|
|
|
if (DBG) Log.d(TAG, "Stop service resolution");
|
|
|
|
|
args = (ListenerArgs) msg.obj;
|
|
|
|
|
clientInfo = mClients.get(args.connector);
|
|
|
|
|
// If the binder death notification for a INsdManagerCallback was received
|
|
|
|
|
// before any calls are received by NsdService, the clientInfo would be
|
|
|
|
|
// cleared and cause NPE. Add a null check here to prevent this corner case.
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, "Unknown connector in stop resolution");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id = clientInfo.mClientIds.get(clientId);
|
|
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
|
|
|
|
if (stopResolveService(id)) {
|
|
|
|
|
clientInfo.onStopResolutionSucceeded(clientId);
|
|
|
|
|
} else {
|
|
|
|
|
clientInfo.onStopResolutionFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_OPERATION_NOT_RUNNING);
|
|
|
|
|
}
|
|
|
|
|
clientInfo.mResolvedService = null;
|
|
|
|
|
// TODO: Implement the stop resolution with MdnsDiscoveryManager.
|
|
|
|
|
break;
|
2022-12-27 08:48:48 +00:00
|
|
|
case NsdManager.REGISTER_SERVICE_CALLBACK:
|
|
|
|
|
if (DBG) Log.d(TAG, "Register a service callback");
|
|
|
|
|
args = (ListenerArgs) msg.obj;
|
|
|
|
|
clientInfo = mClients.get(args.connector);
|
|
|
|
|
// If the binder death notification for a INsdManagerCallback was received
|
|
|
|
|
// before any calls are received by NsdService, the clientInfo would be
|
|
|
|
|
// cleared and cause NPE. Add a null check here to prevent this corner case.
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, "Unknown connector in callback registration");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (clientInfo.mRegisteredService != null) {
|
|
|
|
|
clientInfo.onServiceInfoCallbackRegistrationFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_ALREADY_ACTIVE);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maybeStartDaemon();
|
|
|
|
|
id = getUniqueId();
|
|
|
|
|
if (resolveService(id, args.serviceInfo)) {
|
|
|
|
|
clientInfo.mRegisteredService = new NsdServiceInfo();
|
|
|
|
|
clientInfo.mClientIdForServiceUpdates = clientId;
|
|
|
|
|
storeRequestMap(clientId, id, clientInfo, msg.what);
|
|
|
|
|
} else {
|
|
|
|
|
clientInfo.onServiceInfoCallbackRegistrationFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_BAD_PARAMETERS);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NsdManager.UNREGISTER_SERVICE_CALLBACK:
|
|
|
|
|
if (DBG) Log.d(TAG, "Unregister a service callback");
|
|
|
|
|
args = (ListenerArgs) msg.obj;
|
|
|
|
|
clientInfo = mClients.get(args.connector);
|
|
|
|
|
// If the binder death notification for a INsdManagerCallback was received
|
|
|
|
|
// before any calls are received by NsdService, the clientInfo would be
|
|
|
|
|
// cleared and cause NPE. Add a null check here to prevent this corner case.
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, "Unknown connector in callback unregistration");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id = clientInfo.mClientIds.get(clientId);
|
|
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
|
|
|
|
if (stopResolveService(id)) {
|
|
|
|
|
clientInfo.onServiceInfoCallbackUnregistered(clientId);
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Failed to unregister service info callback");
|
|
|
|
|
}
|
|
|
|
|
clearRegisteredServiceInfo(clientInfo);
|
|
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
case MDNS_SERVICE_EVENT:
|
|
|
|
|
if (!handleMDnsServiceEvent(msg.arg1, msg.arg2, msg.obj)) {
|
2017-04-05 14:43:29 +09:00
|
|
|
return NOT_HANDLED;
|
2012-08-05 13:14:12 -07:00
|
|
|
}
|
2012-05-03 16:44:27 -07:00
|
|
|
break;
|
2023-01-13 23:26:49 +08:00
|
|
|
case MDNS_DISCOVERY_MANAGER_EVENT:
|
|
|
|
|
if (!handleMdnsDiscoveryManagerEvent(msg.arg1, msg.arg2, msg.obj)) {
|
|
|
|
|
return NOT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2012-04-17 23:15:29 -07:00
|
|
|
default:
|
2017-04-05 14:43:29 +09:00
|
|
|
return NOT_HANDLED;
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
2017-04-05 14:43:29 +09:00
|
|
|
return HANDLED;
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
2012-08-05 13:14:12 -07:00
|
|
|
|
2022-12-27 08:48:48 +00:00
|
|
|
private void notifyResolveFailedResult(boolean isListenedToUpdates, int clientId,
|
|
|
|
|
ClientInfo clientInfo, int error) {
|
|
|
|
|
if (isListenedToUpdates) {
|
|
|
|
|
clientInfo.onServiceInfoCallbackRegistrationFailed(clientId, error);
|
|
|
|
|
clearRegisteredServiceInfo(clientInfo);
|
|
|
|
|
} else {
|
|
|
|
|
// The resolve API always returned FAILURE_INTERNAL_ERROR on error; keep it
|
|
|
|
|
// for backwards compatibility.
|
|
|
|
|
clientInfo.onResolveServiceFailed(clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
clientInfo.mResolvedService = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:58:32 +08:00
|
|
|
private boolean handleMDnsServiceEvent(int code, int id, Object obj) {
|
2012-08-05 13:14:12 -07:00
|
|
|
NsdServiceInfo servInfo;
|
|
|
|
|
ClientInfo clientInfo = mIdToClientInfoMap.get(id);
|
|
|
|
|
if (clientInfo == null) {
|
2022-02-10 21:58:32 +08:00
|
|
|
Log.e(TAG, String.format("id %d for %d has no client mapping", id, code));
|
2017-04-05 14:43:29 +09:00
|
|
|
return false;
|
2012-08-05 13:14:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This goes in response as msg.arg2 */
|
2014-04-25 18:39:07 -07:00
|
|
|
int clientId = clientInfo.getClientId(id);
|
|
|
|
|
if (clientId < 0) {
|
2013-06-25 19:45:03 -07:00
|
|
|
// This can happen because of race conditions. For example,
|
|
|
|
|
// SERVICE_FOUND may race with STOP_SERVICE_DISCOVERY,
|
|
|
|
|
// and we may get in this situation.
|
2022-02-10 21:58:32 +08:00
|
|
|
Log.d(TAG, String.format("%d for listener id %d that is no longer active",
|
|
|
|
|
code, id));
|
2017-04-05 14:43:29 +09:00
|
|
|
return false;
|
2012-08-05 13:14:12 -07:00
|
|
|
}
|
2017-04-05 14:06:11 +09:00
|
|
|
if (DBG) {
|
2022-02-10 21:58:32 +08:00
|
|
|
Log.d(TAG, String.format("MDns service event code:%d id=%d", code, id));
|
2012-08-05 13:14:12 -07:00
|
|
|
}
|
|
|
|
|
switch (code) {
|
2022-02-10 21:58:32 +08:00
|
|
|
case IMDnsEventListener.SERVICE_FOUND: {
|
|
|
|
|
final DiscoveryInfo info = (DiscoveryInfo) obj;
|
|
|
|
|
final String name = info.serviceName;
|
|
|
|
|
final String type = info.registrationType;
|
|
|
|
|
servInfo = new NsdServiceInfo(name, type);
|
|
|
|
|
final int foundNetId = info.netId;
|
2021-12-16 15:31:16 +09:00
|
|
|
if (foundNetId == 0L) {
|
|
|
|
|
// Ignore services that do not have a Network: they are not usable
|
|
|
|
|
// by apps, as they would need privileged permissions to use
|
|
|
|
|
// interfaces that do not have an associated Network.
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-01-23 19:14:57 +09:00
|
|
|
if (foundNetId == INetd.DUMMY_NET_ID) {
|
|
|
|
|
// Ignore services on the dummy0 interface: they are only seen when
|
|
|
|
|
// discovering locally advertised services, and are not reachable
|
|
|
|
|
// through that interface.
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-05-30 12:42:24 +09:00
|
|
|
setServiceNetworkForCallback(servInfo, info.netId, info.interfaceIdx);
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onServiceFound(clientId, servInfo);
|
2012-08-05 13:14:12 -07:00
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
}
|
|
|
|
|
case IMDnsEventListener.SERVICE_LOST: {
|
|
|
|
|
final DiscoveryInfo info = (DiscoveryInfo) obj;
|
|
|
|
|
final String name = info.serviceName;
|
|
|
|
|
final String type = info.registrationType;
|
|
|
|
|
final int lostNetId = info.netId;
|
|
|
|
|
servInfo = new NsdServiceInfo(name, type);
|
2022-05-30 12:42:24 +09:00
|
|
|
// The network could be set to null (netId 0) if it was torn down when the
|
|
|
|
|
// service is lost
|
|
|
|
|
// TODO: avoid returning null in that case, possibly by remembering
|
|
|
|
|
// found services on the same interface index and their network at the time
|
|
|
|
|
setServiceNetworkForCallback(servInfo, lostNetId, info.interfaceIdx);
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onServiceLost(clientId, servInfo);
|
2022-12-27 08:48:48 +00:00
|
|
|
// TODO: also support registered service lost when not discovering
|
|
|
|
|
clientInfo.maybeNotifyRegisteredServiceLost(servInfo);
|
2012-08-05 13:14:12 -07:00
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
}
|
|
|
|
|
case IMDnsEventListener.SERVICE_DISCOVERY_FAILED:
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onDiscoverServicesFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
2012-08-05 13:14:12 -07:00
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
case IMDnsEventListener.SERVICE_REGISTERED: {
|
|
|
|
|
final RegistrationInfo info = (RegistrationInfo) obj;
|
|
|
|
|
final String name = info.serviceName;
|
|
|
|
|
servInfo = new NsdServiceInfo(name, null /* serviceType */);
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onRegisterServiceSucceeded(clientId, servInfo);
|
2012-08-05 13:14:12 -07:00
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
}
|
|
|
|
|
case IMDnsEventListener.SERVICE_REGISTRATION_FAILED:
|
2021-09-09 17:39:05 +09:00
|
|
|
clientInfo.onRegisterServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
2012-08-05 13:14:12 -07:00
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
case IMDnsEventListener.SERVICE_RESOLVED: {
|
|
|
|
|
final ResolutionInfo info = (ResolutionInfo) obj;
|
2014-09-03 15:45:59 -07:00
|
|
|
int index = 0;
|
2022-02-10 21:58:32 +08:00
|
|
|
final String fullName = info.serviceFullName;
|
|
|
|
|
while (index < fullName.length() && fullName.charAt(index) != '.') {
|
|
|
|
|
if (fullName.charAt(index) == '\\') {
|
2014-09-03 15:45:59 -07:00
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
++index;
|
|
|
|
|
}
|
2022-02-10 21:58:32 +08:00
|
|
|
if (index >= fullName.length()) {
|
|
|
|
|
Log.e(TAG, "Invalid service found " + fullName);
|
2012-08-05 13:14:12 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2021-12-16 15:31:16 +09:00
|
|
|
|
2022-04-12 07:18:23 +00:00
|
|
|
String name = unescape(fullName.substring(0, index));
|
2022-02-10 21:58:32 +08:00
|
|
|
String rest = fullName.substring(index);
|
2012-08-05 13:14:12 -07:00
|
|
|
String type = rest.replace(".local.", "");
|
|
|
|
|
|
2022-12-27 08:48:48 +00:00
|
|
|
final boolean isListenedToUpdates =
|
|
|
|
|
clientId == clientInfo.mClientIdForServiceUpdates;
|
|
|
|
|
final NsdServiceInfo serviceInfo = isListenedToUpdates
|
|
|
|
|
? clientInfo.mRegisteredService : clientInfo.mResolvedService;
|
|
|
|
|
|
|
|
|
|
serviceInfo.setServiceName(name);
|
|
|
|
|
serviceInfo.setServiceType(type);
|
|
|
|
|
serviceInfo.setPort(info.port);
|
|
|
|
|
serviceInfo.setTxtRecords(info.txtRecord);
|
2021-12-16 15:31:16 +09:00
|
|
|
// Network will be added after SERVICE_GET_ADDR_SUCCESS
|
2012-08-05 13:14:12 -07:00
|
|
|
|
|
|
|
|
stopResolveService(id);
|
2013-11-12 15:36:37 -08:00
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
|
|
|
|
|
2022-02-10 21:58:32 +08:00
|
|
|
final int id2 = getUniqueId();
|
|
|
|
|
if (getAddrInfo(id2, info.hostname, info.interfaceIdx)) {
|
2014-03-07 14:48:22 -08:00
|
|
|
storeRequestMap(clientId, id2, clientInfo, NsdManager.RESOLVE_SERVICE);
|
2013-11-12 15:36:37 -08:00
|
|
|
} else {
|
2022-12-27 08:48:48 +00:00
|
|
|
notifyResolveFailedResult(isListenedToUpdates, clientId, clientInfo,
|
|
|
|
|
NsdManager.FAILURE_BAD_PARAMETERS);
|
2012-08-05 13:14:12 -07:00
|
|
|
}
|
|
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
}
|
|
|
|
|
case IMDnsEventListener.SERVICE_RESOLUTION_FAILED:
|
2012-08-05 13:14:12 -07:00
|
|
|
/* NNN resolveId errorCode */
|
|
|
|
|
stopResolveService(id);
|
|
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
2022-12-27 08:48:48 +00:00
|
|
|
notifyResolveFailedResult(
|
|
|
|
|
clientId == clientInfo.mClientIdForServiceUpdates,
|
|
|
|
|
clientId, clientInfo, NsdManager.FAILURE_BAD_PARAMETERS);
|
2012-08-05 13:14:12 -07:00
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
case IMDnsEventListener.SERVICE_GET_ADDR_FAILED:
|
2012-08-05 13:14:12 -07:00
|
|
|
/* NNN resolveId errorCode */
|
|
|
|
|
stopGetAddrInfo(id);
|
|
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
2022-12-27 08:48:48 +00:00
|
|
|
notifyResolveFailedResult(
|
|
|
|
|
clientId == clientInfo.mClientIdForServiceUpdates,
|
|
|
|
|
clientId, clientInfo, NsdManager.FAILURE_BAD_PARAMETERS);
|
2012-08-05 13:14:12 -07:00
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
case IMDnsEventListener.SERVICE_GET_ADDR_SUCCESS: {
|
2021-12-16 15:31:16 +09:00
|
|
|
/* NNN resolveId hostname ttl addr interfaceIdx netId */
|
2022-02-10 21:58:32 +08:00
|
|
|
final GetAddressInfo info = (GetAddressInfo) obj;
|
|
|
|
|
final String address = info.address;
|
|
|
|
|
final int netId = info.netId;
|
2021-12-16 15:31:16 +09:00
|
|
|
InetAddress serviceHost = null;
|
|
|
|
|
try {
|
2022-02-10 21:58:32 +08:00
|
|
|
serviceHost = InetAddress.getByName(address);
|
2021-12-16 15:31:16 +09:00
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
Log.wtf(TAG, "Invalid host in GET_ADDR_SUCCESS", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the resolved service is on an interface without a network, consider it
|
|
|
|
|
// as a failure: it would not be usable by apps as they would need
|
|
|
|
|
// privileged permissions.
|
2022-12-27 08:48:48 +00:00
|
|
|
if (clientId == clientInfo.mClientIdForServiceUpdates) {
|
|
|
|
|
if (netId != NETID_UNSET && serviceHost != null) {
|
|
|
|
|
setServiceNetworkForCallback(clientInfo.mRegisteredService,
|
|
|
|
|
netId, info.interfaceIdx);
|
|
|
|
|
final List<InetAddress> addresses =
|
|
|
|
|
clientInfo.mRegisteredService.getHostAddresses();
|
|
|
|
|
addresses.add(serviceHost);
|
|
|
|
|
clientInfo.mRegisteredService.setHostAddresses(addresses);
|
|
|
|
|
clientInfo.onServiceUpdated(
|
|
|
|
|
clientId, clientInfo.mRegisteredService);
|
|
|
|
|
} else {
|
|
|
|
|
stopGetAddrInfo(id);
|
|
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
|
|
|
|
clearRegisteredServiceInfo(clientInfo);
|
|
|
|
|
clientInfo.onServiceInfoCallbackRegistrationFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_BAD_PARAMETERS);
|
|
|
|
|
}
|
2021-12-16 15:31:16 +09:00
|
|
|
} else {
|
2022-12-27 08:48:48 +00:00
|
|
|
if (netId != NETID_UNSET && serviceHost != null) {
|
|
|
|
|
clientInfo.mResolvedService.setHost(serviceHost);
|
|
|
|
|
setServiceNetworkForCallback(clientInfo.mResolvedService,
|
|
|
|
|
netId, info.interfaceIdx);
|
|
|
|
|
clientInfo.onResolveServiceSucceeded(
|
|
|
|
|
clientId, clientInfo.mResolvedService);
|
|
|
|
|
} else {
|
|
|
|
|
clientInfo.onResolveServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
|
|
|
|
stopGetAddrInfo(id);
|
|
|
|
|
removeRequestMap(clientId, id, clientInfo);
|
|
|
|
|
clientInfo.mResolvedService = null;
|
2012-08-05 13:14:12 -07:00
|
|
|
}
|
|
|
|
|
break;
|
2022-02-10 21:58:32 +08:00
|
|
|
}
|
2012-08-05 13:14:12 -07:00
|
|
|
default:
|
2017-04-05 14:43:29 +09:00
|
|
|
return false;
|
2012-08-05 13:14:12 -07:00
|
|
|
}
|
2017-04-05 14:43:29 +09:00
|
|
|
return true;
|
2012-08-05 13:14:12 -07:00
|
|
|
}
|
2023-01-13 23:26:49 +08:00
|
|
|
|
|
|
|
|
private NsdServiceInfo buildNsdServiceInfoFromMdnsEvent(final MdnsEvent event) {
|
|
|
|
|
final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo;
|
|
|
|
|
final String serviceType = event.mRequestedServiceType;
|
|
|
|
|
final String serviceName = serviceInfo.getServiceInstanceName();
|
|
|
|
|
final NsdServiceInfo servInfo = new NsdServiceInfo(serviceName, serviceType);
|
|
|
|
|
final Network network = serviceInfo.getNetwork();
|
|
|
|
|
setServiceNetworkForCallback(
|
|
|
|
|
servInfo,
|
|
|
|
|
network == null ? NETID_UNSET : network.netId,
|
|
|
|
|
serviceInfo.getInterfaceIndex());
|
|
|
|
|
return servInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean handleMdnsDiscoveryManagerEvent(
|
|
|
|
|
int transactionId, int code, Object obj) {
|
|
|
|
|
final ClientInfo clientInfo = mIdToClientInfoMap.get(transactionId);
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, String.format(
|
|
|
|
|
"id %d for %d has no client mapping", transactionId, code));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final MdnsEvent event = (MdnsEvent) obj;
|
|
|
|
|
final int clientId = event.mClientId;
|
2023-01-13 23:56:34 +08:00
|
|
|
final NsdServiceInfo info = buildNsdServiceInfoFromMdnsEvent(event);
|
2023-01-13 23:26:49 +08:00
|
|
|
if (DBG) {
|
|
|
|
|
Log.d(TAG, String.format("MdnsDiscoveryManager event code=%s transactionId=%d",
|
|
|
|
|
NsdManager.nameOf(code), transactionId));
|
|
|
|
|
}
|
|
|
|
|
switch (code) {
|
|
|
|
|
case NsdManager.SERVICE_FOUND:
|
2023-01-13 23:56:34 +08:00
|
|
|
clientInfo.onServiceFound(clientId, info);
|
|
|
|
|
break;
|
|
|
|
|
case NsdManager.SERVICE_LOST:
|
|
|
|
|
clientInfo.onServiceLost(clientId, info);
|
2023-01-13 23:26:49 +08:00
|
|
|
break;
|
2023-01-14 00:31:09 +08:00
|
|
|
case NsdManager.RESOLVE_SERVICE_SUCCEEDED: {
|
|
|
|
|
final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo;
|
|
|
|
|
// Add '.' in front of the service type that aligns with historical behavior
|
|
|
|
|
info.setServiceType("." + event.mRequestedServiceType);
|
|
|
|
|
info.setPort(serviceInfo.getPort());
|
|
|
|
|
|
|
|
|
|
Map<String, String> attrs = serviceInfo.getAttributes();
|
|
|
|
|
for (Map.Entry<String, String> kv : attrs.entrySet()) {
|
|
|
|
|
final String key = kv.getKey();
|
|
|
|
|
try {
|
|
|
|
|
info.setAttribute(key, serviceInfo.getAttributeAsBytes(key));
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
Log.e(TAG, "Invalid attribute", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
if (serviceInfo.getIpv4Address() != null) {
|
|
|
|
|
info.setHost(InetAddresses.parseNumericAddress(
|
|
|
|
|
serviceInfo.getIpv4Address()));
|
|
|
|
|
} else {
|
|
|
|
|
info.setHost(InetAddresses.parseNumericAddress(
|
|
|
|
|
serviceInfo.getIpv6Address()));
|
|
|
|
|
}
|
|
|
|
|
clientInfo.onResolveServiceSucceeded(clientId, info);
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
Log.wtf(TAG, "Invalid address in RESOLVE_SERVICE_SUCCEEDED", e);
|
|
|
|
|
clientInfo.onResolveServiceFailed(
|
|
|
|
|
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unregister the listener immediately like IMDnsEventListener design
|
|
|
|
|
final MdnsListener listener = clientInfo.mListeners.get(clientId);
|
|
|
|
|
mMdnsDiscoveryManager.unregisterListener(
|
|
|
|
|
listener.getListenedServiceType(), listener);
|
|
|
|
|
removeListenerMap(clientId, transactionId, clientInfo);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-01-13 23:26:49 +08:00
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
2012-03-22 17:01:39 -07:00
|
|
|
|
2022-05-30 12:42:24 +09:00
|
|
|
private static void setServiceNetworkForCallback(NsdServiceInfo info, int netId, int ifaceIdx) {
|
|
|
|
|
switch (netId) {
|
|
|
|
|
case NETID_UNSET:
|
|
|
|
|
info.setNetwork(null);
|
|
|
|
|
break;
|
|
|
|
|
case INetd.LOCAL_NET_ID:
|
|
|
|
|
// Special case for LOCAL_NET_ID: Networks on netId 99 are not generally
|
|
|
|
|
// visible / usable for apps, so do not return it. Store the interface
|
|
|
|
|
// index instead, so at least if the client tries to resolve the service
|
|
|
|
|
// with that NsdServiceInfo, it will be done on the same interface.
|
|
|
|
|
// If they recreate the NsdServiceInfo themselves, resolution would be
|
|
|
|
|
// done on all interfaces as before T, which should also work.
|
|
|
|
|
info.setNetwork(null);
|
|
|
|
|
info.setInterfaceIndex(ifaceIdx);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
info.setNetwork(new Network(netId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 07:18:23 +00:00
|
|
|
// The full service name is escaped from standard DNS rules on mdnsresponder, making it suitable
|
|
|
|
|
// for passing to standard system DNS APIs such as res_query() . Thus, make the service name
|
|
|
|
|
// unescape for getting right service address. See "Notes on DNS Name Escaping" on
|
|
|
|
|
// external/mdnsresponder/mDNSShared/dns_sd.h for more details.
|
|
|
|
|
private String unescape(String s) {
|
|
|
|
|
StringBuilder sb = new StringBuilder(s.length());
|
|
|
|
|
for (int i = 0; i < s.length(); ++i) {
|
|
|
|
|
char c = s.charAt(i);
|
|
|
|
|
if (c == '\\') {
|
|
|
|
|
if (++i >= s.length()) {
|
|
|
|
|
Log.e(TAG, "Unexpected end of escape sequence in: " + s);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
c = s.charAt(i);
|
|
|
|
|
if (c != '.' && c != '\\') {
|
|
|
|
|
if (i + 2 >= s.length()) {
|
|
|
|
|
Log.e(TAG, "Unexpected end of escape sequence in: " + s);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
c = (char) ((c - '0') * 100 + (s.charAt(i + 1) - '0') * 10
|
|
|
|
|
+ (s.charAt(i + 2) - '0'));
|
|
|
|
|
i += 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append(c);
|
|
|
|
|
}
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 11:35:06 +09:00
|
|
|
@VisibleForTesting
|
2022-02-10 21:58:32 +08:00
|
|
|
NsdService(Context ctx, Handler handler, long cleanupDelayMs) {
|
2023-01-12 13:42:07 +08:00
|
|
|
this(ctx, handler, cleanupDelayMs, new Dependencies());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
NsdService(Context ctx, Handler handler, long cleanupDelayMs, Dependencies deps) {
|
2021-06-13 16:52:05 +00:00
|
|
|
mCleanupDelayMs = cleanupDelayMs;
|
2017-04-24 11:35:06 +09:00
|
|
|
mContext = ctx;
|
|
|
|
|
mNsdStateMachine = new NsdStateMachine(TAG, handler);
|
2012-04-17 23:15:29 -07:00
|
|
|
mNsdStateMachine.start();
|
2022-02-10 21:58:32 +08:00
|
|
|
mMDnsManager = ctx.getSystemService(MDnsManager.class);
|
|
|
|
|
mMDnsEventCallback = new MDnsEventCallback(mNsdStateMachine);
|
2023-01-18 15:58:03 +09:00
|
|
|
|
|
|
|
|
final boolean discoveryManagerEnabled = deps.isMdnsDiscoveryManagerEnabled(ctx);
|
|
|
|
|
final boolean advertiserEnabled = deps.isMdnsAdvertiserEnabled(ctx);
|
|
|
|
|
if (discoveryManagerEnabled || advertiserEnabled) {
|
2023-01-12 13:42:07 +08:00
|
|
|
mMdnsSocketProvider = deps.makeMdnsSocketProvider(ctx, handler.getLooper());
|
2023-01-18 15:58:03 +09:00
|
|
|
} else {
|
|
|
|
|
mMdnsSocketProvider = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (discoveryManagerEnabled) {
|
2023-01-12 13:42:07 +08:00
|
|
|
mMdnsSocketClient =
|
|
|
|
|
new MdnsMultinetworkSocketClient(handler.getLooper(), mMdnsSocketProvider);
|
|
|
|
|
mMdnsDiscoveryManager =
|
|
|
|
|
deps.makeMdnsDiscoveryManager(new ExecutorProvider(), mMdnsSocketClient);
|
|
|
|
|
handler.post(() -> mMdnsSocketClient.setCallback(mMdnsDiscoveryManager));
|
|
|
|
|
} else {
|
|
|
|
|
mMdnsSocketClient = null;
|
|
|
|
|
mMdnsDiscoveryManager = null;
|
|
|
|
|
}
|
2023-01-18 15:58:03 +09:00
|
|
|
|
|
|
|
|
if (advertiserEnabled) {
|
|
|
|
|
mAdvertiser = deps.makeMdnsAdvertiser(handler.getLooper(), mMdnsSocketProvider,
|
|
|
|
|
new AdvertiserCallback());
|
|
|
|
|
} else {
|
|
|
|
|
mAdvertiser = null;
|
|
|
|
|
}
|
2023-01-12 13:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dependencies of NsdService, for injection in tests.
|
|
|
|
|
*/
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
public static class Dependencies {
|
|
|
|
|
/**
|
2023-01-18 15:58:03 +09:00
|
|
|
* Check whether the MdnsDiscoveryManager feature is enabled.
|
2023-01-12 13:42:07 +08:00
|
|
|
*
|
|
|
|
|
* @param context The global context information about an app environment.
|
2023-01-18 15:58:03 +09:00
|
|
|
* @return true if the MdnsDiscoveryManager feature is enabled.
|
2023-01-12 13:42:07 +08:00
|
|
|
*/
|
|
|
|
|
public boolean isMdnsDiscoveryManagerEnabled(Context context) {
|
|
|
|
|
return DeviceConfigUtils.isFeatureEnabled(context, NAMESPACE_CONNECTIVITY,
|
|
|
|
|
MDNS_DISCOVERY_MANAGER_VERSION, false /* defaultEnabled */);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-18 15:58:03 +09:00
|
|
|
/**
|
|
|
|
|
* Check whether the MdnsAdvertiser feature is enabled.
|
|
|
|
|
*
|
|
|
|
|
* @param context The global context information about an app environment.
|
|
|
|
|
* @return true if the MdnsAdvertiser feature is enabled.
|
|
|
|
|
*/
|
|
|
|
|
public boolean isMdnsAdvertiserEnabled(Context context) {
|
|
|
|
|
return DeviceConfigUtils.isFeatureEnabled(context, NAMESPACE_CONNECTIVITY,
|
|
|
|
|
MDNS_ADVERTISER_VERSION, false /* defaultEnabled */);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:42:07 +08:00
|
|
|
/**
|
|
|
|
|
* @see MdnsDiscoveryManager
|
|
|
|
|
*/
|
|
|
|
|
public MdnsDiscoveryManager makeMdnsDiscoveryManager(
|
|
|
|
|
ExecutorProvider executorProvider, MdnsSocketClientBase socketClient) {
|
|
|
|
|
return new MdnsDiscoveryManager(executorProvider, socketClient);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-18 15:58:03 +09:00
|
|
|
/**
|
|
|
|
|
* @see MdnsAdvertiser
|
|
|
|
|
*/
|
|
|
|
|
public MdnsAdvertiser makeMdnsAdvertiser(
|
|
|
|
|
@NonNull Looper looper, @NonNull MdnsSocketProvider socketProvider,
|
|
|
|
|
@NonNull MdnsAdvertiser.AdvertiserCallback cb) {
|
|
|
|
|
return new MdnsAdvertiser(looper, socketProvider, cb);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:42:07 +08:00
|
|
|
/**
|
|
|
|
|
* @see MdnsSocketProvider
|
|
|
|
|
*/
|
|
|
|
|
public MdnsSocketProvider makeMdnsSocketProvider(Context context, Looper looper) {
|
|
|
|
|
return new MdnsSocketProvider(context, looper);
|
|
|
|
|
}
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
2022-04-08 14:48:41 +08:00
|
|
|
public static NsdService create(Context context) {
|
2017-04-24 11:35:06 +09:00
|
|
|
HandlerThread thread = new HandlerThread(TAG);
|
|
|
|
|
thread.start();
|
|
|
|
|
Handler handler = new Handler(thread.getLooper());
|
2022-02-10 21:58:32 +08:00
|
|
|
NsdService service = new NsdService(context, handler, CLEANUP_DELAY_MS);
|
2012-03-22 17:01:39 -07:00
|
|
|
return service;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:58:32 +08:00
|
|
|
private static class MDnsEventCallback extends IMDnsEventListener.Stub {
|
|
|
|
|
private final StateMachine mStateMachine;
|
|
|
|
|
|
|
|
|
|
MDnsEventCallback(StateMachine sm) {
|
|
|
|
|
mStateMachine = sm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceRegistrationStatus(final RegistrationInfo status) {
|
|
|
|
|
mStateMachine.sendMessage(
|
|
|
|
|
MDNS_SERVICE_EVENT, status.result, status.id, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceDiscoveryStatus(final DiscoveryInfo status) {
|
|
|
|
|
mStateMachine.sendMessage(
|
|
|
|
|
MDNS_SERVICE_EVENT, status.result, status.id, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceResolutionStatus(final ResolutionInfo status) {
|
|
|
|
|
mStateMachine.sendMessage(
|
|
|
|
|
MDNS_SERVICE_EVENT, status.result, status.id, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onGettingServiceAddressStatus(final GetAddressInfo status) {
|
|
|
|
|
mStateMachine.sendMessage(
|
|
|
|
|
MDNS_SERVICE_EVENT, status.result, status.id, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getInterfaceVersion() throws RemoteException {
|
|
|
|
|
return this.VERSION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getInterfaceHash() throws RemoteException {
|
|
|
|
|
return this.HASH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-18 15:58:03 +09:00
|
|
|
private class AdvertiserCallback implements MdnsAdvertiser.AdvertiserCallback {
|
|
|
|
|
@Override
|
|
|
|
|
public void onRegisterServiceSucceeded(int serviceId, NsdServiceInfo registeredInfo) {
|
|
|
|
|
final ClientInfo clientInfo = getClientInfoOrLog(serviceId);
|
|
|
|
|
if (clientInfo == null) return;
|
|
|
|
|
|
|
|
|
|
final int clientId = getClientIdOrLog(clientInfo, serviceId);
|
|
|
|
|
if (clientId < 0) return;
|
|
|
|
|
|
|
|
|
|
// onRegisterServiceSucceeded only has the service name in its info. This aligns with
|
|
|
|
|
// historical behavior.
|
|
|
|
|
final NsdServiceInfo cbInfo = new NsdServiceInfo(registeredInfo.getServiceName(), null);
|
|
|
|
|
clientInfo.onRegisterServiceSucceeded(clientId, cbInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onRegisterServiceFailed(int serviceId, int errorCode) {
|
|
|
|
|
final ClientInfo clientInfo = getClientInfoOrLog(serviceId);
|
|
|
|
|
if (clientInfo == null) return;
|
|
|
|
|
|
|
|
|
|
final int clientId = getClientIdOrLog(clientInfo, serviceId);
|
|
|
|
|
if (clientId < 0) return;
|
|
|
|
|
|
|
|
|
|
clientInfo.onRegisterServiceFailed(clientId, errorCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ClientInfo getClientInfoOrLog(int serviceId) {
|
|
|
|
|
final ClientInfo clientInfo = mIdToClientInfoMap.get(serviceId);
|
|
|
|
|
if (clientInfo == null) {
|
|
|
|
|
Log.e(TAG, String.format("Callback for service %d has no client", serviceId));
|
|
|
|
|
}
|
|
|
|
|
return clientInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getClientIdOrLog(@NonNull ClientInfo info, int serviceId) {
|
|
|
|
|
final int clientId = info.getClientId(serviceId);
|
|
|
|
|
if (clientId < 0) {
|
|
|
|
|
Log.e(TAG, String.format("Client ID not found for service %d", serviceId));
|
|
|
|
|
}
|
|
|
|
|
return clientId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 17:39:05 +09:00
|
|
|
@Override
|
|
|
|
|
public INsdServiceConnector connect(INsdManagerCallback cb) {
|
2017-04-24 11:35:06 +09:00
|
|
|
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INTERNET, "NsdService");
|
2021-09-09 17:39:05 +09:00
|
|
|
final INsdServiceConnector connector = new NsdServiceConnector();
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.REGISTER_CLIENT, new Pair<>(connector, cb)));
|
|
|
|
|
return connector;
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-09 17:39:05 +09:00
|
|
|
private static class ListenerArgs {
|
|
|
|
|
public final NsdServiceConnector connector;
|
|
|
|
|
public final NsdServiceInfo serviceInfo;
|
|
|
|
|
ListenerArgs(NsdServiceConnector connector, NsdServiceInfo serviceInfo) {
|
|
|
|
|
this.connector = connector;
|
|
|
|
|
this.serviceInfo = serviceInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class NsdServiceConnector extends INsdServiceConnector.Stub
|
|
|
|
|
implements IBinder.DeathRecipient {
|
|
|
|
|
@Override
|
|
|
|
|
public void registerService(int listenerKey, NsdServiceInfo serviceInfo) {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.REGISTER_SERVICE, 0, listenerKey,
|
|
|
|
|
new ListenerArgs(this, serviceInfo)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void unregisterService(int listenerKey) {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.UNREGISTER_SERVICE, 0, listenerKey,
|
|
|
|
|
new ListenerArgs(this, null)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void discoverServices(int listenerKey, NsdServiceInfo serviceInfo) {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.DISCOVER_SERVICES, 0, listenerKey,
|
|
|
|
|
new ListenerArgs(this, serviceInfo)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void stopDiscovery(int listenerKey) {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.STOP_DISCOVERY, 0, listenerKey, new ListenerArgs(this, null)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void resolveService(int listenerKey, NsdServiceInfo serviceInfo) {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.RESOLVE_SERVICE, 0, listenerKey,
|
|
|
|
|
new ListenerArgs(this, serviceInfo)));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 09:24:42 +00:00
|
|
|
@Override
|
|
|
|
|
public void stopResolution(int listenerKey) {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.STOP_RESOLUTION, 0, listenerKey, new ListenerArgs(this, null)));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 08:48:48 +00:00
|
|
|
@Override
|
|
|
|
|
public void registerServiceInfoCallback(int listenerKey, NsdServiceInfo serviceInfo) {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.REGISTER_SERVICE_CALLBACK, 0, listenerKey,
|
|
|
|
|
new ListenerArgs(this, serviceInfo)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void unregisterServiceInfoCallback(int listenerKey) {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.UNREGISTER_SERVICE_CALLBACK, 0, listenerKey,
|
|
|
|
|
new ListenerArgs(this, null)));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 17:39:05 +09:00
|
|
|
@Override
|
|
|
|
|
public void startDaemon() {
|
|
|
|
|
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
|
|
|
|
|
NsdManager.DAEMON_STARTUP, new ListenerArgs(this, null)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void binderDied() {
|
|
|
|
|
mNsdStateMachine.sendMessage(
|
|
|
|
|
mNsdStateMachine.obtainMessage(NsdManager.UNREGISTER_CLIENT, this));
|
|
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-24 16:41:03 +09:00
|
|
|
private void sendNsdStateChangeBroadcast(boolean isEnabled) {
|
2012-04-19 10:26:34 -07:00
|
|
|
final Intent intent = new Intent(NsdManager.ACTION_NSD_STATE_CHANGED);
|
2012-04-17 23:15:29 -07:00
|
|
|
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
|
2017-04-24 16:41:03 +09:00
|
|
|
int nsdState = isEnabled ? NsdManager.NSD_STATE_ENABLED : NsdManager.NSD_STATE_DISABLED;
|
|
|
|
|
intent.putExtra(NsdManager.EXTRA_NSD_STATE, nsdState);
|
2012-08-29 18:32:08 -07:00
|
|
|
mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2012-04-11 14:52:19 -07:00
|
|
|
private int getUniqueId() {
|
|
|
|
|
if (++mUniqueId == INVALID_ID) return ++mUniqueId;
|
|
|
|
|
return mUniqueId;
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
2012-05-03 16:44:27 -07:00
|
|
|
private boolean registerService(int regId, NsdServiceInfo service) {
|
2017-04-24 16:19:58 +09:00
|
|
|
if (DBG) {
|
2021-11-17 09:35:33 +08:00
|
|
|
Log.d(TAG, "registerService: " + regId + " " + service);
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
2017-04-24 16:19:58 +09:00
|
|
|
String name = service.getServiceName();
|
|
|
|
|
String type = service.getServiceType();
|
|
|
|
|
int port = service.getPort();
|
|
|
|
|
byte[] textRecord = service.getTxtRecord();
|
2022-05-30 12:42:24 +09:00
|
|
|
final int registerInterface = getNetworkInterfaceIndex(service);
|
|
|
|
|
if (service.getNetwork() != null && registerInterface == IFACE_IDX_ANY) {
|
2022-04-26 11:14:14 +08:00
|
|
|
Log.e(TAG, "Interface to register service on not found");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return mMDnsManager.registerService(regId, name, type, port, textRecord, registerInterface);
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
2012-04-11 14:52:19 -07:00
|
|
|
private boolean unregisterService(int regId) {
|
2022-02-10 21:58:32 +08:00
|
|
|
return mMDnsManager.stopOperation(regId);
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
2021-12-16 15:31:16 +09:00
|
|
|
private boolean discoverServices(int discoveryId, NsdServiceInfo serviceInfo) {
|
2022-02-10 21:58:32 +08:00
|
|
|
final String type = serviceInfo.getServiceType();
|
2022-05-30 12:42:24 +09:00
|
|
|
final int discoverInterface = getNetworkInterfaceIndex(serviceInfo);
|
|
|
|
|
if (serviceInfo.getNetwork() != null && discoverInterface == IFACE_IDX_ANY) {
|
2021-12-16 15:31:16 +09:00
|
|
|
Log.e(TAG, "Interface to discover service on not found");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-10 21:58:32 +08:00
|
|
|
return mMDnsManager.discover(discoveryId, type, discoverInterface);
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
2012-04-11 14:52:19 -07:00
|
|
|
private boolean stopServiceDiscovery(int discoveryId) {
|
2022-02-10 21:58:32 +08:00
|
|
|
return mMDnsManager.stopOperation(discoveryId);
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
2012-05-03 16:44:27 -07:00
|
|
|
private boolean resolveService(int resolveId, NsdServiceInfo service) {
|
2021-12-16 15:31:16 +09:00
|
|
|
final String name = service.getServiceName();
|
|
|
|
|
final String type = service.getServiceType();
|
2022-05-30 12:42:24 +09:00
|
|
|
final int resolveInterface = getNetworkInterfaceIndex(service);
|
|
|
|
|
if (service.getNetwork() != null && resolveInterface == IFACE_IDX_ANY) {
|
2021-12-16 15:31:16 +09:00
|
|
|
Log.e(TAG, "Interface to resolve service on not found");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-10 21:58:32 +08:00
|
|
|
return mMDnsManager.resolve(resolveId, name, type, "local.", resolveInterface);
|
2021-12-16 15:31:16 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Guess the interface to use to resolve or discover a service on a specific network.
|
|
|
|
|
*
|
|
|
|
|
* This is an imperfect guess, as for example the network may be gone or not yet fully
|
|
|
|
|
* registered. This is fine as failing is correct if the network is gone, and a client
|
|
|
|
|
* attempting to resolve/discover on a network not yet setup would have a bad time anyway; also
|
|
|
|
|
* this is to support the legacy mdnsresponder implementation, which historically resolved
|
|
|
|
|
* services on an unspecified network.
|
|
|
|
|
*/
|
2022-05-30 12:42:24 +09:00
|
|
|
private int getNetworkInterfaceIndex(NsdServiceInfo serviceInfo) {
|
|
|
|
|
final Network network = serviceInfo.getNetwork();
|
|
|
|
|
if (network == null) {
|
|
|
|
|
// Fallback to getInterfaceIndex if present (typically if the NsdServiceInfo was
|
|
|
|
|
// provided by NsdService from discovery results, and the service was found on an
|
|
|
|
|
// interface that has no app-usable Network).
|
|
|
|
|
if (serviceInfo.getInterfaceIndex() != 0) {
|
|
|
|
|
return serviceInfo.getInterfaceIndex();
|
|
|
|
|
}
|
|
|
|
|
return IFACE_IDX_ANY;
|
|
|
|
|
}
|
2021-12-16 15:31:16 +09:00
|
|
|
|
|
|
|
|
final ConnectivityManager cm = mContext.getSystemService(ConnectivityManager.class);
|
|
|
|
|
if (cm == null) {
|
|
|
|
|
Log.wtf(TAG, "No ConnectivityManager for resolveService");
|
|
|
|
|
return IFACE_IDX_ANY;
|
|
|
|
|
}
|
|
|
|
|
final LinkProperties lp = cm.getLinkProperties(network);
|
|
|
|
|
if (lp == null) return IFACE_IDX_ANY;
|
|
|
|
|
|
|
|
|
|
// Only resolve on non-stacked interfaces
|
|
|
|
|
final NetworkInterface iface;
|
|
|
|
|
try {
|
|
|
|
|
iface = NetworkInterface.getByName(lp.getInterfaceName());
|
|
|
|
|
} catch (SocketException e) {
|
|
|
|
|
Log.e(TAG, "Error querying interface", e);
|
|
|
|
|
return IFACE_IDX_ANY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (iface == null) {
|
|
|
|
|
Log.e(TAG, "Interface not found: " + lp.getInterfaceName());
|
|
|
|
|
return IFACE_IDX_ANY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return iface.getIndex();
|
2012-04-11 14:52:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean stopResolveService(int resolveId) {
|
2022-02-10 21:58:32 +08:00
|
|
|
return mMDnsManager.stopOperation(resolveId);
|
2012-04-11 14:52:19 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:58:32 +08:00
|
|
|
private boolean getAddrInfo(int resolveId, String hostname, int interfaceIdx) {
|
|
|
|
|
return mMDnsManager.getServiceAddress(resolveId, hostname, interfaceIdx);
|
2012-04-11 14:52:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean stopGetAddrInfo(int resolveId) {
|
2022-02-10 21:58:32 +08:00
|
|
|
return mMDnsManager.stopOperation(resolveId);
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2012-04-17 23:15:29 -07:00
|
|
|
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
|
2021-09-16 10:15:22 +08:00
|
|
|
if (!PermissionUtils.checkDumpPermission(mContext, TAG, pw)) return;
|
2012-03-22 17:01:39 -07:00
|
|
|
|
2012-04-17 23:15:29 -07:00
|
|
|
for (ClientInfo client : mClients.values()) {
|
|
|
|
|
pw.println("Client Info");
|
|
|
|
|
pw.println(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mNsdStateMachine.dump(fd, pw, args);
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|
2012-04-11 14:52:19 -07:00
|
|
|
|
|
|
|
|
/* Information tracked per client */
|
|
|
|
|
private class ClientInfo {
|
|
|
|
|
|
2012-05-03 16:44:27 -07:00
|
|
|
private static final int MAX_LIMIT = 10;
|
2021-09-09 17:39:05 +09:00
|
|
|
private final INsdManagerCallback mCb;
|
2012-04-11 14:52:19 -07:00
|
|
|
/* Remembers a resolved service until getaddrinfo completes */
|
2012-05-03 16:44:27 -07:00
|
|
|
private NsdServiceInfo mResolvedService;
|
|
|
|
|
|
|
|
|
|
/* A map from client id to unique id sent to mDns */
|
2017-04-11 14:42:47 +09:00
|
|
|
private final SparseIntArray mClientIds = new SparseIntArray();
|
2012-04-11 14:52:19 -07:00
|
|
|
|
2014-03-07 14:48:22 -08:00
|
|
|
/* A map from client id to the type of the request we had received */
|
2017-04-11 14:42:47 +09:00
|
|
|
private final SparseIntArray mClientRequests = new SparseIntArray();
|
2014-03-07 14:48:22 -08:00
|
|
|
|
2023-01-13 22:57:24 +08:00
|
|
|
/* A map from client id to the MdnsListener */
|
|
|
|
|
private final SparseArray<MdnsListener> mListeners = new SparseArray<>();
|
|
|
|
|
|
2021-07-12 21:15:10 +08:00
|
|
|
// The target SDK of this client < Build.VERSION_CODES.S
|
|
|
|
|
private boolean mIsLegacy = false;
|
|
|
|
|
|
2022-12-27 08:48:48 +00:00
|
|
|
/*** The service that is registered to listen to its updates */
|
|
|
|
|
private NsdServiceInfo mRegisteredService;
|
|
|
|
|
/*** The client id that listen to updates */
|
|
|
|
|
private int mClientIdForServiceUpdates;
|
|
|
|
|
|
2021-09-09 17:39:05 +09:00
|
|
|
private ClientInfo(INsdManagerCallback cb) {
|
|
|
|
|
mCb = cb;
|
2021-11-17 09:35:33 +08:00
|
|
|
if (DBG) Log.d(TAG, "New client");
|
2012-04-11 14:52:19 -07:00
|
|
|
}
|
2012-04-17 23:15:29 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
2020-10-17 21:20:13 -06:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2012-04-17 23:15:29 -07:00
|
|
|
sb.append("mResolvedService ").append(mResolvedService).append("\n");
|
2021-07-12 21:15:10 +08:00
|
|
|
sb.append("mIsLegacy ").append(mIsLegacy).append("\n");
|
2012-05-03 16:44:27 -07:00
|
|
|
for(int i = 0; i< mClientIds.size(); i++) {
|
2014-03-07 14:48:22 -08:00
|
|
|
int clientID = mClientIds.keyAt(i);
|
|
|
|
|
sb.append("clientId ").append(clientID).
|
|
|
|
|
append(" mDnsId ").append(mClientIds.valueAt(i)).
|
|
|
|
|
append(" type ").append(mClientRequests.get(clientID)).append("\n");
|
2012-04-17 23:15:29 -07:00
|
|
|
}
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
2014-03-07 14:48:22 -08:00
|
|
|
|
2021-07-12 21:15:10 +08:00
|
|
|
private boolean isLegacy() {
|
|
|
|
|
return mIsLegacy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setLegacy() {
|
|
|
|
|
mIsLegacy = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-07 14:48:22 -08:00
|
|
|
// Remove any pending requests from the global map when we get rid of a client,
|
|
|
|
|
// and send cancellations to the daemon.
|
|
|
|
|
private void expungeAllRequests() {
|
|
|
|
|
int globalId, clientId, i;
|
2017-04-11 14:42:47 +09:00
|
|
|
// TODO: to keep handler responsive, do not clean all requests for that client at once.
|
2014-03-07 14:48:22 -08:00
|
|
|
for (i = 0; i < mClientIds.size(); i++) {
|
|
|
|
|
clientId = mClientIds.keyAt(i);
|
|
|
|
|
globalId = mClientIds.valueAt(i);
|
|
|
|
|
mIdToClientInfoMap.remove(globalId);
|
2021-11-17 09:35:33 +08:00
|
|
|
if (DBG) {
|
|
|
|
|
Log.d(TAG, "Terminating client-ID " + clientId
|
|
|
|
|
+ " global-ID " + globalId + " type " + mClientRequests.get(clientId));
|
|
|
|
|
}
|
2014-03-07 14:48:22 -08:00
|
|
|
switch (mClientRequests.get(clientId)) {
|
|
|
|
|
case NsdManager.DISCOVER_SERVICES:
|
|
|
|
|
stopServiceDiscovery(globalId);
|
|
|
|
|
break;
|
|
|
|
|
case NsdManager.RESOLVE_SERVICE:
|
|
|
|
|
stopResolveService(globalId);
|
|
|
|
|
break;
|
|
|
|
|
case NsdManager.REGISTER_SERVICE:
|
2023-01-18 15:58:03 +09:00
|
|
|
if (mAdvertiser != null) {
|
|
|
|
|
mAdvertiser.removeService(globalId);
|
|
|
|
|
} else {
|
|
|
|
|
unregisterService(globalId);
|
|
|
|
|
}
|
2014-03-07 14:48:22 -08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mClientIds.clear();
|
|
|
|
|
mClientRequests.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 22:57:24 +08:00
|
|
|
void unregisterAllListeners() {
|
|
|
|
|
for (int i = 0; i < mListeners.size(); i++) {
|
|
|
|
|
final MdnsListener listener = mListeners.valueAt(i);
|
|
|
|
|
mMdnsDiscoveryManager.unregisterListener(
|
|
|
|
|
listener.getListenedServiceType(), listener);
|
|
|
|
|
}
|
|
|
|
|
mListeners.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 18:39:07 -07:00
|
|
|
// mClientIds is a sparse array of listener id -> mDnsClient id. For a given mDnsClient id,
|
|
|
|
|
// return the corresponding listener id. mDnsClient id is also called a global id.
|
|
|
|
|
private int getClientId(final int globalId) {
|
2017-04-11 14:42:47 +09:00
|
|
|
int idx = mClientIds.indexOfValue(globalId);
|
|
|
|
|
if (idx < 0) {
|
|
|
|
|
return idx;
|
2014-04-25 18:39:07 -07:00
|
|
|
}
|
2017-04-11 14:42:47 +09:00
|
|
|
return mClientIds.keyAt(idx);
|
2014-04-25 18:39:07 -07:00
|
|
|
}
|
2021-09-09 17:39:05 +09:00
|
|
|
|
2022-12-27 08:48:48 +00:00
|
|
|
private void maybeNotifyRegisteredServiceLost(@NonNull NsdServiceInfo info) {
|
|
|
|
|
if (mRegisteredService == null) return;
|
|
|
|
|
if (!Objects.equals(mRegisteredService.getServiceName(), info.getServiceName())) return;
|
|
|
|
|
// Resolved services have a leading dot appended at the beginning of their type, but in
|
|
|
|
|
// discovered info it's at the end
|
|
|
|
|
if (!Objects.equals(
|
|
|
|
|
mRegisteredService.getServiceType() + ".", "." + info.getServiceType())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
onServiceUpdatedLost(mClientIdForServiceUpdates);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 17:39:05 +09:00
|
|
|
void onDiscoverServicesStarted(int listenerKey, NsdServiceInfo info) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onDiscoverServicesStarted(listenerKey, info);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onDiscoverServicesStarted", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onDiscoverServicesFailed(int listenerKey, int error) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onDiscoverServicesFailed(listenerKey, error);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onDiscoverServicesFailed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onServiceFound(int listenerKey, NsdServiceInfo info) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onServiceFound(listenerKey, info);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onServiceFound(", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onServiceLost(int listenerKey, NsdServiceInfo info) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onServiceLost(listenerKey, info);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onServiceLost(", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onStopDiscoveryFailed(int listenerKey, int error) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onStopDiscoveryFailed(listenerKey, error);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onStopDiscoveryFailed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onStopDiscoverySucceeded(int listenerKey) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onStopDiscoverySucceeded(listenerKey);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onStopDiscoverySucceeded", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onRegisterServiceFailed(int listenerKey, int error) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onRegisterServiceFailed(listenerKey, error);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onRegisterServiceFailed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onRegisterServiceSucceeded(int listenerKey, NsdServiceInfo info) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onRegisterServiceSucceeded(listenerKey, info);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onRegisterServiceSucceeded", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onUnregisterServiceFailed(int listenerKey, int error) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onUnregisterServiceFailed(listenerKey, error);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onUnregisterServiceFailed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onUnregisterServiceSucceeded(int listenerKey) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onUnregisterServiceSucceeded(listenerKey);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onUnregisterServiceSucceeded", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onResolveServiceFailed(int listenerKey, int error) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onResolveServiceFailed(listenerKey, error);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onResolveServiceFailed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onResolveServiceSucceeded(int listenerKey, NsdServiceInfo info) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onResolveServiceSucceeded(listenerKey, info);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onResolveServiceSucceeded", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-26 09:24:42 +00:00
|
|
|
|
|
|
|
|
void onStopResolutionFailed(int listenerKey, int error) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onStopResolutionFailed(listenerKey, error);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onStopResolutionFailed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onStopResolutionSucceeded(int listenerKey) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onStopResolutionSucceeded(listenerKey);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onStopResolutionSucceeded", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-27 08:48:48 +00:00
|
|
|
|
|
|
|
|
void onServiceInfoCallbackRegistrationFailed(int listenerKey, int error) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onServiceInfoCallbackRegistrationFailed(listenerKey, error);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onServiceInfoCallbackRegistrationFailed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onServiceUpdated(int listenerKey, NsdServiceInfo info) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onServiceUpdated(listenerKey, info);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onServiceUpdated", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onServiceUpdatedLost(int listenerKey) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onServiceUpdatedLost(listenerKey);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onServiceUpdatedLost", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onServiceInfoCallbackUnregistered(int listenerKey) {
|
|
|
|
|
try {
|
|
|
|
|
mCb.onServiceInfoCallbackUnregistered(listenerKey);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, "Error calling onServiceInfoCallbackUnregistered", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-11 14:52:19 -07:00
|
|
|
}
|
2012-03-22 17:01:39 -07:00
|
|
|
}
|