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

Commit ed126409 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Bind addr and prefixLength together in LinkAddress

bug:2542681
Change-Id: I90200446216d78c676498144946832afe75efdb8
parent ecede391
Loading
Loading
Loading
Loading
+4 −20
Original line number Diff line number Diff line
@@ -28,8 +28,7 @@ import java.net.UnknownHostException;
 */
public class InterfaceConfiguration implements Parcelable {
    public String hwAddr;
    public InetAddress addr;
    public InetAddress mask;
    public LinkAddress addr;
    public String interfaceFlags;

    public InterfaceConfiguration() {
@@ -41,8 +40,6 @@ public class InterfaceConfiguration implements Parcelable {

        str.append("ipddress ");
        str.append((addr != null) ? addr.toString() : "NULL");
        str.append(" netmask ");
        str.append((mask != null) ? mask.toString() : "NULL");
        str.append(" flags ").append(interfaceFlags);
        str.append(" hwaddr ").append(hwAddr);

@@ -59,7 +56,7 @@ public class InterfaceConfiguration implements Parcelable {
    public boolean isActive() {
        try {
            if(interfaceFlags.contains("up")) {
                for (byte b : addr.getAddress()) {
                for (byte b : addr.getAddress().getAddress()) {
                    if (b != 0) return true;
                }
            }
@@ -79,13 +76,7 @@ public class InterfaceConfiguration implements Parcelable {
        dest.writeString(hwAddr);
        if (addr != null) {
            dest.writeByte((byte)1);
            dest.writeByteArray(addr.getAddress());
        } else {
            dest.writeByte((byte)0);
        }
        if (mask != null) {
            dest.writeByte((byte)1);
            dest.writeByteArray(mask.getAddress());
            dest.writeParcelable(addr, flags);
        } else {
            dest.writeByte((byte)0);
        }
@@ -99,14 +90,7 @@ public class InterfaceConfiguration implements Parcelable {
                InterfaceConfiguration info = new InterfaceConfiguration();
                info.hwAddr = in.readString();
                if (in.readByte() == 1) {
                    try {
                        info.addr = InetAddress.getByAddress(in.createByteArray());
                    } catch (UnknownHostException e) {}
                }
                if (in.readByte() == 1) {
                    try {
                        info.mask = InetAddress.getByAddress(in.createByteArray());
                    } catch (UnknownHostException e) {}
                    info.addr = in.readParcelable(null);
                }
                info.interfaceFlags = in.readString();
                return info;
+9 −5
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import android.content.SharedPreferences;
import android.content.res.Resources.NotFoundException;
import android.net.ConnectivityManager;
import android.net.InterfaceConfiguration;
import android.net.LinkAddress;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
@@ -1697,12 +1698,15 @@ public class BluetoothService extends IBluetooth.Stub {
        try {
            ifcg = service.getInterfaceConfig(iface);
            if (ifcg != null) {
                ifcg.mask = InetAddress.getByName(BLUETOOTH_NETMASK);

                if (ifcg.addr == null || ifcg.addr.equals(InetAddress.getByName("0.0.0.0"))) {
                    ifcg.addr = InetAddress.getByName(address);
                    ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
                InetAddress mask = InetAddress.getByName(BLUETOOTH_NETMASK);
                InetAddress addr = null;
                if (ifcg.addr == null || (addr = ifcg.addr.getAddress()) == null ||
                        addr.equals(InetAddress.getByName("0.0.0.0")) ||
                        addr.equals(InetAddress.getByName("::0"))) {
                    addr = InetAddress.getByName(address);
                }
                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
                ifcg.addr = new LinkAddress(addr, mask);
                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("  "," ");
                service.setInterfaceConfig(iface, ifcg);
+20 −4
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import android.content.pm.PackageManager;
import android.net.Uri;
import android.net.InterfaceConfiguration;
import android.net.INetworkManagementEventObserver;
import android.net.LinkAddress;
import android.net.NetworkUtils;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.os.INetworkManagementService;
@@ -265,18 +267,21 @@ class NetworkManagementService extends INetworkManagementService.Stub {

            cfg = new InterfaceConfiguration();
            cfg.hwAddr = st.nextToken(" ");
            InetAddress addr = null;
            InetAddress mask = null;
            try {
                cfg.addr = InetAddress.getByName(st.nextToken(" "));
                addr = InetAddress.getByName(st.nextToken(" "));
            } catch (UnknownHostException uhe) {
                Slog.e(TAG, "Failed to parse ipaddr", uhe);
            }

            try {
                cfg.mask = InetAddress.getByName(st.nextToken(" "));
                mask = InetAddress.getByName(st.nextToken(" "));
            } catch (UnknownHostException uhe) {
                Slog.e(TAG, "Failed to parse netmask", uhe);
            }

            cfg.addr = new LinkAddress(addr, mask);
            cfg.interfaceFlags = st.nextToken("]").trim() +"]";
        } catch (NoSuchElementException nsee) {
            throw new IllegalStateException(
@@ -288,9 +293,20 @@ class NetworkManagementService extends INetworkManagementService.Stub {

    public void setInterfaceConfig(
            String iface, InterfaceConfiguration cfg) throws IllegalStateException {
        LinkAddress linkAddr = cfg.addr;
        if (linkAddr == null) throw new IllegalStateException("Null LinkAddress given");
        InetAddress addr = linkAddr.getAddress();
        // TODO - fix this to pass prefixlength and be v6 capapble
        InetAddress mask = null;
        try {
            mask = NetworkUtils.intToInetAddress(NetworkUtils.prefixLengthToNetmaskInt(
                    linkAddr.getNetworkPrefixLength()));
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException(e);
        }
        if (addr == null || mask == null) throw new IllegalStateException("Null Address given");
        String cmd = String.format("interface setcfg %s %s %s %s", iface,
                cfg.addr.getHostAddress(), cfg.mask.getHostAddress(),
                cfg.interfaceFlags);
                addr.getHostAddress(), mask.getHostAddress(), cfg.interfaceFlags);
        try {
            mConnector.doCommand(cmd);
        } catch (NativeDaemonConnectorException e) {
+4 −2
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.net.ConnectivityManager;
import android.net.InterfaceConfiguration;
import android.net.IConnectivityManager;
import android.net.INetworkManagementEventObserver;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkInfo;
import android.os.Binder;
@@ -566,8 +567,9 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
                try {
                    ifcg = service.getInterfaceConfig(iface);
                    if (ifcg != null) {
                        ifcg.addr = InetAddress.getByName(USB_NEAR_IFACE_ADDR);
                        ifcg.mask = InetAddress.getByName(USB_NETMASK);
                        InetAddress addr = InetAddress.getByName(USB_NEAR_IFACE_ADDR);
                        InetAddress mask = InetAddress.getByName(USB_NETMASK);
                        ifcg.addr = new LinkAddress(addr, mask);
                        if (enabled) {
                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
                        } else {
+2 −5
Original line number Diff line number Diff line
@@ -1013,8 +1013,7 @@ public class WifiStateMachine extends HierarchicalStateMachine {
                        ifcg = service.getInterfaceConfig(intf);
                        if (ifcg != null) {
                            /* IP/netmask: 192.168.43.1/255.255.255.0 */
                            ifcg.addr = InetAddress.getByName("192.168.43.1");
                            ifcg.mask = InetAddress.getByName("255.255.255.0");
                            ifcg.addr = new LinkAddress(InetAddress.getByName("192.168.43.1"), 24);
                            ifcg.interfaceFlags = "[up]";

                            service.setInterfaceConfig(intf, ifcg);
@@ -2529,9 +2528,7 @@ public class WifiStateMachine extends HierarchicalStateMachine {
                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
                INetworkManagementService netd = INetworkManagementService.Stub.asInterface(b);
                InterfaceConfiguration ifcg = new InterfaceConfiguration();
                ifcg.addr = NetworkUtils.numericToInetAddress(dhcpInfoInternal.ipAddress);
                ifcg.mask = NetworkUtils.intToInetAddress(
                        NetworkUtils.prefixLengthToNetmaskInt(dhcpInfoInternal.prefixLength));
                ifcg.addr = dhcpInfoInternal.makeLinkAddress();
                ifcg.interfaceFlags = "[up]";
                try {
                    netd.setInterfaceConfig(mInterfaceName, ifcg);