1、openharmony静态IP配置

方式一:uboot设置

#如果是uboot启动,上电后,按回车键进入uboot界面,进行如下的参数设置,具体参数根据网络环境设置。
setenv ipaddr 192.168.1.22 #开发板 IP地址
setenv ethaddr 00:04:9f:04:d2:35 #MAC地址
setenv gatewayip 192.168.1.1 #网关
setenv netmask 255.255.255.0 #子网掩码
setenv serverip 192.168.1.21 #服务器地址
saveenv

方式二:命令行配置(临时)

ifconfig eth0 192.168.3.23 netmask 255.255.255.0
route add default gw 192.168.3.1
/etc/resolv.conf  #通过挂载文件系统或者hdc文件传输的方式,添加DNS的地址192.168.3.1

方式三:配置文件配置

/foundation/communication/netmanager_ext/services/ethernetmanager/config/ethernet_interfaces.json文件进行IP配置,重新编译烧录后即可。
#cat /system/etc/communication/netmanager_ext/ethernet_interfaces.json
{
  "config_ethernet_interfaces":[
    {
      "iface":"eth0",
      "caps":[
      ],
      "ip":"192.168.3.88",
      "gateway":"192.168.3.1",
      "dns":"192.168.3.1",
      "netmask":"255.255.255.0",
      "route":"0.0.0.0,192.168.3.0",
      "routemask":"0.0.0.0,0.0.0.0"
    },
    {
      "iface":"eth1",
      "caps":[
      ],
      "ip":"192.168.3.89",
      "gateway":"192.168.3.1",
      "dns":"192.168.3.1",
      "netmask":"255.255.255.0",
      "route":"0.0.0.0,192.168.3.0",
      "routemask":"0.0.0.0,0.0.0.0"
    }
  ]
}

方式四:应用配置

oh应用通过netManage子系统提供的NAPI接口进行网络配置,oh目前没有提供参考hap,以下是博客上提供的参考hap。
参考链接:
https://blog.csdn.net/Devlin_/article/details/138124882

2、OpenHarmony 动态IP配置

2.1 在路由器环境下,openharmony通过DHCP自动配置网络信息,ifconfig查看配置信息:

# ifconfig
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0 
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 

eth0      Link encap:Ethernet  HWaddr 24:dc:0f:1d:c2:64  Driver macb
          inet addr:192.168.3.23  Bcast:192.168.3.255  Mask:255.255.255.0 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:149 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:232 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:21766 TX bytes:47821 
          Interrupt:92 Base address:0xc000 

eth1      Link encap:Ethernet  HWaddr 24:dc:0f:1d:c2:65  Driver macb
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 
          Interrupt:100 Base address:0xe000

ifconfig通过ioctl系统调用与内核通信,读取并显示网络接口的配置信息。它打开一个套接字,发送各种ioctl命令来获取网络接口的状态和配置参数,然后将这些信息格式化并输出到终端。通过ifconfig命令来设置ip,并没有保存在文件中,所以掉电后ip配置也会擦除。

2.2 查看路由表信息:

# cat /proc/net/fib_trie                                                       
Main:
  +-- 0.0.0.0/0 3 0 5
     |-- 0.0.0.0
        /0 universe UNICAST
     +-- 127.0.0.0/8 2 0 2
        +-- 127.0.0.0/31 1 0 0
           |-- 127.0.0.0
              /32 link BROADCAST
              /8 host LOCAL
           |-- 127.0.0.1
              /32 host LOCAL
        |-- 127.255.255.255
           /32 link BROADCAST
     +-- 192.168.3.0/24 2 0 1
        |-- 192.168.3.0
           /32 link BROADCAST
           /24 link UNICAST
        |-- 192.168.3.123
           /32 host LOCAL
        |-- 192.168.3.255
           /32 link BROADCAST
Local:
  +-- 0.0.0.0/0 3 0 5
     |-- 0.0.0.0
        /0 universe UNICAST
     +-- 127.0.0.0/8 2 0 2
        +-- 127.0.0.0/31 1 0 0
           |-- 127.0.0.0
              /32 link BROADCAST
              /8 host LOCAL
           |-- 127.0.0.1
              /32 host LOCAL
        |-- 127.255.255.255
           /32 link BROADCAST
     +-- 192.168.3.0/24 2 0 1
        |-- 192.168.3.0
           /32 link BROADCAST
           /24 link UNICAST
        |-- 192.168.3.23
           /32 host LOCAL
        |-- 192.168.3.255
           /32 link BROADCAST

/proc/net/fib_trie 是 Linux 系统中的一个虚拟文件,用于显示系统内核中路由表的 FIB (Forwarding Information Base) 结构。FIB 是一种存储和查找 IP 路由信息的数据结构,主要用于加快路由查找的速度。在这个文件中,可以看到与路由相关的信息,例如路由表项、网络接口、目的地址等。这个文件是只读的,不可修改,用于显示当前内核中的路由信息,供系统管理员和调试工具查看。

2.3 openharmony通过hidumper命令查看网络信息

# hidumper -s EthernetManager -a -h
----------------------------------EthernetManager---------------------------------
DevInterfaceState: 
    DevName: eth0
    ConnLinkState: 3
    LinkUp: 0
    DHCPReqState: 0
  [NetLinkInfo]  ifaceName_ = eth0  domain_ =     netAddrList_ =   [INetAddr]  type = 0  family = 0  prefixLength = 24  address = 192.168.*.**  netMask = 255.255.255.0  hostName =   port = 0    dnsList_ = null 0
  gateway_ =   [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 0.0.*.*  netMask =   hostName =   port = 0
  rtnType_ = 1
  mtu_ = 0
  isHost_ = false
  hasGateway_ = true
  isDefaultRoute = falserouteList_ =   mtu_ = 0  tcpBufferSizes_ =   httpProxy =        0
  [NetSupplierInfo]  isAvailable_ = 0  isRoaming_ = 0  strength_ = 0  frequency_ = 0  linkUpBandwidthKbps_ = 0  linkDownBandwidthKbps_ = 0  uid_ = 0

    InterfaceConfig: 
      Mode: 0

Config: 
    IpAddr:       [INetAddr]  type = 0  family = 0  prefixLength = 24  address = 192.168.*.**  netMask = 255.255.255.0  hostName =   port = 0
    Route: 
    GateWay:       [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 192.168.*.***  netMask =   hostName =   port = 0
    NetMask:       [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 255.255.***.*  netMask =   hostName =   port = 0
    DNSServers: 
    Domain: 
    NetCaps: {12, }
    BearerType :3
DevInterfaceState: 
    DevName: eth1
    ConnLinkState: 2
    LinkUp: 1
    DHCPReqState: 0
  [NetLinkInfo]  ifaceName_ = eth1  domain_ =     netAddrList_ =   [INetAddr]  type = 0  family = 0  prefixLength = 24  address = 192.168.*.**  netMask = 255.255.255.0  hostName =   port = 0    dnsList_ =   [IN0
  gateway_ =   [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 192.168.*.*  netMask =   hostName =   port = 0
  rtnType_ = 1
  mtu_ = 0
  isHost_ = false
  hasGateway_ = true
  isDefaultRoute = false  [Route]  iface_ = eth1  destination_ =   [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 192.168.*.*  netMask =   hostName =   port = 0
  gateway_ =   [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 192.168.*.*  netMask =   hostName =   port = 0
  rtnType_ = 1
  mtu_ = 0
  isHost_ = false
  hasGateway_ = true
  isDefaultRoute = false  [Route]  iface_ = eth1  destination_ =   [INetAddr]  type = 2  family = 0  prefixLength = 24  address = 192.168.*.*  netMask =   hostName =   port = 0
  gateway_ =   [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 0.0.*.*  netMask =   hostName =   port = 0
  rtnType_ = 1
  mtu_ = 0
  isHost_ = false
  hasGateway_ = true
  isDefaultRoute = falserouteList_ =   mtu_ = 0  tcpBufferSizes_ =   httpProxy =        0
  [NetSupplierInfo]  isAvailable_ = 1  isRoaming_ = 0  strength_ = 0  frequency_ = 0  linkUpBandwidthKbps_ = 0  linkDownBandwidthKbps_ = 0  uid_ = 0

    InterfaceConfig: 
      Mode: 0

Config: 
    IpAddr:       [INetAddr]  type = 0  family = 0  prefixLength = 24  address = 192.168.*.**  netMask = 255.255.255.0  hostName =   port = 0
    Route:       [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 0.0.*.*  netMask =   hostName =   port = 0      [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 192.168.*.*  netMask 0
    GateWay:       [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 192.168.*.*  netMask =   hostName =   port = 0      [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 192.168.*.*  ne0
    NetMask:       [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 255.255.***.*  netMask =   hostName =   port = 0
    DNSServers:       [INetAddr]  type = 0  family = 0  prefixLength = 0  address = 192.168.*.*  netMask =   hostName =   port = 0
    Domain: 
    NetCaps: {12, }
    BearerType :3

2.4 DHCP代码分析

openharmony netManage子系统源代码位于//foundation/communication/netmanager_base,netManage子系统会通过setIfaceConfig接口将getIfaceConfig接口获取到的ip信息写入/data/service/el1/public/netmanager/ethernet/这个路径下。

代码调用逻辑:从内核进行获取通过ioctl的方式Iface信息:
InterfaceConfigurationParcel InterfaceManager::GetIfaceConfig(const std::string &ifName)
{
    NETNATIVE_LOGI("GetIfaceConfig in. ifName %{public}s", ifName.c_str());
    struct in_addr addr = {};
    nmd::InterfaceConfigurationParcel ifaceConfig;

    int fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
    struct ifreq ifr = {};
    strncpy_s(ifr.ifr_name, IFNAMSIZ, ifName.c_str(), ifName.length());

    ifaceConfig.ifName = ifName;
    if (ioctl(fd, SIOCGIFADDR, &ifr) != -1) {
        addr.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
        ifaceConfig.ipv4Addr = std::string(inet_ntoa(addr));
    }
    if (ioctl(fd, SIOCGIFNETMASK, &ifr) != -1) {
        ifaceConfig.prefixLength = Ipv4NetmaskToPrefixLength(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);
    }
    if (ioctl(fd, SIOCGIFFLAGS, &ifr) != -1) {
        UpdateIfaceConfigFlags(ifr.ifr_flags, ifaceConfig);
    }
    if (ioctl(fd, SIOCGIFHWADDR, &ifr) != -1) {
        ifaceConfig.hwAddr = HwAddrToStr(ifr.ifr_hwaddr.sa_data);
    }
    close(fd);
    return ifaceConfig;
}

设置Iface配置信息:
void Network::UpdateIpAddrs(const NetLinkInfo &netLinkInfo) #更新ip信息
bool ExecSetIfaceConfig(SetIfaceConfigContext *context)
int32_t EthernetService::SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic) #设置ip配置
int32_t EthernetManagement::UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg)
bool EthernetConfiguration::WriteUserConfiguration(const std::string &iface, sptr<InterfaceConfiguration> &cfg)

bool EthernetConfiguration::WriteUserConfiguration(const std::string &iface, sptr<InterfaceConfiguration> &cfg)
{
    if (cfg == nullptr) {
        NETMGR_EXT_LOG_E("cfg is nullptr");
        return false;
    }
    if (!CreateDir(USER_CONFIG_DIR)) {
        NETMGR_EXT_LOG_E("create dir failed");
        return false;
    }

    if (cfg->mode_ == STATIC) {
        ParserIfaceIpAndRoute(cfg, std::string());
    }

    std::string fileContent;
    GenCfgContent(iface, cfg, fileContent);

    std::string filePath = std::string(USER_CONFIG_DIR) + FILE_OBLIQUE_LINE + iface; //IP信息写入该地址
    return WriteFile(filePath, fileContent);
}

USER_CONFIG_DIR = \"/data/service/el1/public/netmanager/ethernet\"

2.5 /data/service/el1/public/netmanager/ethernet/路径下查看ip信息:

# cat /data/service/el1/public/netmanager/ethernet/eth0
DEVICE=eth0
BOOTPROTO=STATIC
IPADDR=192.168.3.23
NETMASK=255.255.255.0
GATEWAY=192.168.3.1,192.168.3.1
ROUTE=0.0.0.0,192.168.3.0
ROUTE_NETMASK=0.0.0.0,0.0.0.0
DNS=192.168.3.1
PROXY_HOST=
PROXY_PORT=0
PROXY_EXCLUSIONS=

可以通过挂载文件系统或者用hdc file send的方式对该文件进行修改达到手动修改IP的目的。

ps:应用就是通过这种方式将ip配置信息保存在/data/service/el1/public/netmanager/ethernet/路径下,所以掉电之后ip配置信息还存在。

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐