[ function ]netd What is the main responsibility for what functions
Why this time contact netd mainly because in the setting up the firewall encountered. About NETD can do what can be netd from the source of Commandlistener in the answer. According to my understanding, (if there are omissions or mistakes please point out),
The NETD handles transactions using command design patterns. So you can see which command functions are all responsible for a glance, as follows:
[Web excerpt] NETD is responsible for the implementation of functions related to network operations involving physical ports, such as bandwidth control (Bandwidth), network address translation (NAT), personal Local area network (PAN), PPP link, soft-ap, shared Internet (tether), etc... are organized according to the module (. cpp+.h) in the netd file directory ~
[Web excerpt] Android Netd is related to the four parts of the basic framework:
(1) The Linux Kernel is used to detect all event events related to network.
(2) Netd as a bridge between the Kernel and the Framework.
(3) The Framework layer operates NETD, sending an operation command to the NETD.
(4) UI interacts with the Framework for the user to manipulate the network.
The main line task is for the time being concerned only with firewall firewallcmd ().
[ function ]networkmanagementservice What's the use?
(1) Connect via socket NETD
(2) Notify the Observer by Observer
The main observers have Connectivityservice,ethernetnetworkfactory,wifistatemachine,vpn,networkpolicymanagerservice,nat464xlat, Commontimemanagementservice, etc.
[Socket]networkmanagementservice how to work with Netd to communicate?
The Networkmanagementservice is communicated through the socket to the netd.
The socket is simple to communicate as follows:
Service Side Client
1. Create socket 1, create socket ()
2. Bind ()
3, Listen ()
4. Accept ()
5. Block and wait for connection 2, connect ()
6. Read data (recv) 3, write data (send)
7, write Data (send) 4, read data (recv)
8. Close socket (closesocket ()) 5, Close socket (closesocket ())
Netd as a bridge between the kernel and the Android framework, so NETD will send out two kinds of messages. One is the message that the kernel wants to escalate to the Android framework, one that is returned after the Android frameworks request.
Here we set the firewall action belongs to the latter, NETD is as the server, Networkmanagementservice as the client.
Netd is as a service-side analysis
Netd as a daemon, it applies the socket resource in the init.rc,
Description of the netd in------init.rc-----
Service netd/system/bin/netd
Class Main
Socket netd Stream 0660 root system
Socket dnsproxyd Stream 0660 root inet
Socket MDNs Stream 0660 root system
Socket FWMARKD Stream 0660 root inet
---------------------------------------------------
Get the socket resource in Commandlistener
Commandlistener inherits from SocketListener, and in SocketListener's Startlistener function, it will pass
The android_get_control_socket function takes the file descriptor of the socket in the INIT.RC
And then listen, here msock is actually a listening socket.
Then accept, get the client socket that communicates with the client
In addition, the UML diagram structure of several data structures described above, you can see Frameworklistener and Commandlistener responsible for the framework to netd command processing commands.
Netlinklistener and Netlinkhandler are responsible for pushing information to the framework to the core
The Frameworklistener family and the Netlinklistener family call ondataavailable () to receive data.
In the ondataavailable function, Frameworklistener calls the Dispatchcommand () Distribution command, and the RunCommand () processing command implemented by the subclass Commandlistener. The Sendgenericokfail () uses the client socket to return the results to Android frameworks.
Netlinklistener invokes the OnEvent () function implemented by the subclass Netlinkhandler to process the kernel escalated message, and the Notify function uses the client socket to escalate the message to the Android framework.
Android Framework as a client analysis
In Networkmanagementservice, the Nativedaemonconnector is responsible for the connection operation, which is placed in a thread and is synchronized with Countdownlatch.
The CREATE function passed in the parameter socket (String) is "netd".
In Nativedaemonconnector (is a Runnable object, mainly see the Run function)
Perform a socket.connect connection operation, and call the ondeamonconnected function of the callback object to tell Networkmanagementservice that the main thread is already connected.
The UML diagrams for several of the classes mentioned above are as follows, because the class diagram build model that does not find Java in Viso uses C #.
[Android]netd and Networkmanagementservice First impressions