【原】Android DHCP&nb…

来源:互联网 发布:看中国婐聊网络 编辑:程序博客网 时间:2024/05/01 15:08
原文地址:DHCP 启动分析【2】">【原】Android DHCP 启动分析【2】作者:sunwillow

一、DHCP client dhcpcd server直接的信息交互:

Client server 通过property_get/set 共享内存来共享信息。

property_get/set(key,value),保存在共享内存中,系统中的各个进程可以通过property_service访问。

 

二、DHCP 客户端:

               启动dhcp 请求:

源码:Gingerbread/System/core/libnetutils/dhcp_utils.c

int dhcp_do_request(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_set(“dhcp_eth0.result”, "");

 

 

   property_set("ctl.start","dhcpcd_eth0");

 

 

wait_for_property(“init.svc.dhcpcd_eth0”,” running”, 10);

 

 

wait_for_property(“dhcp_eth0.result”, NULL, 30);

 

 

property_get(“dhcp_eth0.result”, prop_value, NULL));

 

 

    if(strcmp(prop_value, "ok") == 0) {

       fill_ip_info(interface, ipaddr,gateway, mask, dns1, dns2, server, lease);

       return 0;

    } else{

       snprintf(errmsg, sizeof(errmsg), "DHCP result was %s",prop_value);

       return -1;

    }

}

 

 

static void fill_ip_info(constchar *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);

 

}

 

三、Dchpcd 服务端进程:

源码:Gingerbread/external/dhcpcd/dhcpcd.c

Main()

{

dhcp_run(options,&pid_fd);

}

dhcp_run()

{

              

client_setup(state, options);

               for (;;) {

                               if (r == 0)

                                               r = handle_timeout(state, options);/client状态机处理,发送dhcp 请求/

                               else if (r > 0) {

                                               handle_dhcp_packet(state, options);

}

 

 

handle_timeout(struct if_state *state, const struct options*options)

{

               switch(state->state) {

               case STATE_RENEW_REQUESTED:

                                               state->state = STATE_REBINDING;

               case STATE_INIT:

                                               state->state = STATE_REQUESTING;

}

               

               switch (state->state) {

               case STATE_DISCOVERING:

                               send_message(state, DHCP_DISCOVER, options);

                               break;

               case STATE_REQUESTING:

                               if (state->options & DHCPCD_INFORM){

                                               send_message(state, DHCP_INFORM, options);

                                               break;

                               }

                               

               case STATE_RENEWING:  

               case STATE_REBINDING:

                               if (iface->raw_fd == -1)

                                               do_socket(state, SOCKET_OPEN);

                               send_message(state, DHCP_REQUEST,options);

                               break;

               }}

 

handle_dhcp_packet()àhandle_dhcp()àbind_dhcp()àconfigure()àrun_script()

执行脚本文件/system/etc/dhcpcd/dhcpcd-run-hooks,脚本文件调用setprop命令设置dhcp相关参数,并设置dhcp result.

 

case "${reason}" in

BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)

   setprop dhcp.${interface}.ipaddress "${new_ip_address}"

   setpropdhcp.${interface}.gateway  "${new_routers%% *}"

   setpropdhcp.${interface}.mask     "${new_subnet_mask}"

   setprop dhcp.${interface}.leasetime"${new_dhcp_lease_time}"

   setpropdhcp.${interface}.server   "${new_dhcp_server_identifier}"

   setprop dhcp.${interface}.result "ok"

    ;;

 

EXPIRE|FAIL|IPV4LL|STOP)

    setpropdhcp.${interface}.result "failed"

    ;;

RELEASE)

    setpropdhcp.${interface}.result "released"

    ;;

 

0 0