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

devtools/2024/10/23 1:00:30/

源码环境:高通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*/@Overridepublic 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创建interfacevoid 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中的writemIpConfigurations.put(iface, ipConfig);}4、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetConfigStore.javaprivate 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.txtpublic 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.ppmy.cn/devtools/5897.html

相关文章

Excel文件转Asc文件

单个转换 import os import pandas as pdfilename (10)result01-1.xlsx df pd.read_excel(filename) # 读取Excel文件# 将数据保存为ASC格式 asc_filename os.path.splitext(filename)[0] .asc # 获取文件名并替换扩展名 with open(asc_filename, w) as file:# 写入文件…

设计模式(023)行为型之中介者模式

中介者模式是一种行为型设计模式&#xff0c;用于减少对象之间的直接通信&#xff0c;而是通过一个中介对象来进行间接通信。这种模式有助于减少对象之间的耦合&#xff0c;使得系统更易于维护和扩展。 在中介者模式中&#xff0c;主要有以下几个角色&#xff1a;① 中介者&…

设计模式学习(七)——《大话设计模式》

文章目录 设计模式学习&#xff08;七&#xff09;——《大话设计模式》工作原理工作流程示例 工作策略模式的应用场景策略模式的优点策略模式的缺点示例代码&#xff08;Python&#xff09; 策略模式UML类图具体应用和使用场景支付方式选择数据压缩工具表单验证路由算法日志记…

IaC:实现持续交付和 DevOps 自动化的关键

基础架构即代码&#xff08;IaC&#xff09;和 CI/CD 流水线最初似乎并不匹配。因为它们代表了两种不同的流程。IaC 主要关注基础设施的配置和开发&#xff0c;而 CI/CD 则围绕软件开发、测试和部署。 然而&#xff0c;将 IaC 集成到 CI/CD 流水线中具有多种优势。首先&#xf…

对组合模式的理解

目录 一、场景1、题目描述 【[案例来源](https://kamacoder.com/problempage.php?pid1090)】2、输入描述3、输出描述4、输入示例5、输出示例 二、实现&#xff08;假的组合模式&#xff09;1、代码2、为什么上面的写法是假的组合模式&#xff1f; 三、实现&#xff08;真的组合…

十分钟快速制作一个俄罗斯方块桌面游戏

准备 安装 Python: 下载 Python 安装程序: 访问 Python 官方网站,在下载页面选择适合您操作系统的 Python 版本。通常推荐下载最新版本。 运行安装程序: 下载完成后,运行下载的安装程序。在安装过程中,请确保勾选“Add Python X.X to PATH”选项(X.X 代表您下载的 Pyth…

模块与包及json模块学习

【一】模块与包介绍 【1】什么是模块 在Python中&#xff0c;一个py文件其实就是一个模块 文件名 knight.py中 py就是模块名 【2】模块的优点 有了模块以后可以增加程序的可读性&#xff0c;提高开发效率 【3】模块的来源 &#xff08;1&#xff09;在Python解释器内部内…

MySQL数据库企业级开发技术(下篇)

使用语言 MySQL 使用工具 Navicat Premium 16 代码能力快速提升小方法&#xff0c;看完代码自己敲一遍&#xff0c;十分有用 拖动表名到查询文件中就可以直接把名字拉进来中括号&#xff0c;就代表可写可不写 目录 1. 视图 1.1 需要视图的原因 1.2 视图介绍 1.2.1 …