V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
ybjaychou
V2EX  ›  分享创造

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

  •  
  •   ybjaychou · 2016-11-08 11:31:04 +08:00 · 2632 次点击
    这是一个创建于 2930 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题回顾

    之前一直想着把后续写下的,事情太多给搞忘了,这几天有几个人加我 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()");
                }
        ...
    }
    
    3 条回复    2017-07-15 15:36:02 +08:00
    csrbot
        1
    csrbot  
       2017-05-28 17:42:45 +08:00
    非常感谢楼主的分享。
    但是我路由配置那里需要将 16 该成 24 才能 ping 通。
    csrbot
        2
    csrbot  
       2017-07-15 14:46:33 +08:00
    楼主,android5.1 里面如何修改这个 wifi 和以太网的优先级,我想把 wifi 的优先级高于以太网,但是没有成功。
    ybjaychou
        3
    ybjaychou  
    OP
       2017-07-15 15:36:02 +08:00
    @csrbot 试试改这个文件的`networkAttributes`
    `frameworks/base/core/res/res/values/config.xml`

    改完之后最好在代码里面打印出来确认下有没有修改成功
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2838 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:57 · PVG 14:57 · LAX 22:57 · JFK 01:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.