Android DHCP 启动分析

一、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(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);

 

}

 

三、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}"

    setprop dhcp.${interface}.gateway   "${new_routers%% *}"

    setprop dhcp.${interface}.mask      "${new_subnet_mask}"

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

    setprop dhcp.${interface}.server    "${new_dhcp_server_identifier}"

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

    ;;

 

EXPIRE|FAIL|IPV4LL|STOP)

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

    ;;

RELEASE)

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

    ;;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值