Clean up InterfaceConfiguration flags.
Offer modifier methods for interface flags instead of mutating directly, and remove square brackets. Change-Id: I4cce719dccedfb3f0e8448c111e65b93c0008cbb
This commit is contained in:
@@ -16,34 +16,100 @@
|
||||
|
||||
package android.net;
|
||||
|
||||
import android.os.Parcelable;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import com.google.android.collect.Sets;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* A simple object for retrieving / setting an interfaces configuration
|
||||
* Configuration details for a network interface.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public class InterfaceConfiguration implements Parcelable {
|
||||
public String hwAddr;
|
||||
public LinkAddress addr;
|
||||
public String interfaceFlags;
|
||||
private String mHwAddr;
|
||||
private LinkAddress mAddr;
|
||||
private HashSet<String> mFlags = Sets.newHashSet();
|
||||
|
||||
public InterfaceConfiguration() {
|
||||
super();
|
||||
private static final String FLAG_UP = "up";
|
||||
private static final String FLAG_DOWN = "down";
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("mHwAddr=").append(mHwAddr);
|
||||
builder.append(" mAddr=").append(String.valueOf(mAddr));
|
||||
builder.append(" mFlags=").append(getFlags());
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer str = new StringBuffer();
|
||||
/**
|
||||
* Return flags separated by spaces.
|
||||
*/
|
||||
public String getFlags() {
|
||||
final int size = mFlags.size();
|
||||
if (size == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
str.append("ipddress ");
|
||||
str.append((addr != null) ? addr.toString() : "NULL");
|
||||
str.append(" flags ").append(interfaceFlags);
|
||||
str.append(" hwaddr ").append(hwAddr);
|
||||
final String[] flags = mFlags.toArray(new String[size]);
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
|
||||
return str.toString();
|
||||
builder.append(flags[0]);
|
||||
for (int i = 1; i < flags.length; i++) {
|
||||
builder.append(' ');
|
||||
builder.append(flags[i]);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public boolean hasFlag(String flag) {
|
||||
validateFlag(flag);
|
||||
return mFlags.contains(flag);
|
||||
}
|
||||
|
||||
public void clearFlag(String flag) {
|
||||
validateFlag(flag);
|
||||
mFlags.remove(flag);
|
||||
}
|
||||
|
||||
public void setFlag(String flag) {
|
||||
validateFlag(flag);
|
||||
mFlags.add(flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set flags to mark interface as up.
|
||||
*/
|
||||
public void setInterfaceUp() {
|
||||
mFlags.remove(FLAG_DOWN);
|
||||
mFlags.add(FLAG_UP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set flags to mark interface as down.
|
||||
*/
|
||||
public void setInterfaceDown() {
|
||||
mFlags.remove(FLAG_UP);
|
||||
mFlags.add(FLAG_DOWN);
|
||||
}
|
||||
|
||||
public LinkAddress getLinkAddress() {
|
||||
return mAddr;
|
||||
}
|
||||
|
||||
public void setLinkAddress(LinkAddress addr) {
|
||||
mAddr = addr;
|
||||
}
|
||||
|
||||
public String getHardwareAddress() {
|
||||
return mHwAddr;
|
||||
}
|
||||
|
||||
public void setHardwareAddress(String hwAddr) {
|
||||
mHwAddr = hwAddr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,8 +121,8 @@ public class InterfaceConfiguration implements Parcelable {
|
||||
*/
|
||||
public boolean isActive() {
|
||||
try {
|
||||
if(interfaceFlags.contains("up")) {
|
||||
for (byte b : addr.getAddress().getAddress()) {
|
||||
if (hasFlag(FLAG_UP)) {
|
||||
for (byte b : mAddr.getAddress().getAddress()) {
|
||||
if (b != 0) return true;
|
||||
}
|
||||
}
|
||||
@@ -66,38 +132,49 @@ public class InterfaceConfiguration implements Parcelable {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Implement the Parcelable interface {@hide} */
|
||||
/** {@inheritDoc} */
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Implement the Parcelable interface {@hide} */
|
||||
/** {@inheritDoc} */
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeString(hwAddr);
|
||||
if (addr != null) {
|
||||
dest.writeString(mHwAddr);
|
||||
if (mAddr != null) {
|
||||
dest.writeByte((byte)1);
|
||||
dest.writeParcelable(addr, flags);
|
||||
dest.writeParcelable(mAddr, flags);
|
||||
} else {
|
||||
dest.writeByte((byte)0);
|
||||
}
|
||||
dest.writeString(interfaceFlags);
|
||||
dest.writeInt(mFlags.size());
|
||||
for (String flag : mFlags) {
|
||||
dest.writeString(flag);
|
||||
}
|
||||
}
|
||||
|
||||
/** Implement the Parcelable interface {@hide} */
|
||||
public static final Creator<InterfaceConfiguration> CREATOR =
|
||||
new Creator<InterfaceConfiguration>() {
|
||||
public InterfaceConfiguration createFromParcel(Parcel in) {
|
||||
InterfaceConfiguration info = new InterfaceConfiguration();
|
||||
info.hwAddr = in.readString();
|
||||
if (in.readByte() == 1) {
|
||||
info.addr = in.readParcelable(null);
|
||||
}
|
||||
info.interfaceFlags = in.readString();
|
||||
return info;
|
||||
public static final Creator<InterfaceConfiguration> CREATOR = new Creator<
|
||||
InterfaceConfiguration>() {
|
||||
public InterfaceConfiguration createFromParcel(Parcel in) {
|
||||
InterfaceConfiguration info = new InterfaceConfiguration();
|
||||
info.mHwAddr = in.readString();
|
||||
if (in.readByte() == 1) {
|
||||
info.mAddr = in.readParcelable(null);
|
||||
}
|
||||
final int size = in.readInt();
|
||||
for (int i = 0; i < size; i++) {
|
||||
info.mFlags.add(in.readString());
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
public InterfaceConfiguration[] newArray(int size) {
|
||||
return new InterfaceConfiguration[size];
|
||||
}
|
||||
};
|
||||
public InterfaceConfiguration[] newArray(int size) {
|
||||
return new InterfaceConfiguration[size];
|
||||
}
|
||||
};
|
||||
|
||||
private static void validateFlag(String flag) {
|
||||
if (flag.indexOf(' ') >= 0) {
|
||||
throw new IllegalArgumentException("flag contains space: " + flag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -377,16 +377,16 @@ final class BluetoothPanProfileHandler {
|
||||
try {
|
||||
ifcg = service.getInterfaceConfig(iface);
|
||||
if (ifcg != null) {
|
||||
final LinkAddress linkAddr = ifcg.getLinkAddress();
|
||||
InetAddress addr = null;
|
||||
if (ifcg.addr == null || (addr = ifcg.addr.getAddress()) == null ||
|
||||
if (linkAddr == null || (addr = linkAddr.getAddress()) == null ||
|
||||
addr.equals(NetworkUtils.numericToInetAddress("0.0.0.0")) ||
|
||||
addr.equals(NetworkUtils.numericToInetAddress("::0"))) {
|
||||
addr = NetworkUtils.numericToInetAddress(address);
|
||||
}
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
|
||||
ifcg.addr = new LinkAddress(addr, BLUETOOTH_PREFIX_LENGTH);
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace(" "," ");
|
||||
ifcg.setInterfaceUp();
|
||||
ifcg.clearFlag("running");
|
||||
ifcg.setLinkAddress(new LinkAddress(addr, BLUETOOTH_PREFIX_LENGTH));
|
||||
service.setInterfaceConfig(iface, ifcg);
|
||||
if (cm.tether(iface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
|
||||
Log.e(TAG, "Error tethering "+iface);
|
||||
|
||||
@@ -395,7 +395,7 @@ public class NetworkManagementService extends INetworkManagementService.Stub
|
||||
}
|
||||
|
||||
cfg = new InterfaceConfiguration();
|
||||
cfg.hwAddr = st.nextToken(" ");
|
||||
cfg.setHardwareAddress(st.nextToken(" "));
|
||||
InetAddress addr = null;
|
||||
int prefixLength = 0;
|
||||
try {
|
||||
@@ -410,27 +410,29 @@ public class NetworkManagementService extends INetworkManagementService.Stub
|
||||
Slog.e(TAG, "Failed to parse prefixLength", nfe);
|
||||
}
|
||||
|
||||
cfg.addr = new LinkAddress(addr, prefixLength);
|
||||
cfg.interfaceFlags = st.nextToken("]").trim() +"]";
|
||||
cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
|
||||
while (st.hasMoreTokens()) {
|
||||
cfg.setFlag(st.nextToken());
|
||||
}
|
||||
} catch (NoSuchElementException nsee) {
|
||||
throw new IllegalStateException(
|
||||
String.format("Invalid response from daemon (%s)", rsp));
|
||||
}
|
||||
Slog.d(TAG, String.format("flags <%s>", cfg.interfaceFlags));
|
||||
Slog.d(TAG, String.format("flags <%s>", cfg.getFlags()));
|
||||
return cfg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
|
||||
mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
|
||||
LinkAddress linkAddr = cfg.addr;
|
||||
LinkAddress linkAddr = cfg.getLinkAddress();
|
||||
if (linkAddr == null || linkAddr.getAddress() == null) {
|
||||
throw new IllegalStateException("Null LinkAddress given");
|
||||
}
|
||||
String cmd = String.format("interface setcfg %s %s %d %s", iface,
|
||||
linkAddr.getAddress().getHostAddress(),
|
||||
linkAddr.getNetworkPrefixLength(),
|
||||
cfg.interfaceFlags);
|
||||
cfg.getFlags());
|
||||
try {
|
||||
mConnector.doCommand(cmd);
|
||||
} catch (NativeDaemonConnectorException e) {
|
||||
@@ -443,7 +445,7 @@ public class NetworkManagementService extends INetworkManagementService.Stub
|
||||
public void setInterfaceDown(String iface) {
|
||||
mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
|
||||
final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down");
|
||||
ifcg.setInterfaceDown();
|
||||
setInterfaceConfig(iface, ifcg);
|
||||
}
|
||||
|
||||
@@ -451,7 +453,7 @@ public class NetworkManagementService extends INetworkManagementService.Stub
|
||||
public void setInterfaceUp(String iface) {
|
||||
mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
|
||||
final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
|
||||
ifcg.setInterfaceUp();
|
||||
setInterfaceConfig(iface, ifcg);
|
||||
}
|
||||
|
||||
|
||||
@@ -545,14 +545,13 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
|
||||
ifcg = mNMService.getInterfaceConfig(iface);
|
||||
if (ifcg != null) {
|
||||
InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
|
||||
ifcg.addr = new LinkAddress(addr, USB_PREFIX_LENGTH);
|
||||
ifcg.setLinkAddress(new LinkAddress(addr, USB_PREFIX_LENGTH));
|
||||
if (enabled) {
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
|
||||
ifcg.setInterfaceUp();
|
||||
} else {
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down");
|
||||
ifcg.setInterfaceDown();
|
||||
}
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
|
||||
ifcg.interfaceFlags = ifcg.interfaceFlags.replace(" "," ");
|
||||
ifcg.clearFlag("running");
|
||||
mNMService.setInterfaceConfig(iface, ifcg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -1153,9 +1153,9 @@ public class WifiStateMachine extends StateMachine {
|
||||
ifcg = mNwService.getInterfaceConfig(intf);
|
||||
if (ifcg != null) {
|
||||
/* IP/netmask: 192.168.43.1/255.255.255.0 */
|
||||
ifcg.addr = new LinkAddress(NetworkUtils.numericToInetAddress(
|
||||
"192.168.43.1"), 24);
|
||||
ifcg.interfaceFlags = "[up]";
|
||||
ifcg.setLinkAddress(new LinkAddress(
|
||||
NetworkUtils.numericToInetAddress("192.168.43.1"), 24));
|
||||
ifcg.setInterfaceUp();
|
||||
|
||||
mNwService.setInterfaceConfig(intf, ifcg);
|
||||
}
|
||||
@@ -1187,8 +1187,8 @@ public class WifiStateMachine extends StateMachine {
|
||||
try {
|
||||
ifcg = mNwService.getInterfaceConfig(mInterfaceName);
|
||||
if (ifcg != null) {
|
||||
ifcg.addr = new LinkAddress(NetworkUtils.numericToInetAddress(
|
||||
"0.0.0.0"), 0);
|
||||
ifcg.setLinkAddress(
|
||||
new LinkAddress(NetworkUtils.numericToInetAddress("0.0.0.0"), 0));
|
||||
mNwService.setInterfaceConfig(mInterfaceName, ifcg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -2911,8 +2911,8 @@ public class WifiStateMachine extends StateMachine {
|
||||
DhcpInfoInternal dhcpInfoInternal = WifiConfigStore.getIpConfiguration(
|
||||
mLastNetworkId);
|
||||
InterfaceConfiguration ifcg = new InterfaceConfiguration();
|
||||
ifcg.addr = dhcpInfoInternal.makeLinkAddress();
|
||||
ifcg.interfaceFlags = "[up]";
|
||||
ifcg.setLinkAddress(dhcpInfoInternal.makeLinkAddress());
|
||||
ifcg.setInterfaceUp();
|
||||
try {
|
||||
mNwService.setInterfaceConfig(mInterfaceName, ifcg);
|
||||
if (DBG) log("Static IP configuration succeeded");
|
||||
|
||||
@@ -1230,9 +1230,9 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
|
||||
InterfaceConfiguration ifcg = null;
|
||||
try {
|
||||
ifcg = mNwService.getInterfaceConfig(intf);
|
||||
ifcg.addr = new LinkAddress(NetworkUtils.numericToInetAddress(
|
||||
SERVER_ADDRESS), 24);
|
||||
ifcg.interfaceFlags = "[up]";
|
||||
ifcg.setLinkAddress(new LinkAddress(NetworkUtils.numericToInetAddress(
|
||||
SERVER_ADDRESS), 24));
|
||||
ifcg.setInterfaceUp();
|
||||
mNwService.setInterfaceConfig(intf, ifcg);
|
||||
/* This starts the dnsmasq server */
|
||||
mNwService.startTethering(DHCP_RANGE);
|
||||
|
||||
Reference in New Issue
Block a user