Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 992564e4 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Update LinkProperties treatment of gateways

A link can have multiple gateways (think ipv4/ipv6 for a trivial example).
.

bug:3438810
Change-Id: I28c90a6947cd50b82e5ca9a0113148f98b3f4dd8
parent 2157f045
Loading
Loading
Loading
Loading
+11 −11
Original line number Diff line number Diff line
@@ -44,12 +44,14 @@ public class DhcpInfoInternal {
    }

    private int convertToInt(String addr) {
        if (addr != null) {
            try {
                InetAddress inetAddress = NetworkUtils.numericToInetAddress(addr);
                if (inetAddress instanceof Inet4Address) {
                    return NetworkUtils.inetAddressToInt(inetAddress);
                }
            } catch (IllegalArgumentException e) {}
        }
        return 0;
    }

@@ -80,19 +82,17 @@ public class DhcpInfoInternal {
        LinkProperties p = new LinkProperties();
        p.addLinkAddress(makeLinkAddress());
        if (TextUtils.isEmpty(gateway) == false) {
            p.setGateway(NetworkUtils.numericToInetAddress(gateway));
        } else {
            Log.e(TAG, "makeLinkProperties with empty gateway!");
            p.addGateway(NetworkUtils.numericToInetAddress(gateway));
        }
        if (TextUtils.isEmpty(dns1) == false) {
            p.addDns(NetworkUtils.numericToInetAddress(dns1));
        } else {
            Log.e(TAG, "makeLinkProperties with empty dns1!");
            Log.d(TAG, "makeLinkProperties with empty dns1!");
        }
        if (TextUtils.isEmpty(dns2) == false) {
            p.addDns(NetworkUtils.numericToInetAddress(dns2));
        } else {
            Log.e(TAG, "makeLinkProperties with empty dns2!");
            Log.d(TAG, "makeLinkProperties with empty dns2!");
        }
        return p;
    }
+38 −18
Original line number Diff line number Diff line
@@ -31,7 +31,24 @@ import java.util.Collections;

/**
 * Describes the properties of a network link.
 * TODO - consider adding optional fields like Apn and ApnType
 *
 * A link represents a connection to a network.
 * It may have multiple addresses and multiple gateways,
 * multiple dns servers but only one http proxy.
 *
 * Because it's a single network, the dns's
 * are interchangeable and don't need associating with
 * particular addresses.  The gateways similarly don't
 * need associating with particular addresses.
 *
 * A dual stack interface works fine in this model:
 * each address has it's own prefix length to describe
 * the local network.  The dns servers all return
 * both v4 addresses and v6 addresses regardless of the
 * address family of the server itself (rfc4213) and we
 * don't care which is used.  The gateways will be
 * selected based on the destination address and the
 * source address has no relavence.
 * @hide
 */
public class LinkProperties implements Parcelable {
@@ -39,7 +56,7 @@ public class LinkProperties implements Parcelable {
    String mIfaceName;
    private Collection<LinkAddress> mLinkAddresses;
    private Collection<InetAddress> mDnses;
    private InetAddress mGateway;
    private Collection<InetAddress> mGateways;
    private ProxyProperties mHttpProxy;

    public LinkProperties() {
@@ -52,7 +69,7 @@ public class LinkProperties implements Parcelable {
            mIfaceName = source.getInterfaceName();
            mLinkAddresses = source.getLinkAddresses();
            mDnses = source.getDnses();
            mGateway = source.getGateway();
            mGateways = source.getGateways();
            mHttpProxy = new ProxyProperties(source.getHttpProxy());
        }
    }
@@ -89,11 +106,11 @@ public class LinkProperties implements Parcelable {
        return Collections.unmodifiableCollection(mDnses);
    }

    public void setGateway(InetAddress gateway) {
        mGateway = gateway;
    public void addGateway(InetAddress gateway) {
        mGateways.add(gateway);
    }
    public InetAddress getGateway() {
        return mGateway;
    public Collection<InetAddress> getGateways() {
        return Collections.unmodifiableCollection(mGateways);
    }

    public void setHttpProxy(ProxyProperties proxy) {
@@ -107,7 +124,7 @@ public class LinkProperties implements Parcelable {
        mIfaceName = null;
        mLinkAddresses = new ArrayList<LinkAddress>();
        mDnses = new ArrayList<InetAddress>();
        mGateway = null;
        mGateways = new ArrayList<InetAddress>();
        mHttpProxy = null;
    }

@@ -131,10 +148,12 @@ public class LinkProperties implements Parcelable {
        for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
        dns += "] ";

        String gateways = "Gateways: [";
        for (InetAddress gw : mGateways) gateways += gw.getHostAddress() + ",";
        gateways += "] ";
        String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");
        String gateway = (mGateway == null ? "" : "Gateway: " + mGateway.getHostAddress() + " ");

        return ifaceName + linkAddresses + gateway + dns + proxy;
        return ifaceName + linkAddresses + gateways + dns + proxy;
    }

    /**
@@ -152,12 +171,12 @@ public class LinkProperties implements Parcelable {
        for(InetAddress d : mDnses) {
            dest.writeByteArray(d.getAddress());
        }
        if (mGateway != null) {
            dest.writeByte((byte)1);
            dest.writeByteArray(mGateway.getAddress());
        } else {
            dest.writeByte((byte)0);

        dest.writeInt(mGateways.size());
        for(InetAddress gw : mGateways) {
            dest.writeByteArray(gw.getAddress());
        }

        if (mHttpProxy != null) {
            dest.writeByte((byte)1);
            dest.writeParcelable(mHttpProxy, flags);
@@ -192,9 +211,10 @@ public class LinkProperties implements Parcelable {
                        netProp.addDns(InetAddress.getByAddress(in.createByteArray()));
                    } catch (UnknownHostException e) { }
                }
                if (in.readByte() == 1) {
                addressCount = in.readInt();
                for (int i=0; i<addressCount; i++) {
                    try {
                        netProp.setGateway(InetAddress.getByAddress(in.createByteArray()));
                        netProp.addGateway(InetAddress.getByAddress(in.createByteArray()));
                    } catch (UnknownHostException e) { }
                }
                if (in.readByte() == 1) {
+1 −1
Original line number Diff line number Diff line
@@ -303,7 +303,7 @@ public class AccessPointParserHelper {
                    if (!InetAddress.isNumeric(gwAddr)) {
                        throw new SAXException();
                    }
                    mLinkProperties.setGateway(InetAddress.getByName(gwAddr));
                    mLinkProperties.addGateway(InetAddress.getByName(gwAddr));
                } catch (UnknownHostException e) {
                    throw new SAXException();
                }
+4 −4
Original line number Diff line number Diff line
@@ -1414,13 +1414,13 @@ public class ConnectivityService extends IConnectivityManager.Stub {
        LinkProperties p = nt.getLinkProperties();
        if (p == null) return;
        String interfaceName = p.getInterfaceName();
        InetAddress defaultGatewayAddr = p.getGateway();
        if (TextUtils.isEmpty(interfaceName)) return;
        for (InetAddress gateway : p.getGateways()) {

        if ((interfaceName != null) && (defaultGatewayAddr != null )) {
            if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
            if (!NetworkUtils.addDefaultRoute(interfaceName, gateway) && DBG) {
                NetworkInfo networkInfo = nt.getNetworkInfo();
                log("addDefaultRoute for " + networkInfo.getTypeName() +
                        " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
                        " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
            }
        }
    }
+20 −14
Original line number Diff line number Diff line
@@ -445,7 +445,10 @@ class WifiConfigStore {
            if (iter.hasNext()) {
                LinkAddress linkAddress = iter.next();
                dhcpInfoInternal.ipAddress = linkAddress.getAddress().getHostAddress();
                dhcpInfoInternal.gateway = linkProperties.getGateway().getHostAddress();
                Iterator<InetAddress>gateways = linkProperties.getGateways().iterator();
                if (gateways.hasNext()) {
                    dhcpInfoInternal.gateway = gateways.next().getHostAddress();
                }
                dhcpInfoInternal.prefixLength = linkAddress.getNetworkPrefixLength();
                Iterator<InetAddress> dnsIterator = linkProperties.getDnses().iterator();
                dhcpInfoInternal.dns1 = dnsIterator.next().getHostAddress();
@@ -582,8 +585,7 @@ class WifiConfigStore {
                                    out.writeUTF(linkAddr.getAddress().getHostAddress());
                                    out.writeInt(linkAddr.getNetworkPrefixLength());
                                }
                                InetAddress gateway = linkProperties.getGateway();
                                if (gateway != null) {
                                for (InetAddress gateway : linkProperties.getGateways()) {
                                    out.writeUTF(GATEWAY_KEY);
                                    out.writeUTF(gateway.getHostAddress());
                                }
@@ -688,7 +690,7 @@ class WifiConfigStore {
                                    in.readUTF()), in.readInt());
                            linkProperties.addLinkAddress(linkAddr);
                        } else if (key.equals(GATEWAY_KEY)) {
                            linkProperties.setGateway(InetAddress.getByName(in.readUTF()));
                            linkProperties.addGateway(InetAddress.getByName(in.readUTF()));
                        } else if (key.equals(DNS_KEY)) {
                            linkProperties.addDns(InetAddress.getByName(in.readUTF()));
                        } else if (key.equals(PROXY_SETTINGS_KEY)) {
@@ -999,15 +1001,17 @@ class WifiConfigStore {
                        .getLinkAddresses();
                Collection<InetAddress> currentDnses = currentConfig.linkProperties.getDnses();
                Collection<InetAddress> newDnses = newConfig.linkProperties.getDnses();
                InetAddress currentGateway = currentConfig.linkProperties.getGateway();
                InetAddress newGateway = newConfig.linkProperties.getGateway();

                boolean linkAddressesDiffer = !currentLinkAddresses.containsAll(newLinkAddresses) ||
                        (currentLinkAddresses.size() != newLinkAddresses.size());
                boolean dnsesDiffer = !currentDnses.containsAll(newDnses) ||
                        (currentDnses.size() != newDnses.size());
                boolean gatewaysDiffer = (currentGateway == null) ||
                        !currentGateway.equals(newGateway);
                Collection<InetAddress> currentGateways =
                        currentConfig.linkProperties.getGateways();
                Collection<InetAddress> newGateways = newConfig.linkProperties.getGateways();

                boolean linkAddressesDiffer =
                        (currentLinkAddresses.size() != newLinkAddresses.size()) ||
                        !currentLinkAddresses.containsAll(newLinkAddresses);
                boolean dnsesDiffer = (currentDnses.size() != newDnses.size()) ||
                        !currentDnses.containsAll(newDnses);
                boolean gatewaysDiffer = (currentGateways.size() != newGateways.size()) ||
                        !currentGateways.containsAll(newGateways);

                if ((currentConfig.ipAssignment != newConfig.ipAssignment) ||
                        linkAddressesDiffer ||
@@ -1087,7 +1091,9 @@ class WifiConfigStore {
        for (LinkAddress linkAddr : config.linkProperties.getLinkAddresses()) {
            linkProperties.addLinkAddress(linkAddr);
        }
        linkProperties.setGateway(config.linkProperties.getGateway());
        for (InetAddress gateway : config.linkProperties.getGateways()) {
            linkProperties.addGateway(gateway);
        }
        for (InetAddress dns : config.linkProperties.getDnses()) {
            linkProperties.addDns(dns);
        }