Stop registering native mdns service am: 80c9f6f35f

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2832130

Change-Id: I16670e2d4ceac97afa8de915c506ccb8aeb98600
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Ken Chen 2023-12-27 14:24:25 +00:00 • committed by Automerger Merge Worker
commit d0147f3b40
5 changed files with 108 additions and 23 deletions

View file

@ -27,6 +27,8 @@ import android.net.nsd.NsdManager;
import android.net.thread.IThreadNetworkManager;
import android.net.thread.ThreadNetworkManager;
import com.android.modules.utils.build.SdkLevel;
/**
* Class for performing registration for Connectivity services which are exposed via updatable APIs
* since Android T.
@ -83,14 +85,17 @@ public final class ConnectivityFrameworkInitializerTiramisu {
}
);
SystemServiceRegistry.registerStaticService(
MDnsManager.MDNS_SERVICE,
MDnsManager.class,
(serviceBinder) -> {
IMDns service = IMDns.Stub.asInterface(serviceBinder);
return new MDnsManager(service);
}
);
// mdns service is removed from Netd from Android V.
if (!SdkLevel.isAtLeastV()) {
SystemServiceRegistry.registerStaticService(
MDnsManager.MDNS_SERVICE,
MDnsManager.class,
(serviceBinder) -> {
IMDns service = IMDns.Stub.asInterface(serviceBinder);
return new MDnsManager(service);
}
);
}
SystemServiceRegistry.registerContextAwareService(
ThreadNetworkManager.SERVICE_NAME,

View file

@ -51,6 +51,7 @@ import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.build.SdkLevel;
import com.android.net.module.util.CollectionUtils;
import java.lang.annotation.Retention;
@ -688,9 +689,12 @@ public final class NsdManager {
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_T_AND_LATER)) {
// Only proactively start the daemon if the target SDK < S AND platform < V, For target
// SDK >= S AND platform < V, the internal service would automatically start/stop the native
// daemon as needed. For platform >= V, no action is required because the native daemon is
// completely removed.
if (!CompatChanges.isChangeEnabled(RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
&& !SdkLevel.isAtLeastV()) {
try {
mService.startDaemon();
} catch (RemoteException e) {

View file

@ -199,7 +199,8 @@ public class NsdService extends INsdManager.Stub {
private final Context mContext;
private final NsdStateMachine mNsdStateMachine;
private final MDnsManager mMDnsManager;
// It can be null on V+ device since mdns native service provided by netd is removed.
private final @Nullable MDnsManager mMDnsManager;
private final MDnsEventCallback mMDnsEventCallback;
@NonNull
private final Dependencies mDeps;
@ -541,6 +542,11 @@ public class NsdService extends INsdManager.Stub {
}
private void maybeStartDaemon() {
if (mMDnsManager == null) {
Log.wtf(TAG, "maybeStartDaemon: mMDnsManager is null");
return;
}
if (mIsDaemonStarted) {
if (DBG) Log.d(TAG, "Daemon is already started.");
return;
@ -553,6 +559,11 @@ public class NsdService extends INsdManager.Stub {
}
private void maybeStopDaemon() {
if (mMDnsManager == null) {
Log.wtf(TAG, "maybeStopDaemon: mMDnsManager is null");
return;
}
if (!mIsDaemonStarted) {
if (DBG) Log.d(TAG, "Daemon has not been started.");
return;
@ -1698,7 +1709,8 @@ public class NsdService extends INsdManager.Stub {
mContext = ctx;
mNsdStateMachine = new NsdStateMachine(TAG, handler);
mNsdStateMachine.start();
mMDnsManager = ctx.getSystemService(MDnsManager.class);
// It can fail on V+ device since mdns native service provided by netd is removed.
mMDnsManager = SdkLevel.isAtLeastV() ? null : ctx.getSystemService(MDnsManager.class);
mMDnsEventCallback = new MDnsEventCallback(mNsdStateMachine);
mDeps = deps;
@ -2235,6 +2247,11 @@ public class NsdService extends INsdManager.Stub {
}
private boolean registerService(int transactionId, NsdServiceInfo service) {
if (mMDnsManager == null) {
Log.wtf(TAG, "registerService: mMDnsManager is null");
return false;
}
if (DBG) {
Log.d(TAG, "registerService: " + transactionId + " " + service);
}
@ -2252,10 +2269,19 @@ public class NsdService extends INsdManager.Stub {
}
private boolean unregisterService(int transactionId) {
if (mMDnsManager == null) {
Log.wtf(TAG, "unregisterService: mMDnsManager is null");
return false;
}
return mMDnsManager.stopOperation(transactionId);
}
private boolean discoverServices(int transactionId, NsdServiceInfo serviceInfo) {
if (mMDnsManager == null) {
Log.wtf(TAG, "discoverServices: mMDnsManager is null");
return false;
}
final String type = serviceInfo.getServiceType();
final int discoverInterface = getNetworkInterfaceIndex(serviceInfo);
if (serviceInfo.getNetwork() != null && discoverInterface == IFACE_IDX_ANY) {
@ -2266,10 +2292,18 @@ public class NsdService extends INsdManager.Stub {
}
private boolean stopServiceDiscovery(int transactionId) {
if (mMDnsManager == null) {
Log.wtf(TAG, "stopServiceDiscovery: mMDnsManager is null");
return false;
}
return mMDnsManager.stopOperation(transactionId);
}
private boolean resolveService(int transactionId, NsdServiceInfo service) {
if (mMDnsManager == null) {
Log.wtf(TAG, "resolveService: mMDnsManager is null");
return false;
}
final String name = service.getServiceName();
final String type = service.getServiceType();
final int resolveInterface = getNetworkInterfaceIndex(service);
@ -2343,14 +2377,26 @@ public class NsdService extends INsdManager.Stub {
}
private boolean stopResolveService(int transactionId) {
if (mMDnsManager == null) {
Log.wtf(TAG, "stopResolveService: mMDnsManager is null");
return false;
}
return mMDnsManager.stopOperation(transactionId);
}
private boolean getAddrInfo(int transactionId, String hostname, int interfaceIdx) {
if (mMDnsManager == null) {
Log.wtf(TAG, "getAddrInfo: mMDnsManager is null");
return false;
}
return mMDnsManager.getServiceAddress(transactionId, hostname, interfaceIdx);
}
private boolean stopGetAddrInfo(int transactionId) {
if (mMDnsManager == null) {
Log.wtf(TAG, "stopGetAddrInfo: mMDnsManager is null");
return false;
}
return mMDnsManager.stopOperation(transactionId);
}

View file

@ -38,6 +38,7 @@ import android.os.Build;
import androidx.test.filters.SmallTest;
import com.android.modules.utils.build.SdkLevel;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner;
import com.android.testutils.FunctionalUtils.ThrowingConsumer;
@ -86,73 +87,81 @@ public class NsdManagerTest {
@Test
@EnableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testResolveServiceS() throws Exception {
verify(mServiceConn, never()).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ false);
doTestResolveService();
}
@Test
@DisableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testResolveServicePreS() throws Exception {
verify(mServiceConn).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ true);
doTestResolveService();
}
@Test
@EnableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testDiscoverServiceS() throws Exception {
verify(mServiceConn, never()).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ false);
doTestDiscoverService();
}
@Test
@DisableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testDiscoverServicePreS() throws Exception {
verify(mServiceConn).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ true);
doTestDiscoverService();
}
@Test
@EnableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testParallelResolveServiceS() throws Exception {
verify(mServiceConn, never()).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ false);
doTestParallelResolveService();
}
@Test
@DisableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testParallelResolveServicePreS() throws Exception {
verify(mServiceConn).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ true);
doTestParallelResolveService();
}
@Test
@EnableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testInvalidCallsS() throws Exception {
verify(mServiceConn, never()).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ false);
doTestInvalidCalls();
}
@Test
@DisableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testInvalidCallsPreS() throws Exception {
verify(mServiceConn).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ true);
doTestInvalidCalls();
}
@Test
@EnableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testRegisterServiceS() throws Exception {
verify(mServiceConn, never()).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ false);
doTestRegisterService();
}
@Test
@DisableCompatChanges(ConnectivityCompatChanges.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
public void testRegisterServicePreS() throws Exception {
verify(mServiceConn).startDaemon();
verifyDaemonStarted(/* targetSdkPreS= */ true);
doTestRegisterService();
}
private void verifyDaemonStarted(boolean targetSdkPreS) throws Exception {
if (targetSdkPreS && !SdkLevel.isAtLeastV()) {
verify(mServiceConn).startDaemon();
} else {
verify(mServiceConn, never()).startDaemon();
}
}
private void doTestResolveService() throws Exception {
NsdManager manager = mManager;

View file

@ -150,6 +150,9 @@ import java.util.Set;
@SmallTest
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
public class NsdServiceTest {
@Rule
public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule();
static final int PROTOCOL = NsdManager.PROTOCOL_DNS_SD;
private static final long CLEANUP_DELAY_MS = 500;
private static final long TIMEOUT_MS = 500;
@ -255,6 +258,8 @@ public class NsdServiceTest {
}
}
// Native mdns provided by Netd is removed after U.
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@Test
@DisableCompatChanges({
RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER,
@ -287,6 +292,7 @@ public class NsdServiceTest {
@Test
@EnableCompatChanges(RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testNoDaemonStartedWhenClientsConnect() throws Exception {
// Creating an NsdManager will not cause daemon startup.
connectClient(mService);
@ -322,6 +328,7 @@ public class NsdServiceTest {
@Test
@EnableCompatChanges(RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testClientRequestsAreGCedAtDisconnection() throws Exception {
final NsdManager client = connectClient(mService);
final INsdManagerCallback cb1 = getCallback();
@ -366,6 +373,7 @@ public class NsdServiceTest {
@Test
@EnableCompatChanges(RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER)
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testCleanupDelayNoRequestActive() throws Exception {
final NsdManager client = connectClient(mService);
@ -402,6 +410,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testDiscoverOnTetheringDownstream() throws Exception {
final NsdManager client = connectClient(mService);
final int interfaceIdx = 123;
@ -500,6 +509,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testDiscoverOnBlackholeNetwork() throws Exception {
final NsdManager client = connectClient(mService);
final DiscoveryListener discListener = mock(DiscoveryListener.class);
@ -532,6 +542,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testServiceRegistrationSuccessfulAndFailed() throws Exception {
final NsdManager client = connectClient(mService);
final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
@ -586,6 +597,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testServiceDiscoveryFailed() throws Exception {
final NsdManager client = connectClient(mService);
final DiscoveryListener discListener = mock(DiscoveryListener.class);
@ -618,6 +630,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testServiceResolutionFailed() throws Exception {
final NsdManager client = connectClient(mService);
final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
@ -653,6 +666,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testGettingAddressFailed() throws Exception {
final NsdManager client = connectClient(mService);
final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
@ -704,6 +718,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testNoCrashWhenProcessResolutionAfterBinderDied() throws Exception {
final NsdManager client = connectClient(mService);
final INsdManagerCallback cb = getCallback();
@ -724,6 +739,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testStopServiceResolution() {
final NsdManager client = connectClient(mService);
final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
@ -750,6 +766,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testStopResolutionFailed() {
final NsdManager client = connectClient(mService);
final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
@ -775,6 +792,7 @@ public class NsdServiceTest {
@Test @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testStopResolutionDuringGettingAddress() throws RemoteException {
final NsdManager client = connectClient(mService);
final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
@ -956,6 +974,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testMdnsDiscoveryManagerFeature() {
// Create NsdService w/o feature enabled.
final NsdManager client = connectClient(mService);
@ -1203,6 +1222,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testMdnsAdvertiserFeatureFlagging() {
// Create NsdService w/o feature enabled.
final NsdManager client = connectClient(mService);
@ -1241,6 +1261,7 @@ public class NsdServiceTest {
@Test
@DisableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND)
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void testTypeSpecificFeatureFlagging() {
doReturn("_type1._tcp:flag1,_type2._tcp:flag2").when(mDeps).getTypeAllowlistFlags();
doReturn(true).when(mDeps).isFeatureEnabled(any(),