NetworkAgentInfo.java revision d49159f4e997faae6ef4141b667ad6fef74a6724
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.connectivity; 18 19import android.content.Context; 20import android.net.LinkProperties; 21import android.net.Network; 22import android.net.NetworkCapabilities; 23import android.net.NetworkInfo; 24import android.net.NetworkMisc; 25import android.net.NetworkRequest; 26import android.os.Handler; 27import android.os.Messenger; 28import android.util.SparseArray; 29 30import com.android.internal.util.AsyncChannel; 31import com.android.server.connectivity.NetworkMonitor; 32 33import java.util.ArrayList; 34 35/** 36 * A bag class used by ConnectivityService for holding a collection of most recent 37 * information published by a particular NetworkAgent as well as the 38 * AsyncChannel/messenger for reaching that NetworkAgent and lists of NetworkRequests 39 * interested in using it. 40 */ 41public class NetworkAgentInfo { 42 public NetworkInfo networkInfo; 43 // This Network object should always be used if possible, so as to encourage reuse of the 44 // enclosed socket factory and connection pool. Avoid creating other Network objects. 45 // This Network object is always valid. 46 public final Network network; 47 public LinkProperties linkProperties; 48 public NetworkCapabilities networkCapabilities; 49 public final NetworkMonitor networkMonitor; 50 public final NetworkMisc networkMisc; 51 // Indicates if netd has been told to create this Network. Once created the appropriate routing 52 // rules are setup and routes are added so packets can begin flowing over the Network. 53 // This is a sticky bit; once set it is never cleared. 54 public boolean created; 55 // Set to true if this Network successfully passed validation or if it did not satisfy the 56 // default NetworkRequest in which case validation will not be attempted. 57 // This is a sticky bit; once set it is never cleared even if future validation attempts fail. 58 public boolean everValidated; 59 60 // The result of the last validation attempt on this network (true if validated, false if not). 61 // This bit exists only because we never unvalidate a network once it's been validated, and that 62 // is because the network scoring and revalidation code does not (may not?) deal properly with 63 // networks becoming unvalidated. 64 // TODO: Fix the network scoring code, remove this, and rename everValidated to validated. 65 public boolean lastValidated; 66 67 // Whether a captive portal was ever detected on this network. 68 // This is a sticky bit; once set it is never cleared. 69 public boolean captivePortalDetected; 70 71 // This represents the last score received from the NetworkAgent. 72 private int currentScore; 73 // Penalty applied to scores of Networks that have not been validated. 74 private static final int UNVALIDATED_SCORE_PENALTY = 40; 75 76 // Score for explicitly connected network. 77 // 78 // This ensures that a) the explicitly selected network is never trumped by anything else, and 79 // b) the explicitly selected network is never torn down. 80 private static final int MAXIMUM_NETWORK_SCORE = 100; 81 82 // The list of NetworkRequests being satisfied by this Network. 83 public final SparseArray<NetworkRequest> networkRequests = new SparseArray<NetworkRequest>(); 84 // The list of NetworkRequests that this Network previously satisfied with the highest 85 // score. A non-empty list indicates that if this Network was validated it is lingered. 86 // NOTE: This list is only used for debugging. 87 public final ArrayList<NetworkRequest> networkLingered = new ArrayList<NetworkRequest>(); 88 89 public final Messenger messenger; 90 public final AsyncChannel asyncChannel; 91 92 // Used by ConnectivityService to keep track of 464xlat. 93 public Nat464Xlat clatd; 94 95 public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, Network net, NetworkInfo info, 96 LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler, 97 NetworkMisc misc, NetworkRequest defaultRequest) { 98 this.messenger = messenger; 99 asyncChannel = ac; 100 network = net; 101 networkInfo = info; 102 linkProperties = lp; 103 networkCapabilities = nc; 104 currentScore = score; 105 networkMonitor = new NetworkMonitor(context, handler, this, defaultRequest); 106 networkMisc = misc; 107 } 108 109 public void addRequest(NetworkRequest networkRequest) { 110 networkRequests.put(networkRequest.requestId, networkRequest); 111 } 112 113 // Does this network satisfy request? 114 public boolean satisfies(NetworkRequest request) { 115 return created && 116 request.networkCapabilities.satisfiedByNetworkCapabilities(networkCapabilities); 117 } 118 119 public boolean isVPN() { 120 return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN); 121 } 122 123 private int getCurrentScore(boolean pretendValidated) { 124 // TODO: We may want to refactor this into a NetworkScore class that takes a base score from 125 // the NetworkAgent and signals from the NetworkAgent and uses those signals to modify the 126 // score. The NetworkScore class would provide a nice place to centralize score constants 127 // so they are not scattered about the transports. 128 129 // If this network is explicitly selected and the user has decided to use it even if it's 130 // unvalidated, give it the maximum score. Also give it the maximum score if it's explicitly 131 // selected and we're trying to see what its score could be. This ensures that we don't tear 132 // down an explicitly selected network before the user gets a chance to prefer it when 133 // a higher-scoring network (e.g., Ethernet) is available. 134 if (networkMisc.explicitlySelected && (networkMisc.acceptUnvalidated || pretendValidated)) { 135 return MAXIMUM_NETWORK_SCORE; 136 } 137 138 int score = currentScore; 139 if (!everValidated && !pretendValidated) score -= UNVALIDATED_SCORE_PENALTY; 140 if (score < 0) score = 0; 141 return score; 142 } 143 144 // Get the current score for this Network. This may be modified from what the 145 // NetworkAgent sent, as it has modifiers applied to it. 146 public int getCurrentScore() { 147 return getCurrentScore(false); 148 } 149 150 // Get the current score for this Network as if it was validated. This may be modified from 151 // what the NetworkAgent sent, as it has modifiers applied to it. 152 public int getCurrentScoreAsValidated() { 153 return getCurrentScore(true); 154 } 155 156 public void setCurrentScore(int newScore) { 157 currentScore = newScore; 158 } 159 160 public String toString() { 161 return "NetworkAgentInfo{ ni{" + networkInfo + "} network{" + 162 network + "} lp{" + 163 linkProperties + "} nc{" + 164 networkCapabilities + "} Score{" + getCurrentScore() + "} " + 165 "everValidated{" + everValidated + "} lastValidated{" + lastValidated + "} " + 166 "created{" + created + "} " + 167 "explicitlySelected{" + networkMisc.explicitlySelected + "} " + 168 "acceptUnvalidated{" + networkMisc.acceptUnvalidated + "} " + 169 "captivePortalDetected{" + captivePortalDetected + "} " + 170 "}"; 171 } 172 173 public String name() { 174 return "NetworkAgentInfo [" + networkInfo.getTypeName() + " (" + 175 networkInfo.getSubtypeName() + ") - " + 176 (network == null ? "null" : network.toString()) + "]"; 177 } 178} 179