You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android环境下获取默认网络接口及inet地址的方法咨询

Android NDK: Network Interface & Routing Questions

Alright, let's break down your questions since you can't use getifaddrs() in the Android NDK (no ifaddrs.h header available). Here's practical, native-code friendly solutions:


1. How to get the default network interface (ppp0) and its IPv4 address

First, let's confirm: your "default interface" is ppp0, which shows up as the lowest-metric default route in your routing table. Here's how to fetch this in native code:

Step 1: Identify the default interface (ppp0)

You can parse the /proc/net/route file—this is a read-only system file that exposes the kernel routing table. Look for entries where the destination is 0.0.0.0 (hex 00000000), then pick the one with the lowest metric (hex 00000000 for ppp0 in your case).

Here's a simple C snippet to do this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char* get_default_interface() {
    FILE* route_file = fopen("/proc/net/route", "r");
    if (!route_file) return NULL;

    char line_buf[256];
    // Skip the header line
    fgets(line_buf, sizeof(line_buf), route_file);

    while (fgets(line_buf, sizeof(line_buf), route_file)) {
        char iface_name[32], dest_hex[16], metric_hex[16];
        // Parse key columns: Interface Name, Destination (hex), Metric (hex)
        if (sscanf(line_buf, "%s %s %*s %*s %*s %s", iface_name, dest_hex, metric_hex) == 3) {
            // Check if this is the default route (0.0.0.0)
            if (strcmp(dest_hex, "00000000") == 0) {
                // Check for the lowest metric (0, stored as hex 00000000)
                if (strcmp(metric_hex, "00000000") == 0) {
                    char* result = malloc(strlen(iface_name) + 1);
                    strcpy(result, iface_name);
                    fclose(route_file);
                    return result;
                }
            }
        }
    }

    fclose(route_file);
    return NULL;
}

Step 2: Fetch the IPv4 address for ppp0

Once you have the interface name, use ioctl with SIOCGIFADDR to get its IPv4 address—this works in the NDK without ifaddrs.h.

Here's the code snippet:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

char* get_interface_ipv4(const char* iface) {
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) return NULL;

    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);

    if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) {
        struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr;
        char* ip_str = malloc(INET_ADDRSTRLEN);
        inet_ntop(AF_INET, &addr->sin_addr, ip_str, INET_ADDRSTRLEN);
        close(sock);
        return ip_str;
    }

    close(sock);
    return NULL;
}

Just call get_default_interface() first to get "ppp0", then pass that to get_interface_ipv4() to get its IPv4 address.


2. Which interface is used by default for WAN (non-LAN) access?

Looking at your provided routing table:

Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 ppp0
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 wlp3s0
172.30.0.1 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 wlp3s0

For WAN traffic (any address not in the 192.168.1.0/24 LAN subnet), your system will default to ppp0. Here's why:

  • Linux uses the route with the lowest Metric value when multiple default routes exist. ppp0's default route has a Metric of 0, which is lower than wlp3s0's Metric of 100.
  • LAN addresses (192.168.1.x) will use wlp3s0, but all other traffic (WAN) falls back to the ppp0 default route since it has higher priority.

内容的提问来源于stack exchange,提问作者RevolverOcelot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:53:18