|
|
@@ -1,87 +1,97 @@
|
|
|
-#include "no_warn_host.h"
|
|
|
+/*
|
|
|
+ * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
|
|
+ *
|
|
|
+ * SPDX-License-Identifier: Apache-2.0
|
|
|
+ */
|
|
|
+#include <stdint.h>
|
|
|
+#include <string.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#ifndef NOT_MOCK_DNS
|
|
|
+#include "lwip/dns.h"
|
|
|
+#endif
|
|
|
+#include "lwip/etharp.h"
|
|
|
+#include "lwip/mem.h"
|
|
|
+#include "lwip/netif.h"
|
|
|
#include "lwip/opt.h"
|
|
|
#include "lwip/pbuf.h"
|
|
|
+#include "lwip/timeouts.h"
|
|
|
#include "lwip/udp.h"
|
|
|
-#include "esp_netif.h"
|
|
|
#include "lwip/timeouts.h"
|
|
|
-#include <string.h>
|
|
|
+#include "esp32_mock.h"
|
|
|
+#include "no_warn_host.h"
|
|
|
+
|
|
|
+#define ESP_OK 0
|
|
|
+
|
|
|
+/* ---------------------------------------------------- Variables ------------------------------------------------------
|
|
|
+ *
|
|
|
+ * ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
+// -------------------- LWIP Globals -----------------------
|
|
|
+
|
|
|
+// ip_addr.h
|
|
|
const ip_addr_t ip_addr_any;
|
|
|
const ip_addr_t ip_addr_broadcast;
|
|
|
const ip_addr_t ip_addr_any_type;
|
|
|
+// ip.h
|
|
|
struct ip_globals ip_data;
|
|
|
+// netif.h
|
|
|
struct netif *netif_list;
|
|
|
-struct udp_pcb mock_pcb;
|
|
|
+
|
|
|
+// ---------------- AFL Host Test Globals ------------------
|
|
|
+
|
|
|
+// dns_di.h
|
|
|
uint32_t g_random_numbers[8] = {0};
|
|
|
uint32_t g_random_numbers_cnt = 0;
|
|
|
|
|
|
+// ----------------------- Locals --------------------------
|
|
|
|
|
|
-struct pbuf* pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset)
|
|
|
-{
|
|
|
- u16_t offset_left = in_offset;
|
|
|
- struct pbuf* q = in;
|
|
|
+struct udp_pcb mock_pcb;
|
|
|
|
|
|
- /* get the correct pbuf */
|
|
|
- while ((q != NULL) && (q->len <= offset_left)) {
|
|
|
- offset_left -= q->len;
|
|
|
- q = q->next;
|
|
|
- }
|
|
|
- if (out_offset != NULL) {
|
|
|
- *out_offset = offset_left;
|
|
|
- }
|
|
|
- return q;
|
|
|
-}
|
|
|
+/* ---------------------------------------------------- LWIP Mock ------------------------------------------------------
|
|
|
+ *
|
|
|
+ * ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
-int pbuf_try_get_at(const struct pbuf* p, u16_t offset)
|
|
|
-{
|
|
|
- u16_t q_idx;
|
|
|
- struct pbuf* q = pbuf_skip(p, offset, &q_idx);
|
|
|
+// --------------------- lwip/dns.h ------------------------
|
|
|
|
|
|
- /* return requested data if pbuf is OK */
|
|
|
- if ((q != NULL) && (q->len > q_idx)) {
|
|
|
- return ((u8_t*)q->payload)[q_idx];
|
|
|
- }
|
|
|
- return -1;
|
|
|
+#ifndef NOT_MOCK_DNS
|
|
|
+void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
|
|
|
+{
|
|
|
}
|
|
|
+#endif
|
|
|
|
|
|
-void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data)
|
|
|
-{
|
|
|
- u16_t q_idx;
|
|
|
- struct pbuf* q = pbuf_skip(p, offset, &q_idx);
|
|
|
+// -------------------- lwip/etharp.h ----------------------
|
|
|
|
|
|
- /* write requested data if pbuf is OK */
|
|
|
- if ((q != NULL) && (q->len > q_idx)) {
|
|
|
- ((u8_t*)q->payload)[q_idx] = data;
|
|
|
- }
|
|
|
+err_t etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q)
|
|
|
+{
|
|
|
+ return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-u8_t pbuf_get_at(const struct pbuf* p, u16_t offset)
|
|
|
-{
|
|
|
- u16_t q_idx;
|
|
|
- struct pbuf* q = pbuf_skip(p, offset, &q_idx);
|
|
|
+// --------------------- lwip/mem.h ------------------------
|
|
|
|
|
|
- /* return requested data if pbuf is OK */
|
|
|
- if ((q != NULL) && (q->len > q_idx)) {
|
|
|
- return ((u8_t*)q->payload)[q_idx];
|
|
|
- }
|
|
|
- return 0;
|
|
|
+void * mem_malloc(mem_size_t size)
|
|
|
+{
|
|
|
+ return malloc(size);
|
|
|
}
|
|
|
|
|
|
-err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
|
|
|
+void * mem_calloc(size_t nr, mem_size_t size)
|
|
|
{
|
|
|
- return ERR_OK;
|
|
|
+ return calloc(nr, size);
|
|
|
}
|
|
|
|
|
|
-err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
|
|
|
+void mem_free(void *rmem)
|
|
|
{
|
|
|
- return ERR_OK;
|
|
|
+ free(rmem);
|
|
|
}
|
|
|
|
|
|
-struct udp_pcb * udp_new_ip_type(u8_t type)
|
|
|
+// -------------------- lwip/netif.h -----------------------
|
|
|
+
|
|
|
+void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
|
|
|
+ const ip4_addr_t *gw)
|
|
|
{
|
|
|
- return &mock_pcb;
|
|
|
}
|
|
|
|
|
|
+// --------------------- lwip/pbuf.h -----------------------
|
|
|
+
|
|
|
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
|
|
|
{
|
|
|
struct pbuf * p;
|
|
|
@@ -94,6 +104,18 @@ struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
|
|
|
return p;
|
|
|
}
|
|
|
|
|
|
+void pbuf_realloc(struct pbuf *p, u16_t size)
|
|
|
+{
|
|
|
+ if (p != NULL)
|
|
|
+ {
|
|
|
+ uint8_t *buf = malloc(size);
|
|
|
+ free(p->payload);
|
|
|
+ p->payload = buf;
|
|
|
+ p->len = size;
|
|
|
+ p->tot_len = size;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
u8_t pbuf_free(struct pbuf *p)
|
|
|
{
|
|
|
if (p) {
|
|
|
@@ -107,71 +129,6 @@ u8_t pbuf_free(struct pbuf *p)
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
-err_t udp_sendto(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port)
|
|
|
-{
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
-void udp_remove(struct udp_pcb *pcb)
|
|
|
-{
|
|
|
- if (pcb == NULL)
|
|
|
- {
|
|
|
- free(pcb);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-struct udp_pcb *udp_new(void)
|
|
|
-{
|
|
|
- return malloc(sizeof(struct udp_pcb));
|
|
|
-}
|
|
|
-
|
|
|
-err_t udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
|
|
|
-{
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
-void udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-void udp_disconnect(struct udp_pcb *pcb)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-#ifndef NOT_MOCK_DNS
|
|
|
-void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
|
|
|
-{
|
|
|
-}
|
|
|
-#endif
|
|
|
-
|
|
|
-uint32_t esp_random(void)
|
|
|
-{
|
|
|
- // Preparation for injecting favorable random numbers
|
|
|
- return g_random_numbers[g_random_numbers_cnt++ % 8];
|
|
|
-}
|
|
|
-
|
|
|
-err_t etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q)
|
|
|
-{
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
-void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
|
|
|
- const ip4_addr_t *gw)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-void pbuf_realloc(struct pbuf *p, u16_t size)
|
|
|
-{
|
|
|
- if (p != NULL)
|
|
|
- {
|
|
|
- uint8_t *buf = malloc(size);
|
|
|
- free(p->payload);
|
|
|
- p->payload = buf;
|
|
|
- p->len = size;
|
|
|
- p->tot_len = size;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
|
|
|
{
|
|
|
struct pbuf *p;
|
|
|
@@ -209,40 +166,138 @@ u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t
|
|
|
return copied_total;
|
|
|
}
|
|
|
|
|
|
-err_t udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
|
|
|
+err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
|
|
|
+{
|
|
|
+ return ERR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
|
|
|
+{
|
|
|
+ return ERR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+struct pbuf* pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset)
|
|
|
+{
|
|
|
+ u16_t offset_left = in_offset;
|
|
|
+ struct pbuf* q = in;
|
|
|
+
|
|
|
+ /* get the correct pbuf */
|
|
|
+ while ((q != NULL) && (q->len <= offset_left)) {
|
|
|
+ offset_left -= q->len;
|
|
|
+ q = q->next;
|
|
|
+ }
|
|
|
+ if (out_offset != NULL) {
|
|
|
+ *out_offset = offset_left;
|
|
|
+ }
|
|
|
+ return q;
|
|
|
+}
|
|
|
+
|
|
|
+u8_t pbuf_get_at(const struct pbuf* p, u16_t offset)
|
|
|
+{
|
|
|
+ u16_t q_idx;
|
|
|
+ struct pbuf* q = pbuf_skip(p, offset, &q_idx);
|
|
|
+
|
|
|
+ /* return requested data if pbuf is OK */
|
|
|
+ if ((q != NULL) && (q->len > q_idx)) {
|
|
|
+ return ((u8_t*)q->payload)[q_idx];
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int pbuf_try_get_at(const struct pbuf* p, u16_t offset)
|
|
|
+{
|
|
|
+ u16_t q_idx;
|
|
|
+ struct pbuf* q = pbuf_skip(p, offset, &q_idx);
|
|
|
+
|
|
|
+ /* return requested data if pbuf is OK */
|
|
|
+ if ((q != NULL) && (q->len > q_idx)) {
|
|
|
+ return ((u8_t*)q->payload)[q_idx];
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data)
|
|
|
+{
|
|
|
+ u16_t q_idx;
|
|
|
+ struct pbuf* q = pbuf_skip(p, offset, &q_idx);
|
|
|
+
|
|
|
+ /* write requested data if pbuf is OK */
|
|
|
+ if ((q != NULL) && (q->len > q_idx)) {
|
|
|
+ ((u8_t*)q->payload)[q_idx] = data;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// ------------------- lwip/timeouts.h ---------------------
|
|
|
+
|
|
|
+void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+void sys_untimeout(sys_timeout_handler handler, void *arg)
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+// --------------------- lwip/udp.h ------------------------
|
|
|
+
|
|
|
+struct udp_pcb *udp_new(void)
|
|
|
+{
|
|
|
+ return malloc(sizeof(struct udp_pcb));
|
|
|
+}
|
|
|
+
|
|
|
+struct udp_pcb * udp_new_ip_type(u8_t type)
|
|
|
+{
|
|
|
+ return &mock_pcb;
|
|
|
+}
|
|
|
+
|
|
|
+void udp_remove(struct udp_pcb *pcb)
|
|
|
+{
|
|
|
+ if (pcb == NULL)
|
|
|
+ {
|
|
|
+ free(pcb);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+err_t udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
|
|
|
{
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-err_t udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
|
|
|
+err_t udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
|
|
|
{
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-err_t udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip)
|
|
|
+err_t udp_sendto(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port)
|
|
|
{
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-void * mem_malloc(mem_size_t size)
|
|
|
+void udp_disconnect(struct udp_pcb *pcb)
|
|
|
{
|
|
|
- return malloc(size);
|
|
|
}
|
|
|
|
|
|
-void * mem_calloc(size_t nr, mem_size_t size)
|
|
|
+void udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
|
|
|
{
|
|
|
- return calloc(nr, size);
|
|
|
}
|
|
|
|
|
|
-void mem_free(void *rmem)
|
|
|
+err_t udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
|
|
|
{
|
|
|
- free(rmem);
|
|
|
+ return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
|
|
|
+err_t udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip)
|
|
|
{
|
|
|
+ return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-void sys_untimeout(sys_timeout_handler handler, void *arg)
|
|
|
+/* ------------------------------------------------- ESP32 Port Mock ---------------------------------------------------
|
|
|
+ *
|
|
|
+ * ------------------------------------------------------------------------------------------------------------------ */
|
|
|
+
|
|
|
+// --------------------- lwipopts.h ------------------------
|
|
|
+
|
|
|
+uint32_t esp_random(void)
|
|
|
{
|
|
|
+ // Preparation for injecting favorable random numbers
|
|
|
+ return g_random_numbers[g_random_numbers_cnt++ % 8];
|
|
|
}
|