From bff73490ac373f8095f54ebfa26fc30d643bcb54 Mon Sep 17 00:00:00 2001 From: Paul Jensen Date: Mon, 28 Apr 2014 10:33:11 -0400 Subject: [PATCH] Separate network and interface addition/removal netd APIs. This should facilitate stacked interfaces (i.e. clatd). Change-Id: Ib3e7a4d3847ef6ec4449451f6da42e75959baa4f --- core/java/android/net/LinkProperties.java | 29 +++++++++++++++++++ .../android/server/ConnectivityService.java | 28 ++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java index 2dcc544f6f..0a09fcba4e 100644 --- a/core/java/android/net/LinkProperties.java +++ b/core/java/android/net/LinkProperties.java @@ -642,6 +642,35 @@ public class LinkProperties implements Parcelable { return result; } + /** + * Compares all interface names in this LinkProperties with another + * LinkProperties, examining both the the base link and all stacked links. + * + * @param target a LinkProperties with the new list of interface names + * @return the differences between the interface names. + * @hide + */ + public CompareResult compareAllInterfaceNames(LinkProperties target) { + /* + * Duplicate the interface names into removed, we will be removing + * interface names which are common between this and target + * leaving the interface names that are different. And interface names which + * are in target but not in this are placed in added. + */ + CompareResult result = new CompareResult(); + + result.removed = getAllInterfaceNames(); + result.added.clear(); + if (target != null) { + for (String r : target.getAllInterfaceNames()) { + if (! result.removed.remove(r)) { + result.added.add(r); + } + } + } + return result; + } + @Override /** diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index cdb82e76a6..6cc738b763 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -5088,6 +5088,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { LinkProperties newLp = networkAgent.linkProperties; int netId = networkAgent.network.netId; + updateInterfaces(newLp, oldLp, netId); updateMtu(newLp, oldLp); // TODO - figure out what to do for clat // for (LinkProperties lp : newLp.getStackedLinks()) { @@ -5096,6 +5097,30 @@ public class ConnectivityService extends IConnectivityManager.Stub { updateRoutes(newLp, oldLp, netId); updateDnses(newLp, oldLp, netId); } + + private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) { + CompareResult interfaceDiff = new CompareResult(); + if (oldLp != null) { + interfaceDiff = oldLp.compareAllInterfaceNames(newLp); + } else if (newLp != null) { + interfaceDiff.added = newLp.getAllInterfaceNames(); + } + for (String iface : interfaceDiff.added) { + try { + mNetd.addInterfaceToNetwork(iface, netId); + } catch (Exception e) { + loge("Exception adding interface: " + e); + } + } + for (String iface : interfaceDiff.removed) { + try { + mNetd.removeInterfaceFromNetwork(iface, netId); + } catch (Exception e) { + loge("Exception removing interface: " + e); + } + } + } + private void updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { CompareResult routeDiff = new CompareResult(); if (oldLp != null) { @@ -5300,8 +5325,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { if (state == NetworkInfo.State.CONNECTED) { // TODO - check if we want it (optimization) try { - mNetd.createNetwork(networkAgent.network.netId, - networkAgent.linkProperties.getInterfaceName()); + mNetd.createNetwork(networkAgent.network.netId); } catch (Exception e) { loge("Error creating Network " + networkAgent.network.netId); }