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

Commit 96ca9176 authored by Irfan Sheriff's avatar Irfan Sheriff
Browse files

Use LinkProperties for IP and proxy configuration

Change-Id: I4ae817fb00141e9a742216b7fd02dca1ed228270
parent 38d50969
Loading
Loading
Loading
Loading
+16 −16
Original line number Diff line number Diff line
@@ -34,26 +34,26 @@ public class LinkAddress implements Parcelable {
    private final InetAddress address;

    /**
     * Network prefix
     * Network prefix length
     */
    private final int prefix;
    private final int prefixLength;

    public LinkAddress(InetAddress address, InetAddress mask) {
        this.address = address;
        this.prefix = computeprefix(mask);
        this.prefixLength = computeprefixLength(mask);
    }

    public LinkAddress(InetAddress address, int prefix) {
    public LinkAddress(InetAddress address, int prefixLength) {
        this.address = address;
        this.prefix = prefix;
        this.prefixLength = prefixLength;
    }

    public LinkAddress(InterfaceAddress interfaceAddress) {
        this.address = interfaceAddress.getAddress();
        this.prefix = interfaceAddress.getNetworkPrefixLength();
        this.prefixLength = interfaceAddress.getNetworkPrefixLength();
    }

    private static int computeprefix(InetAddress mask) {
    private static int computeprefixLength(InetAddress mask) {
        int count = 0;
        for (byte b : mask.getAddress()) {
            for (int i = 0; i < 8; ++i) {
@@ -67,12 +67,12 @@ public class LinkAddress implements Parcelable {

    @Override
    public String toString() {
        return (address == null ? "" : (address.getHostAddress() + "/" + prefix));
        return (address == null ? "" : (address.getHostAddress() + "/" + prefixLength));
    }

    /**
     * Compares this {@code LinkAddress} instance against the specified address
     * in {@code obj}. Two addresses are equal if their InetAddress and prefix
     * in {@code obj}. Two addresses are equal if their InetAddress and prefixLength
     * are equal
     *
     * @param obj the object to be tested for equality.
@@ -85,7 +85,7 @@ public class LinkAddress implements Parcelable {
        }
        LinkAddress linkAddress = (LinkAddress) obj;
        return this.address.equals(linkAddress.address) &&
            this.prefix == linkAddress.prefix;
            this.prefixLength == linkAddress.prefixLength;
    }

    /**
@@ -98,8 +98,8 @@ public class LinkAddress implements Parcelable {
    /**
     * Get network prefix length
     */
    public int getNetworkPrefix() {
        return prefix;
    public int getNetworkPrefixLength() {
        return prefixLength;
    }

    /**
@@ -118,7 +118,7 @@ public class LinkAddress implements Parcelable {
        if (address != null) {
            dest.writeByte((byte)1);
            dest.writeByteArray(address.getAddress());
            dest.writeInt(prefix);
            dest.writeInt(prefixLength);
        } else {
            dest.writeByte((byte)0);
        }
@@ -132,14 +132,14 @@ public class LinkAddress implements Parcelable {
        new Creator<LinkAddress>() {
            public LinkAddress createFromParcel(Parcel in) {
                InetAddress address = null;
                int prefix = 0;
                int prefixLength = 0;
                if (in.readByte() == 1) {
                    try {
                        address = InetAddress.getByAddress(in.createByteArray());
                        prefix = in.readInt();
                        prefixLength = in.readInt();
                    } catch (UnknownHostException e) { }
                }
                return new LinkAddress(address, prefix);
                return new LinkAddress(address, prefixLength);
            }

            public LinkAddress[] newArray(int size) {
+51 −0
Original line number Diff line number Diff line
@@ -145,6 +145,57 @@ public class NetworkUtils {
        return inetAddress;
    }

    /**
     * Convert a IPv4 address from an InetAddress to an integer
     * @param inetAddr is an InetAddress corresponding to the IPv4 address
     * @return the IP address as an integer in network byte order
     */
    public static int inetAddressToInt(InetAddress inetAddr)
            throws IllegalArgumentException {
        byte [] addr = inetAddr.getAddress();
        if (addr.length != 4) {
            throw new IllegalArgumentException("Not an IPv4 address");
        }
        return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
                ((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
    }

    /**
     * Convert a network prefix length to an IPv4 netmask integer
     * @param prefixLength
     * @return the IPv4 netmask as an integer in network byte order
     */
    public static int prefixLengthToNetmaskInt(int prefixLength)
            throws IllegalArgumentException {
        if (prefixLength < 0 || prefixLength > 32) {
            throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)");
        }
        int value = 0xffffffff << (32 - prefixLength);
        return Integer.reverseBytes(value);
    }

    public static boolean isIpAddress(String address) {
        //TODO: Add NetworkUtils support for IPv6 configuration and
        //remove IPv4 validation and use a generic InetAddress validation
        try {
            String[] parts = address.split("\\.");
            if (parts.length != 4) {
                return false;
            }
            int a = Integer.parseInt(parts[0]);
            if (a < 0 || a > 255) return false;
            a = Integer.parseInt(parts[1]);
            if (a < 0 || a > 255) return false;
            a = Integer.parseInt(parts[2]);
            if (a < 0 || a > 255) return false;
            a = Integer.parseInt(parts[3]);
            if (a < 0 || a > 255) return false;
        } catch (NumberFormatException ex) {
            return false;
        }
        return true;
    }

    /**
     * Add a default route through the specified gateway.
     * @param interfaceName interface on which the route should be added
+286 −151

File changed.

Preview size limit exceeded, changes collapsed.

+14 −39
Original line number Diff line number Diff line
@@ -16,8 +16,7 @@

package android.net.wifi;

import android.net.DhcpInfo;
import android.net.ProxyProperties;
import android.net.LinkProperties;
import android.os.Parcelable;
import android.os.Parcel;

@@ -303,7 +302,7 @@ public class WifiConfiguration implements Parcelable {
     */
    public enum IpAssignment {
        /* Use statically configured IP settings. Configuration can be accessed
         * with ipConfig */
         * with linkProperties */
        STATIC,
        /* Use dynamically configured IP settigns */
        DHCP,
@@ -315,10 +314,6 @@ public class WifiConfiguration implements Parcelable {
     * @hide
     */
    public IpAssignment ipAssignment;
    /**
     * @hide
     */
    public DhcpInfo ipConfig;

    /**
     * @hide
@@ -328,7 +323,7 @@ public class WifiConfiguration implements Parcelable {
         * should be cleared. */
        NONE,
        /* Use statically configured proxy. Configuration can be accessed
         * with proxyProperties */
         * with linkProperties */
        STATIC,
        /* no proxy details are assigned, this is used to indicate
         * that any existing proxy settings should be retained */
@@ -341,7 +336,7 @@ public class WifiConfiguration implements Parcelable {
    /**
     * @hide
     */
    public ProxyProperties proxyProperties;
    public LinkProperties linkProperties;

    public WifiConfiguration() {
        networkId = INVALID_NETWORK_ID;
@@ -361,9 +356,8 @@ public class WifiConfiguration implements Parcelable {
            field.setValue(null);
        }
        ipAssignment = IpAssignment.UNASSIGNED;
        ipConfig = new DhcpInfo();
        proxySettings = ProxySettings.UNASSIGNED;
        proxyProperties = new ProxyProperties();
        linkProperties = new LinkProperties();
    }

    public String toString() {
@@ -445,17 +439,13 @@ public class WifiConfiguration implements Parcelable {
            if (value != null) sbuf.append(value);
        }
        sbuf.append('\n');
        if (ipAssignment == IpAssignment.STATIC) {
            sbuf.append(" ").append("Static IP configuration:").append('\n');
            sbuf.append(" ").append(ipConfig);
        }
        sbuf.append('\n');
        sbuf.append("IP assignment: " + ipAssignment.toString());
        sbuf.append("\n");
        sbuf.append("Proxy settings: " + proxySettings.toString());
        sbuf.append("\n");
        sbuf.append(linkProperties.toString());
        sbuf.append("\n");

        if (proxySettings == ProxySettings.STATIC) {
            sbuf.append(" ").append("Proxy configuration:").append('\n');
            sbuf.append(" ").append(proxyProperties);
        }
        sbuf.append('\n');
        return sbuf.toString();
    }

@@ -521,9 +511,8 @@ public class WifiConfiguration implements Parcelable {
                enterpriseFields[i].setValue(source.enterpriseFields[i].value());
            }
            ipAssignment = source.ipAssignment;
            ipConfig = new DhcpInfo(source.ipConfig);
            proxySettings = source.proxySettings;
            proxyProperties = new ProxyProperties(source.proxyProperties);
            linkProperties = new LinkProperties(source.linkProperties);
        }
    }

@@ -550,15 +539,8 @@ public class WifiConfiguration implements Parcelable {
            dest.writeString(field.value());
        }
        dest.writeString(ipAssignment.name());
        dest.writeInt(ipConfig.ipAddress);
        dest.writeInt(ipConfig.netmask);
        dest.writeInt(ipConfig.gateway);
        dest.writeInt(ipConfig.dns1);
        dest.writeInt(ipConfig.dns2);
        dest.writeInt(ipConfig.serverAddress);
        dest.writeInt(ipConfig.leaseDuration);
        dest.writeString(proxySettings.name());
        dest.writeParcelable(proxyProperties, flags);
        dest.writeParcelable(linkProperties, flags);
    }

    /** Implement the Parcelable interface {@hide} */
@@ -587,15 +569,8 @@ public class WifiConfiguration implements Parcelable {
                }

                config.ipAssignment = IpAssignment.valueOf(in.readString());
                config.ipConfig.ipAddress = in.readInt();
                config.ipConfig.netmask = in.readInt();
                config.ipConfig.gateway = in.readInt();
                config.ipConfig.dns1 = in.readInt();
                config.ipConfig.dns2 = in.readInt();
                config.ipConfig.serverAddress = in.readInt();
                config.ipConfig.leaseDuration = in.readInt();
                config.proxySettings = ProxySettings.valueOf(in.readString());
                config.proxyProperties = in.readParcelable(null);
                config.linkProperties = in.readParcelable(null);
                return config;
            }

+15 −19
Original line number Diff line number Diff line
@@ -1253,9 +1253,9 @@ public class WifiStateMachine extends HierarchicalStateMachine {
    }

    private void configureLinkProperties() {

        mLinkProperties.setInterfaceName(mInterfaceName);

        if (WifiConfigStore.isUsingStaticIp(mLastNetworkId)) {
            mLinkProperties = WifiConfigStore.getLinkProperties(mLastNetworkId);
        } else {
            // TODO - fix this for v6
            synchronized (mDhcpInfo) {
                mLinkProperties.addLinkAddress(new LinkAddress(
@@ -1265,13 +1265,10 @@ public class WifiStateMachine extends HierarchicalStateMachine {
                mLinkProperties.addDns(NetworkUtils.intToInetAddress(mDhcpInfo.dns1));
                mLinkProperties.addDns(NetworkUtils.intToInetAddress(mDhcpInfo.dns2));
            }

        ProxyProperties proxyProperties = WifiConfigStore.getProxyProperties(mLastNetworkId);
        if (proxyProperties != null) {
            mLinkProperties.setHttpProxy(proxyProperties);
            Log.d(TAG, "netId=" + mLastNetworkId  + " proxy configured: "
                    + proxyProperties.toString());
            mLinkProperties.setHttpProxy(WifiConfigStore.getProxyProperties(mLastNetworkId));
        }
        mLinkProperties.setInterfaceName(mInterfaceName);
        Log.d(TAG, "netId=" + mLastNetworkId  + " Link configured: " + mLinkProperties.toString());
    }

    private int getMaxDhcpRetries() {
@@ -2571,7 +2568,6 @@ public class WifiStateMachine extends HierarchicalStateMachine {
                  mLastSignalLevel = -1; // force update of signal strength
                  synchronized (mDhcpInfo) {
                      mWifiInfo.setIpAddress(mDhcpInfo.ipAddress);
                      Log.d(TAG, "IP configuration: " + mDhcpInfo);
                  }
                  configureLinkProperties();
                  setDetailedState(DetailedState.CONNECTED);