From 0e724eb5b14aa7032436771c9ef4c02231beab9a Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Thu, 26 Oct 2023 19:37:55 +0200 Subject: [PATCH] initial commit (shoulda done this earlier) --- .gitignore | 1 + CMakeLists.txt | 3 +++ include/lwip_hooks.h | 27 +++++++++++++++++++++++ platformio.ini | 10 +++++++++ src/CMakeLists.txt | 6 +++++ src/lwip_hooks.c | 50 ++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 include/lwip_hooks.h create mode 100644 platformio.ini create mode 100644 src/CMakeLists.txt create mode 100644 src/lwip_hooks.c create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..16539a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6a44ca2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.16.0) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(idf_hello_world) diff --git a/include/lwip_hooks.h b/include/lwip_hooks.h new file mode 100644 index 0000000..5bc81be --- /dev/null +++ b/include/lwip_hooks.h @@ -0,0 +1,27 @@ +// file: +#ifndef _LWIP_HOOKS_H_ +#define _LWIP_HOOKS_H_ + +#include "lwip/netif.h" +#include "lwip/pbuf.h" +#include "lwip/ip4.h" + +#include "lwip/prot/udp.h" +#include "lwip/prot/tcp.h" + +#include "esp_log.h" +#include "esp_err.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + int lwip_hook_ip4_input(struct pbuf *pbuf, struct netif *input_netif); +#define LWIP_HOOK_IP4_INPUT lwip_hook_ip4_input + +#ifdef __cplusplus +} +#endif + +#endif /* _LWIP_HOOKS_H_ */ diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..1820f36 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,10 @@ +[env:adafruit_qtpy_esp32s2] +platform = espressif32 +board = adafruit_qtpy_esp32s2 +framework = espidf, arduino +build_flags = + '-Iinclude' + '-DESP_IDF_LWIP_HOOK_FILENAME="lwip_hooks.h"' +lib_deps = + adafruit/Adafruit NeoPixel + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..ab3ad38 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ +# This file was automatically generated for projects +# without default 'CMakeLists.txt' file. + +FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) + +idf_component_register(SRCS ${app_sources}) diff --git a/src/lwip_hooks.c b/src/lwip_hooks.c new file mode 100644 index 0000000..f645a8f --- /dev/null +++ b/src/lwip_hooks.c @@ -0,0 +1,50 @@ +// file: +#include "lwip_hooks.h" +extern void log_packet(uint16_t); + +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) +{ + const struct ip_hdr *iphdr; + const struct tcp_hdr *tcphdr; + const struct udp_hdr *udphdr; + char ip_address[IP_HLEN]; + u16_t protocol; + uint16_t port; + + iphdr = (struct ip_hdr *)pbuf->payload; + u16_t ilen = IPH_HL(iphdr); + protocol = (u16_t)IPH_PROTO(iphdr); + if (protocol == 6) { + //tcphdr = (struct tcp_hdr *)pbuf->payload; + tcphdr = (struct tcp_hdr *)&((u32_t*)iphdr)[ilen]; + port = lwip_ntohs(tcphdr->dest); + } else if (protocol == 17) { + udphdr = (struct udp_hdr *)&((u32_t*)iphdr)[ilen]; + port = lwip_ntohs(udphdr->dest); + } else { + port = 0; + } + + 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 %u", + get_protocol((u16_t)IPH_PROTO(iphdr)), ip_address, port); + + log_packet(port); + + return ESP_OK; +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..466a701 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,52 @@ +//file: main.c or main.cpp +#include "Arduino.h" +#include "WiFi.h" +#include + +#define NUMPIXELS 1 +#define PIN_NEOPIXEL 39 +#define NEOPIXEL_POWER 38 + +char ssid[] = "MyWifi"; // your network SSID (name) +char pass[] = "MyPassphrase"; // your network password (use for WPA, or use as key for WEP) + +Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); + +extern "C" { + void log_packet(uint16_t port) { + if (WiFi.status() == WL_CONNECTED) { + pixels.rainbow(port); + pixels.show(); + } + } +} + +extern "C" void app_main() +{ + initArduino(); + +#if defined(NEOPIXEL_POWER) + // If this board has a power control pin, we must set it to output and high + // in order to enable the NeoPixels. We put this in an #if defined so it can + // be reused for other boards without compilation errors + pinMode(NEOPIXEL_POWER, OUTPUT); + digitalWrite(NEOPIXEL_POWER, HIGH); +#endif + pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) + pixels.setBrightness(20); // not so bright + + printf("Connecting"); + WiFi.begin(ssid, pass); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + printf("."); + } + printf("\nConnected!\n"); + printf("SSID: %s\n", WiFi.SSID().c_str()); + printf("IP: %s\n", WiFi.localIP().toString().c_str()); + + // Arduino-like loop() + while(true){ + ; + } +}