获取 静态 IP地址命令:

settings get system ethernet_static_ip

设置静态IP地址命令:

settings put system ethernet_static_ip 192.168.1.222

对应的网关,子网掩码,DNS对应的key string为

ethernet_static_netmask,ethernet_static_gateway,ethernet_static_dns1,ethernet_static_dns2

该命令的实现文件目录为:

frameworks/base/cmds/settings/src/com/android/commands/settings/SettingsCmd.java
具体使用提示如下:

private static void printUsage() {
System.err.println("usage: settings [--user NUM] get namespace key");
System.err.println(" settings [--user NUM] put namespace key value");
System.err.println(" settings [--user NUM] delete namespace key");
System.err.println("\n'namespace' is one of {system, secure, global}, case-insensitive");
System.err.println("If '--user NUM' is not given, the operations are performed on the owner user.");
}

settings数据库存放路径:/data/data/com.android.providers.settings/databases
默认数据加载文件:/frameworks/base/packages/SettingsProvider/res/values/defaults.xml


添加dns

root@android:/ # setprop net.eth0.dns1 8.8.8.8
setprop net.eth0.dns1 8.8.8.8

root@android:/ # setprop net.eth0.dns2 8.8.4.4
setprop net.eth0.dns2 8.8.4.4

查看eth配置信息

root@android:/ # getprop | grep eth0
getprop | grep eth0
[dhcp.eth0.dns1]: [192.168.0.1]
[dhcp.eth0.dns2]: []
[dhcp.eth0.dns3]: []
[dhcp.eth0.dns4]: []
[dhcp.eth0.gateway]: [192.168.0.1]
[dhcp.eth0.ipaddress]: [192.168.0.180]
[dhcp.eth0.leasetime]: [3600]
[dhcp.eth0.mask]: [255.255.255.0]
[dhcp.eth0.pid]: [13800]
[dhcp.eth0.reason]: [PREINIT]
[dhcp.eth0.result]: [failed]
[dhcp.eth0.server]: [192.168.0.1]
[dhcp.eth0.vendorInfo]: []
[net.change]: [net.eth0.dns2]
[net.eth0.dns1]: [8.8.8.8]
[net.eth0.dns2]: [8.8.4.4]



Platform: RK3368,OS: Android 6.0,Kernel: 3.10.0

1、以太网与Wi-Fi共存问题

想要多个网卡同时使用,首先就要解决多网卡共存问题。Android系统默认会将不需要的网络给关闭的,以降低功耗。所以要解决以太网与Wi-Fi共存问题,需要做如下改动:

--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -4247,7 +4248,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
loge("Dead network still had at least " + nr);
break;
}
- nai.asyncChannel.disconnect();
+ log("don't teardownUnneededNetwork " + nai);
+ //nai.asyncChannel.disconnect();
}

private void handleLingerComplete(NetworkAgentInfo oldNetwork) {

 

查一下当前系统的网络优先级配置:
frameworks/base/core/res/res/values/config.xml

<string-array translatable="false" name="networkAttributes">
<item>"wifi,1,1,2,-1,true"</item>
<item>"mobile,0,0,0,-1,true"</item>
<item>"mobile_mms,2,0,2,60000,true"</item>
<item>"mobile_supl,3,0,2,60000,true"</item>
<item>"mobile_dun,4,0,2,60000,true"</item>
<item>"mobile_hipri,5,0,3,60000,true"</item>
<item>"mobile_fota,10,0,2,60000,true"</item>
<item>"mobile_ims,11,0,2,60000,true"</item>
<item>"mobile_cbs,12,0,2,60000,true"</item>
<item>"bluetooth,7,7,0,-1,true"</item>
<item>"ethernet,9,9,9,-1,true"</item>
</string-array>

可以看到以太网优先级高于wifi,也就是说以太网和wifi同时打开,默认会使用以太网。

2、修改ip路由

解决了多网卡共存问题,那么接下来就是要配置ip路由了,先ifconfig看一下设备的网络信息如下,一个以太网,一个wifi:

# ifconfig
eth0 Link encap:Ethernet HWaddr C8:97:06:8B:F0:02
inet addr:192.168.88.29 Bcast:192.168.88.255 Mask:255.255.255.0
inet6 addr: fe80::ca97:6ff:fe8b:f002/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:980 errors:0 dropped:0 overruns:0 frame:0
TX packets:50 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:76080 TX bytes:16928
Interrupt:59

wlan0 Link encap:Ethernet HWaddr 88:06:95:C9:1B:1A
inet addr:192.168.43.3 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::8a06:95ff:fec9:1b1a/64 Scope: Link
inet6 addr: 240e:c0:f612:2990:c159:79be:bbdd:ba2c/64 Scope: Global
inet6 addr: 240e:c0:f612:2990:8a06:95ff:fec9:1b1a/64 Scope: Global
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:56 errors:0 dropped:101 overruns:0 frame:0
TX packets:58 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:7712 TX bytes:7511
......

    可以看到两个网卡为不同网段的IP地址,但这个时候相互ping的话是ping不通的.

我们再查看当前路由信息:

ip route

192.168.43.0/24 dev wlan0 proto kernel scope link src 192.168.43.3
192.168.88.0/24 dev eth0 proto kernel scope link src 192.168.88.29

IP路由似乎也是没问题的,系统自动添加了路由配置。
这如果是ubuntu,debian等linux系统的话当然可以ping通;但是在Android系统中就行不通了。由于Android系统默认使用的是优先级和评分高的网络,而这台设备系统配置的是以太网优先级高于wifi,所以默认路由使用的是以太网的路由规则。直接用ping命令是不能ping通wifi那边的网络。需要将无线的默认路由规则添加到以太网的路由表中(也就是Android系统当前用的路由表为优先级最高的那个).

所以现在需要修改eth0路由表:

ip route add 192.168.43.0/24 dev wlan0 proto static scope link table eth0

# 或者直接复制wlan0路由表的规则添加到eth0路由表:
ip route add 192.168.43.0/24 dev wlan0 proto kernel scope link src 192.168.43.3 table eth0

上面命令的意思是:向路由表eth0中添加一条‘192.168.43.0/24 dev wlan0’路由规则.这样发送到192.168.43网段的数据就会通过wifi出去了。在设备中就可以ping通无线和有线两个网络了.

3、 双IP地址也是类似

比如要将一个网卡设置两个IP地址。

ip address add 10.0.0.10/8 dev wlan0

ip route
10.0.0.0/8 dev wlan0 proto kernel scope link src 10.0.0.10

#将默认的路由配置,复制到wlan0路由表中:
ip route add 10.0.0.0/8 dev wlan0 proto kernel scope link src 10.0.0.10 table wlan0

  4、 Android 路由相关代码:

当然啦!以上都是手动敲命令实现的,如果要做到自动修改路由表,还需要修改系统相关代码,要等有时间再去研究了。
相关代码目录:framework/base/

     services/core/java/com/android/server/ConnectivityService.java
     services/core/java/com/android/server/NetworkManagementService.java
     services/core/java/com/android/server/net/IpConfigStore.java

网络调试相关日志查看:

$ logcat -s ConnectivityService ConnectivityManager EthernetNetworkFactory 
WifiConfigStore IpConfigStore NetworkManagement