This article was reproduced from: http://blog.csdn.net/myvest/article/details/51483647
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Directory (?) [+]
1. Background of the problem
Recently, a problem has been encountered in which DNS cannot be successfully modified in a project, either statically or DHCP.
Finally found, is because/etc/dhcpcd/dhcpcd-hooks/20-dns.conf this script, the DNS is written dead, the following code slices, each time after setting up, but also to set up once.
setprop dhcp.${intf}.dns${dnsvalue} 203.82.48.3setprop dhcp.${intf}.dns${standydnsvalue} 203.82.48.4
# Set NET.<IFACE>.DNSN properties that contain the# DNS server addresses given by the DHCP server.if [[$interface = = p2p*]]Then intf=p2pelse intf=$interfaceFiSet_dns_props () {Case"${new_domain_name_servers} "Inch"")Return0;;Esac count=1For IInch1234;Do setprop DHCP.${intf}.dns${i}""Done count=1For DNSADDRInch${new_domain_name_servers};Do setprop DHCP.${intf}.dns${count}${DNSADDR} count=$ (($count +1))Done dnsvalue=1 standydnsvalue=2 SetProp DHCP.${intf}.dns${dnsvalue}203.82.48.3 SetProp DHCP.${intf}.dns${standydnsvalue}203.82.48.4 separator=" "if [-Z"$new _domain_name "];Then separator=""Elseif [-Z"$new _domain_search "];Then separator=""FiFi setprop DHCP.${interface}.domain"${new_domain_name}$separator${new_domain_search} "}unset_dns_props () {for i in Span class= "Hljs-number" >1 2 3 4; Span class= "Hljs-keyword" >do setprop DHCP. ${intf}.dns${i} "done setprop DHCP. ${interface}.domain "}case" Span class= "hljs-string" > "${reason}" inbound| inform| rebind| Reboot| renew| TIMEOUT) set_dns_props;; expire| fail| ipv4ll| release| STOP) unset_dns_props;; esac
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
So what's the use of this script? Why this script can write dead DNS.
This is because in Android, DHCP is divided into two parts, one for the DHCP client side and one for the server side.
2. DHCP Client
The client side is the DHCP request that we send from the upper app to the framework. This process is not difficult, generally the last call to the RUNDHCP function, its jni in
Frameworks\base\core\jni\android_net_netutils.cpp
In the Android_net_utils_rundhcpcommon. The Dhcp_do_request function is then called. Here, the part of the framework is finished, and then it is called to System/core/libnetutils.
In the Dhcp_do_request function, when the [Dhcp.eth0.result] property changes to [OK], the Fill_ip_info function is called, which functions to read the IP information.
static void Fill_ip_info (const char *Interfacein_addr_t *IPADDR,in_addr_t *Gatewayin_addr_t *mask, in_addr_t *dns1, in_addr_t *dns2, in_addr_t * server, uint32_t *lease) {Property_get ("Dhcp.eth0. IPAddress ", Prop_value,null);p roperty_get (" Dhcp.eth0. Gateway ", Prop_value,null);p roperty_get (" Dhcp.eth0. Mask ", Prop_value,null);p roperty_get (" Dhcp.eth0. Dns1 ", Prop_value,null);p roperty_get (" Dhcp.eth0. Dns2 ", Prop_value,null);p roperty_get (" Dhcp.eth0. Server ", Prop_value,null);p roperty_get (" Dhcp.eth0. Leasetime ", Prop_value,null);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Who is going to actually do the work of DHCP, is dhcpd this daemon. This process is controlled according to the INIT.SVC.DHCPCD. This process will set these properties, which are finally set according to the script, which is the script 20-dns.conf and 95-configured we mentioned above. The two scripts were also etc/dhcpcd/dhcpcd-run-hooks.
The approximate process is as follows:
3. DHCP Server Side
DHCPCD Call Procedure:
The code is under EXTERNAL/DHCPCD,
=>main# define SYSCONFDIR "/SYSTEM/ETC/DHCPCD"#define PACKAGE "DHCPCD"# define CONFIG Sysconfdir "/" package ". conf"# define LIBEXECDIR "/SYSTEM/ETC/DHCPCD"# define SCRIPT Libexecdir "/" package "-run-hooks" =>strlcpy (Options->script, SCRIPT, sizeof (Options->script) );The default options->script= "/system/etc/dhcpcd/dhcpcd-run-hooks" =>f = fopen (cf? Cf:config,"R");If you do not specify a. conf file, use the default. conf file =>parse_config_lineResolves the "/system/etc/dhcpcd/dhcpcd.conf" default configuration file =>parse_option=> if the"/system/etc/dhcpcd/dhcpcd.conf" hasThe section "script" is the one that executes strlcpy (Options->script, Oarg, sizeof (Options->script)); direct copy/* {"script", required_argument, NULL, ' C '}, {"option", Required_argument, NULL, ' O '}, "/system/etc/dhcpcd/dhcpcd.conf" Some of the contents are as follows: ... option domain_name_servers, domain_name, Domain_search, host_name ... */=>dhcp_run=>handle_dhcp_ Packet=>handle_dhcp=>bind_dhcp reason ="TIMEOUT"; reason ="BOUND"; reason ="REBIND"; reason ="RENEW"; system/extra/dhcpcd-4.0.0-beta9/configure.c=> Configure (iface, Reason, state->New, State->old, &state->lease, Options,1);If the DHCP time-out or DHCP succeeds, Exec_script is called to execute the script.Execute SetProp dhcp.${interface}.result "failed" orExecute setprop dhcp.${interface}.result "OK" =>exec_script (options, iface->name, Reason,NULL, old);=> then configure_env passes reason through the environment variable to the script int exec_script (const struct options *options, const char *iface, const char *reason,const struct dhcp_message *DHCPN, const struct dhcp_message *dhcpo) =>pid = fork (); =>if ( PID = = 0) Execve (Options->script, argv, env); //child Process Execution script, default "/system/etc/dhcpcd/dhcpcd-run-hooks" // The Dhcpcd-run-hooks script will decide whether to execute the corresponding file under the system/etc/dhcpcd/dhcpcd-hook/* directory according to the level value // Our system has the following 2 files in the system/etc/dhcpcd/dhcpcd-hook/* directory //95-configured //20-dns.conf=> parent process returns while (Waitpid (PID, &status, Span class= "Hljs-number" >0) = =-1) wait for child process script execution to complete
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
4. DHCP protocol
One thing, when filtering network packets, you should filter the BOOTP, not DHCP. Oh, the type of DHCP message has the following:
(1) DHCPDISCOVER (0x01), which is the first message for the client to start the DHCP process;
(2) DHCPOFFER (0x02), which is the response of the server to the DHCPDISCOVER message;
(3) DHCPREQUEST (0x03), which is the slient of the DHCPOFFER message to the server during the start of the DHCP process, or the message sent when the client renews the IP address lease period;
(4) DHCPDecline (0x04), when the client discovers that the server assigned to its IP address is unusable, such as an IP address conflict, this message will be issued to notify the server to prohibit the use of IP addresses;
(5) DHCPACK (0x05), the server to the client's DHCPREQUEST message acknowledgment response message, the client received this message, only really obtain the IP address and related configuration information;
(6) Dhcpnak (0x06), the server to the client's DHCPREQUEST message rejection response message, the client receives this message, the general will restart the new DHCP process;
(7) Dhcprelease (0x07), the client actively releases the server assigned to its IP address of the message, when the server receives this message, it can reclaim the IP address, can be assigned to other clients;
(8) DHCPInform (0x08), the client has obtained an IP address, send this message, just to get some other network configuration information from DHCPServer, such as ROUTEIP,DNSIP, the application of this message is very rare.
Android DHCP process "Go"