Ver Fonte

update: 关联pcap工具,支持从uart2导出pcap格式的mac数据包,用于给小鲨鱼分析通信

Wendal Chen há 2 anos atrás
pai
commit
f4d2466922
3 ficheiros alterados com 53 adições e 3 exclusões
  1. 23 1
      app/main.c
  2. 26 2
      src/network/lwip2.1.3/netif/ethernetif.c
  3. 4 0
      xmake.lua

+ 23 - 1
app/main.c

@@ -17,6 +17,8 @@
 #include "luat_msgbus.h"
 #include "luat_pm.h"
 #include "luat_rtc.h"
+#include "luat_pcap.h"
+#include "luat_uart.h"
 
 #include <string.h>
 #include "wm_irq.h"
@@ -33,9 +35,15 @@
 #define LUAT_LOG_TAG "main"
 #include "luat_log.h"
 
+#define LUAT_PCAP_UART_ID 2
+
 void luat_heap_init(void);
 
 
+static void pcap_uart_write(void *ptr, const void* buf, size_t len) {
+	luat_uart_write(LUAT_PCAP_UART_ID, buf, len);
+}
+
 static void luat_start(void *sdata){
 	luat_heap_init();
 	luat_main();
@@ -206,8 +214,22 @@ void UserMain(void){
 	psram_init(PSRAM_QPI);
 	//uint8_t* psram_ptr = (uint8_t*)(PSRAM_ADDR_START);
 #endif
-	
+
 #ifdef __LUATOS__
+// PCAP抓包
+#ifdef LUAT_USE_PCAP
+	// 初始化pcap
+	luat_uart_t uart2 = {
+		.id = LUAT_PCAP_UART_ID,
+		.baud_rate = 115200,
+		.data_bits = 8,
+		.stop_bits = 1,
+		.parity = 0
+	};
+	luat_uart_setup(&uart2);
+	luat_pcap_init(pcap_uart_write, NULL);
+	luat_pcap_write_head();
+#endif
 #ifdef LUAT_USE_LVGL
 	lv_init();
 	static tls_os_timer_t *os_timer = NULL;

+ 26 - 2
src/network/lwip2.1.3/netif/ethernetif.c

@@ -63,7 +63,7 @@
 #if TLS_CONFIG_AP_OPT_FWD
 #include "lwip/tcpip.h"
 #endif
-
+#include "stdio.h"
 /* Define those to better describe your network interface. */
 #define IFNAME0 'e'
 #define IFNAME1 'n'
@@ -74,6 +74,9 @@
 /** Network link speed */
 #define NET_LINK_SPEED  100000000
 
+#include "luat_conf_bsp.h"
+#include "luat_pcap.h"
+
 /**
  * Helper struct to hold private data used to operate your ethernet interface.
  * Keeping the ethernet address of the MAC in this struct is not necessary
@@ -197,6 +200,17 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
 		datalen += q->len;
 	}
 
+    #ifdef LUAT_USE_PCAP
+    u8 mac_source[6];
+    u8 mac_dest[6];
+    memcpy(mac_source, buf + 6, 6);
+    memcpy(mac_dest, buf, 6);
+    printf("OUT %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x %u len %u\n", 
+        mac_source[0], mac_source[1], mac_source[2], mac_source[3], mac_source[4], mac_source[5],  
+        mac_dest[0], mac_dest[1], mac_dest[2], mac_dest[3], mac_dest[4], mac_dest[5],
+        buf[12], p->tot_len);
+    luat_pcap_write_macpkg(buf, datalen);
+    #endif
 #if TLS_CONFIG_AP
     if (netif != tls_get_netif())
 	    tls_wifi_buffer_release(true, buf);
@@ -226,7 +240,17 @@ static struct pbuf *low_level_input(struct netif *netif, u8 *buf, u32 buf_len)
     struct pbuf *p = NULL, *q = NULL;
     u16_t s_len;
     u8_t *bufptr;
-
+    #ifdef LUAT_USE_PCAP
+    u8 mac_source[6];
+    u8 mac_dest[6];
+    memcpy(mac_source, buf + 6, 6);
+    memcpy(mac_dest, buf, 6);
+    printf("IN %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x %u len %u\n", 
+        mac_source[0], mac_source[1], mac_source[2], mac_source[3], mac_source[4], mac_source[5],  
+        mac_dest[0], mac_dest[1], mac_dest[2], mac_dest[3], mac_dest[4], mac_dest[5],
+        buf[12], buf_len);
+    luat_pcap_write_macpkg(buf, buf_len);
+    #endif
     /* Obtain the size of the packet and put it into the "len"
      * variable. */
 

+ 4 - 0
xmake.lua

@@ -390,6 +390,10 @@ target("network")
     add_files(luatos.."components/xxtea/src/*.c")
     add_files(luatos.."components/xxtea/binding/*.c")
 
+    -- pcap
+    add_includedirs(luatos.."components/network/pcap/include",{public = true})
+    add_files(luatos.."components/network/pcap/src/*.c")
+
 target_end()
 
 target("nes")