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

2024-04-18 01:52

本文主要是介绍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*/@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;}

这篇关于Android 13 有线以太网静态ip保存逻辑梳理分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/913456

相关文章

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺