在前面文章介绍了其自身定义的接口功能提供给客户端:https://blog.youkuaiyun.com/Vince_/article/details/115203280
在与底层 netd 通信过程中实际上采用了 NetdUnsolicitedEventListener 内部类定义的接口进行,它自身作为服务端,netd 作为客户端对该功能进行引用调用。
NetdUnsolicitedEventListener 的 定义在 NetworkManagementService.java 中,不过其继承实现了接口类 INetdUnsolicitedEventListener.Stub,其定义如下:
文件:system/netd/server/binder/android/net/INetdUnsolicitedEventListener.aidl
/**
* Unsolicited netd events which are reported by the kernel via netlink.
* This one-way interface groups asynchronous notifications sent
* by netd to any process that registered itself via INetd.registerUnsolEventListener.
*
* {@hide}
*/
oneway interface INetdUnsolicitedEventListener {/**
* Notifies that an interface has been idle/active for a certain period of time.
* It is the event for idletimer.
*
* @param isActive true for active status, false for idle
* @param timerLabel unique identifier of the idletimer.
* Since NMS only set the identifier as int, only report event with int label.
* @param timestampNs kernel timestamp of this event, 0 for no timestamp
* @param uid uid of this event, -1 for no uid.
* It represents the uid that was responsible for waking the radio.
*/
void onInterfaceClassActivityChanged(
boolean isActive,
int timerLabel,
long timestampNs,
int uid);/**
* Notifies that a specific interface reached its quota limit.
*
* @param alertName alert name of the quota limit
* @param ifName interface which reached the limit
*/
void onQuotaLimitReached(@utf8InCpp String alertName, @utf8InCpp String ifName);/**
* Provides information on IPv6 DNS servers on a specific interface.
*
* @param ifName interface name
* @param lifetimeS lifetime for the DNS servers in seconds
* @param servers the address of servers.
* e.g. IpV6: "2001:4860:4860::6464"
*
*/
void onInterfaceDnsServerInfo(
@utf8InCpp String ifName, long lifetimeS, in @utf8InCpp String[] servers);/**
* Notifies that an address has updated on a specific interface.
*
* @param addr address that is being updated
* @param ifName the name of the interface on which the address is configured
* @param flags address flags, see ifa_flags in if_addr.h
* @param scope current scope of the address
*/
void onInterfaceAddressUpdated(
@utf8InCpp String addr,
@utf8InCpp String ifName,
int flags,
int scope);/**
* Notifies that an address has been removed on a specific interface.
*
* @param addr address of this change
* @param ifName the name of the interface that changed addresses
* @param flags address flags, see ifa_flags in if_addr.h
* @param scope address address scope
*/
void onInterfaceAddressRemoved(
@utf8InCpp String addr,
@utf8InCpp String ifName,
int flags,
int scope);/**
* Notifies that an interface has been added.
*
* @param ifName the name of the added interface
*/
void onInterfaceAdded(@utf8InCpp String ifName);/**
* Notifies that an interface has been removed.
*
* @param ifName the name of the removed interface
*/
void onInterfaceRemoved(@utf8InCpp String ifName);/**
* Notifies that the status of the specific interface has changed.
*
* @param ifName the name of the interface that changed status
* @param up true for interface up, false for down
*/
void onInterfaceChanged(@utf8InCpp String ifName, boolean up);/**
* Notifies that the link state of the specific interface has changed.
*
* @param ifName the name of the interface whose link state has changed
* @param up true for interface link state up, false for link state down
*/
void onInterfaceLinkStateChanged(@utf8InCpp String ifName, boolean up);/**
* Notifies that an IP route has changed.
*
* @param updated true for update, false for remove
* @param route destination prefix of this route, e.g., "2001:db8::/64"
* @param gateway address of gateway, empty string for no gateway
* @param ifName interface name of this route, empty string for no interface
*/
void onRouteChanged(
boolean updated,
@utf8InCpp String route,
@utf8InCpp String gateway,
@utf8InCpp String ifName);/**
* Notifies that kernel has detected a socket sending data not wrapped
* inside a layer of SSL/TLS encryption.
*
* @param uid uid of this event
* @param hex packet content in hex format
*/
void onStrictCleartextDetected(int uid, @utf8InCpp String hex);
}
可以看到主要有如下几个接口:
- void onInterfaceClassActivityChanged(boolean isActive, int timerLabel, long timestampNs, int uid);
- void onQuotaLimitReached(String alertName, String ifName);
- void onInterfaceDnsServerInfo(String ifName, long lifetimeS, String[] servers);
- void onInterfaceAddressUpdated(String addr, String ifName, int flags, int scope);
- void onInterfaceAddressRemoved(String addr, String ifName, int flags, int scope);
- void onInterfaceAdded(String ifName);
- void onInterfaceRemoved(String ifName);
- void onInterfaceChanged(String ifName, boolean up);
- void onInterfaceLinkStateChanged(String ifName, boolean up);
- void onRouteChanged(boolean updated, String route, String gateway, String ifName);
- void onStrictCleartextDetected(int uid, String hex);
主要实现了接口 Active 状态改变,配合用完,Dns 信息,地址更新或删除,接口添加、删除或者更新,接口链接状态变化,路由变更,严格路由信息等的回调。这里看起来是回调形式的定义,其实并非回调,而是一个 binder 的 Client 和 Server 接口的直接调用。binder 接口以私有的方式从 NetworkManagementService 类进程传递到 netd 后台进程中,以客户端的形式存在进行调用。