Search in sources :

Example 1 with RouteInfo

use of android.net.RouteInfo in project android_frameworks_base by ParanoidAndroid.

the class ConnectivityService method updateRoutes.

/**
     * Add and remove routes using the old properties (null if not previously connected),
     * new properties (null if becoming disconnected).  May even be double null, which
     * is a noop.
     * Uses isLinkDefault to determine if default routes should be set or conversely if
     * host routes should be set to the dns servers
     * returns a boolean indicating the routes changed
     */
private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp, boolean isLinkDefault) {
    Collection<RouteInfo> routesToAdd = null;
    CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>();
    CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>();
    if (curLp != null) {
        // check for the delta between the current set and the new
        routeDiff = curLp.compareRoutes(newLp);
        dnsDiff = curLp.compareDnses(newLp);
    } else if (newLp != null) {
        routeDiff.added = newLp.getAllRoutes();
        dnsDiff.added = newLp.getDnses();
    }
    boolean routesChanged = (routeDiff.removed.size() != 0 || routeDiff.added.size() != 0);
    for (RouteInfo r : routeDiff.removed) {
        if (isLinkDefault || !r.isDefaultRoute()) {
            if (VDBG)
                log("updateRoutes: default remove route r=" + r);
            removeRoute(curLp, r, TO_DEFAULT_TABLE);
        }
        if (isLinkDefault == false) {
            // remove from a secondary route table
            removeRoute(curLp, r, TO_SECONDARY_TABLE);
        }
    }
    if (!isLinkDefault) {
        // handle DNS routes
        if (routesChanged) {
            // routes changed - remove all old dns entries and add new
            if (curLp != null) {
                for (InetAddress oldDns : curLp.getDnses()) {
                    removeRouteToAddress(curLp, oldDns);
                }
            }
            if (newLp != null) {
                for (InetAddress newDns : newLp.getDnses()) {
                    addRouteToAddress(newLp, newDns);
                }
            }
        } else {
            // no change in routes, check for change in dns themselves
            for (InetAddress oldDns : dnsDiff.removed) {
                removeRouteToAddress(curLp, oldDns);
            }
            for (InetAddress newDns : dnsDiff.added) {
                addRouteToAddress(newLp, newDns);
            }
        }
    }
    for (RouteInfo r : routeDiff.added) {
        if (isLinkDefault || !r.isDefaultRoute()) {
            addRoute(newLp, r, TO_DEFAULT_TABLE);
        } else {
            // add to a secondary route table
            addRoute(newLp, r, TO_SECONDARY_TABLE);
            // many radios add a default route even when we don't want one.
            // remove the default route unless somebody else has asked for it
            String ifaceName = newLp.getInterfaceName();
            if (TextUtils.isEmpty(ifaceName) == false && mAddedRoutes.contains(r) == false) {
                try {
                    mNetd.removeRoute(ifaceName, r);
                } catch (Exception e) {
                    // never crash - catch them all
                    if (DBG)
                        loge("Exception trying to remove a route: " + e);
                }
            }
        }
    }
    return routesChanged;
}
Also used : RouteInfo(android.net.RouteInfo) CompareResult(android.net.LinkProperties.CompareResult) InetAddress(java.net.InetAddress) RemoteException(android.os.RemoteException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 2 with RouteInfo

use of android.net.RouteInfo in project android_frameworks_base by ParanoidAndroid.

the class ConnectivityService method modifyRoute.

private boolean modifyRoute(LinkProperties lp, RouteInfo r, int cycleCount, boolean doAdd, boolean toDefaultTable) {
    if ((lp == null) || (r == null)) {
        if (DBG)
            log("modifyRoute got unexpected null: " + lp + ", " + r);
        return false;
    }
    if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
        loge("Error modifying route - too much recursion");
        return false;
    }
    String ifaceName = r.getInterface();
    if (ifaceName == null) {
        loge("Error modifying route - no interface name");
        return false;
    }
    if (r.hasGateway()) {
        RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), r.getGateway());
        if (bestRoute != null) {
            if (bestRoute.getGateway().equals(r.getGateway())) {
                // if there is no better route, add the implied hostroute for our gateway
                bestRoute = RouteInfo.makeHostRoute(r.getGateway(), ifaceName);
            } else {
                // if we will connect to our gateway through another route, add a direct
                // route to it's gateway
                bestRoute = RouteInfo.makeHostRoute(r.getGateway(), bestRoute.getGateway(), ifaceName);
            }
            modifyRoute(lp, bestRoute, cycleCount + 1, doAdd, toDefaultTable);
        }
    }
    if (doAdd) {
        if (VDBG)
            log("Adding " + r + " for interface " + ifaceName);
        try {
            if (toDefaultTable) {
                // only track default table - only one apps can effect
                mAddedRoutes.add(r);
                mNetd.addRoute(ifaceName, r);
            } else {
                mNetd.addSecondaryRoute(ifaceName, r);
            }
        } catch (Exception e) {
            // never crash - catch them all
            if (DBG)
                loge("Exception trying to add a route: " + e);
            return false;
        }
    } else {
        // we can remove it from the table
        if (toDefaultTable) {
            mAddedRoutes.remove(r);
            if (mAddedRoutes.contains(r) == false) {
                if (VDBG)
                    log("Removing " + r + " for interface " + ifaceName);
                try {
                    mNetd.removeRoute(ifaceName, r);
                } catch (Exception e) {
                    // never crash - catch them all
                    if (VDBG)
                        loge("Exception trying to remove a route: " + e);
                    return false;
                }
            } else {
                if (VDBG)
                    log("not removing " + r + " as it's still in use");
            }
        } else {
            if (VDBG)
                log("Removing " + r + " for interface " + ifaceName);
            try {
                mNetd.removeSecondaryRoute(ifaceName, r);
            } catch (Exception e) {
                // never crash - catch them all
                if (VDBG)
                    loge("Exception trying to remove a route: " + e);
                return false;
            }
        }
    }
    return true;
}
Also used : RouteInfo(android.net.RouteInfo) RemoteException(android.os.RemoteException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 3 with RouteInfo

use of android.net.RouteInfo in project android_frameworks_base by ParanoidAndroid.

the class Nat464Xlat method interfaceAdded.

@Override
public void interfaceAdded(String iface) {
    if (iface.equals(CLAT_INTERFACE_NAME)) {
        Slog.i(TAG, "interface " + CLAT_INTERFACE_NAME + " added, mIsRunning = " + mIsRunning + " -> true");
        mIsRunning = true;
        // it's running on.
        try {
            InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
            mLP.clear();
            mLP.setInterfaceName(iface);
            RouteInfo ipv4Default = new RouteInfo(new LinkAddress(Inet4Address.ANY, 0), null, iface);
            mLP.addRoute(ipv4Default);
            mLP.addLinkAddress(config.getLinkAddress());
            mTracker.addStackedLink(mLP);
            Slog.i(TAG, "Adding stacked link. tracker LP: " + mTracker.getLinkProperties());
        } catch (RemoteException e) {
            Slog.e(TAG, "Error getting link properties: " + e);
        }
        // Inform ConnectivityService that things have changed.
        Message msg = mHandler.obtainMessage(NetworkStateTracker.EVENT_CONFIGURATION_CHANGED, mTracker.getNetworkInfo());
        Slog.i(TAG, "sending message to ConnectivityService: " + msg);
        msg.sendToTarget();
    }
}
Also used : LinkAddress(android.net.LinkAddress) Message(android.os.Message) RouteInfo(android.net.RouteInfo) RemoteException(android.os.RemoteException) InterfaceConfiguration(android.net.InterfaceConfiguration)

Example 4 with RouteInfo

use of android.net.RouteInfo in project android_frameworks_base by ParanoidAndroid.

the class WifiConfigStore method copyIpSettingsFromConfig.

private LinkProperties copyIpSettingsFromConfig(WifiConfiguration config) {
    LinkProperties linkProperties = new LinkProperties();
    linkProperties.setInterfaceName(config.linkProperties.getInterfaceName());
    for (LinkAddress linkAddr : config.linkProperties.getLinkAddresses()) {
        linkProperties.addLinkAddress(linkAddr);
    }
    for (RouteInfo route : config.linkProperties.getRoutes()) {
        linkProperties.addRoute(route);
    }
    for (InetAddress dns : config.linkProperties.getDnses()) {
        linkProperties.addDns(dns);
    }
    return linkProperties;
}
Also used : LinkAddress(android.net.LinkAddress) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

Example 5 with RouteInfo

use of android.net.RouteInfo in project android_frameworks_base by ParanoidAndroid.

the class WifiConfigStore method writeIpAndProxyConfigurationsOnChange.

/* Compare current and new configuration and write to file on change */
private NetworkUpdateResult writeIpAndProxyConfigurationsOnChange(WifiConfiguration currentConfig, WifiConfiguration newConfig) {
    boolean ipChanged = false;
    boolean proxyChanged = false;
    LinkProperties linkProperties = null;
    switch(newConfig.ipAssignment) {
        case STATIC:
            Collection<LinkAddress> currentLinkAddresses = currentConfig.linkProperties.getLinkAddresses();
            Collection<LinkAddress> newLinkAddresses = newConfig.linkProperties.getLinkAddresses();
            Collection<InetAddress> currentDnses = currentConfig.linkProperties.getDnses();
            Collection<InetAddress> newDnses = newConfig.linkProperties.getDnses();
            Collection<RouteInfo> currentRoutes = currentConfig.linkProperties.getRoutes();
            Collection<RouteInfo> newRoutes = newConfig.linkProperties.getRoutes();
            boolean linkAddressesDiffer = (currentLinkAddresses.size() != newLinkAddresses.size()) || !currentLinkAddresses.containsAll(newLinkAddresses);
            boolean dnsesDiffer = (currentDnses.size() != newDnses.size()) || !currentDnses.containsAll(newDnses);
            boolean routesDiffer = (currentRoutes.size() != newRoutes.size()) || !currentRoutes.containsAll(newRoutes);
            if ((currentConfig.ipAssignment != newConfig.ipAssignment) || linkAddressesDiffer || dnsesDiffer || routesDiffer) {
                ipChanged = true;
            }
            break;
        case DHCP:
            if (currentConfig.ipAssignment != newConfig.ipAssignment) {
                ipChanged = true;
            }
            break;
        case UNASSIGNED:
            /* Ignore */
            break;
        default:
            loge("Ignore invalid ip assignment during write");
            break;
    }
    switch(newConfig.proxySettings) {
        case STATIC:
            ProxyProperties newHttpProxy = newConfig.linkProperties.getHttpProxy();
            ProxyProperties currentHttpProxy = currentConfig.linkProperties.getHttpProxy();
            if (newHttpProxy != null) {
                proxyChanged = !newHttpProxy.equals(currentHttpProxy);
            } else {
                proxyChanged = (currentHttpProxy != null);
            }
            break;
        case NONE:
            if (currentConfig.proxySettings != newConfig.proxySettings) {
                proxyChanged = true;
            }
            break;
        case UNASSIGNED:
            /* Ignore */
            break;
        default:
            loge("Ignore invalid proxy configuration during write");
            break;
    }
    if (!ipChanged) {
        linkProperties = copyIpSettingsFromConfig(currentConfig);
    } else {
        currentConfig.ipAssignment = newConfig.ipAssignment;
        linkProperties = copyIpSettingsFromConfig(newConfig);
        log("IP config changed SSID = " + currentConfig.SSID + " linkProperties: " + linkProperties.toString());
    }
    if (!proxyChanged) {
        linkProperties.setHttpProxy(currentConfig.linkProperties.getHttpProxy());
    } else {
        currentConfig.proxySettings = newConfig.proxySettings;
        linkProperties.setHttpProxy(newConfig.linkProperties.getHttpProxy());
        log("proxy changed SSID = " + currentConfig.SSID);
        if (linkProperties.getHttpProxy() != null) {
            log(" proxyProperties: " + linkProperties.getHttpProxy().toString());
        }
    }
    if (ipChanged || proxyChanged) {
        currentConfig.linkProperties = linkProperties;
        writeIpAndProxyConfigurations();
        sendConfiguredNetworksChangedBroadcast(currentConfig, WifiManager.CHANGE_REASON_CONFIG_CHANGE);
    }
    return new NetworkUpdateResult(ipChanged, proxyChanged);
}
Also used : LinkAddress(android.net.LinkAddress) ProxyProperties(android.net.ProxyProperties) NetworkUpdateResult(android.net.wifi.NetworkUpdateResult) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

Aggregations

RouteInfo (android.net.RouteInfo)168 LinkProperties (android.net.LinkProperties)73 LinkAddress (android.net.LinkAddress)60 InetAddress (java.net.InetAddress)57 SmallTest (android.test.suitebuilder.annotation.SmallTest)45 IpPrefix (android.net.IpPrefix)35 Inet6Address (java.net.Inet6Address)21 Inet4Address (java.net.Inet4Address)20 IOException (java.io.IOException)11 StaticIpConfiguration (android.net.StaticIpConfiguration)10 EOFException (java.io.EOFException)8 DataInputStream (java.io.DataInputStream)7 UnknownHostException (java.net.UnknownHostException)7 HashMap (java.util.HashMap)7 StringJoiner (java.util.StringJoiner)7 Parcel (android.os.Parcel)6 BufferedInputStream (java.io.BufferedInputStream)6 FileInputStream (java.io.FileInputStream)6 ArrayList (java.util.ArrayList)6 UserInfo (android.content.pm.UserInfo)5