使用ESP32获取DHCP选项

发布于 2025-01-26 13:21:45 字数 920 浏览 4 评论 0 原文

我已经处理了ESP32模块(ESP-IDF),试图通过WiFi和以太网连接到DHCP服务器。关键是,我正在尝试获得DHCP选项,但是我失败了...

我尝试使用LWIP和ESP-NETIF。 (我还必须猜测什么是xxxx和yyyy)

// Create an esp_netif pointer to store current interface
  esp_netif_t* ifscan = esp_netif_next(NULL);

  // Stores the unique interface descriptor, such as "PP1", etc
  char ifdesc[7];
  ifdesc[6] = 0;  // Ensure null terminated string
  uint32_t value = 0;

  while (ifscan != NULL)
  {
      esp_netif_get_netif_impl_name(ifscan, ifdesc);
      Serial.printf("IF NAME: %s\n", ifdesc);

      esp_err_t code = esp_netif_dhcpc_option(ifscan, ESP_NETIF_OP_GET, ESP_NETIF_DOMAIN_NAME_SERVER, XXXX, YYYY);
      Serial.printf("RES: %s - OPTION: %s\n", esp_err_to_name(code), XXXXX);
   


      // Get the next interface
      ifscan = esp_netif_next(ifscan);
  }
      Serial.printf("Done listing network interfaces");

有人有一个示例的示例示例,显示如何在连接后获得选项?

先感谢您。

I've dealing with an ESP32 module (ESP-IDF) trying to connect via Wifi and ethernet to a dhcp server. The point is, I'm trying to get the DHCP options, but I failed...

I tried using LWIP and also ESP-NETIF. (I have also to guess whats XXXX and YYYY)

// Create an esp_netif pointer to store current interface
  esp_netif_t* ifscan = esp_netif_next(NULL);

  // Stores the unique interface descriptor, such as "PP1", etc
  char ifdesc[7];
  ifdesc[6] = 0;  // Ensure null terminated string
  uint32_t value = 0;

  while (ifscan != NULL)
  {
      esp_netif_get_netif_impl_name(ifscan, ifdesc);
      Serial.printf("IF NAME: %s\n", ifdesc);

      esp_err_t code = esp_netif_dhcpc_option(ifscan, ESP_NETIF_OP_GET, ESP_NETIF_DOMAIN_NAME_SERVER, XXXX, YYYY);
      Serial.printf("RES: %s - OPTION: %s\n", esp_err_to_name(code), XXXXX);
   


      // Get the next interface
      ifscan = esp_netif_next(ifscan);
  }
      Serial.printf("Done listing network interfaces");

Does anyone have an example of sourcecode showing how to get the options after connecting?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2025-02-02 13:21:45

代码中只有几个DHCP选项。
检查对于 ESP_NETIF_DHCPC_OPTION
esp_netif_domain_name_server eq不是。

对于 ESP_NETIF_OP_GET 仅有 ESP_NETIF_IP_REQUEST_RETRY_TIME ESP_NETIF_VENDOR_VENDOR_SPECIFIC_INFO 实现。

否则,您将获得 ESP_ERR_ERS_ESP_NETIF_INVALID_PARAMS 20481 0x5001 )。

esp_err_t esp_netif_dhcpc_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, void *opt_val,
                                 uint32_t opt_len)
{
...
    if (opt_op == ESP_NETIF_OP_GET) {
        if (esp_netif->dhcpc_status == ESP_NETIF_DHCP_STOPPED) {
            return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED;
        }
        switch (opt_id) {
            case ESP_NETIF_IP_REQUEST_RETRY_TIME:
                if (opt_len == sizeof(dhcp->tries)) {
                    *(uint8_t *)opt_val = dhcp->tries;
                }
                break;
#if ESP_DHCP && !ESP_DHCP_DISABLE_VENDOR_CLASS_IDENTIFIER
            case ESP_NETIF_VENDOR_SPECIFIC_INFO:
                return dhcp_get_vendor_specific_information(opt_len, opt_val);
#endif
            default:
                return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
        }
...
}

我在我的ESP32结果上尝试了您的代码和选项 224

  Serial.println("Option >");
  esp_err_t result = 0;
  uint32_t value = 0;
  esp_netif_t * ifscan = esp_netif_next(NULL);
  char ifname[10];
  result = esp_netif_get_netif_impl_name(ifscan, ifname);
  Serial.print("esp_netif_get_netif_impl_name: ");
  Serial.println(result);
  result = esp_netif_dhcpc_option(ifscan, ESP_NETIF_OP_GET, (esp_netif_dhcp_option_id_t)224, &value, 4);
  Serial.print("esp_netif_dhcpc_option: ");
  Serial.println(result);
  Serial.print("esp_netif_dhcpc_option value: ");
  Serial.println(value);
  Serial.println("Option <");

Option >
esp_netif_get_netif_impl_name: 0
esp_netif_dhcpc_option: 20481
esp_netif_dhcpc_option value: 0
Option <

There are only a couple of DHCP options implemented in code.
Check the source code (github) for esp_netif_dhcpc_option.
ESP_NETIF_DOMAIN_NAME_SERVER e.q. is not.

For ESP_NETIF_OP_GET there is only ESP_NETIF_IP_REQUEST_RETRY_TIME and ESP_NETIF_VENDOR_SPECIFIC_INFO implemented.
Otherwise you get ESP_ERR_ESP_NETIF_INVALID_PARAMS (20481 0x5001) as a result.

esp_err_t esp_netif_dhcpc_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, void *opt_val,
                                 uint32_t opt_len)
{
...
    if (opt_op == ESP_NETIF_OP_GET) {
        if (esp_netif->dhcpc_status == ESP_NETIF_DHCP_STOPPED) {
            return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED;
        }
        switch (opt_id) {
            case ESP_NETIF_IP_REQUEST_RETRY_TIME:
                if (opt_len == sizeof(dhcp->tries)) {
                    *(uint8_t *)opt_val = dhcp->tries;
                }
                break;
#if ESP_DHCP && !ESP_DHCP_DISABLE_VENDOR_CLASS_IDENTIFIER
            case ESP_NETIF_VENDOR_SPECIFIC_INFO:
                return dhcp_get_vendor_specific_information(opt_len, opt_val);
#endif
            default:
                return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
        }
...
}

I tried with your code and option 224 on my ESP32

  Serial.println("Option >");
  esp_err_t result = 0;
  uint32_t value = 0;
  esp_netif_t * ifscan = esp_netif_next(NULL);
  char ifname[10];
  result = esp_netif_get_netif_impl_name(ifscan, ifname);
  Serial.print("esp_netif_get_netif_impl_name: ");
  Serial.println(result);
  result = esp_netif_dhcpc_option(ifscan, ESP_NETIF_OP_GET, (esp_netif_dhcp_option_id_t)224, &value, 4);
  Serial.print("esp_netif_dhcpc_option: ");
  Serial.println(result);
  Serial.print("esp_netif_dhcpc_option value: ");
  Serial.println(value);
  Serial.println("Option <");

Result:

Option >
esp_netif_get_netif_impl_name: 0
esp_netif_dhcpc_option: 20481
esp_netif_dhcpc_option value: 0
Option <
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文