android8.1 修改网卡IP

android8.1 修改网卡IP

  • android8.1 修改网卡IP
    • 分析ipconfig.txt
    • 生成ipconfig.txt
    • MAC地址修改
    • 注意

android8.1 修改网卡IP

android8.1,通过ifconfig命令配置IP地址,会被系统重新改回去。这就需要修改/data/misc/ethernet/ipconfig.txt。

分析ipconfig.txt

源码位于frameworks/base/services/core/java/com/android/server/net/IpConfigStore.java

@VisibleForTesting
    public static boolean writeConfig(DataOutputStream out, int configKey,
                                IpConfiguration config) throws IOException {
        boolean written = false;

        try {
            switch (config.ipAssignment) {
                case STATIC:
                    out.writeUTF(IP_ASSIGNMENT_KEY);
                    out.writeUTF(config.ipAssignment.toString());
                    StaticIpConfiguration staticIpConfiguration = config.staticIpConfiguration;
                    if (staticIpConfiguration != null) {
                        if (staticIpConfiguration.ipAddress != null) {
                            LinkAddress ipAddress = staticIpConfiguration.ipAddress;
                            out.writeUTF(LINK_ADDRESS_KEY);
                            out.writeUTF(ipAddress.getAddress().getHostAddress());
                            out.writeInt(ipAddress.getPrefixLength());
                        }
                        if (staticIpConfiguration.gateway != null) {
                            out.writeUTF(GATEWAY_KEY);
                            out.writeInt(0);  // Default route.
                            out.writeInt(1);  // Have a gateway.
                            out.writeUTF(staticIpConfiguration.gateway.getHostAddress());
                        }
                        for (InetAddress inetAddr : staticIpConfiguration.dnsServers) {
                            out.writeUTF(DNS_KEY);
                            out.writeUTF(inetAddr.getHostAddress());
                        }
                    }
                    written = true;
                    break;
                case DHCP:
                    out.writeUTF(IP_ASSIGNMENT_KEY);
                    out.writeUTF(config.ipAssignment.toString());
                    written = true;
                    break;
                case PPPOE:
                    out.writeUTF(IP_ASSIGNMENT_KEY);
                    out.writeUTF(config.ipAssignment.toString());
                    out.writeUTF(PPPOE_ACCOUNT_KEY);
                    out.writeUTF(config.pppoeAccount);
                    out.writeUTF(PPPOE_PASSWORD_KEY);
                    out.writeUTF(config.pppoePassword);
                    written = true;
                    break;
                case UNASSIGNED:
                /* Ignore */
                    break;
                default:
                    loge("Ignore invalid ip assignment while writing");
                    break;
            }

            switch (config.proxySettings) {
                case STATIC:
                    ProxyInfo proxyProperties = config.httpProxy;
                    String exclusionList = proxyProperties.getExclusionListAsString();
                    out.writeUTF(PROXY_SETTINGS_KEY);
                    out.writeUTF(config.proxySettings.toString());
                    out.writeUTF(PROXY_HOST_KEY);
                    out.writeUTF(proxyProperties.getHost());
                    out.writeUTF(PROXY_PORT_KEY);
                    out.writeInt(proxyProperties.getPort());
                    if (exclusionList != null) {
                        out.writeUTF(EXCLUSION_LIST_KEY);
                        out.writeUTF(exclusionList);
                    }
                    written = true;
                    break;
                case PAC:
                    ProxyInfo proxyPacProperties = config.httpProxy;
                    out.writeUTF(PROXY_SETTINGS_KEY);
                    out.writeUTF(config.proxySettings.toString());
                    out.writeUTF(PROXY_PAC_FILE);
                    out.writeUTF(proxyPacProperties.getPacFileUrl().toString());
                    written = true;
                    break;
                case NONE:
                    out.writeUTF(PROXY_SETTINGS_KEY);
                    out.writeUTF(config.proxySettings.toString());
                    written = true;
                    break;
                case UNASSIGNED:
                    /* Ignore */
                        break;
                    default:
                        loge("Ignore invalid proxy settings while writing");
                        break;
            }

            if (written) {
                out.writeUTF(ID_KEY);
                out.writeInt(configKey);
            }
        } catch (NullPointerException e) {
            loge("Failure in writing " + config + e);
        }
        out.writeUTF(EOS);

        return written;
    }

生成ipconfig.txt

仔细看下,挺清晰的。按照协议写了个静态IP配置方法

private boolean saveNetConfig(String ipAddr, String netMask, String netGate) {
	try {
		final String ipConfigFile = Environment.getDataDirectory() + "/misc/ethernet/ipconfig.txt";
		File file = new File(ipConfigFile);
		FileOutputStream fos = new FileOutputStream(file);
		DataOutputStream out = new DataOutputStream(fos);
		
		final int IPCONFIG_FILE_VERSION = 2;
		out.writeInt(IPCONFIG_FILE_VERSION);

		final String IP_ASSIGNMENT_KEY = "ipAssignment";
		out.writeUTF(IP_ASSIGNMENT_KEY);
		out.writeUTF("STATIC");

		final String LINK_ADDRESS_KEY = "linkAddress";
		out.writeUTF(LINK_ADDRESS_KEY);
		out.writeUTF(ipAddr);
		out.writeInt(24); // 地址前缀192.168.1.110则前缀192.168.1为24

		final String GATEWAY_KEY = "gateway";
		out.writeUTF(GATEWAY_KEY);
		out.writeInt(0);  // Default route.
		out.writeInt(1);  // Have a gateway.
		out.writeUTF(netGate);

		final String DNS_KEY = "dns";
		out.writeUTF(DNS_KEY);
		out.writeUTF(netGate); // 一般路由器,网关也是它的DNS
		out.writeUTF(DNS_KEY);
		out.writeUTF("8.8.8.8");

		final String PROXY_SETTINGS_KEY = "proxySettings";
		out.writeUTF(PROXY_SETTINGS_KEY);
		out.writeUTF("NONE");

		final String ID_KEY = "id";
		out.writeUTF(ID_KEY);
		out.writeInt(0);

		final String EOS = "eos";
		out.writeUTF(EOS);
		
		out.flush();
		out.close();
		
	}catch (Exception e) {
		e.printStackTrace();
		return false;
	}
	
	return true;
}

MAC地址修改

busybox ifconfig eth0 down hw ether 66:EB:5C:AC:1E:33
busybox ifconfig eth0 up

注意

1)读写ipconfig.txt这个文件需要系统权限,因此程序需要进行系统签名
2)不想签名的话,如果有root权限,也可修改ipconfig.txt文件的权限。chmod 777 /data/misc;chmod 777 /data/misc/ethernet;chmod 666 /data/misc/ethernet/ipconfig.txt
3)配置后需要重启系统后生效,在设置中可以看到
android8.1 修改网卡IP_第1张图片

你可能感兴趣的:(Android)