在上一篇中有提到是java层EthernetStateTaracker接受到消息EVENT_DHCP_START,会调用NetworkUtils.runDhcp(mInterfaceName, mDhcpInfo),这个接口在android_net_NetUtils.cpp中有实现,通过jni调用。
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);