Jelajahi Sumber

esp-netif: Added methods for conversion IP4/IP6 addresses from their string interpretation

Closes https://github.com/espressif/esp-idf/issues/5545
Jiri Schiebel 5 tahun lalu
induk
melakukan
d1356bca68

+ 25 - 0
components/esp_netif/include/esp_netif.h

@@ -707,6 +707,31 @@ char *esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen);
 */
 uint32_t esp_ip4addr_aton(const char *addr);
 
+/**
+ * @brief Converts Ascii internet IPv4 address into esp_ip4_addr_t
+ *
+ * @param[in] src IPv4 address in ascii representation (e.g. "127.0.0.1")
+ * @param[out] dst Address of the target esp_ip4_addr_t structure to receive converted address
+ * @return
+ *         - ESP_OK on success
+ *         - ESP_FAIL if conversion failed
+ *         - ESP_ERR_INVALID_ARG if invalid parameter is passed into
+ */
+esp_err_t esp_netif_str_to_ip4(const char *src, esp_ip4_addr_t *dst);
+
+/**
+ * @brief Converts Ascii internet IPv6 address into esp_ip4_addr_t
+ * Zeros in the IP address can be stripped or completely ommited: "2001:db8:85a3:0:0:0:2:1" or "2001:db8::2:1")
+ *
+ * @param[in] src IPv6 address in ascii representation (e.g. ""2001:0db8:85a3:0000:0000:0000:0002:0001")
+ * @param[out] dst Address of the target esp_ip6_addr_t structure to receive converted address
+ * @return
+ *         - ESP_OK on success
+ *         - ESP_FAIL if conversion failed
+ *         - ESP_ERR_INVALID_ARG if invalid parameter is passed into
+ */
+esp_err_t esp_netif_str_to_ip6(const char *src, esp_ip6_addr_t *dst);
+
 /**
  * @}
  */

+ 18 - 0
components/esp_netif/lwip/esp_netif_lwip.c

@@ -14,6 +14,7 @@
 
 #include <string.h>
 #include <lwip/ip_addr.h>
+#include <lwip/sockets.h>
 
 #include "esp_netif_lwip_internal.h"
 
@@ -247,6 +248,23 @@ uint32_t esp_ip4addr_aton(const char *addr)
     return ipaddr_addr(addr);
 }
 
+esp_err_t esp_netif_str_to_ip4(const char *src, esp_ip4_addr_t *dst)
+{
+    if (src == NULL || dst == NULL) {
+        return ESP_ERR_INVALID_ARG;
+    }
+    int err = inet_pton(AF_INET, src, dst);
+    return err == 1 ? ESP_OK : ESP_FAIL;
+}
+
+esp_err_t esp_netif_str_to_ip6(const char *src, esp_ip6_addr_t *dst)
+{
+    if (src == NULL || dst == NULL) {
+        return ESP_ERR_INVALID_ARG;
+    }
+    int err = inet_pton(AF_INET6, src, dst);
+    return err == 1 ? ESP_OK : ESP_FAIL;
+}
 
 esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif)
 {

+ 24 - 0
components/esp_netif/test/test_esp_netif.c

@@ -286,3 +286,27 @@ TEST_CASE("esp_netif: get/set hostname", "[esp_netif]")
 
     esp_netif_destroy(esp_netif);
 }
+
+TEST_CASE("esp_netif: convert ip address from string", "[esp_netif]")
+{
+    const char *ipv4_src[] = {"127.168.1.1", "255.255.255.0", "305.500.721.801", "127.168.1..", "abc.def.***.ddd"};
+    esp_ip4_addr_t ipv4;
+    TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[0], &ipv4));
+    TEST_ASSERT_EQUAL(ipv4.addr, ESP_IP4TOADDR(127, 168, 1, 1));
+    TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[1], &ipv4));
+    TEST_ASSERT_EQUAL(ipv4.addr, ESP_IP4TOADDR(255, 255, 255, 0));
+    TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[2], &ipv4));
+    TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[3], &ipv4));
+    TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[4], &ipv4));
+    TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_netif_str_to_ip4(NULL, &ipv4));
+    TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_netif_str_to_ip4(ipv4_src[0], NULL));
+
+    const char *ipv6_src[] = {"127:168:6:8:188:65:1:0", "255:255:255:0:0:0:65:56", "305:500:721:888:777:458:555:666", "EFGH.127:168::55"};
+    esp_ip6_addr_t ipv6;
+    TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip6(ipv6_src[0], &ipv6));
+    TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip6(ipv6_src[1], &ipv6));
+    TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip6(ipv6_src[2], &ipv6));
+    TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_str_to_ip6(ipv6_src[3], &ipv6));
+    TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_netif_str_to_ip6(NULL, &ipv6));
+    TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_netif_str_to_ip6(ipv6_src[0], NULL));
+}