关于之前在这提问路由表相关问题的后续结果

2016-11-08 11:31:04 +08:00
 ybjaychou

问题回顾

之前一直想着把后续写下的,事情太多给搞忘了,这几天有几个人加我 QQ 问我有没解决,我才想起来

以下是我这边经过修改可以达到预期效果的方法,仅供参考! 思路是:先保证系统多个网卡能同时获取到 IP ,然后设置路由表分配什么流量走什么网卡

=============================================

当以太网连接后 Wi-Fi 仍可连 frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachine.java

private void setScanResults() {
	...
	if (getCurrentState() == mRoamingState
                || getCurrentState() == mObtainingIpState
                || getCurrentState() == mScanModeState
                || getCurrentState() == mDisconnectingState
                || (getCurrentState() == mConnectedState
                && !mWifiConfigStore.enableAutoJoinWhenAssociated)
                || linkDebouncing
                || state == SupplicantState.ASSOCIATING
                || state == SupplicantState.AUTHENTICATING
                || state == SupplicantState.FOUR_WAY_HANDSHAKE
                || state == SupplicantState.GROUP_HANDSHAKE
                || (/* keep autojoin enabled if user has manually selected a wifi network,
                        so as to make sure we reliably remain connected to this network */
                        mConnectionRequests == 0 && selection == null)) {
            // Dont attempt auto-joining again while we are already attempting to join
            // and/or obtaining Ip address
            attemptAutoJoin = false;
        }
    //add by a_bin for blzn network
    if(SystemProperties.getBoolean("ro.zwqx.blzn_network",false)){
            attemptAutoJoin = true;
    }
    ...
    
}

连接高优先级的网络,低优先级的不断开 frameworks/base/services/core/java/com/android/server/ConnectivityService.java

private void teardownUnneededNetwork(NetworkAgentInfo nai) {
	//add by a_bin for blzn network
    if(SystemProperties.getBoolean("ro.zwqx.blzn_network",false)){
            return;
    }
}

最后就是路由表的设置了,这里需要 root 权限 frameworks/base/services/core/java/com/android/server/ConnectivityService.java

private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties oldLp){
	...
    if(networkAgent.linkProperties.getInterfaceName().equals("wlan0")){
                            java.lang.Process pro = Runtime.getRuntime().exec("su");
                            DataOutputStream outputStream = new DataOutputStream(pro.getOutputStream());
                            outputStream.writeBytes("ip route flush table 3\n");
                            outputStream.writeBytes("ip route add dev wlan0 table 3\n");
                            outputStream.writeBytes("ip rule add to 10.1.0.0/16 table 3\n");
                            outputStream.writeBytes("ip route flush cache\n");
                            outputStream.writeBytes("exit\n");
                            outputStream.flush();
                            outputStream.close();
                            pro.waitFor();
            }else if(networkAgent.linkProperties.getInterfaceName().equals("eth0")){
                            java.lang.Process pro = Runtime.getRuntime().exec("su");
                            DataOutputStream outputStream = new DataOutputStream(pro.getOutputStream());
                            outputStream.writeBytes("ip route flush table 1\n");
                            outputStream.writeBytes("ip route add dev eth0 table 1\n");
                            outputStream.writeBytes("ip rule add to 192.168.0.0/16 table 1\n");
                            outputStream.writeBytes("ip route flush cache\n");
                            outputStream.writeBytes("exit\n");
                            outputStream.flush();
                            outputStream.close();
                            pro.waitFor(); ok::updateLinkProperties()");
            }
    ...
}
2667 次点击
所在节点    分享创造
3 条回复
csrbot
2017-05-28 17:42:45 +08:00
非常感谢楼主的分享。
但是我路由配置那里需要将 16 该成 24 才能 ping 通。
csrbot
2017-07-15 14:46:33 +08:00
楼主,android5.1 里面如何修改这个 wifi 和以太网的优先级,我想把 wifi 的优先级高于以太网,但是没有成功。
ybjaychou
2017-07-15 15:36:02 +08:00
@csrbot 试试改这个文件的`networkAttributes`
`frameworks/base/core/res/res/values/config.xml`

改完之后最好在代码里面打印出来确认下有没有修改成功

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/318946

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX