Avoid reusing a currently active NetID.

There are only a limited number (65526) of NetIDs so the chance for reusing
one exists.  Reusing a currently active NetID will cause problems like netd
failures and overwriting entries in mNetworkForNetId.

bug:16815182

Change-Id: Ib75302beae3179c3f3b90c345cf4d2cf5f4ad2be
This commit is contained in:
Paul Jensen
2014-08-05 14:13:48 -04:00
parent cbec2b12ca
commit 46e129be74
2 changed files with 21 additions and 12 deletions

View File

@@ -756,10 +756,20 @@ public class ConnectivityService extends IConnectivityManager.Stub {
return mNextNetworkRequestId++;
}
private synchronized int nextNetId() {
int netId = mNextNetId;
if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID;
return netId;
private void assignNextNetId(NetworkAgentInfo nai) {
synchronized (mNetworkForNetId) {
for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) {
int netId = mNextNetId;
if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID;
// Make sure NetID unused. http://b/16815182
if (mNetworkForNetId.get(netId) == null) {
nai.network = new Network(netId);
mNetworkForNetId.put(netId, nai);
return;
}
}
}
throw new IllegalStateException("No free netIds");
}
private int getConnectivityChangeDelay() {
@@ -4104,7 +4114,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
int currentScore, NetworkMisc networkMisc) {
enforceConnectivityInternalPermission();
NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), nextNetId(),
NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(),
new NetworkInfo(networkInfo), new LinkProperties(linkProperties),
new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler,
networkMisc);
@@ -4118,9 +4128,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
private void handleRegisterNetworkAgent(NetworkAgentInfo na) {
if (VDBG) log("Got NetworkAgent Messenger");
mNetworkAgentInfos.put(na.messenger, na);
synchronized (mNetworkForNetId) {
mNetworkForNetId.put(na.network.netId, na);
}
assignNextNetId(na);
na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger);
NetworkInfo networkInfo = na.networkInfo;
na.networkInfo = null;

View File

@@ -40,7 +40,7 @@ import java.util.ArrayList;
*/
public class NetworkAgentInfo {
public NetworkInfo networkInfo;
public final Network network;
public Network network;
public LinkProperties linkProperties;
public NetworkCapabilities networkCapabilities;
public int currentScore;
@@ -55,12 +55,12 @@ public class NetworkAgentInfo {
public final Messenger messenger;
public final AsyncChannel asyncChannel;
public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, int netId, NetworkInfo info,
public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, NetworkInfo info,
LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler,
NetworkMisc misc) {
this.messenger = messenger;
asyncChannel = ac;
network = new Network(netId);
network = null;
networkInfo = info;
linkProperties = lp;
networkCapabilities = nc;
@@ -87,6 +87,7 @@ public class NetworkAgentInfo {
public String name() {
return "NetworkAgentInfo [" + networkInfo.getTypeName() + " (" +
networkInfo.getSubtypeName() + ") - " + network.toString() + "]";
networkInfo.getSubtypeName() + ") - " +
(network == null ? "null" : network.toString()) + "]";
}
}