From 5513b6b923eea32661c95360a4734ca1d703bd6c Mon Sep 17 00:00:00 2001 From: puhaodong Date: Sat, 30 Aug 2025 11:41:01 +0800 Subject: [PATCH] add ipconfigstore --- .../system/core/system-core-0001.patch | 277 +++++++++++++++++- 1 file changed, 276 insertions(+), 1 deletion(-) diff --git a/patchForAndroid15/system/core/system-core-0001.patch b/patchForAndroid15/system/core/system-core-0001.patch index c4beea6..e3b8ec7 100644 --- a/patchForAndroid15/system/core/system-core-0001.patch +++ b/patchForAndroid15/system/core/system-core-0001.patch @@ -186,6 +186,281 @@ index 6c8089926..36fa8e4b7 100644 LOG(INFO) << "init second stage started!"; SelinuxSetupKernelLogging(); +diff --git a/init/ipconfigstore/Android.bp b/init/ipconfigstore/Android.bp +new file mode 100644 +index 000000000..34ed932dd +--- /dev/null ++++ b/init/ipconfigstore/Android.bp +@@ -0,0 +1,8 @@ ++cc_binary { ++ name: "ipconfigstore", ++ srcs: ["*.cc"], ++ cflags: ["-Wall", "-Werror"], ++ shared_libs: ["libbase"], ++ vendor: true, ++} ++ +diff --git a/init/ipconfigstore/data.cc b/init/ipconfigstore/data.cc +new file mode 100644 +index 000000000..b87e77069 +--- /dev/null ++++ b/init/ipconfigstore/data.cc +@@ -0,0 +1,65 @@ ++#include ++#include ++#include ++#include ++#include ++ ++#include "data.h" ++ ++uint16_t convertBigEndianUInt16(uint16_t value) ++{ ++ union { uint16_t value; unsigned char data[2]; } aux = { 0x4142 }; ++ ++ if (aux.data[0] == 0x41) ++ { ++ return value; ++ } ++ ++ aux.data[0] = (value >> 8) & 0xff; ++ aux.data[1] = value & 0xff; ++ ++ return aux.value; ++} ++ ++uint32_t convertBigEndianUInt32(uint32_t value) ++{ ++ union { uint32_t value; unsigned char data[4]; } aux = { 0x41424344 }; ++ ++ if (aux.data[0] == 0x41) ++ { ++ return value; ++ } ++ ++ aux.data[0] = (value >> 24) & 0xff; ++ aux.data[1] = (value >> 16) & 0xff; ++ aux.data[2] = (value >> 8) & 0xff; ++ aux.data[3] = value & 0xff; ++ ++ return aux.value; ++} ++ ++bool writePackedString(const std::string& str, FILE *stream) ++{ ++ const char *string = str.c_str(); ++ size_t stringLength = strlen(string); ++ ++ if (!writePackedUInt16(stringLength, stream)) ++ { ++ return false; ++ } ++ ++ return fwrite(string, stringLength, 1, stream) == 1; ++} ++ ++bool writePackedUInt16(uint16_t value, FILE *stream) ++{ ++ uint16_t buffer = convertBigEndianUInt16(value); ++ return fwrite(&buffer, sizeof buffer, 1, stream) == 1; ++} ++ ++bool writePackedUInt32(uint32_t value, FILE *stream) ++{ ++ uint32_t buffer = convertBigEndianUInt32(value); ++ return fwrite(&buffer, sizeof buffer, 1, stream) == 1; ++} ++ +diff --git a/init/ipconfigstore/data.h b/init/ipconfigstore/data.h +new file mode 100644 +index 000000000..12c3a1af9 +--- /dev/null ++++ b/init/ipconfigstore/data.h +@@ -0,0 +1,13 @@ ++#pragma once ++ ++#include ++#include ++ ++#include ++ ++uint16_t convertBigEndianUInt16(uint16_t value); ++uint32_t convertBigEndianUInt32(uint32_t value); ++ ++bool writePackedString(const std::string& str, FILE *stream); ++bool writePackedUInt16(uint16_t value, FILE *stream); ++bool writePackedUInt32(uint32_t value, FILE *stream); +diff --git a/init/ipconfigstore/main.cc b/init/ipconfigstore/main.cc +new file mode 100644 +index 000000000..ef4a2a5ae +--- /dev/null ++++ b/init/ipconfigstore/main.cc +@@ -0,0 +1,165 @@ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include ++ ++#include ++ ++#include "data.h" ++ ++using namespace android::base; ++ ++struct ipconfig { ++ uint32_t mask; ++ char ipv4[16]; ++ char gateway[16]; ++}; ++ ++static int get_gateway(char *dev, char *ret) { ++ FILE *fp; ++ char buf[256]; // 128 is enough for linux ++ char iface[16]; ++ unsigned long dest_addr, gate_addr; ++ fp = fopen("/proc/net/route", "r"); ++ if (fp == NULL) return -1; ++ /* Skip title line */ ++ fgets(buf, sizeof(buf), fp); ++ while (fgets(buf, sizeof(buf), fp)) { ++ if (sscanf(buf, "%s\t%lX\t%lX", iface, &dest_addr, &gate_addr) != 3 || dest_addr != 0 || strcmp(dev, iface)) continue; ++ inet_ntop(AF_INET, &gate_addr, ret, INET_ADDRSTRLEN); ++ break; ++ } ++ ++ fclose(fp); ++ return 0; ++} ++ ++static int bitcount(uint32_t n) ++{ ++ int count=0; ++ while (n) { ++ count++; ++ n &= (n - 1); ++ } ++ return count; ++} ++ ++static int get_conf(struct ipconfig *conf) { ++ struct ifaddrs *ifAddrStruct; ++ void *tmpAddrPtr=NULL; ++ getifaddrs(&ifAddrStruct); ++ while (ifAddrStruct != NULL) { ++ if (ifAddrStruct->ifa_addr->sa_family==AF_INET && !strcmp("eth0", ifAddrStruct->ifa_name)) { ++ tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; ++ inet_ntop(AF_INET, tmpAddrPtr, conf->ipv4, INET_ADDRSTRLEN); ++ conf->mask = bitcount(((struct sockaddr_in *)ifAddrStruct->ifa_netmask)->sin_addr.s_addr); ++ break; ++ } ++ ifAddrStruct=ifAddrStruct->ifa_next; ++ } ++ freeifaddrs(ifAddrStruct); ++ get_gateway((char *) "eth0", conf->gateway); ++ return 0; ++} ++ ++static void write_dns(FILE *fp) { ++ std::set dnsList; ++ auto ndns = GetIntProperty("ro.boot.kbox_net_ndns", 0); ++ for (int i = 1; i <= ndns; ++i) { ++ dnsList.insert(GetProperty("ro.boot.kbox_net_dns" + std::to_string(i), "")); ++ } ++ if (dnsList.empty()) dnsList.insert("8.8.8.8"); ++ ++ for (auto& dns: dnsList) { ++ writePackedString("dns", fp); ++ writePackedString(dns.c_str(), fp); ++ } ++} ++ ++static void write_proxy(FILE *fp) { ++ // static | pac | none | unassigned ++ std::string proxy_type = GetProperty("ro.boot.kbox_net_proxy_type", ""); ++ if ("static" == proxy_type) { ++ writePackedString("proxySettings", fp); ++ writePackedString("STATIC", fp); ++ ++ writePackedString("proxyHost", fp); ++ writePackedString(GetProperty("ro.boot.kbox_net_proxy_host", "").c_str(), fp); ++ ++ writePackedString("proxyPort", fp); ++ writePackedUInt32(GetIntProperty("ro.boot.kbox_net_proxy_port", 3128), fp); ++ ++ writePackedString("exclusionList", fp); ++ writePackedString(GetProperty("ro.boot.kbox_net_proxy_exclude_list", "").c_str(), fp); ++ } else if ("pac" == proxy_type) { ++ writePackedString("proxySettings", fp); ++ writePackedString("PAC", fp); ++ ++ writePackedString("proxyPac", fp); ++ writePackedString(GetProperty("ro.boot.kbox_net_proxy_pac", "").c_str(), fp); ++ } else if ("none" == proxy_type) { ++ writePackedString("proxySettings", fp); ++ writePackedString("NONE", fp); ++ } else { ++ // ignored ++ } ++} ++ ++static int write_conf(struct ipconfig *conf, uint32_t v) { ++ FILE *fp = fopen("/data/misc/ethernet/ipconfig.txt", "w+"); ++ ++ writePackedUInt32(v, fp); // version ++ ++ writePackedString("ipAssignment", fp); ++ writePackedString("STATIC", fp); ++ ++ writePackedString("linkAddress", fp); ++ writePackedString(conf->ipv4, fp); ++ writePackedUInt32(conf->mask, fp); ++ ++ writePackedString("gateway", fp); ++ writePackedUInt32(1, fp); // Default route (dest). ++ writePackedString("0.0.0.0", fp); ++ writePackedUInt32(0, fp); ++ writePackedUInt32(1, fp); // Have a gateway. ++ writePackedString(conf->gateway, fp); ++ ++ write_dns(fp); ++ ++ write_proxy(fp); ++ ++ writePackedString("id", fp); ++ if (v == 2) writePackedUInt32(0, fp); ++ else writePackedString("eth0", fp); ++ ++ writePackedString("eos", fp); ++ ++ fclose(fp); ++ return 0; ++} ++ ++int main(int argc, char **argv) { ++ (void)argc; ++ (void)argv; ++ ++ uint32_t v = 3; ++ // use V2 for Android 8.1 ++ if (GetIntProperty("ro.build.version.sdk", 0) <= 27) v = 2; ++ ++ struct ipconfig conf; ++ get_conf(&conf); ++ printf("ipconfig: ipv4: %s, mask: %i, gateway: %s", conf.ipv4, conf.mask, conf.gateway); ++ write_conf(&conf, v); ++ return 0; ++} ++ diff --git a/init/property_service.cpp b/init/property_service.cpp index f2606e3c5..62f9c34ad 100644 --- a/init/property_service.cpp @@ -330,7 +605,7 @@ index 1acd63774..12d696a6d 100644 # Mount default storage into root namespace mount none /mnt/user/0 /storage bind rec diff --git a/rootdir/init.usb.rc b/rootdir/init.usb.rc -index b30d6d02c..e9d84b63a 100644 +index b30d6d02c..4dd943149 100644 --- a/rootdir/init.usb.rc +++ b/rootdir/init.usb.rc @@ -27,9 +27,10 @@ on property:vendor.sys.usb.adb.disabled=* -- Gitee