android ethernet DHCP小記(接上篇)

在上一篇中有提到是java層EthernetStateTaracker接受到消息EVENT_DHCP_START,會調用NetworkUtils.runDhcp(mInterfaceName, mDhcpInfo),這個接口在android_net_NetUtils.cpp中有實現,經過jni調用。 html

static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
{
    int result;
    in_addr_t ipaddr, gateway, mask, dns1, dns2, server;
    uint32_t lease;

    const char *nameStr = env->GetStringUTFChars(ifname, NULL);
    result = ::dhcp_do_request(nameStr, &ipaddr, &gateway, &mask,
                                        &dns1, &dns2, &server, &lease); //在system/core/libnetutils/dhcp_utils.c中實現
    env->ReleaseStringUTFChars(ifname, nameStr);
    if (result == 0 && dhcpInfoFieldIds.dhcpInfoClass != NULL) {
        env->SetIntField(info, dhcpInfoFieldIds.ipaddress, ipaddr);
        env->SetIntField(info, dhcpInfoFieldIds.gateway, gateway);
        env->SetIntField(info, dhcpInfoFieldIds.netmask, mask);
        env->SetIntField(info, dhcpInfoFieldIds.dns1, dns1);
        env->SetIntField(info, dhcpInfoFieldIds.dns2, dns2);
        env->SetIntField(info, dhcpInfoFieldIds.serverAddress, server);
        env->SetIntField(info, dhcpInfoFieldIds.leaseDuration, lease);
    }
    return (jboolean)(result == 0);

其中dhcp_do_request裏有調用do_dhcp申請ip地址,並get_dhcp_info( ipaddr,  gateway, mask,  dns1,  dns2,  server, lease)獲取ip地址信息,並經過上面rundhcp接口 java

env->SetIntField(info, dhcpInfoFieldIds.ipaddress, ipaddr);
...
設置信息到mDhcpInfo對象中。


1.下面這層都在/system/core/libnetutils/下,編譯以後生成一個so庫文件,這是真正實現請求並分配ip地址的模塊。
do_dhcp在dhcpclient.c中實現,並調用dhcp_init_ifc(iname),iname是網卡名字。 android

int do_dhcp(char *iname)
{
 #if 0 
    g_dhcp_mode++;
    LOGI("do_dhcp g_dhcp_mode = %d",g_dhcp_mode);
	LOGI("get iname = %s",iname);
	if(memcmp(iname,"eth1",strlen("eth1")) == 0){	
		iname="eth0";
		dhcp_flag=1;
	}else{
		dhcp_flag=0;
	}
#endif 	
    if (ifc_set_addr(iname, 0)) {
        printerr("failed to set ip addr for %s to 0.0.0.0: %s\n", iname, strerror(errno));
        return -1;
    }

    if (ifc_up(iname)) {
        printerr("failed to bring up interface %s: %s\n", iname, strerror(errno));
        return -1;
    }
	sleep(2);
    return dhcp_init_ifc(iname);
}


2.接下來是真正dhcp原理的東東,這裏有很詳細的介紹http://download.csdn.net/detail/new_abc/4520361(還好是免費的資源O(∩_∩)O)
主要有5消息,其中第4個和第7個我沒怎麼見過╮(╯▽╰)╭,你們看上面那個dhcp.ppt吧:
    Value=1,標示DHCP Discover
    Value=2,標示DHCP Offer
    Value=3,標示DHCP Request
    Value=4,標示DHCP Decline(略)
    Value=5,標示DHCP ACK(請求到有效地址)
    Value=6,標示DHCP NAK (無效地址)
    Value=7,標示DHCP Release(略) ui


這裏有個介紹dhcp幾種狀態很是詳細的地址:http://wenku.baidu.com/view/dcbd91395727a5e9856a61fd.html


 如下內容一塊兒研究補充。。。。。。。。。。 spa


http://blog.csdn.net/new_abc/article/details/7898543 .net

相關文章
相關標籤/搜索