Merge "interface-related commands porting"

am: 808c2fa747

Change-Id: Ibd1a04cbe8aa657148ac8536814a08aba1a6f71a
This commit is contained in:
Luke Huang
2018-11-01 00:22:18 -07:00
committed by android-build-merger
2 changed files with 72 additions and 66 deletions

View File

@@ -19,9 +19,11 @@ package android.net;
import android.annotation.UnsupportedAppUsage;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import com.google.android.collect.Sets;
import java.net.InetAddress;
import java.util.HashSet;
/**
@@ -34,8 +36,10 @@ public class InterfaceConfiguration implements Parcelable {
private LinkAddress mAddr;
private HashSet<String> mFlags = Sets.newHashSet();
private static final String FLAG_UP = "up";
private static final String FLAG_DOWN = "down";
private static final String FLAG_UP = INetd.IF_STATE_UP;
private static final String FLAG_DOWN = INetd.IF_STATE_DOWN;
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@Override
public String toString() {
@@ -111,6 +115,40 @@ public class InterfaceConfiguration implements Parcelable {
mHwAddr = hwAddr;
}
/**
* Construct InterfaceConfiguration from InterfaceConfigurationParcel.
*/
public static InterfaceConfiguration fromParcel(InterfaceConfigurationParcel p) {
InterfaceConfiguration cfg = new InterfaceConfiguration();
cfg.setHardwareAddress(p.hwAddr);
final InetAddress addr = NetworkUtils.numericToInetAddress(p.ipv4Addr);
cfg.setLinkAddress(new LinkAddress(addr, p.prefixLength));
for (String flag : p.flags) {
cfg.setFlag(flag);
}
return cfg;
}
/**
* Convert InterfaceConfiguration to InterfaceConfigurationParcel with given ifname.
*/
public InterfaceConfigurationParcel toParcel(String iface) {
InterfaceConfigurationParcel cfgParcel = new InterfaceConfigurationParcel();
cfgParcel.ifName = iface;
if (!TextUtils.isEmpty(mHwAddr)) {
cfgParcel.hwAddr = mHwAddr;
} else {
cfgParcel.hwAddr = "";
}
cfgParcel.ipv4Addr = mAddr.getAddress().getHostAddress();
cfgParcel.prefixLength = mAddr.getPrefixLength();
cfgParcel.flags = mFlags.toArray(EMPTY_STRING_ARRAY);
return cfgParcel;
}
/**
* This function determines if the interface is up and has a valid IP
* configuration (IP address has a non zero octet).

View File

@@ -61,6 +61,7 @@ import android.net.TetherStatsParcel;
import android.net.INetworkManagementEventObserver;
import android.net.ITetheringStatsProvider;
import android.net.InterfaceConfiguration;
import android.net.InterfaceConfigurationParcel;
import android.net.IpPrefix;
import android.net.LinkAddress;
import android.net.Network;
@@ -965,55 +966,29 @@ public class NetworkManagementService extends INetworkManagementService.Stub
public String[] listInterfaces() {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
try {
return NativeDaemonEvent.filterMessageList(
mConnector.executeForList("interface", "list"), InterfaceListResult);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
final List<String> result = mNetdService.interfaceGetList();
return result.toArray(EMPTY_STRING_ARRAY);
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
}
@Override
public InterfaceConfiguration getInterfaceConfig(String iface) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
final NativeDaemonEvent event;
final InterfaceConfigurationParcel result;
try {
event = mConnector.execute("interface", "getcfg", iface);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
result = mNetdService.interfaceGetCfg(iface);
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
event.checkCode(InterfaceGetCfgResult);
// Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
final StringTokenizer st = new StringTokenizer(event.getMessage());
InterfaceConfiguration cfg;
try {
cfg = new InterfaceConfiguration();
cfg.setHardwareAddress(st.nextToken(" "));
InetAddress addr = null;
int prefixLength = 0;
try {
addr = NetworkUtils.numericToInetAddress(st.nextToken());
} catch (IllegalArgumentException iae) {
Slog.e(TAG, "Failed to parse ipaddr", iae);
}
try {
prefixLength = Integer.parseInt(st.nextToken());
} catch (NumberFormatException nfe) {
Slog.e(TAG, "Failed to parse prefixLength", nfe);
}
cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
while (st.hasMoreTokens()) {
cfg.setFlag(st.nextToken());
}
} catch (NoSuchElementException nsee) {
throw new IllegalStateException("Invalid response from daemon: " + event);
final InterfaceConfiguration cfg = InterfaceConfiguration.fromParcel(result);
return cfg;
} catch (IllegalArgumentException iae) {
throw new IllegalStateException("Invalid InterfaceConfigurationParcel", iae);
}
return cfg;
}
@Override
@@ -1024,17 +999,12 @@ public class NetworkManagementService extends INetworkManagementService.Stub
throw new IllegalStateException("Null LinkAddress given");
}
final Command cmd = new Command("interface", "setcfg", iface,
linkAddr.getAddress().getHostAddress(),
linkAddr.getPrefixLength());
for (String flag : cfg.getFlags()) {
cmd.appendArg(flag);
}
final InterfaceConfigurationParcel cfgParcel = cfg.toParcel(iface);
try {
mConnector.execute(cmd);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
mNetdService.interfaceSetCfg(cfgParcel);
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
}
@@ -1058,10 +1028,9 @@ public class NetworkManagementService extends INetworkManagementService.Stub
public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
try {
mConnector.execute(
"interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
mNetdService.interfaceSetIPv6PrivacyExtensions(iface, enable);
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
}
@@ -1071,9 +1040,9 @@ public class NetworkManagementService extends INetworkManagementService.Stub
public void clearInterfaceAddresses(String iface) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
try {
mConnector.execute("interface", "clearaddrs", iface);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
mNetdService.interfaceClearAddrs(iface);
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
}
@@ -1081,9 +1050,9 @@ public class NetworkManagementService extends INetworkManagementService.Stub
public void enableIpv6(String iface) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
try {
mConnector.execute("interface", "ipv6", iface, "enable");
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
mNetdService.interfaceSetEnableIPv6(iface, true);
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
}
@@ -1100,9 +1069,9 @@ public class NetworkManagementService extends INetworkManagementService.Stub
public void disableIpv6(String iface) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
try {
mConnector.execute("interface", "ipv6", iface, "disable");
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
mNetdService.interfaceSetEnableIPv6(iface, false);
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
}
@@ -1184,11 +1153,10 @@ public class NetworkManagementService extends INetworkManagementService.Stub
public void setMtu(String iface, int mtu) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
final NativeDaemonEvent event;
try {
event = mConnector.execute("interface", "setmtu", iface, mtu);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
mNetdService.interfaceSetMtu(iface, mtu);
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
}