Răsfoiți Sursa

Merge branch 'bugfix/mdns_lwip_agnostic' into 'master'

mdns: Clean the main mdns module from lwip dependencies

See merge request espressif/esp-idf!14187
David Čermák 4 ani în urmă
părinte
comite
cd47ba03bc

+ 27 - 0
components/esp_netif/include/esp_netif_ip_addr.h

@@ -85,6 +85,9 @@ extern "C" {
 
 #define ESP_IP4TOADDR(a,b,c,d) esp_netif_htonl(ESP_IP4TOUINT32(a, b, c, d))
 
+#define ESP_IP4ADDR_INIT(a, b, c, d)  { .type = ESP_IPADDR_TYPE_V4, .u_addr = { .ip4 = { .addr = ESP_IP4TOADDR(a, b, c, d) }}};
+#define ESP_IP6ADDR_INIT(a, b, c, d)  { .type = ESP_IPADDR_TYPE_V6, .u_addr = { .ip6 = { .addr = { a, b, c, d }, .zone = 0 }}};
+
 struct esp_ip6_addr {
     uint32_t addr[4];
     uint8_t zone;
@@ -124,6 +127,30 @@ typedef enum {
  */
 esp_ip6_addr_type_t esp_netif_ip6_get_addr_type(esp_ip6_addr_t* ip6_addr);
 
+/**
+ * @brief  Copy IP addresses
+ *
+ * @param[out] dest destination IP
+ * @param[in]  src source IP
+ */
+static inline void esp_netif_ip_addr_copy(esp_ip_addr_t *dest, const esp_ip_addr_t *src)
+{
+    dest->type = src->type;
+    if (src->type == ESP_IPADDR_TYPE_V6) {
+        dest->u_addr.ip6.addr[0] = src->u_addr.ip6.addr[0];
+        dest->u_addr.ip6.addr[1] = src->u_addr.ip6.addr[1];
+        dest->u_addr.ip6.addr[2] = src->u_addr.ip6.addr[2];
+        dest->u_addr.ip6.addr[3] = src->u_addr.ip6.addr[3];
+        dest->u_addr.ip6.zone = src->u_addr.ip6.zone;
+    } else {
+        dest->u_addr.ip4.addr = src->u_addr.ip4.addr;
+        dest->u_addr.ip6.addr[1] = 0;
+        dest->u_addr.ip6.addr[2] = 0;
+        dest->u_addr.ip6.addr[3] = 0;
+        dest->u_addr.ip6.zone = 0;
+    }
+}
+
 #ifdef __cplusplus
 }
 #endif

+ 19 - 25
components/mdns/mdns.c

@@ -1115,7 +1115,7 @@ static void _mdns_dispatch_tx_packet(mdns_tx_packet_t * p)
 
 #ifdef MDNS_ENABLE_DEBUG
     _mdns_dbg_printf("\nTX[%u][%u]: ", p->tcpip_if, p->ip_protocol);
-    if (p->dst.type == IPADDR_TYPE_V4) {
+    if (p->dst.type == ESP_IPADDR_TYPE_V4) {
         _mdns_dbg_printf("To: " IPSTR ":%u, ", IP2STR(&p->dst.u_addr.ip4), p->port);
     } else {
         _mdns_dbg_printf("To: " IPV6STR ":%u, ", IPV62STR(p->dst.u_addr.ip6), p->port);
@@ -1345,11 +1345,12 @@ static mdns_tx_packet_t * _mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip
     packet->ip_protocol = ip_protocol;
     packet->port = MDNS_SERVICE_PORT;
     if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
-        IP4_ADDR(&packet->dst.u_addr.ip4, 224, 0, 0, 251);
+        esp_ip_addr_t addr = ESP_IP4ADDR_INIT(224, 0, 0, 251);
+        memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
     }
 #if CONFIG_LWIP_IPV6
     else {
-        esp_ip_addr_t addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
+        esp_ip_addr_t addr = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfb000000);
         memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
     }
 #endif
@@ -2973,15 +2974,15 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
 {
     static mdns_name_t n;
     mdns_header_t header;
-    const uint8_t * data = (const uint8_t*)packet->pb->payload;
-    size_t len = packet->pb->len;
+    const uint8_t * data = _mdns_get_packet_data(packet);
+    size_t len = _mdns_get_packet_len(packet);
     const uint8_t * content = data + MDNS_HEAD_LEN;
     bool do_not_reply = false;
     mdns_search_once_t * search_result = NULL;
 
 #ifdef MDNS_ENABLE_DEBUG
     _mdns_dbg_printf("\nRX[%u][%u]: ", packet->tcpip_if, (uint32_t)packet->ip_protocol);
-    if (packet->src.type == IPADDR_TYPE_V4) {
+    if (packet->src.type == ESP_IPADDR_TYPE_V4) {
         _mdns_dbg_printf("From: " IPSTR ":%u, To: " IPSTR ", ", IP2STR(&packet->src.u_addr.ip4), packet->src_port, IP2STR(&packet->dest.u_addr.ip4));
     } else {
         _mdns_dbg_printf("From: " IPV6STR ":%u, To: " IPV6STR ", ", IPV62STR(packet->src.u_addr.ip6), packet->src_port, IPV62STR(packet->dest.u_addr.ip6));
@@ -3023,12 +3024,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
     parsed_packet->authoritative = header.flags.value == MDNS_FLAGS_AUTHORITATIVE;
     parsed_packet->distributed = header.flags.value == MDNS_FLAGS_DISTRIBUTED;
     parsed_packet->id = header.id;
-#if CONFIG_LWIP_IPV6
-    ip_addr_copy(parsed_packet->src, packet->src);
-#else
-    ip4_addr_copy(parsed_packet->src.u_addr.ip4, packet->src.u_addr.ip4);
-#endif
-
+    esp_netif_ip_addr_copy(&parsed_packet->src, &packet->src);
     parsed_packet->src_port = packet->src_port;
 
     if (header.questions) {
@@ -3334,7 +3330,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
 #if CONFIG_LWIP_IPV6
             else if (type == MDNS_TYPE_AAAA) {//ipv6
                 esp_ip_addr_t ip6;
-                ip6.type = IPADDR_TYPE_V6;
+                ip6.type = ESP_IPADDR_TYPE_V6;
                 memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE);
                 if (search_result) {
                     //check for more applicable searches (PTR & A/AAAA at the same time)
@@ -3384,7 +3380,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
 #endif
             else if (type == MDNS_TYPE_A) {
                 esp_ip_addr_t ip;
-                ip.type = IPADDR_TYPE_V4;
+                ip.type = ESP_IPADDR_TYPE_V4;
                 memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4);
                 if (search_result) {
                     //check for more applicable searches (PTR & A/AAAA at the same time)
@@ -3695,7 +3691,7 @@ static mdns_ip_addr_t * _mdns_result_addr_create_ip(esp_ip_addr_t * ip)
     }
     memset(a, 0 , sizeof(mdns_ip_addr_t));
     a->addr.type = ip->type;
-    if (ip->type == IPADDR_TYPE_V6) {
+    if (ip->type == ESP_IPADDR_TYPE_V6) {
         memcpy(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16);
     } else {
         a->addr.u_addr.ip4.addr = ip->u_addr.ip4.addr;
@@ -3711,10 +3707,10 @@ static void _mdns_result_add_ip(mdns_result_t * r, esp_ip_addr_t * ip)
     mdns_ip_addr_t * a = r->addr;
     while (a) {
         if (a->addr.type == ip->type) {
-            if (a->addr.type == IPADDR_TYPE_V4 && a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) {
+            if (a->addr.type == ESP_IPADDR_TYPE_V4 && a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) {
                 return;
             }
-            if (a->addr.type == IPADDR_TYPE_V6 && !memcmp(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) {
+            if (a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) {
                 return;
             }
         }
@@ -3736,8 +3732,8 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char *
     mdns_result_t * r = NULL;
     mdns_ip_addr_t * a = NULL;
 
-    if ((search->type == MDNS_TYPE_A && ip->type == IPADDR_TYPE_V4)
-      || (search->type == MDNS_TYPE_AAAA && ip->type == IPADDR_TYPE_V6)
+    if ((search->type == MDNS_TYPE_A && ip->type == ESP_IPADDR_TYPE_V4)
+      || (search->type == MDNS_TYPE_AAAA && ip->type == ESP_IPADDR_TYPE_V6)
       || search->type == MDNS_TYPE_ANY) {
         r = search->result;
         while (r) {
@@ -4178,8 +4174,7 @@ static void _mdns_free_action(mdns_action_t * action)
         _mdns_free_tx_packet(action->data.tx_handle.packet);
         break;
     case ACTION_RX_HANDLE:
-        pbuf_free(action->data.rx_handle.packet->pb);
-        free(action->data.rx_handle.packet);
+        _mdns_packet_free(action->data.rx_handle.packet);
         break;
     case ACTION_DELEGATE_HOSTNAME_ADD:
         free((char *)action->data.delegate_hostname.hostname);
@@ -4379,8 +4374,7 @@ static void _mdns_execute_action(mdns_action_t * action)
         break;
     case ACTION_RX_HANDLE:
         mdns_parse_packet(action->data.rx_handle.packet);
-        pbuf_free(action->data.rx_handle.packet->pb);
-        free(action->data.rx_handle.packet);
+        _mdns_packet_free(action->data.rx_handle.packet);
         break;
     case ACTION_DELEGATE_HOSTNAME_ADD:
         _mdns_delegate_hostname_add(action->data.delegate_hostname.hostname,
@@ -5421,7 +5415,7 @@ esp_err_t mdns_query_a(const char * name, uint32_t timeout, esp_ip4_addr_t * add
 
     mdns_ip_addr_t * a = result->addr;
     while (a) {
-        if (a->addr.type == IPADDR_TYPE_V4) {
+        if (a->addr.type == ESP_IPADDR_TYPE_V4) {
             addr->addr = a->addr.u_addr.ip4.addr;
             mdns_query_results_free(result);
             return ESP_OK;
@@ -5459,7 +5453,7 @@ esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t *
 
     mdns_ip_addr_t * a = result->addr;
     while (a) {
-        if (a->addr.type == IPADDR_TYPE_V6) {
+        if (a->addr.type == ESP_IPADDR_TYPE_V6) {
             memcpy(addr->addr, a->addr.u_addr.ip6.addr, 16);
             mdns_query_results_free(result);
             return ESP_OK;

+ 25 - 2
components/mdns/mdns_networking.c

@@ -4,11 +4,18 @@
  *
  */
 #include <string.h>
-#include "mdns_networking.h"
 #include "esp_log.h"
+#include "lwip/ip_addr.h"
+#include "lwip/pbuf.h"
+#include "lwip/igmp.h"
+#include "lwip/udp.h"
+#include "lwip/mld6.h"
+#include "lwip/priv/tcpip_priv.h"
+#include "esp_system.h"
+#include "esp_event.h"
+#include "mdns_networking.h"
 #include "esp_netif_net_stack.h"
 
-
 extern mdns_server_t * _mdns_server;
 
 /*
@@ -345,3 +352,19 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c
     }
     return len;
 }
+
+void* _mdns_get_packet_data(mdns_rx_packet_t *packet)
+{
+    return packet->pb->payload;
+}
+
+size_t _mdns_get_packet_len(mdns_rx_packet_t *packet)
+{
+    return packet->pb->len;
+}
+
+void _mdns_packet_free(mdns_rx_packet_t *packet)
+{
+    pbuf_free(packet->pb);
+    free(packet);
+}

+ 15 - 16
components/mdns/private_include/mdns_networking.h

@@ -7,22 +7,6 @@
  */
 #include "mdns.h"
 #include "mdns_private.h"
-#include "sdkconfig.h"
-#include "freertos/FreeRTOS.h"
-#include "freertos/queue.h"
-#include "freertos/semphr.h"
-#include "lwip/ip_addr.h"
-#include "lwip/pbuf.h"
-#include "lwip/igmp.h"
-#include "lwip/udp.h"
-#include "lwip/mld6.h"
-#include "lwip/priv/tcpip_priv.h"
-#include "esp_wifi.h"
-#include "esp_system.h"
-#include "esp_event.h"
-#if CONFIG_ETH_ENABLED
-#include "esp_eth.h"
-#endif
 
 
 /**
@@ -51,4 +35,19 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
  */
 size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t * data, size_t len);
 
+/**
+ * @brief  Gets data pointer to the mDNS packet
+ */
+void* _mdns_get_packet_data(mdns_rx_packet_t *packet);
+
+/**
+ * @brief  Gets data length of c
+ */
+size_t _mdns_get_packet_len(mdns_rx_packet_t *packet);
+
+/**
+ * @brief  Free the  mDNS packet
+ */
+void _mdns_packet_free(mdns_rx_packet_t *packet);
+
 #endif /* ESP_MDNS_NETWORKING_H_ */

+ 1 - 5
components/mdns/private_include/mdns_private.h

@@ -14,12 +14,8 @@
 #ifndef MDNS_PRIVATE_H_
 #define MDNS_PRIVATE_H_
 
-#include "esp_event_base.h"
-#include "esp_task.h"
-#include "esp_timer.h"
-#include "esp_netif_ip_addr.h"
-#include "freertos/FreeRTOS.h"
 #include "mdns.h"
+#include "esp_task.h"
 
 //#define MDNS_ENABLE_DEBUG
 

+ 3 - 3
components/mdns/test_afl_fuzz_host/Makefile

@@ -3,8 +3,8 @@ FUZZ=afl-fuzz
 COMPONENTS_DIR=../..
 COMPILER_ICLUDE_DIR=$(shell echo `which xtensa-esp32-elf-gcc | xargs dirname | xargs dirname`/xtensa-esp32-elf)
 
-CFLAGS=-g -Wno-unused-value -Wno-missing-declarations -Wno-pointer-bool-conversion -Wno-macro-redefined -Wno-int-to-void-pointer-cast -DHOOK_MALLOC_FAILED -DESP_EVENT_H_ -D__ESP_LOG_H__ -DMDNS_TEST_MODE \
-                 -I. -I.. -I../include -I../private_include -I ./build/config -include esp32_compat.h \
+CFLAGS=-g -Wno-unused-value -Wno-missing-declarations -Wno-pointer-bool-conversion -Wno-macro-redefined -Wno-int-to-void-pointer-cast -DHOOK_MALLOC_FAILED -DESP_EVENT_H_ -D__ESP_LOG_H__ \
+                 -I. -I.. -I../include -I../private_include -I ./build/config  \
                  -I$(COMPONENTS_DIR) \
                  -I$(COMPONENTS_DIR)/driver/include \
                  -I$(COMPONENTS_DIR)/esp_common/include \
@@ -49,7 +49,7 @@ else
 endif
 CPP=$(CC)
 LD=$(CC)
-OBJECTS=esp32_mock.o esp_netif_loopback_mock.o mdns.o test.o esp_netif_objects_mock.o
+OBJECTS=esp32_mock.o mdns.o test.o esp_netif_mock.o
 
 OS := $(shell uname)
 ifeq ($(OS),Darwin)

+ 0 - 188
components/mdns/test_afl_fuzz_host/esp32_compat.h

@@ -1,188 +0,0 @@
-// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#ifndef _ESP32_COMPAT_H_
-#define _ESP32_COMPAT_H_
-
-#ifdef MDNS_TEST_MODE
-
-// Not to include
-#define ESP_MDNS_NETWORKING_H_
-#define _TCPIP_ADAPTER_H_
-
-
-#ifdef USE_BSD_STRING
-#include <bsd/string.h>
-#endif
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
-#include <unistd.h>
-#include <signal.h>
-#include <sys/time.h>
-
-#define ESP_FAIL                    -1
-
-#define ESP_ERR_NO_MEM              0x101
-#define ESP_ERR_INVALID_ARG         0x102
-#define ESP_ERR_INVALID_STATE       0x103
-#define ESP_ERR_INVALID_SIZE        0x104
-#define ESP_ERR_NOT_FOUND           0x105
-#define ESP_ERR_NOT_SUPPORTED       0x106
-#define ESP_ERR_TIMEOUT             0x107
-#define ESP_ERR_INVALID_RESPONSE    0x108
-#define ESP_ERR_INVALID_CRC         0x109
-
-#define pdTRUE                      true
-#define pdFALSE                     false
-#define pdPASS			( pdTRUE )
-#define pdFAIL			( pdFALSE )
-
-#define portMAX_DELAY               0xFFFFFFFF
-#define portTICK_PERIOD_MS          1
-#define ESP_LOGW(a,b)
-#define ESP_LOGD(a,b)
-#define ESP_LOGE(a,b,c)
-#define ESP_LOGV(a,b,c,d)
-
-#define LWIP_HDR_PBUF_H
-#define __ESP_SYSTEM_H__
-#define INC_TASK_H
-
-#define pdMS_TO_TICKS(a) a
-#define portTICK_RATE_MS 10
-#define xTaskDelete(a)
-#define vTaskDelete(a)              free(a)
-#define xSemaphoreGive(s)
-#define xSemaphoreTake(s,d)         true
-#define xQueueCreateMutex(s)
-#define _mdns_pcb_init(a,b)         true
-#define _mdns_pcb_deinit(a,b)       true
-#define xSemaphoreCreateMutex()     malloc(1)
-#define xSemaphoreCreateBinary()    malloc(1)
-#define vSemaphoreDelete(s)         free(s)
-#define queueQUEUE_TYPE_MUTEX       ( ( uint8_t ) 1U
-#define xTaskCreatePinnedToCore(a,b,c,d,e,f,g)     *(f) = malloc(1)
-#define vTaskDelay(m)               usleep((m)*0)
-#define pbuf_free(p)                free(p)
-#define esp_random()                (rand()%UINT32_MAX)
-#define tcpip_adapter_get_ip_info(i,d)          true
-#define tcpip_adapter_dhcpc_get_status(a, b)    TCPIP_ADAPTER_DHCP_STARTED
-#define tcpip_adapter_get_ip6_linklocal(i,d)    (ESP_OK)
-#define tcpip_adapter_get_hostname(i, n)        *(n) = "esp32-0123456789AB"
-
-#define IP4_ADDR(ipaddr, a,b,c,d) \
-        (ipaddr)->addr = ((uint32_t)((d) & 0xff) << 24) | \
-                         ((uint32_t)((c) & 0xff) << 16) | \
-                         ((uint32_t)((b) & 0xff) << 8)  | \
-                          (uint32_t)((a) & 0xff)
-#define ip_2_ip6(ipaddr)   (&((ipaddr)->u_addr.ip6))
-#define ip_2_ip4(ipaddr)   (&((ipaddr)->u_addr.ip4))
-
-#define IP_SET_TYPE_VAL(ipaddr, iptype) do { (ipaddr).type = (iptype); }while(0)
-#define IP_ADDR4(ipaddr,a,b,c,d)      do { IP4_ADDR(ip_2_ip4(ipaddr),a,b,c,d); \
-                                           IP_SET_TYPE_VAL(*(ipaddr), IPADDR_TYPE_V4); } while(0)
-#define IP_ADDR6(ipaddr,i0,i1,i2,i3)  do { IP6_ADDR(ip_2_ip6(ipaddr),i0,i1,i2,i3); \
-                                           IP_SET_TYPE_VAL(*(ipaddr), IPADDR_TYPE_V6); } while(0)
-
-#define IPADDR6_INIT(a, b, c, d)      { { { { a, b, c, d } } }, IPADDR_TYPE_V6 }
-
-typedef int32_t esp_err_t;
-
-typedef void * xSemaphoreHandle;
-typedef void * SemaphoreHandle_t;
-typedef void * xQueueHandle;
-typedef void * QueueHandle_t;
-typedef void * TaskHandle_t;
-typedef int    BaseType_t;
-typedef uint32_t TickType_t;
-typedef uint32_t portTickType;
-
-
-extern const char * WIFI_EVENT;
-extern const char * IP_EVENT;
-extern const char * ETH_EVENT;
-
-/* status of DHCP client or DHCP server */
-typedef enum {
-    TCPIP_ADAPTER_DHCP_INIT = 0,    /**< DHCP client/server in initial state */
-    TCPIP_ADAPTER_DHCP_STARTED,     /**< DHCP client/server already been started */
-    TCPIP_ADAPTER_DHCP_STOPPED,     /**< DHCP client/server already been stopped */
-    TCPIP_ADAPTER_DHCP_STATUS_MAX
-} tcpip_adapter_dhcp_status_t;
-
-struct udp_pcb {
-    uint8_t dummy;
-};
-
-struct ip4_addr {
-  uint32_t addr;
-};
-typedef struct ip4_addr ip4_addr_t;
-
-struct ip6_addr {
-  uint32_t addr[4];
-};
-typedef struct ip6_addr ip6_addr_t;
-
-typedef struct {
-    ip4_addr_t ip;
-    ip4_addr_t netmask;
-    ip4_addr_t gw;
-} tcpip_adapter_ip_info_t;
-
-typedef enum {
-    TCPIP_ADAPTER_IF_STA = 0,     /**< ESP32 station interface */
-    TCPIP_ADAPTER_IF_AP,          /**< ESP32 soft-AP interface */
-    TCPIP_ADAPTER_IF_ETH,         /**< ESP32 ethernet interface */
-    TCPIP_ADAPTER_IF_MAX
-} tcpip_adapter_if_t;
-
-typedef struct {
-    ip6_addr_t ip;
-} tcpip_adapter_ip6_info_t;
-
-typedef void* system_event_t;
-
-struct pbuf {
-  struct pbuf *next;
-  void *payload;
-  uint16_t tot_len;
-  uint16_t  len;
-  uint8_t  /*pbuf_type*/ type;
-  uint8_t  flags;
-  uint16_t  ref;
-};
-
-#define IP_GET_TYPE(ipaddr)           ((ipaddr)->type)
-#define IP_IS_V6_VAL(ipaddr)          (IP_GET_TYPE(&ipaddr) == IPADDR_TYPE_V6)
-#define ip4_addr_copy(dest, src) ((dest).addr = (src).addr)
-#define ip6_addr_copy(dest, src) do{(dest).addr[0] = (src).addr[0]; \
-                                    (dest).addr[1] = (src).addr[1]; \
-                                    (dest).addr[2] = (src).addr[2]; \
-                                    (dest).addr[3] = (src).addr[3];}while(0)
-
-#define ip_addr_copy(dest, src)      do{ IP_SET_TYPE_VAL(dest, IP_GET_TYPE(&src)); if(IP_IS_V6_VAL(src)){ \
-  ip6_addr_copy(*ip_2_ip6(&(dest)), *ip_2_ip6(&(src))); }else{ \
-  ip4_addr_copy(*ip_2_ip4(&(dest)), *ip_2_ip4(&(src))); }}while(0)
-
-#include "esp32_mock.h"
-
-uint32_t xTaskGetTickCount(void);
-
-
-#endif //MDNS_TEST_MODE
-
-#endif //_ESP32_COMPAT_H_

+ 1 - 1
components/mdns/test_afl_fuzz_host/esp32_mock.c

@@ -3,7 +3,7 @@
 #include <pthread.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include "esp32_compat.h"
+#include "esp32_mock.h"
 
 void*     g_queue;
 int       g_queue_send_shall_fail = 0;

+ 153 - 6
components/mdns/test_afl_fuzz_host/esp32_mock.h

@@ -1,12 +1,161 @@
-#ifndef ESP32_MOCK_H_
-#define ESP32_MOCK_H_
+// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#ifndef _ESP32_COMPAT_H_
+#define _ESP32_COMPAT_H_
+
+// Skip these include files
+#define ESP_MDNS_NETWORKING_H_
+#define _TCPIP_ADAPTER_H_
+#define _ESP_TASK_H_
+
+#ifdef USE_BSD_STRING
+#include <bsd/string.h>
+#endif
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/time.h>
 #include "esp_timer.h"
 
+#define ESP_FAIL                    -1
+
+#define ESP_ERR_NO_MEM              0x101
+#define ESP_ERR_INVALID_ARG         0x102
+#define ESP_ERR_INVALID_STATE       0x103
+#define ESP_ERR_INVALID_SIZE        0x104
+#define ESP_ERR_NOT_FOUND           0x105
+#define ESP_ERR_NOT_SUPPORTED       0x106
+#define ESP_ERR_TIMEOUT             0x107
+#define ESP_ERR_INVALID_RESPONSE    0x108
+#define ESP_ERR_INVALID_CRC         0x109
+
+#define pdTRUE                      true
+#define pdFALSE                     false
+#define pdPASS			( pdTRUE )
+#define pdFAIL			( pdFALSE )
+
+#define portMAX_DELAY               0xFFFFFFFF
+#define portTICK_PERIOD_MS          1
+#define ESP_LOGW(a,b)
+#define ESP_LOGD(a,b)
+#define ESP_LOGE(a,b,c)
+#define ESP_LOGV(a,b,c,d)
+
+#define LWIP_HDR_PBUF_H
+#define __ESP_SYSTEM_H__
+#define INC_TASK_H
+
+#define pdMS_TO_TICKS(a) a
+#define portTICK_RATE_MS 10
+#define xSemaphoreTake(s,d)        true
+#define xTaskDelete(a)
+#define vTaskDelete(a)             free(a)
+#define xSemaphoreGive(s)
+#define xQueueCreateMutex(s)
+#define _mdns_pcb_init(a,b)         true
+#define _mdns_pcb_deinit(a,b)       true
+#define xSemaphoreCreateMutex()     malloc(1)
+#define xSemaphoreCreateBinary()    malloc(1)
+#define vSemaphoreDelete(s)         free(s)
+#define queueQUEUE_TYPE_MUTEX       ( ( uint8_t ) 1U
+#define xTaskCreatePinnedToCore(a,b,c,d,e,f,g)     *(f) = malloc(1)
+#define vTaskDelay(m)               usleep((m)*0)
+#define esp_random()                (rand()%UINT32_MAX)
+
+
+#define ESP_TASK_PRIO_MAX 25
+#define ESP_TASKD_EVENT_PRIO 5
+#define _mdns_udp_pcb_write(tcpip_if, ip_protocol, ip, port, data, len) len
+#define xTaskHandle TaskHandle_t
+
+
+typedef int32_t esp_err_t;
+
+typedef void * xSemaphoreHandle;
+typedef void * SemaphoreHandle_t;
+typedef void * xQueueHandle;
+typedef void * QueueHandle_t;
+typedef void * TaskHandle_t;
+typedef int    BaseType_t;
+typedef uint32_t TickType_t;
+typedef uint32_t portTickType;
+
+
+extern const char * WIFI_EVENT;
+extern const char * IP_EVENT;
+extern const char * ETH_EVENT;
+
+/* status of DHCP client or DHCP server */
+typedef enum {
+    TCPIP_ADAPTER_DHCP_INIT = 0,    /**< DHCP client/server in initial state */
+    TCPIP_ADAPTER_DHCP_STARTED,     /**< DHCP client/server already been started */
+    TCPIP_ADAPTER_DHCP_STOPPED,     /**< DHCP client/server already been stopped */
+    TCPIP_ADAPTER_DHCP_STATUS_MAX
+} tcpip_adapter_dhcp_status_t;
+
+struct udp_pcb {
+    uint8_t dummy;
+};
+
+struct ip4_addr {
+  uint32_t addr;
+};
+typedef struct ip4_addr ip4_addr_t;
+
+struct ip6_addr {
+  uint32_t addr[4];
+};
+typedef struct ip6_addr ip6_addr_t;
+
+typedef struct {
+    ip4_addr_t ip;
+    ip4_addr_t netmask;
+    ip4_addr_t gw;
+} tcpip_adapter_ip_info_t;
+
+typedef enum {
+    TCPIP_ADAPTER_IF_STA = 0,     /**< ESP32 station interface */
+    TCPIP_ADAPTER_IF_AP,          /**< ESP32 soft-AP interface */
+    TCPIP_ADAPTER_IF_ETH,         /**< ESP32 ethernet interface */
+    TCPIP_ADAPTER_IF_MAX
+} tcpip_adapter_if_t;
+
+typedef struct {
+    ip6_addr_t ip;
+} tcpip_adapter_ip6_info_t;
+
+typedef void* system_event_t;
+
+struct pbuf {
+  struct pbuf *next;
+  void *payload;
+  uint16_t tot_len;
+  uint16_t  len;
+  uint8_t  /*pbuf_type*/ type;
+  uint8_t  flags;
+  uint16_t  ref;
+};
+
+uint32_t xTaskGetTickCount(void);
 typedef void (*esp_timer_cb_t)(void* arg);
 
 // Queue mock
 QueueHandle_t xQueueCreate( uint32_t uxQueueLength,
-                             uint32_t uxItemSize );
+                            uint32_t uxItemSize );
 
 void vQueueDelete( QueueHandle_t xQueue );
 
@@ -22,11 +171,9 @@ esp_err_t esp_event_handler_register(const char * event_base, int32_t event_id,
 
 esp_err_t esp_event_handler_unregister(const char * event_base, int32_t event_id, void* event_handler);
 
-#define _mdns_udp_pcb_write(tcpip_if, ip_protocol, ip, port, data, len) len
 
-// Task signify mock
 TaskHandle_t xTaskGetCurrentTaskHandle(void);
 void xTaskNotifyGive(TaskHandle_t task);
 BaseType_t xTaskNotifyWait(uint32_t bits_entry_clear, uint32_t bits_exit_clear, uint32_t *value, TickType_t wait_time );
 
-#endif /* ESP32_MOCK_H_ */
+#endif //_ESP32_COMPAT_H_

+ 0 - 36
components/mdns/test_afl_fuzz_host/esp_netif_loopback_mock.c

@@ -1,36 +0,0 @@
-// Copyright 2020 Espressif Systems (Shanghai) CO LTD
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "esp32_compat.h"
-#include "esp_netif_lwip_internal.h"
-
-esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info)
-{
-    return ESP_ERR_NOT_SUPPORTED;
-}
-
-esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status)
-{
-    return ESP_ERR_NOT_SUPPORTED;
-}
-
-const char *esp_netif_get_ifkey(esp_netif_t *esp_netif)
-{
-    return NULL;
-}
-
-esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6)
-{
-    return ESP_ERR_NOT_SUPPORTED;
-}

+ 19 - 25
components/mdns/test_afl_fuzz_host/esp_netif_objects_mock.c → components/mdns/test_afl_fuzz_host/esp_netif_mock.c

@@ -12,47 +12,31 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "esp_netif.h"
-#include "sys/queue.h"
-#include "esp_log.h"
-#include "esp_netif_private.h"
+#include <stdio.h>
 #include <string.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "esp32_mock.h"
 
-//
-// Purpose of this module is to provide list of esp-netif structures
-//  - this module has no dependency on a specific network stack (lwip)
-//
+typedef struct esp_netif_s esp_netif_t;
+typedef struct esp_netif_ip_info esp_netif_ip_info_t;
+typedef struct esp_netif_dhcp_status esp_netif_dhcp_status_t;
 
-struct slist_netifs_s {
-    esp_netif_t *netif;
-    SLIST_ENTRY(slist_netifs_s) next;
-};
 
-SLIST_HEAD(slisthead, slist_netifs_s) s_head = { .slh_first = NULL, };
+const char * IP_EVENT = "IP_EVENT";
 
-static size_t s_esp_netif_counter = 0;
 
-ESP_EVENT_DEFINE_BASE(IP_EVENT);
-
-//
-// List manipulation functions
-//
 esp_err_t esp_netif_add_to_list(esp_netif_t *netif)
 {
     return ESP_OK;
 }
 
-
 esp_err_t esp_netif_remove_from_list(esp_netif_t *netif)
 {
     return ESP_ERR_NOT_FOUND;
 }
 
-size_t esp_netif_get_nr_of_ifs(void)
-{
-    return s_esp_netif_counter;
-}
-
 esp_netif_t* esp_netif_next(esp_netif_t* netif)
 {
     return NULL;
@@ -67,3 +51,13 @@ esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key)
 {
     return NULL;
 }
+
+esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info)
+{
+    return ESP_ERR_NOT_SUPPORTED;
+}
+
+esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status)
+{
+    return ESP_ERR_NOT_SUPPORTED;
+}

+ 20 - 4
components/mdns/test_afl_fuzz_host/mdns_mock.h

@@ -1,5 +1,21 @@
 #pragma once
-#define ERR_OK                        0
-#define IPADDR_TYPE_V4                0U
-#define IPADDR_TYPE_V6                6U
-#define IPADDR_TYPE_ANY               46U
+#include "esp32_mock.h"
+#include "mdns.h"
+#include "mdns_private.h"
+
+
+static inline void* _mdns_get_packet_data(mdns_rx_packet_t *packet)
+{
+    return packet->pb->payload;
+}
+
+static inline size_t _mdns_get_packet_len(mdns_rx_packet_t *packet)
+{
+    return packet->pb->len;
+}
+
+static inline void _mdns_packet_free(mdns_rx_packet_t *packet)
+{
+    free(packet->pb);
+    free(packet);
+}

+ 1 - 0
components/mdns/test_afl_fuzz_host/test.c

@@ -18,6 +18,7 @@
 #include <signal.h>
 #include <string.h>
 
+#include "esp32_mock.h"
 #include "mdns.h"
 #include "mdns_private.h"
 

+ 9 - 10
examples/protocols/mdns/mdns_example_test.py

@@ -71,10 +71,12 @@ def mdns_server(esp_host):
     while not stop_mdns_server.is_set():
         try:
             current_time = time.time()
-            if not esp_answered.is_set() and current_time - last_query_timepoint > QUERY_TIMEOUT:
-                sock.sendto(get_dns_query_for_esp(esp_host), (MCAST_GRP,UDP_PORT))
-                sock.sendto(get_dns_query_for_esp(esp_host + '-delegated'), (MCAST_GRP,UDP_PORT))
+            if current_time - last_query_timepoint > QUERY_TIMEOUT:
                 last_query_timepoint = current_time
+                if not esp_answered.is_set():
+                    sock.sendto(get_dns_query_for_esp(esp_host), (MCAST_GRP,UDP_PORT))
+                if not esp_delegated_answered.is_set():
+                    sock.sendto(get_dns_query_for_esp(esp_host + '-delegated'), (MCAST_GRP,UDP_PORT))
             timeout = max(0, QUERY_TIMEOUT - (current_time - last_query_timepoint))
             read_socks, _, _ = select.select([sock], [], [], timeout)
             if not read_socks:
@@ -119,19 +121,16 @@ def test_examples_protocol_mdns(env, extra_data):
     # 1. start mdns application
     dut1.start_app()
     # 2. get the dut host name (and IP address)
-    specific_host = dut1.expect(re.compile(r'mdns hostname set to: \[([^\]]+)\]'), timeout=30)
-    specific_host = str(specific_host[0])
-    thread1 = Thread(target=mdns_server, args=(specific_host,))
-    thread1.start()
+    specific_host = dut1.expect(re.compile(r'mdns hostname set to: \[([^\]]+)\]'), timeout=30)[0]
+    mdns_responder = Thread(target=mdns_server, args=(str(specific_host),))
     try:
         ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)[0]
         console_log('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
-        stop_mdns_server.set()
-        thread1.join()
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
     try:
         # 3. check the mdns name is accessible
+        mdns_responder.start()
         if not esp_answered.wait(timeout=30):
             raise ValueError('Test has failed: did not receive mdns answer within timeout')
         if not esp_delegated_answered.wait(timeout=30):
@@ -149,7 +148,7 @@ def test_examples_protocol_mdns(env, extra_data):
                              "Output should've contained DUT's IP address:{}".format(ip_address))
     finally:
         stop_mdns_server.set()
-        thread1.join()
+        mdns_responder.join()
 
 
 if __name__ == '__main__':

+ 1 - 1
tools/gen_esp_err_to_name.py

@@ -39,7 +39,7 @@ import textwrap
 from io import open
 
 # list files here which should not be parsed
-ignore_files = [os.path.join('components', 'mdns', 'test_afl_fuzz_host', 'esp32_compat.h'),
+ignore_files = [os.path.join('components', 'mdns', 'test_afl_fuzz_host', 'esp32_mock.h'),
                 # tcpip_adapter in compatibility mode from 4.1 (errors reused in esp-netif)
                 os.path.join('components', 'tcpip_adapter', 'include', 'tcpip_adapter_types.h')
                 ]