Android ndc cmd note
ndc是android的NativeDaemonConnector提供framework與底層溝通, 在每一個版本中支援的指令不近相同, 像是這裡有彙整ndc resolver的指令, 另外在這篇有畫大致的架構圖
https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/services/java/com/android/server/ConnectivityService.java
public ConnectivityService(Context context, INetworkManagementService netManager,
mNetd = checkNotNull(netManager, "missing INetworkManagementService");
if (changed) {
try {
mNetd.setDnsServersForInterface(iface, NetworkUtils.makeStrings(dnses));
mNetd.setDefaultInterfaceForDns(iface);
} catch (Exception e) {
if (DBG) loge("exception setting default dns interface: " + e);
}
}
而NetworkManagementService再透過ndc調整網路設定
mConnector = new NativeDaemonConnector(
new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
例如設定指定網路介面用的DNS伺服器
@Override
public void setDnsServersForInterface(String iface, String[] servers) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
final Command cmd = new Command("resolver", "setifdns", iface);
for (String s : servers) {
InetAddress a = NetworkUtils.numericToInetAddress(s);
if (a.isAnyLocalAddress() == false) {
cmd.appendArg(a.getHostAddress());
}
}
try {
mConnector.execute(cmd);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
}
ndc是用system/netd的CommandListener來處理指令
https://android.googlesource.com/platform/system/netd/+/3d4c7585e35a93d9608fce8cc056b7eee9123a53/CommandListener.cpp
} else if (!strcmp(argv[1], "setifdns")) {
// "resolver setifdns
if (argc >= 5) {
rc = sResolverCtrl->setInterfaceDnsServers(argv[2], argv[3], &argv[4], argc - 4);
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Wrong number of arguments to resolver setifdns", false);
return 0;
}
ndc的一些指令,
ndc resolver setnetdns [ ...] [--params ]
ndc resolver clearnetdns
ndc network interface add
ndc network interface remove
ndc network create [permission]
ndc network create vpn
ndc network destroy
ndc network default set
用ndc monitor指令可以觀察ndc被呼叫的狀況, 下面是無線網路開啟的網路變動
# ndc monitor
[Connected to Netd]
600 Iface added wlan0
600 Iface linkstate wlan0 down
600 Iface added p2p0
600 Iface linkstate p2p0 down
600 Iface linkstate wlan0 down
600 Iface linkstate p2p0 down
600 Iface linkstate p2p0 down
600 Iface linkstate p2p0 down
600 Iface linkstate wlan0 down
600 Iface linkstate p2p0 down
600 Iface linkstate wlan0 down
600 Iface linkstate wlan0 down
600 Iface linkstate wlan0 down
600 Iface linkstate wlan0 down
600 Iface linkstate wlan0 down
600 Iface linkstate wlan0 down
600 Iface linkstate wlan0 down
600 Iface linkstate wlan0 down
600 Iface linkstate wlan0 up
600 Iface linkstate wlan0 up
600 Iface linkstate wlan0 up
600 Iface linkstate wlan0 up
600 Iface linkstate wlan0 up
616 Route updated fe80::/64 dev wlan0
614 Address updated fe80::27e:56ff:fe50:47/64 wlan0 128 253
616 Route updated ::/0 via fe80::482c:a0ff:fef3:ea3b dev wlan0
616 Route updated 2001:b400:e285:40d1::/64 dev wlan0
615 DnsInfo servers wlan0 3600 2001:b400:e285:40d1::ed
614 Address updated 2001:b400:e285:40d1:27e:56ff:fe50:47/64 wlan0 0 0
614 Address updated 2001:b400:e285:40d1:9096:3f82:b8fb:fffa/64 wlan0 1 0
614 Address updated 192.168.43.167/24 wlan0 128 0
600 Iface linkstate wlan0 up
600 Iface linkstate wlan0 up
留言