use of android.net.NetworkStateTracker in project android_frameworks_base by ParanoidAndroid.
the class ConnectivityService method requestRouteToHostAddress.
/**
* Ensure that a network route exists to deliver traffic to the specified
* host via the specified network interface.
* @param networkType the type of the network over which traffic to the
* specified host is to be routed
* @param hostAddress the IP address of the host to which the route is
* desired
* @return {@code true} on success, {@code false} on failure
*/
public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
enforceChangePermission();
if (mProtectedNetworks.contains(networkType)) {
enforceConnectivityInternalPermission();
}
if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
if (DBG)
log("requestRouteToHostAddress on invalid network: " + networkType);
return false;
}
NetworkStateTracker tracker = mNetTrackers[networkType];
DetailedState netState = tracker.getNetworkInfo().getDetailedState();
if (tracker == null || (netState != DetailedState.CONNECTED && netState != DetailedState.CAPTIVE_PORTAL_CHECK) || tracker.isTeardownRequested()) {
if (VDBG) {
log("requestRouteToHostAddress on down network " + "(" + networkType + ") - dropped" + " tracker=" + tracker + " netState=" + netState + " isTeardownRequested=" + ((tracker != null) ? tracker.isTeardownRequested() : "tracker:null"));
}
return false;
}
final long token = Binder.clearCallingIdentity();
try {
InetAddress addr = InetAddress.getByAddress(hostAddress);
LinkProperties lp = tracker.getLinkProperties();
boolean ok = addRouteToAddress(lp, addr);
if (DBG)
log("requestRouteToHostAddress ok=" + ok);
return ok;
} catch (UnknownHostException e) {
if (DBG)
log("requestRouteToHostAddress got " + e.toString());
} finally {
Binder.restoreCallingIdentity(token);
}
if (DBG)
log("requestRouteToHostAddress X bottom return false");
return false;
}
use of android.net.NetworkStateTracker in project android_frameworks_base by ParanoidAndroid.
the class ConnectivityService method handleDnsConfigurationChange.
private void handleDnsConfigurationChange(int netType) {
// add default net's dns entries
NetworkStateTracker nt = mNetTrackers[netType];
if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
LinkProperties p = nt.getLinkProperties();
if (p == null)
return;
Collection<InetAddress> dnses = p.getDnses();
if (mNetConfigs[netType].isDefault()) {
String network = nt.getNetworkInfo().getTypeName();
synchronized (mDnsLock) {
if (!mDnsOverridden) {
updateDnsLocked(network, p.getInterfaceName(), dnses, p.getDomains());
}
}
} else {
try {
mNetd.setDnsServersForInterface(p.getInterfaceName(), NetworkUtils.makeStrings(dnses), p.getDomains());
} catch (Exception e) {
if (DBG)
loge("exception setting dns servers: " + e);
}
// set per-pid dns for attached secondary nets
List<Integer> pids = mNetRequestersPids[netType];
for (Integer pid : pids) {
try {
mNetd.setDnsInterfaceForPid(p.getInterfaceName(), pid);
} catch (Exception e) {
Slog.e(TAG, "exception setting interface for pid: " + e);
}
}
}
flushVmDnsCache();
}
}
use of android.net.NetworkStateTracker in project android_frameworks_base by ParanoidAndroid.
the class ConnectivityService method stopUsingNetworkFeature.
private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
int networkType = u.mNetworkType;
String feature = u.mFeature;
int pid = u.mPid;
int uid = u.mUid;
NetworkStateTracker tracker = null;
// used to carry our decision outside of sync block
boolean callTeardown = false;
if (VDBG) {
log("stopUsingNetworkFeature: net " + networkType + ": " + feature);
}
if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
if (DBG) {
log("stopUsingNetworkFeature: net " + networkType + ": " + feature + ", net is invalid");
}
return -1;
}
// sync block
synchronized (this) {
// check if this process still has an outstanding start request
if (!mFeatureUsers.contains(u)) {
if (VDBG) {
log("stopUsingNetworkFeature: this process has no outstanding requests" + ", ignoring");
}
return 1;
}
u.unlinkDeathRecipient();
mFeatureUsers.remove(mFeatureUsers.indexOf(u));
// API does not refcount and a single stop will counter multiple starts.
if (ignoreDups == false) {
for (FeatureUser x : mFeatureUsers) {
if (x.isSameUser(u)) {
if (VDBG)
log("stopUsingNetworkFeature: dup is found, ignoring");
return 1;
}
}
}
// TODO - move to individual network trackers
int usedNetworkType = convertFeatureToNetworkType(networkType, feature);
tracker = mNetTrackers[usedNetworkType];
if (tracker == null) {
if (DBG) {
log("stopUsingNetworkFeature: net " + networkType + ": " + feature + " no known tracker for used net type " + usedNetworkType);
}
return -1;
}
if (usedNetworkType != networkType) {
Integer currentPid = new Integer(pid);
mNetRequestersPids[usedNetworkType].remove(currentPid);
final long token = Binder.clearCallingIdentity();
try {
reassessPidDns(pid, true);
} finally {
Binder.restoreCallingIdentity(token);
}
flushVmDnsCache();
if (mNetRequestersPids[usedNetworkType].size() != 0) {
if (VDBG) {
log("stopUsingNetworkFeature: net " + networkType + ": " + feature + " others still using it");
}
return 1;
}
callTeardown = true;
} else {
if (DBG) {
log("stopUsingNetworkFeature: net " + networkType + ": " + feature + " not a known feature - dropping");
}
}
}
if (callTeardown) {
if (DBG) {
log("stopUsingNetworkFeature: teardown net " + networkType + ": " + feature);
}
tracker.teardown();
return 1;
} else {
return -1;
}
}
use of android.net.NetworkStateTracker in project android_frameworks_base by ParanoidAndroid.
the class ConnectivityService method handleConnectivityChange.
/**
* After a change in the connectivity state of a network. We're mainly
* concerned with making sure that the list of DNS servers is set up
* according to which networks are connected, and ensuring that the
* right routing table entries exist.
*/
private void handleConnectivityChange(int netType, boolean doReset) {
int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
if (VDBG) {
log("handleConnectivityChange: netType=" + netType + " doReset=" + doReset + " resetMask=" + resetMask);
}
/*
* If a non-default network is enabled, add the host routes that
* will allow it's DNS servers to be accessed.
*/
handleDnsConfigurationChange(netType);
LinkProperties curLp = mCurrentLinkProperties[netType];
LinkProperties newLp = null;
if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
newLp = mNetTrackers[netType].getLinkProperties();
if (VDBG) {
log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + " doReset=" + doReset + " resetMask=" + resetMask + "\n curLp=" + curLp + "\n newLp=" + newLp);
}
if (curLp != null) {
if (curLp.isIdenticalInterfaceName(newLp)) {
CompareResult<LinkAddress> car = curLp.compareAddresses(newLp);
if ((car.removed.size() != 0) || (car.added.size() != 0)) {
for (LinkAddress linkAddr : car.removed) {
if (linkAddr.getAddress() instanceof Inet4Address) {
resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
}
if (linkAddr.getAddress() instanceof Inet6Address) {
resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
}
}
if (DBG) {
log("handleConnectivityChange: addresses changed" + " linkProperty[" + netType + "]:" + " resetMask=" + resetMask + "\n car=" + car);
}
} else {
if (DBG) {
log("handleConnectivityChange: address are the same reset per doReset" + " linkProperty[" + netType + "]:" + " resetMask=" + resetMask);
}
}
} else {
resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
if (DBG) {
log("handleConnectivityChange: interface not not equivalent reset both" + " linkProperty[" + netType + "]:" + " resetMask=" + resetMask);
}
}
}
if (mNetConfigs[netType].isDefault()) {
handleApplyDefaultProxy(newLp.getHttpProxy());
}
} else {
if (VDBG) {
log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + " doReset=" + doReset + " resetMask=" + resetMask + "\n curLp=" + curLp + "\n newLp= null");
}
}
mCurrentLinkProperties[netType] = newLp;
boolean resetDns = updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault());
if (resetMask != 0 || resetDns) {
if (VDBG)
log("handleConnectivityChange: resetting");
if (curLp != null) {
if (VDBG)
log("handleConnectivityChange: resetting curLp=" + curLp);
for (String iface : curLp.getAllInterfaceNames()) {
if (TextUtils.isEmpty(iface) == false) {
if (resetMask != 0) {
if (DBG)
log("resetConnections(" + iface + ", " + resetMask + ")");
NetworkUtils.resetConnections(iface, resetMask);
// but effective fix to make VPN aware of the change.
if ((resetMask & NetworkUtils.RESET_IPV4_ADDRESSES) != 0) {
mVpn.interfaceStatusChanged(iface, false);
}
}
if (resetDns) {
flushVmDnsCache();
if (VDBG)
log("resetting DNS cache for " + iface);
try {
mNetd.flushInterfaceDnsCache(iface);
} catch (Exception e) {
// never crash - catch them all
if (DBG)
loge("Exception resetting dns cache: " + e);
}
}
} else {
loge("Can't reset connection for type " + netType);
}
}
}
}
// Update 464xlat state.
NetworkStateTracker tracker = mNetTrackers[netType];
if (mClat.requiresClat(netType, tracker)) {
// still be running. If it's not running, then stopping it is a no-op.
if (Nat464Xlat.isRunningClat(curLp) && !Nat464Xlat.isRunningClat(newLp)) {
mClat.stopClat();
}
// If the link requires clat to be running, then start the daemon now.
if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
mClat.startClat(tracker);
} else {
mClat.stopClat();
}
}
/** Notify TetheringService if interface name has been changed. */
if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(), PhoneConstants.REASON_LINK_PROPERTIES_CHANGED)) {
if (isTetheringSupported()) {
mTethering.handleTetherIfaceChange();
}
}
}
use of android.net.NetworkStateTracker in project android_frameworks_base by ParanoidAndroid.
the class ConnectivityService method getAllNetworkInfo.
@Override
public NetworkInfo[] getAllNetworkInfo() {
enforceAccessPermission();
final int uid = Binder.getCallingUid();
final ArrayList<NetworkInfo> result = Lists.newArrayList();
synchronized (mRulesLock) {
for (NetworkStateTracker tracker : mNetTrackers) {
if (tracker != null) {
result.add(getFilteredNetworkInfo(tracker, uid));
}
}
}
return result.toArray(new NetworkInfo[result.size()]);
}
Aggregations