Android : 有线DHCP信息保存到属性值

修改源码路径:frameworks\base\services\net\java\android\net\dhcp\DhcpClient.java

1、定义对应属性变量:

    // Patch Begin - Sheldon
    private static final String PROPERTY_DHCPCLIENT_IP      = "dhcp.eth0.ipaddress";
    private static final String PROPERTY_DHCPCLIENT_MASK    = "dhcp.eth0.mask";
    private static final String PROPERTY_DHCPCLIENT_GATEWAY = "dhcp.eth0.gateway";
    private static final String PROPERTY_DHCPCLIENT_DNS1    = "dhcp.eth0.dns1";
    private static final String PROPERTY_DHCPCLIENT_DNS2    = "dhcp.eth0.dns2";
    // Patch end

2.从acceptDhcpResults方法的results参数中提取IP地址、子网掩码、网关、DNS等信息。注:子网掩码是通过PrefixLength转换所得。

    private void acceptDhcpResults(DhcpResults results, String msg) {
        mDhcpLease = results;
        mOffer = null;
        Log.d(TAG, msg + " lease: " + mDhcpLease);

        // Patch Begin - Sheldon
        if (mIfaceName != null && (mIfaceName.equals("eth0") || mIfaceName.equals("veth0")))
        {
            try {
                    String ipAddr  = results.ipAddress.getAddress().getHostAddress();
                    int preFixLen  = results.ipAddress.getPrefixLength();

                    // Convert PrefixLength to subnet mask
                    int value = 0xffffffff << (32 - preFixLen);
                    byte[] bytes = new byte[]{ 
                            (byte)(value >> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff)};
                    InetAddress netAddr = InetAddress.getByAddress(bytes);
                    String subNetMask  = netAddr.getHostAddress();

                    String gateWay = deleteCharString(results.gateway.toString(), '/');
                    String dns1    = deleteCharString(results.dnsServers.get(0).toString(), '/');
                    String dns2    = deleteCharString(results.dnsServers.get(1).toString(), '/');

                    SystemProperties.set(PROPERTY_DHCPCLIENT_IP, ipAddr);
                    SystemProperties.set(PROPERTY_DHCPCLIENT_MASK, subNetMask);
                    SystemProperties.set(PROPERTY_DHCPCLIENT_GATEWAY, gateWay);
                    SystemProperties.set(PROPERTY_DHCPCLIENT_DNS1, dns1);
                    SystemProperties.set(PROPERTY_DHCPCLIENT_DNS2, dns2);
           
            } catch (Exception e) {
                    e.printStackTrace();
            }
        }
        //Patch end

        notifySuccess();
    }
上一篇:JSON 序列化的时候忽略无效的属性值


下一篇:Unity3D的RPG游戏中攻击的判定