NetworkAgentInfo.java revision 0cc1732cfb9d68779449b4c12661b4df6bfc720b
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 public Network network; 44 public LinkProperties linkProperties; 45 public NetworkCapabilities networkCapabilities; 46 public final NetworkMonitor networkMonitor; 47 public final NetworkMisc networkMisc; 48 // Indicates if netd has been told to create this Network. Once created the appropriate routing 49 // rules are setup and routes are added so packets can begin flowing over the Network. 50 // NOTE: This is a sticky bit; once set it is never cleared. 51 public boolean created; 52 // Set to true if this Network successfully passed validation or if it did not satisfy the 53 // default NetworkRequest in which case validation will not be attempted. 54 // NOTE: This is a sticky bit; once set it is never cleared even if future validation attempts 55 // fail. 56 public boolean validated; 57 58 // This represents the last score received from the NetworkAgent. 59 private int currentScore; 60 // Penalty applied to scores of Networks that have not been validated. 61 private static final int UNVALIDATED_SCORE_PENALTY = 40; 62 63 // Score for explicitly connected network. 64 private static final int EXPLICITLY_SELECTED_NETWORK_SCORE = 100; 65 66 // The list of NetworkRequests being satisfied by this Network. 67 public final SparseArray<NetworkRequest> networkRequests = new SparseArray<NetworkRequest>(); 68 // The list of NetworkRequests that this Network previously satisfied with the highest 69 // score. A non-empty list indicates that if this Network was validated it is lingered. 70 // NOTE: This list is only used for debugging. 71 public final ArrayList<NetworkRequest> networkLingered = new ArrayList<NetworkRequest>(); 72 73 public final Messenger messenger; 74 public final AsyncChannel asyncChannel; 75 76 // Used by ConnectivityService to keep track of 464xlat. 77 public Nat464Xlat clatd; 78 79 public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, NetworkInfo info, 80 LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler, 81 NetworkMisc misc, NetworkRequest defaultRequest) { 82 this.messenger = messenger; 83 asyncChannel = ac; 84 network = null; 85 networkInfo = info; 86 linkProperties = lp; 87 networkCapabilities = nc; 88 currentScore = score; 89 networkMonitor = new NetworkMonitor(context, handler, this, defaultRequest); 90 networkMisc = misc; 91 created = false; 92 validated = false; 93 } 94 95 public void addRequest(NetworkRequest networkRequest) { 96 networkRequests.put(networkRequest.requestId, networkRequest); 97 } 98 99 // Does this network satisfy request? 100 public boolean satisfies(NetworkRequest request) { 101 return created && 102 request.networkCapabilities.satisfiedByNetworkCapabilities(networkCapabilities); 103 } 104 105 public boolean isVPN() { 106 return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN); 107 } 108 109 // Get the current score for this Network. This may be modified from what the 110 // NetworkAgent sent, as it has modifiers applied to it. 111 public int getCurrentScore() { 112 // TODO: We may want to refactor this into a NetworkScore class that takes a base score from 113 // the NetworkAgent and signals from the NetworkAgent and uses those signals to modify the 114 // score. The NetworkScore class would provide a nice place to centralize score constants 115 // so they are not scattered about the transports. 116 117 int score = currentScore; 118 119 if (!validated) score -= UNVALIDATED_SCORE_PENALTY; 120 if (score < 0) score = 0; 121 122 if (networkMisc.explicitlySelected) score = EXPLICITLY_SELECTED_NETWORK_SCORE; 123 124 return score; 125 } 126 127 public void setCurrentScore(int newScore) { 128 currentScore = newScore; 129 } 130 131 public String toString() { 132 return "NetworkAgentInfo{ ni{" + networkInfo + "} network{" + 133 network + "} lp{" + 134 linkProperties + "} nc{" + 135 networkCapabilities + "} Score{" + getCurrentScore() + "} " + 136 "validated{" + validated + "} created{" + created + "} " + 137 "explicitlySelected{" + networkMisc.explicitlySelected + "} }"; 138 } 139 140 public String name() { 141 return "NetworkAgentInfo [" + networkInfo.getTypeName() + " (" + 142 networkInfo.getSubtypeName() + ") - " + 143 (network == null ? "null" : network.toString()) + "]"; 144 } 145} 146