trying to get ethernet to serve as DHCP server

jakehuang
Posts: 12
Joined: Thu Nov 17, 2016 7:12 pm

trying to get ethernet to serve as DHCP server

Postby jakehuang » Tue Oct 31, 2017 4:12 pm

Hi guys,

Many attempts trying to get ethernet to work as DHCP server (along with wifi as DHCP server AP as well). I think it's kinda odd but it's our requirement. Doesn't seem to work. Ethernet always get assigned an IP. I used tcpip_adapter DHCP calls in event handlers to set it up. Code is as follows.

Any ideas will be much much appreciated!

Code: Select all


esp_err_t IRAM_ATTR user_event_handler(void *ctx, system_event_t *event)
{    
    switch(event->event_id) {
        case SYSTEM_EVENT_ETH_START:

        tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
        ESP_LOGD(tag, "stopping dhcp client");  
        tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_ETH);
        ESP_LOGD(tag, "dhcp server start successfully");

            break;
        case SYSTEM_EVENT_ETH_CONNECTED:     
        
        tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
        ESP_LOGD(tag, "stopping dhcp client");  
        tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_ETH);
        ESP_LOGD(tag, "dhcp server start successfully");  
            break;
        case SYSTEM_EVENT_ETH_GOT_IP:
            eth_dump();
            break;
        case SYSTEM_EVENT_ETH_DISCONNECTED:
            eth_dump();
            break;
        case SYSTEM_EVENT_STA_START:
            break;
        case SYSTEM_EVENT_STA_GOT_IP:
            break;
        case SYSTEM_EVENT_STA_DISCONNECTED:
            break;
        default:
            break;
    }
    return ESP_OK;
}


Noted that tcpip_adapter_dhcps_start() doesn't support TCPIP_ADAPTER_IF_ETH so I commented it out.

Code: Select all

esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if)
{
    TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcps_start_api);

    /* only support ap now */
    // if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
    //     ESP_LOGD(TAG, "dhcp server invalid if=%d", tcpip_if);
    //     return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
    // }

    if (dhcps_status != TCPIP_ADAPTER_DHCP_STARTED) {
        struct netif *p_netif = esp_netif[tcpip_if];

        if (p_netif != NULL && netif_is_up(p_netif)) {
            tcpip_adapter_ip_info_t default_ip;
            tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &default_ip);
            dhcps_start(p_netif, default_ip.ip);
            dhcps_status = TCPIP_ADAPTER_DHCP_STARTED;
            ESP_LOGD(TAG, "dhcp server start successfully");
            return ESP_OK;
        } else {
            ESP_LOGD(TAG, "dhcp server re init");
            dhcps_status = TCPIP_ADAPTER_DHCP_INIT;
            return ESP_OK;
        }
    }

    ESP_LOGD(TAG, "dhcp server already start");
    return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED;
}

pratik.yadav.455
Posts: 23
Joined: Thu Oct 26, 2017 7:34 am

Re: trying to get ethernet to serve as DHCP server

Postby pratik.yadav.455 » Thu Feb 20, 2020 1:22 pm

hello @ESP_igrr and @jakehuang,

Any update on it?

I also want to start DHCP server on ESP32 on ethernet interface.

Can it possible? Any steps to do it?

I am using esp idf 3.2.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: trying to get ethernet to serve as DHCP server

Postby PeterR » Thu Feb 20, 2020 6:47 pm

I needed to upgrade to IDF 4.1 to get DHCP on ethernet EDIT with ESP api. Only just given an address to my PC and have been diverted to another job. Not bedded/tested fully yet.
See my post.
I will tidy some code & post but basically when you create the netif with DHCP capability (& auto up) it starts the DHCP server but with andress ANY. You then have to stop DHCP so that you can set the IP address and then restart DHCP before you start the low level driver & event loop.
The DHCP code is in IDF 4.1 /lwip/apps.
It looks as if the code uses only lwip related structures which should mean that you can use it within IDF3.2 if you bypass tcp_ and netif_ APIs.
EDIT: That is too say if you do not want to upgrade from 3.2 then maybe copy the lwip server from IDF 4.1? You would need to stop the tcp_ DHCP client and/or drop the tcp_ layer and go 100% lwip. Depends which services you use and are also in lwip. lwip httpd is perfectly good as are many other lwip components.
& I also believe that IDF CAN should be fixed.

c3pp0x
Posts: 1
Joined: Tue Jun 08, 2021 10:03 pm

Re: trying to get ethernet to serve as DHCP server

Postby c3pp0x » Tue Jun 08, 2021 10:20 pm

I'm a little late to the party, but I also encountered this problem with an older idf version today. You only need to add a default ip and change a few things in tcpip_adapter_lwip.c. Line numbers won't match (I patched some other stuff before...) and it's ugly, but I think you'll get the idea :) )
  1. --- tcpip_adapter_lwip_orig.c   2021-06-08 23:50:01.707261243 +0200
  2. +++ tcpip_adapter_lwip_patched.c    2021-06-08 23:50:57.743749589 +0200
  3. @@ -116,6 +116,10 @@
  4.          IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].gw, 192, 168 , 4, 1);
  5.          IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].netmask, 255, 255 , 255, 0);
  6.  
  7. +        IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_ETH].ip, 192, 168 , 5, 1);
  8. +        IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_ETH].gw, 192, 168 , 5, 1);
  9. +        IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_ETH].netmask, 255, 255 , 255, 0);
  10. +
  11.          ret = sys_sem_new(&api_sync_sem, 0);
  12.          if (ERR_OK != ret) {
  13.              ESP_LOGE(TAG, "tcpip adatper api sync sem init fail");
  14. @@ -201,7 +205,7 @@
  15.  
  16.      }
  17.  
  18. -    if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
  19. +    //if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
  20.          netif_set_up(esp_netif[tcpip_if]);
  21.  
  22.          if (dhcps_status == TCPIP_ADAPTER_DHCP_INIT) {
  23. @@ -214,7 +218,7 @@
  24.  
  25.              dhcps_status = TCPIP_ADAPTER_DHCP_STARTED;
  26.          }
  27. -    }
  28. +    //}
  29.  
  30.      tcpip_adapter_update_default_netif();
  31.  
  32. @@ -268,7 +272,7 @@
  33.          return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
  34.      }
  35.  
  36. -    if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
  37. +    if (tcpip_if == TCPIP_ADAPTER_IF_AP) || TCPIP_ADAPTER_IF_ETH {
  38.          dhcps_stop(esp_netif[tcpip_if]);    // TODO: dhcps checks status by its self
  39.          if (TCPIP_ADAPTER_DHCP_STOPPED != dhcps_status) {
  40.              dhcps_status = TCPIP_ADAPTER_DHCP_INIT;
  41. @@ -820,17 +824,17 @@
  42.      TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcps_start_api);
  43.  
  44.      /* only support ap now */
  45. -    if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
  46. +    /*if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
  47.          ESP_LOGD(TAG, "dhcp server invalid if=%d", tcpip_if);
  48.          return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
  49. -    }
  50. +    }*/
  51.  
  52.      if (dhcps_status != TCPIP_ADAPTER_DHCP_STARTED) {
  53.          struct netif *p_netif = esp_netif[tcpip_if];
  54.  
  55.          if (p_netif != NULL && netif_is_up(p_netif)) {
  56.              tcpip_adapter_ip_info_t default_ip;
  57. -            tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &default_ip);
  58. +            tcpip_adapter_get_ip_info(tcpip_if, &default_ip);//ESP_IF_WIFI_AP
  59.              dhcps_start(p_netif, default_ip.ip);
  60.              dhcps_status = TCPIP_ADAPTER_DHCP_STARTED;
  61.              ESP_LOGD(TAG, "dhcp server start successfully");

Who is online

Users browsing this forum: Usama Masood and 80 guests