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

Commit 04808c29 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Fix some IpV4-only code.

InterfaceConfiguration changed to use InetAddress and stop with the string->int->string
conversions.

bug:2542681
Change-Id: I11c4954547333c43bb840fa0469ddde57b0d043b
parent fc29088e
Loading
Loading
Loading
Loading
+29 −15
Original line number Diff line number Diff line
@@ -19,14 +19,17 @@ package android.net;
import android.os.Parcelable;
import android.os.Parcel;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * A simple object for retrieving / setting an interfaces configuration
 * @hide
 */
public class InterfaceConfiguration implements Parcelable {
    public String hwAddr;
    public int ipAddr;
    public int netmask;
    public InetAddress addr;
    public InetAddress mask;
    public String interfaceFlags;

    public InterfaceConfiguration() {
@@ -36,21 +39,14 @@ public class InterfaceConfiguration implements Parcelable {
    public String toString() {
        StringBuffer str = new StringBuffer();

        str.append("ipddress "); putAddress(str, ipAddr);
        str.append(" netmask "); putAddress(str, netmask);
        str.append("ipddress "); str.append(addr.toString());
        str.append(" netmask "); str.append(mask.toString());
        str.append(" flags ").append(interfaceFlags);
        str.append(" hwaddr ").append(hwAddr);

        return str.toString();
    }

    private static void putAddress(StringBuffer buf, int addr) {
        buf.append((addr >> 24) & 0xff).append('.').
            append((addr >> 16) & 0xff).append('.').
            append((addr >> 8) & 0xff).append('.').
            append(addr & 0xff);
    }

    /** Implement the Parcelable interface {@hide} */
    public int describeContents() {
        return 0;
@@ -59,8 +55,18 @@ public class InterfaceConfiguration implements Parcelable {
    /** Implement the Parcelable interface {@hide} */
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(hwAddr);
        dest.writeInt(ipAddr);
        dest.writeInt(netmask);
        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());
        } else {
            dest.writeByte((byte)0);
        }
        dest.writeString(interfaceFlags);
    }

@@ -70,8 +76,16 @@ public class InterfaceConfiguration implements Parcelable {
            public InterfaceConfiguration createFromParcel(Parcel in) {
                InterfaceConfiguration info = new InterfaceConfiguration();
                info.hwAddr = in.readString();
                info.ipAddr = in.readInt();
                info.netmask = in.readInt();
                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.interfaceFlags = in.readString();
                return info;
            }
+5 −14
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -1660,20 +1661,10 @@ public class BluetoothService extends IBluetooth.Stub {
        try {
            ifcg = service.getInterfaceConfig(iface);
            if (ifcg != null) {
                String[] addr = BLUETOOTH_NETMASK.split("\\.");
                ifcg.netmask = (Integer.parseInt(addr[0]) << 24) +
                        (Integer.parseInt(addr[1]) << 16) +
                        (Integer.parseInt(addr[2]) << 8) +
                        (Integer.parseInt(addr[3]));
                if (ifcg.ipAddr == 0) {
                    addr = address.split("\\.");

                    ifcg.ipAddr = (Integer.parseInt(addr[0]) << 24) +
                            (Integer.parseInt(addr[1]) << 16) +
                            (Integer.parseInt(addr[2]) << 8) +
                            (Integer.parseInt(addr[3]));
                    ifcg.interfaceFlags =
                        ifcg.interfaceFlags.replace("down", "up");
                ifcg.mask = InetAddress.getByName(BLUETOOTH_NETMASK);
                if (ifcg.addr == null) {
                    ifcg.addr = InetAddress.getByName(address);
                    ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
                }
                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("  "," ");
+5 −27
Original line number Diff line number Diff line
@@ -219,28 +219,6 @@ class NetworkManagementService extends INetworkManagementService.Stub {
        }
    }

    private static int stringToIpAddr(String addrString) throws UnknownHostException {
        try {
            String[] parts = addrString.split("\\.");
            if (parts.length != 4) {
                throw new UnknownHostException(addrString);
            }

            int a = Integer.parseInt(parts[0]) << 24;
            int b = Integer.parseInt(parts[1]) << 16;
            int c = Integer.parseInt(parts[2]) <<  8;
            int d = Integer.parseInt(parts[3])      ;

            return a | b | c | d;
        } catch (NumberFormatException ex) {
            throw new UnknownHostException(addrString);
        }
    }

    public static String intToIpString(int i) {
        return ((i >> 24 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ((i >>  8 ) & 0xFF) + "." +
               (i & 0xFF);
    }

    //
    // INetworkManagementService members
@@ -288,18 +266,17 @@ class NetworkManagementService extends INetworkManagementService.Stub {
            cfg = new InterfaceConfiguration();
            cfg.hwAddr = st.nextToken(" ");
            try {
                cfg.ipAddr = stringToIpAddr(st.nextToken(" "));
                cfg.addr = InetAddress.getByName(st.nextToken(" "));
            } catch (UnknownHostException uhe) {
                Slog.e(TAG, "Failed to parse ipaddr", uhe);
                cfg.ipAddr = 0;
            }

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

            cfg.interfaceFlags = st.nextToken("]").trim() +"]";
        } catch (NoSuchElementException nsee) {
            throw new IllegalStateException(
@@ -312,7 +289,8 @@ class NetworkManagementService extends INetworkManagementService.Stub {
    public void setInterfaceConfig(
            String iface, InterfaceConfiguration cfg) throws IllegalStateException {
        String cmd = String.format("interface setcfg %s %s %s %s", iface,
                intToIpString(cfg.ipAddr), intToIpString(cfg.netmask), cfg.interfaceFlags);
                cfg.addr.getHostAddress(), cfg.mask.getHostAddress(),
                cfg.interfaceFlags);
        try {
            mConnector.doCommand(cmd);
        } catch (NativeDaemonConnectorException e) {
+3 −2
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ import android.provider.Settings;
import android.text.TextUtils;
import android.util.Slog;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@@ -347,8 +348,8 @@ public class WifiService extends IWifiManager.Stub {
                        ifcg = service.getInterfaceConfig(intf);
                        if (ifcg != null) {
                            /* IP/netmask: 192.168.43.1/255.255.255.0 */
                            ifcg.ipAddr = (192 << 24) + (168 << 16) + (43 << 8) + 1;
                            ifcg.netmask = (255 << 24) + (255 << 16) + (255 << 8) + 0;
                            ifcg.addr = InetAddress.getByName("192.168.43.1");
                            ifcg.mask = InetAddress.getByName("255.255.255.0");
                            ifcg.interfaceFlags = "[up]";

                            service.setInterfaceConfig(intf, ifcg);
+3 −10
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import com.android.internal.util.HierarchicalStateMachine;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
@@ -585,16 +586,8 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
                try {
                    ifcg = service.getInterfaceConfig(iface);
                    if (ifcg != null) {
                        String[] addr = USB_NEAR_IFACE_ADDR.split("\\.");
                        ifcg.ipAddr = (Integer.parseInt(addr[0]) << 24) +
                                (Integer.parseInt(addr[1]) << 16) +
                                (Integer.parseInt(addr[2]) << 8) +
                                (Integer.parseInt(addr[3]));
                        addr = USB_NETMASK.split("\\.");
                        ifcg.netmask = (Integer.parseInt(addr[0]) << 24) +
                                (Integer.parseInt(addr[1]) << 16) +
                                (Integer.parseInt(addr[2]) << 8) +
                                (Integer.parseInt(addr[3]));
                        ifcg.addr = InetAddress.getByName(USB_NEAR_IFACE_ADDR);
                        ifcg.mask = InetAddress.getByName(USB_NETMASK);
                        if (enabled) {
                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
                        } else {