Move f/b/packages/Nsd files to f/b/packages/ConnectivityT

Move Nsd files to ConnectivityT so that nsd, ipsec,
ethernet and netstats files can live in the same
directory and it will be easier to move everything
in frameworks/base/packages/ConnectivityT to
packages/modules/Connectivity after fixing all of the
hidden API usages.

Bug: 204153604
Test: FrameworksNetTests
      CtsNetTestCases
Change-Id: I411e242e8739d15920cfc2fe274115f7f39bd89f
This commit is contained in:
Aaron Huang
2021-12-08 15:24:57 +08:00
parent 8823a30b6f
commit e500616efd
16 changed files with 0 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2021, The Android Open Source Project
*
* 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 android.net.nsd;
import android.net.nsd.INsdManagerCallback;
import android.net.nsd.INsdServiceConnector;
import android.os.Messenger;
/**
* Interface that NsdService implements to connect NsdManager clients.
*
* {@hide}
*/
interface INsdManager {
INsdServiceConnector connect(INsdManagerCallback cb);
}

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2021, The Android Open Source Project
*
* 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 android.net.nsd;
import android.os.Messenger;
import android.net.nsd.NsdServiceInfo;
/**
* Callbacks from NsdService to NsdManager
* @hide
*/
oneway interface INsdManagerCallback {
void onDiscoverServicesStarted(int listenerKey, in NsdServiceInfo info);
void onDiscoverServicesFailed(int listenerKey, int error);
void onServiceFound(int listenerKey, in NsdServiceInfo info);
void onServiceLost(int listenerKey, in NsdServiceInfo info);
void onStopDiscoveryFailed(int listenerKey, int error);
void onStopDiscoverySucceeded(int listenerKey);
void onRegisterServiceFailed(int listenerKey, int error);
void onRegisterServiceSucceeded(int listenerKey, in NsdServiceInfo info);
void onUnregisterServiceFailed(int listenerKey, int error);
void onUnregisterServiceSucceeded(int listenerKey);
void onResolveServiceFailed(int listenerKey, int error);
void onResolveServiceSucceeded(int listenerKey, in NsdServiceInfo info);
}

View File

@@ -0,0 +1,35 @@
/**
* Copyright (c) 2021, The Android Open Source Project
*
* 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 android.net.nsd;
import android.net.nsd.INsdManagerCallback;
import android.net.nsd.NsdServiceInfo;
import android.os.Messenger;
/**
* Interface that NsdService implements for each NsdManager client.
*
* {@hide}
*/
interface INsdServiceConnector {
void registerService(int listenerKey, in NsdServiceInfo serviceInfo);
void unregisterService(int listenerKey);
void discoverServices(int listenerKey, in NsdServiceInfo serviceInfo);
void stopDiscovery(int listenerKey);
void resolveService(int listenerKey, in NsdServiceInfo serviceInfo);
void startDaemon();
}

View File

@@ -0,0 +1,734 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* 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 android.net.nsd;
import static com.android.internal.util.Preconditions.checkArgument;
import static com.android.internal.util.Preconditions.checkNotNull;
import static com.android.internal.util.Preconditions.checkStringNotEmpty;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemService;
import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.util.SparseArray;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.Protocol;
/**
* The Network Service Discovery Manager class provides the API to discover services
* on a network. As an example, if device A and device B are connected over a Wi-Fi
* network, a game registered on device A can be discovered by a game on device
* B. Another example use case is an application discovering printers on the network.
*
* <p> The API currently supports DNS based service discovery and discovery is currently
* limited to a local network over Multicast DNS. DNS service discovery is described at
* http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt
*
* <p> The API is asynchronous, and responses to requests from an application are on listener
* callbacks on a separate internal thread.
*
* <p> There are three main operations the API supports - registration, discovery and resolution.
* <pre>
* Application start
* |
* |
* | onServiceRegistered()
* Register any local services /
* to be advertised with \
* registerService() onRegistrationFailed()
* |
* |
* discoverServices()
* |
* Maintain a list to track
* discovered services
* |
* |--------->
* | |
* | onServiceFound()
* | |
* | add service to list
* | |
* |<----------
* |
* |--------->
* | |
* | onServiceLost()
* | |
* | remove service from list
* | |
* |<----------
* |
* |
* | Connect to a service
* | from list ?
* |
* resolveService()
* |
* onServiceResolved()
* |
* Establish connection to service
* with the host and port information
*
* </pre>
* An application that needs to advertise itself over a network for other applications to
* discover it can do so with a call to {@link #registerService}. If Example is a http based
* application that can provide HTML data to peer services, it can register a name "Example"
* with service type "_http._tcp". A successful registration is notified with a callback to
* {@link RegistrationListener#onServiceRegistered} and a failure to register is notified
* over {@link RegistrationListener#onRegistrationFailed}
*
* <p> A peer application looking for http services can initiate a discovery for "_http._tcp"
* with a call to {@link #discoverServices}. A service found is notified with a callback
* to {@link DiscoveryListener#onServiceFound} and a service lost is notified on
* {@link DiscoveryListener#onServiceLost}.
*
* <p> Once the peer application discovers the "Example" http service, and either needs to read the
* attributes of the service or wants to receive data from the "Example" application, it can
* initiate a resolve with {@link #resolveService} to resolve the attributes, host, and port
* details. A successful resolve is notified on {@link ResolveListener#onServiceResolved} and a
* failure is notified on {@link ResolveListener#onResolveFailed}.
*
* Applications can reserve for a service type at
* http://www.iana.org/form/ports-service. Existing services can be found at
* http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml
*
* {@see NsdServiceInfo}
*/
@SystemService(Context.NSD_SERVICE)
public final class NsdManager {
private static final String TAG = NsdManager.class.getSimpleName();
private static final boolean DBG = false;
/**
* When enabled, apps targeting < Android 12 are considered legacy for
* the NSD native daemon.
* The platform will only keep the daemon running as long as there are
* any legacy apps connected.
*
* After Android 12, directly communicate with native daemon might not
* work since the native damon won't always stay alive.
* Use the NSD APIs from NsdManager as the replacement is recommended.
* An another alternative could be bundling your own mdns solutions instead of
* depending on the system mdns native daemon.
*
* @hide
*/
@ChangeId
@EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.S)
public static final long RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS = 191844585L;
/**
* Broadcast intent action to indicate whether network service discovery is
* enabled or disabled. An extra {@link #EXTRA_NSD_STATE} provides the state
* information as int.
*
* @see #EXTRA_NSD_STATE
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_NSD_STATE_CHANGED = "android.net.nsd.STATE_CHANGED";
/**
* The lookup key for an int that indicates whether network service discovery is enabled
* or disabled. Retrieve it with {@link android.content.Intent#getIntExtra(String,int)}.
*
* @see #NSD_STATE_DISABLED
* @see #NSD_STATE_ENABLED
*/
public static final String EXTRA_NSD_STATE = "nsd_state";
/**
* Network service discovery is disabled
*
* @see #ACTION_NSD_STATE_CHANGED
*/
public static final int NSD_STATE_DISABLED = 1;
/**
* Network service discovery is enabled
*
* @see #ACTION_NSD_STATE_CHANGED
*/
public static final int NSD_STATE_ENABLED = 2;
private static final int BASE = Protocol.BASE_NSD_MANAGER;
/** @hide */
public static final int DISCOVER_SERVICES = BASE + 1;
/** @hide */
public static final int DISCOVER_SERVICES_STARTED = BASE + 2;
/** @hide */
public static final int DISCOVER_SERVICES_FAILED = BASE + 3;
/** @hide */
public static final int SERVICE_FOUND = BASE + 4;
/** @hide */
public static final int SERVICE_LOST = BASE + 5;
/** @hide */
public static final int STOP_DISCOVERY = BASE + 6;
/** @hide */
public static final int STOP_DISCOVERY_FAILED = BASE + 7;
/** @hide */
public static final int STOP_DISCOVERY_SUCCEEDED = BASE + 8;
/** @hide */
public static final int REGISTER_SERVICE = BASE + 9;
/** @hide */
public static final int REGISTER_SERVICE_FAILED = BASE + 10;
/** @hide */
public static final int REGISTER_SERVICE_SUCCEEDED = BASE + 11;
/** @hide */
public static final int UNREGISTER_SERVICE = BASE + 12;
/** @hide */
public static final int UNREGISTER_SERVICE_FAILED = BASE + 13;
/** @hide */
public static final int UNREGISTER_SERVICE_SUCCEEDED = BASE + 14;
/** @hide */
public static final int RESOLVE_SERVICE = BASE + 18;
/** @hide */
public static final int RESOLVE_SERVICE_FAILED = BASE + 19;
/** @hide */
public static final int RESOLVE_SERVICE_SUCCEEDED = BASE + 20;
/** @hide */
public static final int DAEMON_CLEANUP = BASE + 21;
/** @hide */
public static final int DAEMON_STARTUP = BASE + 22;
/** @hide */
public static final int ENABLE = BASE + 24;
/** @hide */
public static final int DISABLE = BASE + 25;
/** @hide */
public static final int NATIVE_DAEMON_EVENT = BASE + 26;
/** @hide */
public static final int REGISTER_CLIENT = BASE + 27;
/** @hide */
public static final int UNREGISTER_CLIENT = BASE + 28;
/** Dns based service discovery protocol */
public static final int PROTOCOL_DNS_SD = 0x0001;
private static final SparseArray<String> EVENT_NAMES = new SparseArray<>();
static {
EVENT_NAMES.put(DISCOVER_SERVICES, "DISCOVER_SERVICES");
EVENT_NAMES.put(DISCOVER_SERVICES_STARTED, "DISCOVER_SERVICES_STARTED");
EVENT_NAMES.put(DISCOVER_SERVICES_FAILED, "DISCOVER_SERVICES_FAILED");
EVENT_NAMES.put(SERVICE_FOUND, "SERVICE_FOUND");
EVENT_NAMES.put(SERVICE_LOST, "SERVICE_LOST");
EVENT_NAMES.put(STOP_DISCOVERY, "STOP_DISCOVERY");
EVENT_NAMES.put(STOP_DISCOVERY_FAILED, "STOP_DISCOVERY_FAILED");
EVENT_NAMES.put(STOP_DISCOVERY_SUCCEEDED, "STOP_DISCOVERY_SUCCEEDED");
EVENT_NAMES.put(REGISTER_SERVICE, "REGISTER_SERVICE");
EVENT_NAMES.put(REGISTER_SERVICE_FAILED, "REGISTER_SERVICE_FAILED");
EVENT_NAMES.put(REGISTER_SERVICE_SUCCEEDED, "REGISTER_SERVICE_SUCCEEDED");
EVENT_NAMES.put(UNREGISTER_SERVICE, "UNREGISTER_SERVICE");
EVENT_NAMES.put(UNREGISTER_SERVICE_FAILED, "UNREGISTER_SERVICE_FAILED");
EVENT_NAMES.put(UNREGISTER_SERVICE_SUCCEEDED, "UNREGISTER_SERVICE_SUCCEEDED");
EVENT_NAMES.put(RESOLVE_SERVICE, "RESOLVE_SERVICE");
EVENT_NAMES.put(RESOLVE_SERVICE_FAILED, "RESOLVE_SERVICE_FAILED");
EVENT_NAMES.put(RESOLVE_SERVICE_SUCCEEDED, "RESOLVE_SERVICE_SUCCEEDED");
EVENT_NAMES.put(DAEMON_CLEANUP, "DAEMON_CLEANUP");
EVENT_NAMES.put(DAEMON_STARTUP, "DAEMON_STARTUP");
EVENT_NAMES.put(ENABLE, "ENABLE");
EVENT_NAMES.put(DISABLE, "DISABLE");
EVENT_NAMES.put(NATIVE_DAEMON_EVENT, "NATIVE_DAEMON_EVENT");
}
/** @hide */
public static String nameOf(int event) {
String name = EVENT_NAMES.get(event);
if (name == null) {
return Integer.toString(event);
}
return name;
}
private static final int FIRST_LISTENER_KEY = 1;
private final INsdServiceConnector mService;
private final Context mContext;
private int mListenerKey = FIRST_LISTENER_KEY;
private final SparseArray mListenerMap = new SparseArray();
private final SparseArray<NsdServiceInfo> mServiceMap = new SparseArray<>();
private final Object mMapLock = new Object();
private final ServiceHandler mHandler;
/**
* Create a new Nsd instance. Applications use
* {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
* {@link android.content.Context#NSD_SERVICE Context.NSD_SERVICE}.
* @param service the Binder interface
* @hide - hide this because it takes in a parameter of type INsdManager, which
* is a system private class.
*/
public NsdManager(Context context, INsdManager service) {
mContext = context;
HandlerThread t = new HandlerThread("NsdManager");
t.start();
mHandler = new ServiceHandler(t.getLooper());
try {
mService = service.connect(new NsdCallbackImpl(mHandler));
} catch (RemoteException e) {
throw new RuntimeException("Failed to connect to NsdService");
}
// Only proactively start the daemon if the target SDK < S, otherwise the internal service
// would automatically start/stop the native daemon as needed.
if (!CompatChanges.isChangeEnabled(RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)) {
try {
mService.startDaemon();
} catch (RemoteException e) {
Log.e(TAG, "Failed to proactively start daemon");
// Continue: the daemon can still be started on-demand later
}
}
}
private static class NsdCallbackImpl extends INsdManagerCallback.Stub {
private final Handler mServHandler;
NsdCallbackImpl(Handler serviceHandler) {
mServHandler = serviceHandler;
}
private void sendInfo(int message, int listenerKey, NsdServiceInfo info) {
mServHandler.sendMessage(mServHandler.obtainMessage(message, 0, listenerKey, info));
}
private void sendError(int message, int listenerKey, int error) {
mServHandler.sendMessage(mServHandler.obtainMessage(message, error, listenerKey));
}
private void sendNoArg(int message, int listenerKey) {
mServHandler.sendMessage(mServHandler.obtainMessage(message, 0, listenerKey));
}
@Override
public void onDiscoverServicesStarted(int listenerKey, NsdServiceInfo info) {
sendInfo(DISCOVER_SERVICES_STARTED, listenerKey, info);
}
@Override
public void onDiscoverServicesFailed(int listenerKey, int error) {
sendError(DISCOVER_SERVICES_FAILED, listenerKey, error);
}
@Override
public void onServiceFound(int listenerKey, NsdServiceInfo info) {
sendInfo(SERVICE_FOUND, listenerKey, info);
}
@Override
public void onServiceLost(int listenerKey, NsdServiceInfo info) {
sendInfo(SERVICE_LOST, listenerKey, info);
}
@Override
public void onStopDiscoveryFailed(int listenerKey, int error) {
sendError(STOP_DISCOVERY_FAILED, listenerKey, error);
}
@Override
public void onStopDiscoverySucceeded(int listenerKey) {
sendNoArg(STOP_DISCOVERY_SUCCEEDED, listenerKey);
}
@Override
public void onRegisterServiceFailed(int listenerKey, int error) {
sendError(REGISTER_SERVICE_FAILED, listenerKey, error);
}
@Override
public void onRegisterServiceSucceeded(int listenerKey, NsdServiceInfo info) {
sendInfo(REGISTER_SERVICE_SUCCEEDED, listenerKey, info);
}
@Override
public void onUnregisterServiceFailed(int listenerKey, int error) {
sendError(UNREGISTER_SERVICE_FAILED, listenerKey, error);
}
@Override
public void onUnregisterServiceSucceeded(int listenerKey) {
sendNoArg(UNREGISTER_SERVICE_SUCCEEDED, listenerKey);
}
@Override
public void onResolveServiceFailed(int listenerKey, int error) {
sendError(RESOLVE_SERVICE_FAILED, listenerKey, error);
}
@Override
public void onResolveServiceSucceeded(int listenerKey, NsdServiceInfo info) {
sendInfo(RESOLVE_SERVICE_SUCCEEDED, listenerKey, info);
}
}
/**
* Failures are passed with {@link RegistrationListener#onRegistrationFailed},
* {@link RegistrationListener#onUnregistrationFailed},
* {@link DiscoveryListener#onStartDiscoveryFailed},
* {@link DiscoveryListener#onStopDiscoveryFailed} or {@link ResolveListener#onResolveFailed}.
*
* Indicates that the operation failed due to an internal error.
*/
public static final int FAILURE_INTERNAL_ERROR = 0;
/**
* Indicates that the operation failed because it is already active.
*/
public static final int FAILURE_ALREADY_ACTIVE = 3;
/**
* Indicates that the operation failed because the maximum outstanding
* requests from the applications have reached.
*/
public static final int FAILURE_MAX_LIMIT = 4;
/** Interface for callback invocation for service discovery */
public interface DiscoveryListener {
public void onStartDiscoveryFailed(String serviceType, int errorCode);
public void onStopDiscoveryFailed(String serviceType, int errorCode);
public void onDiscoveryStarted(String serviceType);
public void onDiscoveryStopped(String serviceType);
public void onServiceFound(NsdServiceInfo serviceInfo);
public void onServiceLost(NsdServiceInfo serviceInfo);
}
/** Interface for callback invocation for service registration */
public interface RegistrationListener {
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode);
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode);
public void onServiceRegistered(NsdServiceInfo serviceInfo);
public void onServiceUnregistered(NsdServiceInfo serviceInfo);
}
/** Interface for callback invocation for service resolution */
public interface ResolveListener {
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode);
public void onServiceResolved(NsdServiceInfo serviceInfo);
}
@VisibleForTesting
class ServiceHandler extends Handler {
ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message message) {
final int what = message.what;
final int key = message.arg2;
final Object listener;
final NsdServiceInfo ns;
synchronized (mMapLock) {
listener = mListenerMap.get(key);
ns = mServiceMap.get(key);
}
if (listener == null) {
Log.d(TAG, "Stale key " + message.arg2);
return;
}
if (DBG) {
Log.d(TAG, "received " + nameOf(what) + " for key " + key + ", service " + ns);
}
switch (what) {
case DISCOVER_SERVICES_STARTED:
String s = getNsdServiceInfoType((NsdServiceInfo) message.obj);
((DiscoveryListener) listener).onDiscoveryStarted(s);
break;
case DISCOVER_SERVICES_FAILED:
removeListener(key);
((DiscoveryListener) listener).onStartDiscoveryFailed(getNsdServiceInfoType(ns),
message.arg1);
break;
case SERVICE_FOUND:
((DiscoveryListener) listener).onServiceFound((NsdServiceInfo) message.obj);
break;
case SERVICE_LOST:
((DiscoveryListener) listener).onServiceLost((NsdServiceInfo) message.obj);
break;
case STOP_DISCOVERY_FAILED:
// TODO: failure to stop discovery should be internal and retried internally, as
// the effect for the client is indistinguishable from STOP_DISCOVERY_SUCCEEDED
removeListener(key);
((DiscoveryListener) listener).onStopDiscoveryFailed(getNsdServiceInfoType(ns),
message.arg1);
break;
case STOP_DISCOVERY_SUCCEEDED:
removeListener(key);
((DiscoveryListener) listener).onDiscoveryStopped(getNsdServiceInfoType(ns));
break;
case REGISTER_SERVICE_FAILED:
removeListener(key);
((RegistrationListener) listener).onRegistrationFailed(ns, message.arg1);
break;
case REGISTER_SERVICE_SUCCEEDED:
((RegistrationListener) listener).onServiceRegistered(
(NsdServiceInfo) message.obj);
break;
case UNREGISTER_SERVICE_FAILED:
removeListener(key);
((RegistrationListener) listener).onUnregistrationFailed(ns, message.arg1);
break;
case UNREGISTER_SERVICE_SUCCEEDED:
// TODO: do not unregister listener until service is unregistered, or provide
// alternative way for unregistering ?
removeListener(message.arg2);
((RegistrationListener) listener).onServiceUnregistered(ns);
break;
case RESOLVE_SERVICE_FAILED:
removeListener(key);
((ResolveListener) listener).onResolveFailed(ns, message.arg1);
break;
case RESOLVE_SERVICE_SUCCEEDED:
removeListener(key);
((ResolveListener) listener).onServiceResolved((NsdServiceInfo) message.obj);
break;
default:
Log.d(TAG, "Ignored " + message);
break;
}
}
}
private int nextListenerKey() {
// Ensure mListenerKey >= FIRST_LISTENER_KEY;
mListenerKey = Math.max(FIRST_LISTENER_KEY, mListenerKey + 1);
return mListenerKey;
}
// Assert that the listener is not in the map, then add it and returns its key
private int putListener(Object listener, NsdServiceInfo s) {
checkListener(listener);
final int key;
synchronized (mMapLock) {
int valueIndex = mListenerMap.indexOfValue(listener);
checkArgument(valueIndex == -1, "listener already in use");
key = nextListenerKey();
mListenerMap.put(key, listener);
mServiceMap.put(key, s);
}
return key;
}
private void removeListener(int key) {
synchronized (mMapLock) {
mListenerMap.remove(key);
mServiceMap.remove(key);
}
}
private int getListenerKey(Object listener) {
checkListener(listener);
synchronized (mMapLock) {
int valueIndex = mListenerMap.indexOfValue(listener);
checkArgument(valueIndex != -1, "listener not registered");
return mListenerMap.keyAt(valueIndex);
}
}
private static String getNsdServiceInfoType(NsdServiceInfo s) {
if (s == null) return "?";
return s.getServiceType();
}
/**
* Register a service to be discovered by other services.
*
* <p> The function call immediately returns after sending a request to register service
* to the framework. The application is notified of a successful registration
* through the callback {@link RegistrationListener#onServiceRegistered} or a failure
* through {@link RegistrationListener#onRegistrationFailed}.
*
* <p> The application should call {@link #unregisterService} when the service
* registration is no longer required, and/or whenever the application is stopped.
*
* @param serviceInfo The service being registered
* @param protocolType The service discovery protocol
* @param listener The listener notifies of a successful registration and is used to
* unregister this service through a call on {@link #unregisterService}. Cannot be null.
* Cannot be in use for an active service registration.
*/
public void registerService(NsdServiceInfo serviceInfo, int protocolType,
RegistrationListener listener) {
checkArgument(serviceInfo.getPort() > 0, "Invalid port number");
checkServiceInfo(serviceInfo);
checkProtocol(protocolType);
int key = putListener(listener, serviceInfo);
try {
mService.registerService(key, serviceInfo);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* Unregister a service registered through {@link #registerService}. A successful
* unregister is notified to the application with a call to
* {@link RegistrationListener#onServiceUnregistered}.
*
* @param listener This should be the listener object that was passed to
* {@link #registerService}. It identifies the service that should be unregistered
* and notifies of a successful or unsuccessful unregistration via the listener
* callbacks. In API versions 20 and above, the listener object may be used for
* another service registration once the callback has been called. In API versions <= 19,
* there is no entirely reliable way to know when a listener may be re-used, and a new
* listener should be created for each service registration request.
*/
public void unregisterService(RegistrationListener listener) {
int id = getListenerKey(listener);
try {
mService.unregisterService(id);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* Initiate service discovery to browse for instances of a service type. Service discovery
* consumes network bandwidth and will continue until the application calls
* {@link #stopServiceDiscovery}.
*
* <p> The function call immediately returns after sending a request to start service
* discovery to the framework. The application is notified of a success to initiate
* discovery through the callback {@link DiscoveryListener#onDiscoveryStarted} or a failure
* through {@link DiscoveryListener#onStartDiscoveryFailed}.
*
* <p> Upon successful start, application is notified when a service is found with
* {@link DiscoveryListener#onServiceFound} or when a service is lost with
* {@link DiscoveryListener#onServiceLost}.
*
* <p> Upon failure to start, service discovery is not active and application does
* not need to invoke {@link #stopServiceDiscovery}
*
* <p> The application should call {@link #stopServiceDiscovery} when discovery of this
* service type is no longer required, and/or whenever the application is paused or
* stopped.
*
* @param serviceType The service type being discovered. Examples include "_http._tcp" for
* http services or "_ipp._tcp" for printers
* @param protocolType The service discovery protocol
* @param listener The listener notifies of a successful discovery and is used
* to stop discovery on this serviceType through a call on {@link #stopServiceDiscovery}.
* Cannot be null. Cannot be in use for an active service discovery.
*/
public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) {
checkStringNotEmpty(serviceType, "Service type cannot be empty");
checkProtocol(protocolType);
NsdServiceInfo s = new NsdServiceInfo();
s.setServiceType(serviceType);
int key = putListener(listener, s);
try {
mService.discoverServices(key, s);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* Stop service discovery initiated with {@link #discoverServices}. An active service
* discovery is notified to the application with {@link DiscoveryListener#onDiscoveryStarted}
* and it stays active until the application invokes a stop service discovery. A successful
* stop is notified to with a call to {@link DiscoveryListener#onDiscoveryStopped}.
*
* <p> Upon failure to stop service discovery, application is notified through
* {@link DiscoveryListener#onStopDiscoveryFailed}.
*
* @param listener This should be the listener object that was passed to {@link #discoverServices}.
* It identifies the discovery that should be stopped and notifies of a successful or
* unsuccessful stop. In API versions 20 and above, the listener object may be used for
* another service discovery once the callback has been called. In API versions <= 19,
* there is no entirely reliable way to know when a listener may be re-used, and a new
* listener should be created for each service discovery request.
*/
public void stopServiceDiscovery(DiscoveryListener listener) {
int id = getListenerKey(listener);
try {
mService.stopDiscovery(id);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* Resolve a discovered service. An application can resolve a service right before
* establishing a connection to fetch the IP and port details on which to setup
* the connection.
*
* @param serviceInfo service to be resolved
* @param listener to receive callback upon success or failure. Cannot be null.
* Cannot be in use for an active service resolution.
*/
public void resolveService(NsdServiceInfo serviceInfo, ResolveListener listener) {
checkServiceInfo(serviceInfo);
int key = putListener(listener, serviceInfo);
try {
mService.resolveService(key, serviceInfo);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
private static void checkListener(Object listener) {
checkNotNull(listener, "listener cannot be null");
}
private static void checkProtocol(int protocolType) {
checkArgument(protocolType == PROTOCOL_DNS_SD, "Unsupported protocol");
}
private static void checkServiceInfo(NsdServiceInfo serviceInfo) {
checkNotNull(serviceInfo, "NsdServiceInfo cannot be null");
checkStringNotEmpty(serviceInfo.getServiceName(), "Service name cannot be empty");
checkStringNotEmpty(serviceInfo.getServiceType(), "Service type cannot be empty");
}
}

View File

@@ -0,0 +1,391 @@
/*
* Copyright (C) 2012 The Android Open Source Project
*
* 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 android.net.nsd;
import android.annotation.NonNull;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Base64;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
/**
* A class representing service information for network service discovery
* {@see NsdManager}
*/
public final class NsdServiceInfo implements Parcelable {
private static final String TAG = "NsdServiceInfo";
private String mServiceName;
private String mServiceType;
private final ArrayMap<String, byte[]> mTxtRecord = new ArrayMap<>();
private InetAddress mHost;
private int mPort;
public NsdServiceInfo() {
}
/** @hide */
public NsdServiceInfo(String sn, String rt) {
mServiceName = sn;
mServiceType = rt;
}
/** Get the service name */
public String getServiceName() {
return mServiceName;
}
/** Set the service name */
public void setServiceName(String s) {
mServiceName = s;
}
/** Get the service type */
public String getServiceType() {
return mServiceType;
}
/** Set the service type */
public void setServiceType(String s) {
mServiceType = s;
}
/** Get the host address. The host address is valid for a resolved service. */
public InetAddress getHost() {
return mHost;
}
/** Set the host address */
public void setHost(InetAddress s) {
mHost = s;
}
/** Get port number. The port number is valid for a resolved service. */
public int getPort() {
return mPort;
}
/** Set port number */
public void setPort(int p) {
mPort = p;
}
/**
* Unpack txt information from a base-64 encoded byte array.
*
* @param rawRecords The raw base64 encoded records string read from netd.
*
* @hide
*/
public void setTxtRecords(@NonNull String rawRecords) {
byte[] txtRecordsRawBytes = Base64.decode(rawRecords, Base64.DEFAULT);
// There can be multiple TXT records after each other. Each record has to following format:
//
// byte type required meaning
// ------------------- ------------------- -------- ----------------------------------
// 0 unsigned 8 bit yes size of record excluding this byte
// 1 - n ASCII but not '=' yes key
// n + 1 '=' optional separator of key and value
// n + 2 - record size uninterpreted bytes optional value
//
// Example legal records:
// [11, 'm', 'y', 'k', 'e', 'y', '=', 0x0, 0x4, 0x65, 0x7, 0xff]
// [17, 'm', 'y', 'K', 'e', 'y', 'W', 'i', 't', 'h', 'N', 'o', 'V', 'a', 'l', 'u', 'e', '=']
// [12, 'm', 'y', 'B', 'o', 'o', 'l', 'e', 'a', 'n', 'K', 'e', 'y']
//
// Example corrupted records
// [3, =, 1, 2] <- key is empty
// [3, 0, =, 2] <- key contains non-ASCII character. We handle this by replacing the
// invalid characters instead of skipping the record.
// [30, 'a', =, 2] <- length exceeds total left over bytes in the TXT records array, we
// handle this by reducing the length of the record as needed.
int pos = 0;
while (pos < txtRecordsRawBytes.length) {
// recordLen is an unsigned 8 bit value
int recordLen = txtRecordsRawBytes[pos] & 0xff;
pos += 1;
try {
if (recordLen == 0) {
throw new IllegalArgumentException("Zero sized txt record");
} else if (pos + recordLen > txtRecordsRawBytes.length) {
Log.w(TAG, "Corrupt record length (pos = " + pos + "): " + recordLen);
recordLen = txtRecordsRawBytes.length - pos;
}
// Decode key-value records
String key = null;
byte[] value = null;
int valueLen = 0;
for (int i = pos; i < pos + recordLen; i++) {
if (key == null) {
if (txtRecordsRawBytes[i] == '=') {
key = new String(txtRecordsRawBytes, pos, i - pos,
StandardCharsets.US_ASCII);
}
} else {
if (value == null) {
value = new byte[recordLen - key.length() - 1];
}
value[valueLen] = txtRecordsRawBytes[i];
valueLen++;
}
}
// If '=' was not found we have a boolean record
if (key == null) {
key = new String(txtRecordsRawBytes, pos, recordLen, StandardCharsets.US_ASCII);
}
if (TextUtils.isEmpty(key)) {
// Empty keys are not allowed (RFC6763 6.4)
throw new IllegalArgumentException("Invalid txt record (key is empty)");
}
if (getAttributes().containsKey(key)) {
// When we have a duplicate record, the later ones are ignored (RFC6763 6.4)
throw new IllegalArgumentException("Invalid txt record (duplicate key \"" + key + "\")");
}
setAttribute(key, value);
} catch (IllegalArgumentException e) {
Log.e(TAG, "While parsing txt records (pos = " + pos + "): " + e.getMessage());
}
pos += recordLen;
}
}
/** @hide */
@UnsupportedAppUsage
public void setAttribute(String key, byte[] value) {
if (TextUtils.isEmpty(key)) {
throw new IllegalArgumentException("Key cannot be empty");
}
// Key must be printable US-ASCII, excluding =.
for (int i = 0; i < key.length(); ++i) {
char character = key.charAt(i);
if (character < 0x20 || character > 0x7E) {
throw new IllegalArgumentException("Key strings must be printable US-ASCII");
} else if (character == 0x3D) {
throw new IllegalArgumentException("Key strings must not include '='");
}
}
// Key length + value length must be < 255.
if (key.length() + (value == null ? 0 : value.length) >= 255) {
throw new IllegalArgumentException("Key length + value length must be < 255 bytes");
}
// Warn if key is > 9 characters, as recommended by RFC 6763 section 6.4.
if (key.length() > 9) {
Log.w(TAG, "Key lengths > 9 are discouraged: " + key);
}
// Check against total TXT record size limits.
// Arbitrary 400 / 1300 byte limits taken from RFC 6763 section 6.2.
int txtRecordSize = getTxtRecordSize();
int futureSize = txtRecordSize + key.length() + (value == null ? 0 : value.length) + 2;
if (futureSize > 1300) {
throw new IllegalArgumentException("Total length of attributes must be < 1300 bytes");
} else if (futureSize > 400) {
Log.w(TAG, "Total length of all attributes exceeds 400 bytes; truncation may occur");
}
mTxtRecord.put(key, value);
}
/**
* Add a service attribute as a key/value pair.
*
* <p> Service attributes are included as DNS-SD TXT record pairs.
*
* <p> The key must be US-ASCII printable characters, excluding the '=' character. Values may
* be UTF-8 strings or null. The total length of key + value must be less than 255 bytes.
*
* <p> Keys should be short, ideally no more than 9 characters, and unique per instance of
* {@link NsdServiceInfo}. Calling {@link #setAttribute} twice with the same key will overwrite
* first value.
*/
public void setAttribute(String key, String value) {
try {
setAttribute(key, value == null ? (byte []) null : value.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Value must be UTF-8");
}
}
/** Remove an attribute by key */
public void removeAttribute(String key) {
mTxtRecord.remove(key);
}
/**
* Retrieve attributes as a map of String keys to byte[] values. The attributes map is only
* valid for a resolved service.
*
* <p> The returned map is unmodifiable; changes must be made through {@link #setAttribute} and
* {@link #removeAttribute}.
*/
public Map<String, byte[]> getAttributes() {
return Collections.unmodifiableMap(mTxtRecord);
}
private int getTxtRecordSize() {
int txtRecordSize = 0;
for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
txtRecordSize += 2; // One for the length byte, one for the = between key and value.
txtRecordSize += entry.getKey().length();
byte[] value = entry.getValue();
txtRecordSize += value == null ? 0 : value.length;
}
return txtRecordSize;
}
/** @hide */
public @NonNull byte[] getTxtRecord() {
int txtRecordSize = getTxtRecordSize();
if (txtRecordSize == 0) {
return new byte[]{};
}
byte[] txtRecord = new byte[txtRecordSize];
int ptr = 0;
for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
String key = entry.getKey();
byte[] value = entry.getValue();
// One byte to record the length of this key/value pair.
txtRecord[ptr++] = (byte) (key.length() + (value == null ? 0 : value.length) + 1);
// The key, in US-ASCII.
// Note: use the StandardCharsets const here because it doesn't raise exceptions and we
// already know the key is ASCII at this point.
System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, txtRecord, ptr,
key.length());
ptr += key.length();
// US-ASCII '=' character.
txtRecord[ptr++] = (byte)'=';
// The value, as any raw bytes.
if (value != null) {
System.arraycopy(value, 0, txtRecord, ptr, value.length);
ptr += value.length;
}
}
return txtRecord;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("name: ").append(mServiceName)
.append(", type: ").append(mServiceType)
.append(", host: ").append(mHost)
.append(", port: ").append(mPort);
byte[] txtRecord = getTxtRecord();
if (txtRecord != null) {
sb.append(", txtRecord: ").append(new String(txtRecord, StandardCharsets.UTF_8));
}
return sb.toString();
}
/** Implement the Parcelable interface */
public int describeContents() {
return 0;
}
/** Implement the Parcelable interface */
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mServiceName);
dest.writeString(mServiceType);
if (mHost != null) {
dest.writeInt(1);
dest.writeByteArray(mHost.getAddress());
} else {
dest.writeInt(0);
}
dest.writeInt(mPort);
// TXT record key/value pairs.
dest.writeInt(mTxtRecord.size());
for (String key : mTxtRecord.keySet()) {
byte[] value = mTxtRecord.get(key);
if (value != null) {
dest.writeInt(1);
dest.writeInt(value.length);
dest.writeByteArray(value);
} else {
dest.writeInt(0);
}
dest.writeString(key);
}
}
/** Implement the Parcelable interface */
public static final @android.annotation.NonNull Creator<NsdServiceInfo> CREATOR =
new Creator<NsdServiceInfo>() {
public NsdServiceInfo createFromParcel(Parcel in) {
NsdServiceInfo info = new NsdServiceInfo();
info.mServiceName = in.readString();
info.mServiceType = in.readString();
if (in.readInt() == 1) {
try {
info.mHost = InetAddress.getByAddress(in.createByteArray());
} catch (java.net.UnknownHostException e) {}
}
info.mPort = in.readInt();
// TXT record key/value pairs.
int recordCount = in.readInt();
for (int i = 0; i < recordCount; ++i) {
byte[] valueArray = null;
if (in.readInt() == 1) {
int valueLength = in.readInt();
valueArray = new byte[valueLength];
in.readByteArray(valueArray);
}
info.mTxtRecord.put(in.readString(), valueArray);
}
return info;
}
public NsdServiceInfo[] newArray(int size) {
return new NsdServiceInfo[size];
}
};
}