android DHCP流程【轉】

來源:互聯網
上載者:User

標籤:開始   led   level   address   run   fine   otp   exp   require   

本文轉載自:http://blog.csdn.net/myvest/article/details/51483647

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

 

目錄(?)[+]

 1、問題背景

最近遇到一個問題,在一個項目中,無論靜態或是DHCP,都無法成功修改DNS。 
最後發現,是因為/etc/dhcpcd/dhcpcd-hooks/20-dns.conf 這個指令碼中,將DNS寫死了,如下代碼片,每次設定完以後,還去在設定一次。

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=p2p    else    intf=$interfacefiset_dns_props(){    case "${new_domain_name_servers}" in    "")   return 0;;    esac    count=1    for i in 1 2 3 4; do        setprop dhcp.${intf}.dns${i} ""    done    count=1    for dnsaddr in ${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=""    else        if [ -z "$new_domain_search" ]; then            separator=""        fi    fi    setprop dhcp.${interface}.domain "${new_domain_name}$separator${new_domain_search}"}unset_dns_props(){    for i in 1 2 3 4; do        setprop dhcp.${intf}.dns${i} ""    done    setprop dhcp.${interface}.domain ""}case "${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
  • 21
  • 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

那麼,這個指令碼是有什麼用處呢?為什麼這個指令碼可以寫死DNS。 
這是因為android中,DHCP分為兩個部分,一個是DHCP Client端,一個是server端。

2、Dhcp用戶端

client端就是我們從上層APP到framework後發出的DHCP請求。這個流程不難,一般是最後會調用runDhcp函數,其JNI在 
frameworks\base\core\jni\android_net_NetUtils.cpp 
中的android_net_utils_runDhcpCommon。然後會在調用dhcp_do_request函數。到這裡,framework的部分就完了,接下來會調用到system/core/libnetutils中去。

在dhcp_do_request函數中,當[dhcp.eth0.result]屬性變為[ok]時,會調用fill_ip_info函數,這個函數的作用,就是去讀取IP資訊而已。

static void fill_ip_info(const char *interface, in_addr_t *ipaddr, in_addr_t *gateway, in_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);property_get(“dhcp.eth0. gateway”, prop_value,NULL);property_get(“dhcp.eth0. mask”, prop_value,NULL);property_get(“dhcp.eth0. dns1”, prop_value,NULL);property_get(“dhcp.eth0. dns2”, prop_value,NULL);property_get(“dhcp.eth0. server”, prop_value,NULL);property_get(“dhcp.eth0. leasetime”, prop_value,NULL);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

那是誰去真正做DHCP的工作呢,是dhcpd這個守護進程。這個進程是根據init.svc.dhcpcd來控制的。這個進程會去設定這些屬性,這些屬性最後都是根據指令碼設定的,也就是我們上面提到的指令碼20-dns.conf和95-configured。這兩個指令碼又是通過etc/dhcpcd/dhcpcd-run-hooks調起來的。 
大概流程如下面:

3、Dhcp server端

dhcpcd 調用過程:

代碼在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));//預設的options->script="/system/etc/dhcpcd /dhcpcd-run-hooks"=>f = fopen(cf ? cf : CONFIG, "r");//如果沒有指定.conf檔案,那麼使用預設.conf檔案=>parse_config_line//解析"/system/etc/dhcpcd/dhcpcd.conf"預設設定檔=>parse_option=>如果在"/system/etc/dhcpcd/dhcpcd.conf"有"script"這個節=>那麼執行strlcpy(options->script, oarg, sizeof(options->script));直接拷貝 /* {"script", required_argument, NULL, ‘c‘}, {"option", required_argument, NULL, ‘o‘},"/system/etc/dhcpcd/dhcpcd.conf"中的部分內容如下: ...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);//如果dhcp逾時或者dhcp成功,都會調用exec_script來執行指令碼,//執行setprop dhcp.${interface}.result "failed"或者//執行setprop dhcp.${interface}.result "ok"=>exec_script(options, iface->name, reason, NULL, old);=>然後configure_env通過環境變數將reason傳遞到指令碼中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);//子進程執行指令碼,預設"/system/etc/dhcpcd/dhcpcd-run-hooks"//dhcpcd-run-hooks指令碼會根據level值,決定是否執行system/etc/dhcpcd/dhcpcd-hook/*目錄下的相應檔案//我們的系統在該system/etc/dhcpcd/dhcpcd-hook/*目錄下有如下2個檔案//95-configured//20-dns.conf=>父進程返回while (waitpid(pid, &status, 0) == -1)等待子進程指令碼執行完成
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
4、DHCP協議

有一點,在過濾網路包的時候,應該過濾的是bootp,而不是dhcp哦,dhcp報文的種類有以下這些:

(1)DHCPDISCOVER(0×01),此為Client開始DHCP過程的第一個報文;

(2)DHCPOFFER(0×02),此為Server對DHCPDISCOVER報文的響應;

(3)DHCPREQUEST(0×03),此報文是Slient開始DHCP過程中對server的DHCPOFFER報文的回應,或者是client續延IP地址租期時發出的報文;

(4)DHCPDECLINE(0×04),當Client發現Server分配給它的IP地址無法使用,如IP地址衝突時,將發出此報文,通知Server禁止使用IP地址;

(5)DHCPACK(0×05),Server對Client的DHCPREQUEST報文的確認響應報文,Client收到此報文後,才真正獲得了IP地址和相關的配置資訊;

(6)DHCPNAK(0×06),Server對Client的DHCPREQUEST報文的拒絕響應報文,Client收到此報文後,一般會重新開始新的DHCP過程;

(7)DHCPRELEASE(0×07),Client主動釋放server分配給它的IP地址的報文,當Server收到此報文後,就可以回收這個IP地址,能夠分配給其他的Client;

(8)DHCPINFORM(0×08),Client已經獲得了IP地址,發送此報文,只是為了從DHCPSERVER處擷取其他的一些網路設定資訊,如routeip,DNSIp等,這種報文的應用非常少見。

android DHCP流程【轉】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.