From 8c368827726dcdd6ee8f40dd2091feb1db4e71f8 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 25 Jul 2022 16:28:55 +0200 Subject: [PATCH] Show the packets arriving at LwIP --- ESPTest/include/lwip_hooks.h | 3 +++ ESPTest/src/lwip_hooks.c | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ESPTest/include/lwip_hooks.h b/ESPTest/include/lwip_hooks.h index f261ee9..13ac3a2 100644 --- a/ESPTest/include/lwip_hooks.h +++ b/ESPTest/include/lwip_hooks.h @@ -3,7 +3,10 @@ #include "lwip/netif.h" #include "lwip/pbuf.h" +#include "lwip/ip4.h" + #include "esp_log.h" +#include "esp_err.h" #ifdef __cplusplus extern "C" diff --git a/ESPTest/src/lwip_hooks.c b/ESPTest/src/lwip_hooks.c index 72e39ba..e0adda7 100644 --- a/ESPTest/src/lwip_hooks.c +++ b/ESPTest/src/lwip_hooks.c @@ -1,7 +1,28 @@ #include "lwip_hooks.h" +const char *get_protocol(u16_t type) +{ + switch (type) + { + case 1: + return "ICMP"; + case 6: + return "TCP"; + case 17: + return "UDP"; + default: + return "-"; + } +} + int lwip_hook_ip4_input(struct pbuf *pbuf, struct netif *input_netif) { - ESP_LOGI("Hook", "Testing..."); - return 0; + const struct ip_hdr *iphdr; + char ip_address[IP_HLEN]; + + iphdr = (struct ip_hdr *)pbuf->payload; + sprintf(ip_address, "%d.%d.%d.%d", ip4_addr1_16_val(iphdr->src), ip4_addr2_16_val(iphdr->src), ip4_addr3_16_val(iphdr->src), ip4_addr4_16_val(iphdr->src)); + ESP_LOGI("HOOK", "%s: %s", + get_protocol((u16_t)IPH_PROTO(iphdr)), ip_address); + return ESP_OK; }