Android 13 有线以太网静态ip保存逻辑梳理分析

源码环境:高通Android 13

这里特别说明,Android13中,ipconfig.txt配置文件目录有改变

以前:/data/misc/ethernet/ipconfig.txt
最新的有线网配置文件保存目录:
/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt

一、Android 版本影响
首先就是Android 11 与 12 的有线网络版本差距还是比较大的,源码目录也变了,之前目录frameworks/opt/net/ethernet/java/com/android/server/ethernet,现在改成了packages/modules/Connectivity/service-t/src/com/android/server/ethernet,所以如果之前只在11上面适配过,那么对于12来说,适配还是需要花费一点功夫,具体的差异在之后的部分会记录,但是12与13的有线网络差异就较小了,并且看起来,13的以太网接口以及逻辑比12来说更为完善。

二、配置以太网所需要的类
配置以太网所需要的java类大概有以下几个,其实这几个类从Android9开始就是以太网配置的主要java类了

(1)、frameworks/base/core/java/android/net/EthernetManager.java

EthernetManager.java此是上层管理以太网的类,我们常通过context.getSystemService(Context.ETHERNET_SERVICE)获得他的实例对象

(2)、packages/modules/Connectivity/framework/src/android/net/IpConfiguration.java

这个类主要就是用来配置IP状态的,包括动态和静态

(3)、packages/modules/Connectivity/framework/src/android/net/StaticIpConfiguration.java

这个类主要就是用来配置静态IP的,这个类之前也是在frameworks/base/core/java/android/net/路径下,12里面也移到了packages/modules/Connectivity/framework/src/android/net/下

三、有线以太网静态ip保存逻辑从源码逐步分析    

1、packages/modules/Connectivity/framework-t/src/android/net/EthernetManager.java 
 public void setConfiguration(@NonNull String iface, @NonNull IpConfiguration config) {
        try {
            mService.setConfiguration(iface, config);//这里调用的是EthernetServiceImpl.java中的setConfiguration
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }



2、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetServiceImpl.java
  /**
     * Set Ethernet configuration
     */
    @Override
    public void setConfiguration(String iface, IpConfiguration config) {
        throwIfEthernetNotStarted();

        PermissionUtils.enforceNetworkStackPermission(mContext);
        if (mTracker.isRestrictedInterface(iface)) {
            PermissionUtils.enforceRestrictedNetworkPermission(mContext, TAG);
        }

        // TODO: this does not check proxy settings, gateways, etc.
        // Fix this by making IpConfiguration a complete representation of static configuration.
        mTracker.updateIpConfiguration(iface, new IpConfiguration(config));//这里更新保存
    }


3、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetTracker.java
EthernetTracker中主要做了以下两件事 :
(1). 首先更新 ip config的,这个和静态ip相关;
(2). 根据iface,调用addInterface创建interface

   void updateIpConfiguration(String iface, IpConfiguration ipConfiguration) {
        if (DBG) {
            Log.i(TAG, "updateIpConfiguration, iface: " + iface + ", cfg: " + ipConfiguration);
        }
        writeIpConfiguration(iface, ipConfiguration);
        mHandler.post(() -> {
            mFactory.updateInterface(iface, ipConfiguration, null, null);
            broadcastInterfaceStateChange(iface);
        });
    }


   private void writeIpConfiguration(@NonNull final String iface,
            @NonNull final IpConfiguration ipConfig) {
        mConfigStore.write(iface, ipConfig);//这里调用了EthernetConfigStore.java中的write
        mIpConfigurations.put(iface, ipConfig);
    }


4、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetConfigStore.java
  private static final String CONFIG_FILE = "ipconfig.txt";
  private static final String FILE_PATH = "/misc/ethernet/";
  private static final String APEX_IP_CONFIG_FILE_PATH = ApexEnvironment.getApexEnvironment(
            TETHERING_MODULE_NAME).getDeviceProtectedDataDir() + FILE_PATH;


 public void write(String iface, IpConfiguration config) {
        write(iface, config, APEX_IP_CONFIG_FILE_PATH + CONFIG_FILE);//注意:这里Android13调整了保存路径,全路径:/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt
        //Android 10版本保存路径为:/data/misc/ethernet/ipconfig.txt
    }



 void write(String iface, IpConfiguration config, String filepath) {
        boolean modified;

        synchronized (mSync) {
            if (config == null) {
                modified = mIpConfigurations.remove(iface) != null;
            } else {
                IpConfiguration oldConfig = mIpConfigurations.put(iface, config);
                modified = !config.equals(oldConfig);
            }

            if (modified) {
                mStore.writeIpConfigurations(filepath, mIpConfigurations);//这里调用的是IpConfigStore.java
            }
        }
    }



5、packages/modules/Connectivity/service-t/src/com/android/server/net/IpConfigStore.java
 /**
     *  Write the IP configuration associated to the target networks to the destination path.
     */
    public void writeIpConfigurations(String filePath,
                                      ArrayMap<String, IpConfiguration> networks) {
        mWriter.write(filePath, out -> {
            out.writeInt(IPCONFIG_FILE_VERSION);
            for (int i = 0; i < networks.size(); i++) {
                writeConfig(out, networks.keyAt(i), networks.valueAt(i));
            }
        });
    }



  private static boolean writeConfig(DataOutputStream out, String configKey,
            IpConfiguration config) throws IOException {
        return writeConfig(out, configKey, config, IPCONFIG_FILE_VERSION);
    }



//这里最终完成静态ip写到配置文件:/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt
  public static boolean writeConfig(DataOutputStream out, String configKey,
                                IpConfiguration config, int version) throws IOException {
        boolean written = false;

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

            switch (config.getProxySettings()) {
                case STATIC:
                    ProxyInfo proxyProperties = config.getHttpProxy();
                    String exclusionList = ProxyUtils.exclusionListAsString(
                            proxyProperties.getExclusionList());
                    out.writeUTF(PROXY_SETTINGS_KEY);
                    out.writeUTF(config.getProxySettings().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.getHttpProxy();
                    out.writeUTF(PROXY_SETTINGS_KEY);
                    out.writeUTF(config.getProxySettings().toString());
                    out.writeUTF(PROXY_PAC_FILE);
                    out.writeUTF(proxyPacProperties.getPacFileUrl().toString());
                    written = true;
                    break;
                case NONE:
                    out.writeUTF(PROXY_SETTINGS_KEY);
                    out.writeUTF(config.getProxySettings().toString());
                    written = true;
                    break;
                case UNASSIGNED:
                    /* Ignore */
                    break;
                default:
                    loge("Ignore invalid proxy settings while writing");
                    break;
            }

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

        return written;
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/555696.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

k8s 控制器StatefulSet原理解析

&#x1f407;明明跟你说过&#xff1a;个人主页 &#x1f3c5;个人专栏&#xff1a;《Kubernetes航线图&#xff1a;从船长到K8s掌舵者》 &#x1f3c5; &#x1f516;行路有良友&#xff0c;便是天堂&#x1f516; 目录 一、前言 1、k8s概述 2、有状态服务和无状态服务…

intelli J中添加maven依赖显示unable to import Maven project?如何解决??

&#x1f3c6;本文收录于「Bug调优」专栏&#xff0c;主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案&#xff0c;希望能够助你一臂之力&#xff0c;帮你早日登顶实现财富自由&#x1f680;&#xff1b;同时&#xff0c;欢迎大家关注&&收藏&&…

计算机网络练习-计算机网络体系结构与参考模型

计算机网络分层结构 ----------------------------------------------------------------------------------------------------------------------------- 1.在ISO/OSI参考模型中&#xff0c;实现两个相邻结点间流量控制功能的是( )。 A.物理层 B. 数据链路层 C.网络层 D.传…

图像分割:Pytorch实现UNet++进行医学细胞分割

图像分割&#xff1a;Pytorch实现UNet进行医学细胞分割 前言相关介绍项目结构具体步骤准备数据集读取数据集设置并解析相关参数定义网络模型定义损失函数定义优化器训练验证 参考 前言 由于本人水平有限&#xff0c;难免出现错漏&#xff0c;敬请批评改正。更多精彩内容&#x…

50-RGMIISGMIIQGMII电路设计

视频链接 RGMII&SGMII&QGMII电路设计01_哔哩哔哩_bilibili RGMII & SGMII & QSGMII电路设计 1、以太网简介&#xff08;参考第2课&#xff1a;千兆以太网电路设计&#xff09; 1.1、以太网的概述 以太网是一种计算机局域网技术。 从硬件的角度来说&#x…

祝贺十九岁的美创,一天拿了5个奖!

今天&#xff0c;和19岁的美创一起数奖&#x1f947;&#x1f947;&#x1f947; 刚刚&#xff0c;北京、杭州两地接连传来好消息—— 北京 被誉为中国IT业界延续时间最长的年度盛会——由赛迪顾问主办的2024年IT市场年会于今日隆重召开&#xff0c;备受瞩目的“首届IT创新大赛…

一份超详细的鸿蒙开发面经分享!上百道鸿蒙经典面试题整理~

鸿蒙&#xff08;HarmonyOS&#xff09;作为华为公司自主研发的全场景分布式操作系统&#xff0c;受到了广泛关注。 在面试中&#xff0c;面试官往往会关注申请人的技术能力、项目经验以及解决问题的能力。 下面是一些关于鸿蒙开发具有3年工作经验的面试题及其相关问答&#…

SpringBoot框架——8.MybatisPlus常见用法(常用注解+内置方法+分页查询)

1.MybatisPlus常用注解&#xff1a; 1.1 当数据库、表名和字段名和实体类完全一致时无需加注解&#xff0c;不一致时&#xff1a; TableName指定库名 TableId指定表名 TableField指定字段名 1.2 自增主键&#xff1a; TableId(typeIdType.AUTO) private Long id; 1.3 实体类中属…

python环境引用《解读》----- 环境隔离

首先我先讲一下Anaconda&#xff0c;因为我用的是Anaconda进行包管理。方便后面好理解一点。 大家在python中引用环境的时候都会经历下面这一步&#xff1a; 那么好多人就会出现以下问题&#xff08;我就是遇到了这个问题&#xff09;&#xff1a; 我明明下载了包&#xff0c…

编程填空题:麻烦的位运算

闲着没事干可以做做 可以看到&#xff0c;那个函数直接return了&#xff0c;也就是说&#xff0c;得找到一个表达式&#xff0c;直接求出结果 简单分析一下&#xff1a; 其第i位为1当且仅当n的右边i位均为1 也就是说&#xff0c;前i-1位有0&#xff0c;第i位就是0 也就是说…

针对springcloud gateway 跨域问题解决方案

springcloud gateway版本 <spring-boot.version>2.3.3.RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version>跨域问题说明 application:1 Access to XMLHttpRequest at https://xxxxxxxxxx from origin http://l…

创新入门|用户体验设计策略:数字化成功的蓝图

今天,我们来深入探讨如何打造令人兴奋的用户体验设计策略。将数字产品和服务从“普通”提升到“太棒了”的高度,这将是我们的主题。为什么这一策略那么重要呢?在当今被各种数字产品“轰炸”的世界,一个可靠的体验策略可能决定用户是否长期使用,还是一弹而走。我们都曾深受那种…

【Canvas技法】六种环状花纹荟萃

【图例】 【代码】 <!DOCTYPE html> <html lang"utf-8"> <meta http-equiv"Content-Type" content"text/html; charsetutf-8"/> <head><title>使用HTML5/Canvas绘制六种环状花纹</title><style type&quo…

万兆以太网10G Ethernet简介

2002年6月IEEE标准协会批准了万兆&#xff08;10G&#xff09;以太网的正式标准。此标准的全名是“10Gbit/s工作的媒体接入控制参数、物理层和管理参数”。 另一个组织是10G以太网联盟(10GEA)。10GEA由网络界的著名企业创建&#xff0c;现已有一百多家企业参加&#xff0c;中国…

林草资源管理系统:构筑绿色长城,守护自然之美

在全球气候变化和生态环境恶化的背景下&#xff0c;森林和草原资源的保护、恢复和合理利用显得尤为重要。林草资源管理系统的建立&#xff0c;旨在通过现代信息技术手段&#xff0c;提升林草资源管理的效率和质量&#xff0c;确保自然资源的可持续发展。 项目背景 森林和草原…

初学python记录:力扣705. 设计哈希集合

题目&#xff1a; 不使用任何内建的哈希表库设计一个哈希集合&#xff08;HashSet&#xff09;。 实现 MyHashSet 类&#xff1a; void add(key) 向哈希集合中插入值 key 。bool contains(key) 返回哈希集合中是否存在这个值 key 。void remove(key) 将给定值 key 从哈希集合…

基于Java+SpringBoot+Vue前后端分离精简博客系统设计和实现

基于JavaSpringBootVue前后端分离精简博客系统设计和实现 &#x1f345; 作者主页 央顺技术团队 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; &#x1f345; 文末获取源码联系方式 &#x1f4dd; &#x1f345; 查看下方微信号获取联系方式 承接各种定制系…

线程互斥,线程安全和线程同步

多线程的基本代码编写步骤 1.创建线程pthread_create() 2.终止线程的三种方法。线程取消pthread_cancel(一般在主线程取消)&#xff0c; 线程终止pthread_exit(在其他线程执行)&#xff0c; 或者使用线程返回return 3.线程等待pthread_join 需要等待的原因是 1.已经退出的线程…

WordPress的全面解析:为什么它是创建博客和网站的首选

在当前的数字化时代&#xff0c;无论是个人博客还是企业网站&#xff0c;都需要一个强大而灵活的平台以支撑其内容和用户交互。WordPress作为全球最流行的内容管理系统&#xff08;CMS&#xff09;&#xff0c;以其强大的功能、灵活的定制性和广泛的用户基础&#xff0c;成为了…

交通管理在线服务系统|基于Springboot的交通管理系统设计与实现(源码+数据库+文档)

交通管理在线服务系统目录 目录 基于Springboot的交通管理系统设计与实现 一、前言 二、系统功能设计 三、系统实现 1、用户信息管理 2、驾驶证业务管理 3、机动车业务管理 4、机动车业务类型管理 四、数据库设计 1、实体ER图 五、核心代码 六、论文参考 七、最新计…
最新文章