NetworkAgentInfo.java revision 71b645fe9cb8106dfcbf025a3fd7f58698c051bb
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) {
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);
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    public boolean isVPN() {
100        return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
101    }
102
103    // Get the current score for this Network.  This may be modified from what the
104    // NetworkAgent sent, as it has modifiers applied to it.
105    public int getCurrentScore() {
106        // TODO: We may want to refactor this into a NetworkScore class that takes a base score from
107        // the NetworkAgent and signals from the NetworkAgent and uses those signals to modify the
108        // score.  The NetworkScore class would provide a nice place to centralize score constants
109        // so they are not scattered about the transports.
110
111        int score = currentScore;
112
113        if (!validated) score -= UNVALIDATED_SCORE_PENALTY;
114        if (score < 0) score = 0;
115
116        if (networkMisc.explicitlySelected) score = EXPLICITLY_SELECTED_NETWORK_SCORE;
117
118        return score;
119    }
120
121    public void setCurrentScore(int newScore) {
122        currentScore = newScore;
123    }
124
125    public String toString() {
126        return "NetworkAgentInfo{ ni{" + networkInfo + "}  network{" +
127                network + "}  lp{" +
128                linkProperties + "}  nc{" +
129                networkCapabilities + "}  Score{" + getCurrentScore() + "} " +
130                "validated{" + validated + "} created{" + created + "} " +
131                "explicitlySelected{" + networkMisc.explicitlySelected + "} }";
132    }
133
134    public String name() {
135        return "NetworkAgentInfo [" + networkInfo.getTypeName() + " (" +
136                networkInfo.getSubtypeName() + ") - " +
137                (network == null ? "null" : network.toString()) + "]";
138    }
139}
140