2022-04-28 11:04:07 -04:00
|
|
|
/*
|
2022-07-22 04:14:37 -04:00
|
|
|
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
2022-04-28 11:04:07 -04:00
|
|
|
*
|
|
|
|
* 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"
|
2018-05-14 09:03:37 -04:00
|
|
|
#include "lwip/opt.h"
|
|
|
|
#include "lwip/pbuf.h"
|
2022-04-28 11:04:07 -04:00
|
|
|
#include "lwip/timeouts.h"
|
2018-05-14 09:03:37 -04:00
|
|
|
#include "lwip/udp.h"
|
2022-01-24 03:58:06 -05:00
|
|
|
#include "lwip/timeouts.h"
|
2022-04-28 11:04:07 -04:00
|
|
|
#include "no_warn_host.h"
|
|
|
|
|
|
|
|
#define ESP_OK 0
|
|
|
|
|
|
|
|
/* ---------------------------------------------------- Variables ------------------------------------------------------
|
|
|
|
*
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
2018-05-15 09:49:10 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
// -------------------- LWIP Globals -----------------------
|
|
|
|
|
|
|
|
// ip_addr.h
|
2018-05-15 09:49:10 -04:00
|
|
|
const ip_addr_t ip_addr_any;
|
|
|
|
const ip_addr_t ip_addr_broadcast;
|
2018-05-18 09:54:08 -04:00
|
|
|
const ip_addr_t ip_addr_any_type;
|
2022-04-28 11:04:07 -04:00
|
|
|
// ip.h
|
2018-05-15 09:49:10 -04:00
|
|
|
struct ip_globals ip_data;
|
2022-04-28 11:04:07 -04:00
|
|
|
// netif.h
|
2018-05-15 09:49:10 -04:00
|
|
|
struct netif *netif_list;
|
2022-04-28 11:04:07 -04:00
|
|
|
|
|
|
|
// ---------------- AFL Host Test Globals ------------------
|
|
|
|
|
|
|
|
// dns_di.h
|
2018-05-18 09:54:08 -04:00
|
|
|
uint32_t g_random_numbers[8] = {0};
|
|
|
|
uint32_t g_random_numbers_cnt = 0;
|
2018-05-15 09:49:10 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
// ----------------------- Locals --------------------------
|
2018-05-14 09:03:37 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
struct udp_pcb mock_pcb;
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
/* ---------------------------------------------------- LWIP Mock ------------------------------------------------------
|
|
|
|
*
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
// --------------------- lwip/dns.h ------------------------
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
#ifndef NOT_MOCK_DNS
|
|
|
|
void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
|
|
|
|
{
|
2018-05-18 09:54:08 -04:00
|
|
|
}
|
2022-04-28 11:04:07 -04:00
|
|
|
#endif
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
// -------------------- lwip/etharp.h ----------------------
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
err_t etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q)
|
|
|
|
{
|
|
|
|
return ESP_OK;
|
2018-05-18 09:54:08 -04:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
// --------------------- lwip/mem.h ------------------------
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
void * mem_malloc(mem_size_t size)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
2018-05-18 09:54:08 -04:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
void * mem_calloc(size_t nr, mem_size_t size)
|
2018-05-18 09:54:08 -04:00
|
|
|
{
|
2022-04-28 11:04:07 -04:00
|
|
|
return calloc(nr, size);
|
2018-05-18 09:54:08 -04:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
void mem_free(void *rmem)
|
2018-05-14 09:03:37 -04:00
|
|
|
{
|
2022-04-28 11:04:07 -04:00
|
|
|
free(rmem);
|
2018-05-14 09:03:37 -04:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
// -------------------- 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)
|
2018-05-14 09:03:37 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
// --------------------- lwip/pbuf.h -----------------------
|
|
|
|
|
2018-05-14 09:03:37 -04:00
|
|
|
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
|
|
|
|
{
|
|
|
|
struct pbuf * p;
|
|
|
|
p = (struct pbuf *)malloc(MEMP_PBUF_POOL);
|
|
|
|
p->tot_len = length;
|
|
|
|
p->next = NULL;
|
2019-06-28 08:12:10 -04:00
|
|
|
p->type_internal = PBUF_POOL;
|
2018-05-14 09:03:37 -04:00
|
|
|
p->len = length;
|
|
|
|
p->payload = malloc(length);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 09:03:37 -04:00
|
|
|
u8_t pbuf_free(struct pbuf *p)
|
|
|
|
{
|
|
|
|
if (p) {
|
|
|
|
if (p->payload) {
|
|
|
|
free(p->payload);
|
|
|
|
p->payload = NULL;
|
|
|
|
}
|
|
|
|
free (p);
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-14 08:30:03 -04:00
|
|
|
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
|
2018-05-15 09:49:10 -04:00
|
|
|
{
|
2018-09-14 08:30:03 -04:00
|
|
|
struct pbuf *p;
|
|
|
|
u16_t left;
|
|
|
|
u16_t buf_copy_len;
|
|
|
|
u16_t copied_total = 0;
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2018-09-14 08:30:03 -04:00
|
|
|
LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
|
|
|
|
LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2018-09-14 08:30:03 -04:00
|
|
|
left = 0;
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2018-09-14 08:30:03 -04:00
|
|
|
if ((buf == NULL) || (dataptr == NULL)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
|
|
|
|
for (p = buf; len != 0 && p != NULL; p = p->next) {
|
|
|
|
if ((offset != 0) && (offset >= p->len)) {
|
|
|
|
/* don't copy from this buffer -> on to the next */
|
|
|
|
offset -= p->len;
|
|
|
|
} else {
|
|
|
|
/* copy from this buffer. maybe only partially. */
|
|
|
|
buf_copy_len = p->len - offset;
|
|
|
|
if (buf_copy_len > len)
|
|
|
|
buf_copy_len = len;
|
|
|
|
/* copy the necessary parts of the buffer */
|
|
|
|
MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
|
|
|
|
copied_total += buf_copy_len;
|
|
|
|
left += buf_copy_len;
|
|
|
|
len -= buf_copy_len;
|
|
|
|
offset = 0;
|
|
|
|
}
|
2018-05-18 09:54:08 -04:00
|
|
|
}
|
2018-09-14 08:30:03 -04:00
|
|
|
return copied_total;
|
2018-05-15 09:49:10 -04:00
|
|
|
}
|
2018-05-18 09:54:08 -04:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
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)
|
2018-05-15 09:49:10 -04:00
|
|
|
{
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
err_t udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
|
2018-05-15 09:49:10 -04:00
|
|
|
{
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
err_t udp_sendto(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port)
|
2018-05-15 09:49:10 -04:00
|
|
|
{
|
|
|
|
return ESP_OK;
|
2018-09-14 08:30:03 -04:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
void udp_disconnect(struct udp_pcb *pcb)
|
2018-09-14 08:30:03 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
void udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
|
2022-02-02 11:30:20 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
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)
|
2018-09-14 08:30:03 -04:00
|
|
|
{
|
2022-04-28 11:04:07 -04:00
|
|
|
return ESP_OK;
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|
2022-01-24 03:58:06 -05:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
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)
|
2022-01-24 03:58:06 -05:00
|
|
|
{
|
2022-04-28 11:04:07 -04:00
|
|
|
return ESP_OK;
|
2022-01-24 03:58:06 -05:00
|
|
|
}
|
2022-01-24 11:41:06 -05:00
|
|
|
|
2022-04-28 11:04:07 -04:00
|
|
|
/* ------------------------------------------------- ESP32 Port Mock ---------------------------------------------------
|
|
|
|
*
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
// --------------------- lwipopts.h ------------------------
|
|
|
|
|
|
|
|
uint32_t esp_random(void)
|
2022-01-24 11:41:06 -05:00
|
|
|
{
|
2022-04-28 11:04:07 -04:00
|
|
|
// Preparation for injecting favorable random numbers
|
|
|
|
return g_random_numbers[g_random_numbers_cnt++ % 8];
|
2022-01-24 11:41:06 -05:00
|
|
|
}
|
2022-03-31 05:51:56 -04:00
|
|
|
|
|
|
|
void dhcp_parse_extra_opts(struct dhcp *dhcp, uint8_t state, uint8_t option, uint8_t len, struct pbuf* p, uint16_t offset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void dhcp_append_extra_opts(struct netif *netif, uint8_t state, struct dhcp_msg *msg_out, uint16_t *options_out_len)
|
|
|
|
{
|
|
|
|
}
|