NetworkManagementService 的listInterfaces方法代码 如下:
//
// INetworkManagementService members
//
@Override
public String[] listInterfaces() {
// TODO: Remove CONNECTIVITY_INTERNAL after bluetooth tethering has no longer called these
// APIs.
NetworkStack.checkNetworkStackPermissionOr(mContext, CONNECTIVITY_INTERNAL);
try {
return mNetdService.interfaceGetList();
} catch (RemoteException | ServiceSpecificException e) {
throw new IllegalStateException(e);
}
}
是调用NetdService.interfaceGetList 方法。
而NetdService是在native层注册的一个服务 ,其代码如下:
binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
const auto& ifaceList = InterfaceController::getIfaceNames();
interfaceListResult->clear();
interfaceListResult->reserve(ifaceList.value().size());
interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
end(ifaceList.value()));
return binder::Status::ok();
}
是调用InterfaceController::getIfaceNames() 方法,其代码如下:
StatusOr<std::vector<std::string>> InterfaceController::getIfaceNames() {
std::vector<std::string> ifaceNames;
DIR* d;
struct dirent* de;
if (!(d = opendir("/sys/class/net"))) {
return statusFromErrno(errno, "Cannot open iface directory");
}
while ((de = readdir(d))) {
if ((de->d_type != DT_DIR) && (de->d_type != DT_LNK)) continue;
if (de->d_name[0] == '.') continue;
ifaceNames.push_back(std::string(de->d_name));
}
closedir(d);
return ifaceNames;
}
是直接读取设备的 /sys/class/net目录 。
adb shell ls /sys/class/net
输出如下:
dummy0
eth0
ip6_vti0
ip6tnl0
ip_vti0
lo
p2p0
sit0
wlan0