Skip to content

Commit be812d2

Browse files
authored
Netump Initial commit (#7527)
1 parent b9db944 commit be812d2

13 files changed

+1803
-0
lines changed

libraries/Netdump/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
esp8266/Arduino goodies
3+
-----------------------
4+
5+
* NetDump (lwip2)
6+
Packet sniffer library to help study network issues, check example-sketches
7+
Log examples on serial console:
8+
```
9+
14:07:01.854 -> in 0 ARP who has 10.43.1.117 tell 10.43.1.254
10+
14:07:01.854 -> out 0 ARP 10.43.1.117 is at 5c:cf:7f:c3:ad:51
11+
12+
[...] hello-world, dumped in packets:
13+
14:07:46.227 -> in 0 IPv4 10.43.1.254>10.43.1.117 TCP 54546>2[P.] seq:1945448681..1945448699 ack:6618 win:29200 len=18
14+
14:07:46.260 -> 5c cf 7f c3 ad 51 74 da 38 3a 1f 61 08 00 45 10 \..Qt.8:.a..E.
15+
14:07:46.260 -> 00 3a b2 bc 40 00 40 06 70 29 0a 2b 01 fe 0a 2b .:..@.@.p).+...+
16+
14:07:46.260 -> 01 75 d5 12 00 02 73 f5 30 e9 00 00 19 da 50 18 .u....s.0.....P.
17+
14:07:46.260 -> 72 10 f8 da 00 00 70 6c 20 68 65 6c 6c 6f 2d 77 r.....pl hello-w
18+
14:07:46.260 -> 6f 72 6c 64 20 31 0d 0a orld 1..
19+
14:07:46.294 -> out 0 IPv4 10.43.1.117>10.43.1.254 TCP 2>54546[P.] seq:6618..6619 ack:1945448699 win:2126 len=1
20+
14:07:46.326 -> 00 20 00 00 00 00 aa aa 03 00 00 00 08 00 45 00 . ............E.
21+
14:07:46.326 -> 00 29 00 0d 00 00 ff 06 a3 f9 0a 2b 01 75 0a 2b .).........+.u.+
22+
14:07:46.327 -> 01 fe 00 02 d5 12 00 00 19 da 73 f5 30 fb 50 18 ..........s.0.P.
23+
14:07:46.327 -> 08 4e 93 d5 00 00 68 .N....h
24+
14:07:46.327 -> in 0 IPv4 10.43.1.254>10.43.1.117 TCP 54546>2[.] seq:1945448699 ack:6619 win:29200
25+
14:07:46.327 -> 5c cf 7f c3 ad 51 74 da 38 3a 1f 61 08 00 45 10 \..Qt.8:.a..E.
26+
14:07:46.360 -> 00 28 b2 bd 40 00 40 06 70 3a 0a 2b 01 fe 0a 2b .(..@.@.p:.+...+
27+
14:07:46.360 -> 01 75 d5 12 00 02 73 f5 30 fb 00 00 19 db 50 10 .u....s.0.....P.
28+
14:07:46.360 -> 72 10 92 1b 00 00 r.....
29+
14:07:46.360 -> out 0 IPv4 10.43.1.117>10.43.1.254 TCP 2>54546[P.] seq:6619..6630 ack:1945448699 win:2126 len=11
30+
14:07:46.360 -> 00 20 00 00 00 00 aa aa 03 00 00 00 08 00 45 00 . ............E.
31+
14:07:46.360 -> 00 33 00 0e 00 00 ff 06 a3 ee 0a 2b 01 75 0a 2b .3.........+.u.+
32+
14:07:46.393 -> 01 fe 00 02 d5 12 00 00 19 db 73 f5 30 fb 50 18 ..........s.0.P.
33+
14:07:46.393 -> 08 4e 16 a1 00 00 65 6c 6c 6f 2d 77 6f 72 6c 64 .N....ello-world
34+
14:07:46.393 -> 0a .
35+
36+
[...] help protocol decoding from inside the esp
37+
14:08:11.715 -> in 0 IPv4 10.43.1.254>239.255.255.250 UDP 50315>1900 len=172
38+
14:08:11.716 -> 01 00 5e 7f ff fa 74 da 38 3a 1f 61 08 00 45 00 ....t.8:.a..E.
39+
14:08:11.716 -> 00 c8 9b 40 40 00 01 11 e1 c1 0a 2b 01 fe ef ff ...@@......+....
40+
14:08:11.749 -> ff fa c4 8b 07 6c 00 b4 9c 28 4d 2d 53 45 41 52 .....l...(M-SEAR
41+
14:08:11.749 -> 43 48 20 2a 20 48 54 54 50 2f 31 2e 31 0d 0a 48 CH * HTTP/1.1..H
42+
14:08:11.749 -> 4f 53 54 3a 20 32 33 39 2e 32 35 35 2e 32 35 35 OST: 239.255.255
43+
14:08:11.749 -> 2e 32 35 30 3a 31 39 30 30 0d 0a 4d 41 4e 3a 20 .250:1900..MAN:
44+
14:08:11.749 -> 22 73 73 64 70 3a 64 69 73 63 6f 76 65 72 22 0d "ssdp:discover".
45+
14:08:11.749 -> 0a 4d 58 3a 20 31 0d 0a 53 54 3a 20 75 72 6e 3a .MX: 1..ST: urn:
46+
14:08:11.782 -> 64 69 61 6c 2d 6d 75 6c 74 69 73 63 72 65 65 6e dial-multiscreen
47+
14:08:11.782 -> 2d 6f 72 67 3a 73 65 72 76 69 63 65 3a 64 69 61 -org:service:dia
48+
14:08:11.782 -> 6c 3a 31 0d 0a 55 53 45 52 2d 41 47 45 4e 54 3a l:1..USER-AGENT:
49+
14:08:11.782 -> 20 47 6f 6f 67 6c 65 20 43 68 72 6f 6d 65 2f 36 Google Chrome/6
50+
14:08:11.782 -> 36 2e 30 2e 33 33 35 39 2e 31 31 37 20 4c 69 6e 6.0.3359.117 Lin
51+
14:08:11.782 -> 75 78 0d 0a 0d 0a ux....
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include "Arduino.h"
2+
3+
#include "Netdump.h"
4+
#include <ESP8266WiFi.h>
5+
#include <ESP8266WebServer.h>
6+
#include <ESP8266mDNS.h>
7+
//#include <FS.h>
8+
#include <LittleFS.h>
9+
#include <map>
10+
11+
using namespace NetCapture;
12+
13+
#ifndef STASSID
14+
#define STASSID "your-ssid"
15+
#define STAPSK "your-password"
16+
#endif
17+
18+
const char* ssid = STASSID;
19+
const char* password = STAPSK;
20+
21+
Netdump nd;
22+
23+
//FS* filesystem = &SPIFFS;
24+
FS* filesystem = &LittleFS;
25+
26+
ESP8266WebServer webServer(80); // Used for sending commands
27+
WiFiServer tcpServer(8000); // Used to show netcat option.
28+
File tracefile;
29+
30+
std::map<PacketType, int> packetCount;
31+
32+
enum class SerialOption : uint8_t {
33+
AllFull,
34+
LocalNone,
35+
HTTPChar
36+
};
37+
38+
void startSerial(SerialOption option) {
39+
switch (option) {
40+
case SerialOption::AllFull : //All Packets, show packet summary.
41+
nd.printDump(Serial, Packet::PacketDetail::FULL);
42+
break;
43+
44+
case SerialOption::LocalNone : // Only local IP traffic, full details
45+
nd.printDump(Serial, Packet::PacketDetail::NONE,
46+
[](Packet n) {
47+
return (n.hasIP(WiFi.localIP()));
48+
}
49+
);
50+
break;
51+
case SerialOption::HTTPChar : // Only HTTP traffic, show packet content as chars
52+
nd.printDump(Serial, Packet::PacketDetail::CHAR,
53+
[](Packet n) {
54+
return (n.isHTTP());
55+
}
56+
);
57+
break;
58+
default :
59+
Serial.printf("No valid SerialOption provided\r\n");
60+
};
61+
}
62+
63+
void startTracefile() {
64+
// To file all traffic, format pcap file
65+
tracefile = filesystem->open("/tr.pcap", "w");
66+
nd.fileDump(tracefile);
67+
}
68+
69+
void startTcpDump() {
70+
// To tcpserver, all traffic.
71+
tcpServer.begin();
72+
nd.tcpDump(tcpServer);
73+
}
74+
75+
void setup(void) {
76+
Serial.begin(115200);
77+
78+
WiFi.mode(WIFI_STA);
79+
WiFi.begin(ssid, password);
80+
81+
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
82+
Serial.println("WiFi Failed, stopping sketch");
83+
while (1) {
84+
delay(1000);
85+
}
86+
}
87+
88+
if (!MDNS.begin("netdumphost")) {
89+
Serial.println("Error setting up MDNS responder!");
90+
}
91+
92+
filesystem->begin();
93+
94+
webServer.on("/list",
95+
[]() {
96+
Dir dir = filesystem->openDir("/");
97+
String d = "<h1>File list</h1>";
98+
while (dir.next()) {
99+
d.concat("<li>" + dir.fileName() + "</li>");
100+
}
101+
webServer.send(200, "text.html", d);
102+
}
103+
);
104+
105+
webServer.on("/req",
106+
[]() {
107+
static int rq = 0;
108+
String a = "<h1>You are connected, Number of requests = " + String(rq++) + "</h1>";
109+
webServer.send(200, "text/html", a);
110+
}
111+
);
112+
113+
webServer.on("/reset",
114+
[]() {
115+
nd.reset();
116+
tracefile.close();
117+
tcpServer.close();
118+
webServer.send(200, "text.html", "<h1>Netdump session reset</h1>");
119+
}
120+
);
121+
122+
webServer.serveStatic("/", *filesystem, "/");
123+
webServer.begin();
124+
125+
startSerial(SerialOption::AllFull); // Serial output examples, use enum SerialOption for selection
126+
127+
// startTcpDump(); // tcpdump option
128+
// startTracefile(); // output to SPIFFS or LittleFS
129+
130+
// use a self provide callback, this count network packets
131+
/*
132+
nd.setCallback(
133+
[](Packet p)
134+
{
135+
Serial.printf("PKT : %s : ",p.sourceIP().toString().c_str());
136+
for ( auto pp : p.allPacketTypes())
137+
{
138+
Serial.printf("%s ",pp.toString().c_str());
139+
packetCount[pp]++;
140+
}
141+
Serial.printf("\r\n CNT ");
142+
for (auto pc : packetCount)
143+
{
144+
Serial.printf("%s %d ", pc.first.toString().c_str(),pc.second);
145+
}
146+
Serial.printf("\r\n");
147+
}
148+
);
149+
*/
150+
}
151+
152+
void loop(void) {
153+
webServer.handleClient();
154+
MDNS.update();
155+
}
156+

libraries/Netdump/keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

libraries/Netdump/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=NetDump
2+
version=2
3+
author=Herman Reintke
4+
maintainer=Herman Reintke
5+
sentence=tcpdump-like logger for esp8266/Arduino
6+
paragraph=Dumps input / output packets on "Print"able type, or provide a TCP server for the real tcpdump. Check examples. Some other unrelated and independant tools are included.
7+
category=Communication
8+
url=https://
9+
architectures=esp8266 lwip

0 commit comments

Comments
 (0)