Ethernet library maintain() problem

I discovered a problem with the Ethernet.maintain() function. It would not renew correctly with MikroTik routers. Tech support people at MikroTik were very helpful in debugging it for me. I let them break into my router for a couple hours and watch as it renewed. They said the dhcp renew request was incorrect, and caused the renew to fail. Once the lease expired, the Ethernet.maintain() function would work, but the dhcp server would issue a new (different) ip.

Anyone else using Ethernet.maintain() and having issues? I modified my Dhcp.cpp file so it is now close to compatible with MikroTik routers and renew correctly. If you are having issues and would like to experiment, I attached the modified file to this post.

edit: If you try it, especially with other makes of routers, let me know how it worked for you.

The problem with the renew request was simple. The client is supposed to send the server the ip it is currently assigned. The current dhcp library does not do that. It sends 0.0.0.0 instead. That causes the renew request to fail at 1/2 the lease time.

Then, a few seconds after the lease expires in the router, the Arduino sends another renew request with 0.0.0.0 as its current ip, and the server attempts to issue the next ip it has in the lease list ip pool for that mac address. That should be the address the Arduino is using. But since the renew request from the Arduino shows 0.0.0.0 as the current ip, the router pings the new ip to see if that ip is still not being used. If a device responds (which the Arduino will), it marks that ip as “busy” in the lease list, and issues the Arduino another ip.

This is all that was added to Dhcp.cpp.

// Tim added    
    if(messageType == DHCP_REQUEST)
    {
      buffer[12] = _dhcpLocalIp[0];
      buffer[13] = _dhcpLocalIp[1];
      buffer[14] = _dhcpLocalIp[2];
      buffer[15] = _dhcpLocalIp[3];
    }
// end Tim added

This is for the v1.0.5 ethernet library. I haven’t checked with any other version.

Dhcp.cpp (14.9 KB)