1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-27 18:02:17 +03:00

Migrate from astyle to clang-format (#8464)

This commit is contained in:
Maxim Prokhorov
2022-02-20 19:23:33 +03:00
committed by Max Prokhorov
parent 46190b61f1
commit 19b7a29720
241 changed files with 15925 additions and 16197 deletions

View File

@ -12,7 +12,7 @@ using namespace NetCapture;
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#define STAPSK "your-password"
#endif
const char* ssid = STASSID;
@ -20,43 +20,36 @@ const char* password = STAPSK;
Netdump nd;
//FS* filesystem = &SPIFFS;
// FS* filesystem = &SPIFFS;
FS* filesystem = &LittleFS;
ESP8266WebServer webServer(80); // Used for sending commands
WiFiServer tcpServer(8000); // Used to show netcat option.
File tracefile;
ESP8266WebServer webServer(80); // Used for sending commands
WiFiServer tcpServer(8000); // Used to show netcat option.
File tracefile;
std::map<PacketType, int> packetCount;
enum class SerialOption : uint8_t {
AllFull,
LocalNone,
HTTPChar
};
enum class SerialOption : uint8_t { AllFull,
LocalNone,
HTTPChar };
void startSerial(SerialOption option) {
switch (option) {
case SerialOption::AllFull : //All Packets, show packet summary.
case SerialOption::AllFull: // All Packets, show packet summary.
nd.printDump(Serial, Packet::PacketDetail::FULL);
break;
case SerialOption::LocalNone : // Only local IP traffic, full details
nd.printDump(Serial, Packet::PacketDetail::NONE,
[](Packet n) {
case SerialOption::LocalNone: // Only local IP traffic, full details
nd.printDump(Serial, Packet::PacketDetail::NONE, [](Packet n) {
return (n.hasIP(WiFi.localIP()));
}
);
});
break;
case SerialOption::HTTPChar : // Only HTTP traffic, show packet content as chars
nd.printDump(Serial, Packet::PacketDetail::CHAR,
[](Packet n) {
case SerialOption::HTTPChar: // Only HTTP traffic, show packet content as chars
nd.printDump(Serial, Packet::PacketDetail::CHAR, [](Packet n) {
return (n.isHTTP());
}
);
});
break;
default :
Serial.printf("No valid SerialOption provided\r\n");
default: Serial.printf("No valid SerialOption provided\r\n");
};
}
@ -80,49 +73,39 @@ void setup(void) {
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed, stopping sketch");
while (1) {
delay(1000);
}
while (1) { delay(1000); }
}
if (!MDNS.begin("netdumphost")) {
Serial.println("Error setting up MDNS responder!");
}
if (!MDNS.begin("netdumphost")) { Serial.println("Error setting up MDNS responder!"); }
filesystem->begin();
webServer.on("/list",
[]() {
webServer.on("/list", []() {
Dir dir = filesystem->openDir("/");
String d = "<h1>File list</h1>";
while (dir.next()) {
d.concat("<li>" + dir.fileName() + "</li>");
}
webServer.send(200, "text.html", d);
}
);
});
webServer.on("/req",
[]() {
webServer.on("/req", []() {
static int rq = 0;
String a = "<h1>You are connected, Number of requests = " + String(rq++) + "</h1>";
webServer.send(200, "text/html", a);
}
);
});
webServer.on("/reset",
[]() {
webServer.on("/reset", []() {
nd.reset();
tracefile.close();
tcpServer.close();
webServer.send(200, "text.html", "<h1>Netdump session reset</h1>");
}
);
});
webServer.serveStatic("/", *filesystem, "/");
webServer.begin();
startSerial(SerialOption::AllFull); // Serial output examples, use enum SerialOption for selection
startSerial(SerialOption::AllFull); // Serial output examples, use enum SerialOption for selection
// startTcpDump(); // tcpdump option
// startTracefile(); // output to SPIFFS or LittleFS
@ -132,18 +115,18 @@ void setup(void) {
nd.setCallback(
[](Packet p)
{
Serial.printf("PKT : %s : ",p.sourceIP().toString().c_str());
for ( auto pp : p.allPacketTypes())
{
Serial.printf("%s ",pp.toString().c_str());
packetCount[pp]++;
}
Serial.printf("\r\n CNT ");
for (auto pc : packetCount)
{
Serial.printf("%s %d ", pc.first.toString().c_str(),pc.second);
}
Serial.printf("\r\n");
Serial.printf("PKT : %s : ",p.sourceIP().toString().c_str());
for ( auto pp : p.allPacketTypes())
{
Serial.printf("%s ",pp.toString().c_str());
packetCount[pp]++;
}
Serial.printf("\r\n CNT ");
for (auto pc : packetCount)
{
Serial.printf("%s %d ", pc.first.toString().c_str(),pc.second);
}
Serial.printf("\r\n");
}
);
*/
@ -153,4 +136,3 @@ void loop(void) {
webServer.handleClient();
MDNS.update();
}

View File

@ -23,7 +23,6 @@
#include <lwip/init.h>
#include "Schedule.h"
namespace NetCapture
{
@ -69,24 +68,26 @@ void Netdump::reset()
void Netdump::printDump(Print& out, Packet::PacketDetail ndd, const Filter nf)
{
out.printf_P(PSTR("netDump starting\r\n"));
setCallback([&out, ndd, this](const Packet & ndp)
{
printDumpProcess(out, ndd, ndp);
}, nf);
setCallback(
[&out, ndd, this](const Packet& ndp)
{
printDumpProcess(out, ndd, ndp);
},
nf);
}
void Netdump::fileDump(File& outfile, const Filter nf)
{
writePcapHeader(outfile);
setCallback([&outfile, this](const Packet & ndp)
{
fileDumpProcess(outfile, ndp);
}, nf);
setCallback(
[&outfile, this](const Packet& ndp)
{
fileDumpProcess(outfile, ndp);
},
nf);
}
bool Netdump::tcpDump(WiFiServer &tcpDumpServer, const Filter nf)
bool Netdump::tcpDump(WiFiServer& tcpDumpServer, const Filter nf)
{
if (!packetBuffer)
{
packetBuffer = new (std::nothrow) char[tcpBufferSize];
@ -98,10 +99,11 @@ bool Netdump::tcpDump(WiFiServer &tcpDumpServer, const Filter nf)
}
bufferIndex = 0;
schedule_function([&tcpDumpServer, this, nf]()
{
tcpDumpLoop(tcpDumpServer, nf);
});
schedule_function(
[&tcpDumpServer, this, nf]()
{
tcpDumpLoop(tcpDumpServer, nf);
});
return true;
}
@ -109,7 +111,8 @@ void Netdump::capture(int netif_idx, const char* data, size_t len, int out, int
{
if (lwipCallback.execute(netif_idx, data, len, out, success) == 0)
{
phy_capture = nullptr; // No active callback/netdump instances, will be set again by new object.
phy_capture
= nullptr; // No active callback/netdump instances, will be set again by new object.
}
}
@ -118,7 +121,7 @@ void Netdump::netdumpCapture(int netif_idx, const char* data, size_t len, int ou
if (netDumpCallback)
{
Packet np(millis(), netif_idx, data, len, out, success);
if (netDumpFilter && !netDumpFilter(np))
if (netDumpFilter && !netDumpFilter(np))
{
return;
}
@ -131,8 +134,8 @@ void Netdump::writePcapHeader(Stream& s) const
uint32_t pcapHeader[6];
pcapHeader[0] = 0xa1b2c3d4; // pcap magic number
pcapHeader[1] = 0x00040002; // pcap major/minor version
pcapHeader[2] = 0; // pcap UTC correction in seconds
pcapHeader[3] = 0; // pcap time stamp accuracy
pcapHeader[2] = 0; // pcap UTC correction in seconds
pcapHeader[3] = 0; // pcap time stamp accuracy
pcapHeader[4] = maxPcapLength; // pcap max packet length per record
pcapHeader[5] = 1; // pacp data linkt type = ethernet
s.write(reinterpret_cast<char*>(pcapHeader), 24);
@ -145,7 +148,7 @@ void Netdump::printDumpProcess(Print& out, Packet::PacketDetail ndd, const Packe
void Netdump::fileDumpProcess(File& outfile, const Packet& np) const
{
size_t incl_len = np.getPacketSize() > maxPcapLength ? maxPcapLength : np.getPacketSize();
size_t incl_len = np.getPacketSize() > maxPcapLength ? maxPcapLength : np.getPacketSize();
uint32_t pcapHeader[4];
struct timeval tv;
@ -154,7 +157,7 @@ void Netdump::fileDumpProcess(File& outfile, const Packet& np) const
pcapHeader[1] = tv.tv_usec;
pcapHeader[2] = incl_len;
pcapHeader[3] = np.getPacketSize();
outfile.write(reinterpret_cast<char*>(pcapHeader), 16); // pcap record header
outfile.write(reinterpret_cast<char*>(pcapHeader), 16); // pcap record header
outfile.write(np.rawData(), incl_len);
}
@ -168,16 +171,16 @@ void Netdump::tcpDumpProcess(const Packet& np)
}
size_t incl_len = np.getPacketSize() > maxPcapLength ? maxPcapLength : np.getPacketSize();
if (bufferIndex + 16 + incl_len < tcpBufferSize) // only add if enough space available
if (bufferIndex + 16 + incl_len < tcpBufferSize) // only add if enough space available
{
struct timeval tv;
gettimeofday(&tv, nullptr);
uint32_t* pcapHeader = reinterpret_cast<uint32_t*>(&packetBuffer[bufferIndex]);
pcapHeader[0] = tv.tv_sec; // add pcap record header
pcapHeader[1] = tv.tv_usec;
pcapHeader[2] = incl_len;
pcapHeader[3] = np.getPacketSize();
bufferIndex += 16; // pcap header size
pcapHeader[0] = tv.tv_sec; // add pcap record header
pcapHeader[1] = tv.tv_usec;
pcapHeader[2] = incl_len;
pcapHeader[3] = np.getPacketSize();
bufferIndex += 16; // pcap header size
memcpy(&packetBuffer[bufferIndex], np.rawData(), incl_len);
bufferIndex += incl_len;
}
@ -189,7 +192,7 @@ void Netdump::tcpDumpProcess(const Packet& np)
}
}
void Netdump::tcpDumpLoop(WiFiServer &tcpDumpServer, const Filter nf)
void Netdump::tcpDumpLoop(WiFiServer& tcpDumpServer, const Filter nf)
{
if (tcpDumpServer.hasClient())
{
@ -199,10 +202,12 @@ void Netdump::tcpDumpLoop(WiFiServer &tcpDumpServer, const Filter nf)
bufferIndex = 0;
writePcapHeader(tcpDumpClient);
setCallback([this](const Packet & ndp)
{
tcpDumpProcess(ndp);
}, nf);
setCallback(
[this](const Packet& ndp)
{
tcpDumpProcess(ndp);
},
nf);
}
if (!tcpDumpClient || !tcpDumpClient.connected())
{
@ -216,11 +221,12 @@ void Netdump::tcpDumpLoop(WiFiServer &tcpDumpServer, const Filter nf)
if (tcpDumpServer.status() != CLOSED)
{
schedule_function([&tcpDumpServer, this, nf]()
{
tcpDumpLoop(tcpDumpServer, nf);
});
schedule_function(
[&tcpDumpServer, this, nf]()
{
tcpDumpLoop(tcpDumpServer, nf);
});
}
}
} // namespace NetCapture
} // namespace NetCapture

View File

@ -38,9 +38,8 @@ using namespace experimental::CBListImplentation;
class Netdump
{
public:
using Filter = std::function<bool(const Packet&)>;
using Callback = std::function<void(const Packet&)>;
using Filter = std::function<bool(const Packet&)>;
using Callback = std::function<void(const Packet&)>;
using LwipCallback = std::function<void(int, const char*, int, int, int)>;
Netdump();
@ -53,15 +52,14 @@ public:
void printDump(Print& out, Packet::PacketDetail ndd, const Filter nf = nullptr);
void fileDump(File& outfile, const Filter nf = nullptr);
bool tcpDump(WiFiServer &tcpDumpServer, const Filter nf = nullptr);
bool tcpDump(WiFiServer& tcpDumpServer, const Filter nf = nullptr);
private:
Callback netDumpCallback = nullptr;
Filter netDumpFilter = nullptr;
static void capture(int netif_idx, const char* data, size_t len, int out, int success);
static CallBackList<LwipCallback> lwipCallback;
static CallBackList<LwipCallback> lwipCallback;
CallBackList<LwipCallback>::CallBackHandler lwipHandler;
void netdumpCapture(int netif_idx, const char* data, size_t len, int out, int success);
@ -69,19 +67,19 @@ private:
void printDumpProcess(Print& out, Packet::PacketDetail ndd, const Packet& np) const;
void fileDumpProcess(File& outfile, const Packet& np) const;
void tcpDumpProcess(const Packet& np);
void tcpDumpLoop(WiFiServer &tcpDumpServer, const Filter nf);
void tcpDumpLoop(WiFiServer& tcpDumpServer, const Filter nf);
void writePcapHeader(Stream& s) const;
WiFiClient tcpDumpClient;
char* packetBuffer = nullptr;
int bufferIndex = 0;
char* packetBuffer = nullptr;
int bufferIndex = 0;
static constexpr int tcpBufferSize = 2048;
static constexpr int maxPcapLength = 1024;
static constexpr uint32_t pcapMagic = 0xa1b2c3d4;
static constexpr int tcpBufferSize = 2048;
static constexpr int maxPcapLength = 1024;
static constexpr uint32_t pcapMagic = 0xa1b2c3d4;
};
} // namespace NetCapture
} // namespace NetCapture
#endif /* __NETDUMP_H */

View File

@ -24,11 +24,10 @@
namespace NetCapture
{
NetdumpIP::NetdumpIP()
{
}
NetdumpIP::NetdumpIP() { }
NetdumpIP::NetdumpIP(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
NetdumpIP::NetdumpIP(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet,
uint8_t fourth_octet)
{
setV4();
(*this)[0] = first_octet;
@ -37,7 +36,7 @@ NetdumpIP::NetdumpIP(uint8_t first_octet, uint8_t second_octet, uint8_t third_oc
(*this)[3] = fourth_octet;
}
NetdumpIP::NetdumpIP(const uint8_t *address, bool v4)
NetdumpIP::NetdumpIP(const uint8_t* address, bool v4)
{
uint8_t cnt;
if (v4)
@ -88,7 +87,7 @@ NetdumpIP::NetdumpIP(const String& ip)
}
}
bool NetdumpIP::fromString(const char *address)
bool NetdumpIP::fromString(const char* address)
{
if (!fromString4(address))
{
@ -97,12 +96,12 @@ bool NetdumpIP::fromString(const char *address)
return true;
}
bool NetdumpIP::fromString4(const char *address)
bool NetdumpIP::fromString4(const char* address)
{
// TODO: (IPv4) add support for "a", "a.b", "a.b.c" formats
uint16_t acc = 0; // Accumulator
uint8_t dots = 0;
uint16_t acc = 0; // Accumulator
uint8_t dots = 0;
while (*address)
{
@ -124,7 +123,7 @@ bool NetdumpIP::fromString4(const char *address)
return false;
}
(*this)[dots++] = acc;
acc = 0;
acc = 0;
}
else
{
@ -144,12 +143,12 @@ bool NetdumpIP::fromString4(const char *address)
return true;
}
bool NetdumpIP::fromString6(const char *address)
bool NetdumpIP::fromString6(const char* address)
{
// TODO: test test test
uint32_t acc = 0; // Accumulator
int dots = 0, doubledots = -1;
uint32_t acc = 0; // Accumulator
int dots = 0, doubledots = -1;
while (*address)
{
@ -162,7 +161,7 @@ bool NetdumpIP::fromString6(const char *address)
}
acc = acc * 16 + (c - '0');
if (acc > 0xffff)
// Value out of range
// Value out of range
{
return false;
}
@ -172,7 +171,7 @@ bool NetdumpIP::fromString6(const char *address)
if (*address == ':')
{
if (doubledots >= 0)
// :: allowed once
// :: allowed once
{
return false;
}
@ -181,22 +180,22 @@ bool NetdumpIP::fromString6(const char *address)
address++;
}
if (dots == 7)
// too many separators
// too many separators
{
return false;
}
reinterpret_cast<uint16_t*>(rawip)[dots++] = PP_HTONS(acc);
acc = 0;
acc = 0;
}
else
// Invalid char
// Invalid char
{
return false;
}
}
if (doubledots == -1 && dots != 7)
// Too few separators
// Too few separators
{
return false;
}
@ -206,7 +205,8 @@ bool NetdumpIP::fromString6(const char *address)
{
for (int i = dots - doubledots - 1; i >= 0; i--)
{
reinterpret_cast<uint16_t*>(rawip)[8 - dots + doubledots + i] = reinterpret_cast<uint16_t*>(rawip)[doubledots + i];
reinterpret_cast<uint16_t*>(rawip)[8 - dots + doubledots + i]
= reinterpret_cast<uint16_t*>(rawip)[doubledots + i];
}
for (int i = doubledots; i < 8 - dots + doubledots; i++)
{
@ -223,12 +223,11 @@ String NetdumpIP::toString()
StreamString sstr;
if (isV6())
{
sstr.reserve(40); // 8 shorts x 4 chars each + 7 colons + nullterm
sstr.reserve(40); // 8 shorts x 4 chars each + 7 colons + nullterm
}
else
{
sstr.reserve(16); // 4 bytes with 3 chars max + 3 dots + nullterm, or '(IP unset)'
sstr.reserve(16); // 4 bytes with 3 chars max + 3 dots + nullterm, or '(IP unset)'
}
printTo(sstr);
return sstr;
@ -253,7 +252,7 @@ size_t NetdumpIP::printTo(Print& p)
{
n += p.printf_P(PSTR("%x"), bit);
if (count0 > 0)
// no more hiding 0
// no more hiding 0
{
count0 = -8;
}
@ -280,7 +279,7 @@ size_t NetdumpIP::printTo(Print& p)
return n;
}
bool NetdumpIP::compareRaw(IPversion v, const uint8_t* a, const uint8_t* b) const
bool NetdumpIP::compareRaw(IPversion v, const uint8_t* a, const uint8_t* b) const
{
for (int i = 0; i < (v == IPversion::IPV4 ? 4 : 16); i++)
{
@ -296,7 +295,7 @@ bool NetdumpIP::compareIP(const IPAddress& ip) const
{
switch (ipv)
{
case IPversion::UNSET :
case IPversion::UNSET:
if (ip.isSet())
{
return false;
@ -306,7 +305,7 @@ bool NetdumpIP::compareIP(const IPAddress& ip) const
return true;
}
break;
case IPversion::IPV4 :
case IPversion::IPV4:
if (ip.isV6() || !ip.isSet())
{
return false;
@ -316,7 +315,7 @@ bool NetdumpIP::compareIP(const IPAddress& ip) const
return compareRaw(IPversion::IPV4, rawip, reinterpret_cast<const uint8_t*>(&ip.v4()));
}
break;
case IPversion::IPV6 :
case IPversion::IPV6:
if (ip.isV4() || !ip.isSet())
{
return false;
@ -326,7 +325,7 @@ bool NetdumpIP::compareIP(const IPAddress& ip) const
return compareRaw(IPversion::IPV6, rawip, reinterpret_cast<const uint8_t*>(ip.raw6()));
}
break;
default :
default:
return false;
break;
}
@ -336,7 +335,7 @@ bool NetdumpIP::compareIP(const NetdumpIP& nip) const
{
switch (ipv)
{
case IPversion::UNSET :
case IPversion::UNSET:
if (nip.isSet())
{
return false;
@ -346,7 +345,7 @@ bool NetdumpIP::compareIP(const NetdumpIP& nip) const
return true;
}
break;
case IPversion::IPV4 :
case IPversion::IPV4:
if (nip.isV6() || !nip.isSet())
{
return false;
@ -356,7 +355,7 @@ bool NetdumpIP::compareIP(const NetdumpIP& nip) const
return compareRaw(IPversion::IPV4, rawip, nip.rawip);
}
break;
case IPversion::IPV6 :
case IPversion::IPV6:
if (nip.isV4() || !nip.isSet())
{
return false;
@ -366,10 +365,10 @@ bool NetdumpIP::compareIP(const NetdumpIP& nip) const
return compareRaw(IPversion::IPV6, rawip, nip.rawip);
}
break;
default :
default:
return false;
break;
}
}
} // namespace NetCapture
} // namespace NetCapture

View File

@ -22,7 +22,7 @@ public:
NetdumpIP();
NetdumpIP(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
NetdumpIP(const uint8_t *address, bool V4 = true);
NetdumpIP(const uint8_t* address, bool V4 = true);
NetdumpIP(const IPAddress& ip);
NetdumpIP(const String& ip);
@ -31,15 +31,20 @@ public:
return rawip[index];
}
bool fromString(const char *address);
bool fromString(const char* address);
String toString();
private:
enum class IPversion {UNSET, IPV4, IPV6};
enum class IPversion
{
UNSET,
IPV4,
IPV6
};
IPversion ipv = IPversion::UNSET;
uint8_t rawip[16] = {0};
uint8_t rawip[16] = { 0 };
void setV4()
{
@ -70,14 +75,15 @@ private:
return (ipv != IPversion::UNSET);
};
bool compareRaw(IPversion v, const uint8_t* a, const uint8_t* b) const;
bool compareRaw(IPversion v, const uint8_t* a, const uint8_t* b) const;
bool compareIP(const IPAddress& ip) const;
bool compareIP(const NetdumpIP& nip) const;
bool fromString4(const char *address);
bool fromString6(const char *address);
bool fromString4(const char* address);
bool fromString6(const char* address);
size_t printTo(Print& p);
public:
bool operator==(const IPAddress& addr) const
{
@ -95,9 +101,8 @@ public:
{
return !compareIP(addr);
};
};
} // namespace NetCapture
} // namespace NetCapture
#endif /* LIBRARIES_ESPGOODIES_HR_SRC_NETDUMP_NETDUMPIP_H_ */

View File

@ -25,7 +25,8 @@
namespace NetCapture
{
void Packet::printDetail(Print& out, const String& indent, const char* data, size_t size, PacketDetail pd) const
void Packet::printDetail(Print& out, const String& indent, const char* data, size_t size,
PacketDetail pd) const
{
if (pd == PacketDetail::NONE)
{
@ -159,21 +160,24 @@ void Packet::MACtoString(int dataIdx, StreamString& sstr) const
sstr.print(':');
}
}
}
void Packet::ARPtoString(PacketDetail netdumpDetail, StreamString& sstr) const
{
switch (getARPType())
{
case 1 : sstr.printf_P(PSTR("who has %s tell %s"), getIP(ETH_HDR_LEN + 24).toString().c_str(), getIP(ETH_HDR_LEN + 14).toString().c_str());
case 1:
sstr.printf_P(PSTR("who has %s tell %s"), getIP(ETH_HDR_LEN + 24).toString().c_str(),
getIP(ETH_HDR_LEN + 14).toString().c_str());
break;
case 2 : sstr.printf_P(PSTR("%s is at "), getIP(ETH_HDR_LEN + 14).toString().c_str());
case 2:
sstr.printf_P(PSTR("%s is at "), getIP(ETH_HDR_LEN + 14).toString().c_str());
MACtoString(ETH_HDR_LEN + 8, sstr);
break;
}
sstr.printf("\r\n");
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN], packetLength - ETH_HDR_LEN, netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN], packetLength - ETH_HDR_LEN,
netdumpDetail);
}
void Packet::DNStoString(PacketDetail netdumpDetail, StreamString& sstr) const
@ -198,8 +202,10 @@ void Packet::DNStoString(PacketDetail netdumpDetail, StreamString& sstr) const
sstr.printf_P(PSTR("DR=%d "), t);
}
sstr.printf_P(PSTR("\r\n"));
printDetail(sstr, PSTR(" H "), &data[ETH_HDR_LEN + getIpHdrLen()], getUdpHdrLen(), netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN + getIpHdrLen() + getUdpHdrLen()], getUdpLen(), netdumpDetail);
printDetail(sstr, PSTR(" H "), &data[ETH_HDR_LEN + getIpHdrLen()], getUdpHdrLen(),
netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN + getIpHdrLen() + getUdpHdrLen()],
getUdpLen(), netdumpDetail);
}
void Packet::UDPtoString(PacketDetail netdumpDetail, StreamString& sstr) const
@ -207,8 +213,10 @@ void Packet::UDPtoString(PacketDetail netdumpDetail, StreamString& sstr) const
sstr.printf_P(PSTR("%s>%s "), sourceIP().toString().c_str(), destIP().toString().c_str());
sstr.printf_P(PSTR("%d:%d"), getSrcPort(), getDstPort());
sstr.printf_P(PSTR("\r\n"));
printDetail(sstr, PSTR(" H "), &data[ETH_HDR_LEN + getIpHdrLen()], getUdpHdrLen(), netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN + getIpHdrLen() + getUdpHdrLen()], getUdpLen(), netdumpDetail);
printDetail(sstr, PSTR(" H "), &data[ETH_HDR_LEN + getIpHdrLen()], getUdpHdrLen(),
netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN + getIpHdrLen() + getUdpHdrLen()],
getUdpLen(), netdumpDetail);
}
void Packet::TCPtoString(PacketDetail netdumpDetail, StreamString& sstr) const
@ -217,17 +225,20 @@ void Packet::TCPtoString(PacketDetail netdumpDetail, StreamString& sstr) const
sstr.printf_P(PSTR("%d:%d "), getSrcPort(), getDstPort());
uint16_t flags = getTcpFlags();
sstr.print('[');
const char chars [] = "FSRPAUECN";
const char chars[] = "FSRPAUECN";
for (uint8_t i = 0; i < sizeof chars; i++)
if (flags & (1 << i))
{
sstr.print(chars[i]);
}
sstr.print(']');
sstr.printf_P(PSTR(" len: %u seq: %u, ack: %u, wnd: %u "), getTcpLen(), getTcpSeq(), getTcpAck(), getTcpWindow());
sstr.printf_P(PSTR(" len: %u seq: %u, ack: %u, wnd: %u "), getTcpLen(), getTcpSeq(),
getTcpAck(), getTcpWindow());
sstr.printf_P(PSTR("\r\n"));
printDetail(sstr, PSTR(" H "), &data[ETH_HDR_LEN + getIpHdrLen()], getTcpHdrLen(), netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN + getIpHdrLen() + getTcpHdrLen()], getTcpLen(), netdumpDetail);
printDetail(sstr, PSTR(" H "), &data[ETH_HDR_LEN + getIpHdrLen()], getTcpHdrLen(),
netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN + getIpHdrLen() + getTcpHdrLen()],
getTcpLen(), netdumpDetail);
}
void Packet::ICMPtoString(PacketDetail, StreamString& sstr) const
@ -237,20 +248,36 @@ void Packet::ICMPtoString(PacketDetail, StreamString& sstr) const
{
switch (getIcmpType())
{
case 0 : sstr.printf_P(PSTR("ping reply")); break;
case 8 : sstr.printf_P(PSTR("ping request")); break;
default: sstr.printf_P(PSTR("type(0x%02x)"), getIcmpType()); break;
case 0:
sstr.printf_P(PSTR("ping reply"));
break;
case 8:
sstr.printf_P(PSTR("ping request"));
break;
default:
sstr.printf_P(PSTR("type(0x%02x)"), getIcmpType());
break;
}
}
if (isIPv6())
{
switch (getIcmpType())
{
case 129 : sstr.printf_P(PSTR("ping reply")); break;
case 128 : sstr.printf_P(PSTR("ping request")); break;
case 135 : sstr.printf_P(PSTR("Neighbour solicitation")); break;
case 136 : sstr.printf_P(PSTR("Neighbour advertisement")); break;
default: sstr.printf_P(PSTR("type(0x%02x)"), getIcmpType()); break;
case 129:
sstr.printf_P(PSTR("ping reply"));
break;
case 128:
sstr.printf_P(PSTR("ping request"));
break;
case 135:
sstr.printf_P(PSTR("Neighbour solicitation"));
break;
case 136:
sstr.printf_P(PSTR("Neighbour advertisement"));
break;
default:
sstr.printf_P(PSTR("type(0x%02x)"), getIcmpType());
break;
}
}
sstr.printf_P(PSTR("\r\n"));
@ -260,18 +287,42 @@ void Packet::IGMPtoString(PacketDetail, StreamString& sstr) const
{
switch (getIgmpType())
{
case 1 : sstr.printf_P(PSTR("Create Group Request")); break;
case 2 : sstr.printf_P(PSTR("Create Group Reply")); break;
case 3 : sstr.printf_P(PSTR("Join Group Request")); break;
case 4 : sstr.printf_P(PSTR("Join Group Reply")); break;
case 5 : sstr.printf_P(PSTR("Leave Group Request")); break;
case 6 : sstr.printf_P(PSTR("Leave Group Reply")); break;
case 7 : sstr.printf_P(PSTR("Confirm Group Request")); break;
case 8 : sstr.printf_P(PSTR("Confirm Group Reply")); break;
case 0x11 : sstr.printf_P(PSTR("Group Membership Query")); break;
case 0x12 : sstr.printf_P(PSTR("IGMPv1 Membership Report")); break;
case 0x22 : sstr.printf_P(PSTR("IGMPv3 Membership Report")); break;
default: sstr.printf_P(PSTR("type(0x%02x)"), getIgmpType()); break;
case 1:
sstr.printf_P(PSTR("Create Group Request"));
break;
case 2:
sstr.printf_P(PSTR("Create Group Reply"));
break;
case 3:
sstr.printf_P(PSTR("Join Group Request"));
break;
case 4:
sstr.printf_P(PSTR("Join Group Reply"));
break;
case 5:
sstr.printf_P(PSTR("Leave Group Request"));
break;
case 6:
sstr.printf_P(PSTR("Leave Group Reply"));
break;
case 7:
sstr.printf_P(PSTR("Confirm Group Request"));
break;
case 8:
sstr.printf_P(PSTR("Confirm Group Reply"));
break;
case 0x11:
sstr.printf_P(PSTR("Group Membership Query"));
break;
case 0x12:
sstr.printf_P(PSTR("IGMPv1 Membership Report"));
break;
case 0x22:
sstr.printf_P(PSTR("IGMPv3 Membership Report"));
break;
default:
sstr.printf_P(PSTR("type(0x%02x)"), getIgmpType());
break;
}
sstr.printf_P(PSTR("\r\n"));
}
@ -281,7 +332,8 @@ void Packet::IPtoString(PacketDetail netdumpDetail, StreamString& sstr) const
sstr.printf_P(PSTR("%s>%s "), sourceIP().toString().c_str(), destIP().toString().c_str());
sstr.printf_P(PSTR("Unknown IP type : %d\r\n"), ipType());
printDetail(sstr, PSTR(" H "), &data[ETH_HDR_LEN], getIpHdrLen(), netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN + getIpHdrLen()], getIpTotalLen() - getIpHdrLen(), netdumpDetail);
printDetail(sstr, PSTR(" D "), &data[ETH_HDR_LEN + getIpHdrLen()],
getIpTotalLen() - getIpHdrLen(), netdumpDetail);
}
void Packet::UKNWtoString(PacketDetail, StreamString& sstr) const
@ -298,13 +350,13 @@ const String Packet::toString() const
return toString(PacketDetail::NONE);
}
const String Packet::toString(PacketDetail netdumpDetail) const
{
StreamString sstr;
sstr.reserve(128);
sstr.printf_P(PSTR("%d %3s %-4s "), netif_idx, out ? "out" : "in ", packetType().toString().c_str());
sstr.printf_P(PSTR("%d %3s %-4s "), netif_idx, out ? "out" : "in ",
packetType().toString().c_str());
if (netdumpDetail == PacketDetail::RAW)
{
@ -320,56 +372,56 @@ const String Packet::toString(PacketDetail netdumpDetail) const
switch (thisPacketType)
{
case PacketType::ARP :
case PacketType::ARP:
{
ARPtoString(netdumpDetail, sstr);
break;
}
case PacketType::MDNS :
case PacketType::DNS :
case PacketType::MDNS:
case PacketType::DNS:
{
DNStoString(netdumpDetail, sstr);
break;
}
case PacketType::SSDP :
case PacketType::DHCP :
case PacketType::WSDD :
case PacketType::NETBIOS :
case PacketType::SMB :
case PacketType::OTA :
case PacketType::UDP :
case PacketType::SSDP:
case PacketType::DHCP:
case PacketType::WSDD:
case PacketType::NETBIOS:
case PacketType::SMB:
case PacketType::OTA:
case PacketType::UDP:
{
UDPtoString(netdumpDetail, sstr);
break;
}
case PacketType::TCP :
case PacketType::HTTP :
case PacketType::TCP:
case PacketType::HTTP:
{
TCPtoString(netdumpDetail, sstr);
break;
}
case PacketType::ICMP :
case PacketType::ICMP:
{
ICMPtoString(netdumpDetail, sstr);
break;
}
case PacketType::IGMP :
case PacketType::IGMP:
{
IGMPtoString(netdumpDetail, sstr);
break;
}
case PacketType::IPv4 :
case PacketType::IPv6 :
case PacketType::IPv4:
case PacketType::IPv6:
{
IPtoString(netdumpDetail, sstr);
break;
}
case PacketType::UKNW :
case PacketType::UKNW:
{
UKNWtoString(netdumpDetail, sstr);
break;
}
default :
default:
{
sstr.printf_P(PSTR("Non identified packet\r\n"));
break;
@ -378,4 +430,4 @@ const String Packet::toString(PacketDetail netdumpDetail) const
return sstr;
}
} // namespace NetCapture
} // namespace NetCapture

View File

@ -37,8 +37,8 @@ int constexpr ETH_HDR_LEN = 14;
class Packet
{
public:
Packet(unsigned long msec, int n, const char* d, size_t l, int o, int s)
: packetTime(msec), netif_idx(n), data(d), packetLength(l), out(o), success(s)
Packet(unsigned long msec, int n, const char* d, size_t l, int o, int s) :
packetTime(msec), netif_idx(n), data(d), packetLength(l), out(o), success(s)
{
setPacketTypes();
};
@ -75,7 +75,7 @@ public:
{
return ntoh16(idx + 2) | (((uint32_t)ntoh16(idx)) << 16);
};
uint8_t byteData(uint16_t idx) const
uint8_t byteData(uint16_t idx) const
{
return data[idx];
}
@ -87,17 +87,18 @@ public:
{
return ntoh16(12);
};
uint8_t ipType() const
uint8_t ipType() const
{
return isIP() ? isIPv4() ? data[ETH_HDR_LEN + 9] : data[ETH_HDR_LEN + 6] : 0;
};
uint16_t getIpHdrLen() const
{
return isIPv4() ? (((unsigned char)data[ETH_HDR_LEN]) & 0x0f) << 2 : 40 ; // IPv6 is fixed length
return isIPv4() ? (((unsigned char)data[ETH_HDR_LEN]) & 0x0f) << 2
: 40; // IPv6 is fixed length
}
uint16_t getIpTotalLen() const
{
return isIP() ? isIPv4() ? ntoh16(ETH_HDR_LEN + 2) : (packetLength - ETH_HDR_LEN) : 0;
return isIP() ? isIPv4() ? ntoh16(ETH_HDR_LEN + 2) : (packetLength - ETH_HDR_LEN) : 0;
}
uint32_t getTcpSeq() const
{
@ -115,20 +116,20 @@ public:
{
return isTCP() ? ntoh16(ETH_HDR_LEN + getIpHdrLen() + 14) : 0;
}
uint8_t getTcpHdrLen() const
uint8_t getTcpHdrLen() const
{
return isTCP() ? (data[ETH_HDR_LEN + getIpHdrLen() + 12] >> 4) * 4 : 0;
};//Header len is in multiple of 4 bytes
}; // Header len is in multiple of 4 bytes
uint16_t getTcpLen() const
{
return isTCP() ? getIpTotalLen() - getIpHdrLen() - getTcpHdrLen() : 0 ;
return isTCP() ? getIpTotalLen() - getIpHdrLen() - getTcpHdrLen() : 0;
};
uint8_t getIcmpType() const
uint8_t getIcmpType() const
{
return isICMP() ? data[ETH_HDR_LEN + getIpHdrLen() + 0] : 0;
}
uint8_t getIgmpType() const
uint8_t getIgmpType() const
{
return isIGMP() ? data[ETH_HDR_LEN + getIpHdrLen() + 0] : 0;
}
@ -136,11 +137,11 @@ public:
{
return isARP() ? data[ETH_HDR_LEN + 7] : 0;
}
bool is_ARP_who() const
bool is_ARP_who() const
{
return (getARPType() == 1);
}
bool is_ARP_is() const
bool is_ARP_is() const
{
return (getARPType() == 2);
}
@ -244,7 +245,7 @@ public:
return ip;
};
bool hasIP(NetdumpIP ip) const
bool hasIP(NetdumpIP ip) const
{
return (isIP() && ((ip == sourceIP()) || (ip == destIP())));
}
@ -270,21 +271,20 @@ public:
{
return isIP() ? ntoh16(ETH_HDR_LEN + getIpHdrLen() + 2) : 0;
}
bool hasPort(uint16_t p) const
bool hasPort(uint16_t p) const
{
return (isIP() && ((getSrcPort() == p) || (getDstPort() == p)));
}
const String toString() const;
const String toString(PacketDetail netdumpDetail) const;
void printDetail(Print& out, const String& indent, const char* data, size_t size, PacketDetail pd) const;
void printDetail(Print& out, const String& indent, const char* data, size_t size,
PacketDetail pd) const;
const PacketType packetType() const;
const PacketType packetType() const;
const std::vector<PacketType>& allPacketTypes() const;
private:
void setPacketType(PacketType);
void setPacketTypes();
@ -298,17 +298,16 @@ private:
void IPtoString(PacketDetail netdumpDetail, StreamString& sstr) const;
void UKNWtoString(PacketDetail netdumpDetail, StreamString& sstr) const;
time_t packetTime;
int netif_idx;
const char* data;
size_t packetLength;
int out;
int success;
PacketType thisPacketType;
time_t packetTime;
int netif_idx;
const char* data;
size_t packetLength;
int out;
int success;
PacketType thisPacketType;
std::vector<PacketType> thisAllPacketTypes;
};
} // namespace NetCapture
} // namespace NetCapture
#endif /* __NETDUMP_PACKET_H */

View File

@ -10,33 +10,50 @@
namespace NetCapture
{
PacketType::PacketType()
{
}
PacketType::PacketType() { }
String PacketType::toString() const
{
switch (ptype)
{
case PType::ARP : return PSTR("ARP");
case PType::IP : return PSTR("IP");
case PType::UDP : return PSTR("UDP");
case PType::MDNS : return PSTR("MDNS");
case PType::DNS : return PSTR("DNS");
case PType::SSDP : return PSTR("SSDP");
case PType::DHCP : return PSTR("DHCP");
case PType::WSDD : return PSTR("WSDD");
case PType::NETBIOS: return PSTR("NBIO");
case PType::SMB : return PSTR("SMB");
case PType::OTA : return PSTR("OTA");
case PType::TCP : return PSTR("TCP");
case PType::HTTP : return PSTR("HTTP");
case PType::ICMP : return PSTR("ICMP");
case PType::IGMP : return PSTR("IGMP");
case PType::IPv4: return PSTR("IPv4");
case PType::IPv6: return PSTR("IPv6");
case PType::UKNW : return PSTR("UKNW");
default : return PSTR("ERR");
case PType::ARP:
return PSTR("ARP");
case PType::IP:
return PSTR("IP");
case PType::UDP:
return PSTR("UDP");
case PType::MDNS:
return PSTR("MDNS");
case PType::DNS:
return PSTR("DNS");
case PType::SSDP:
return PSTR("SSDP");
case PType::DHCP:
return PSTR("DHCP");
case PType::WSDD:
return PSTR("WSDD");
case PType::NETBIOS:
return PSTR("NBIO");
case PType::SMB:
return PSTR("SMB");
case PType::OTA:
return PSTR("OTA");
case PType::TCP:
return PSTR("TCP");
case PType::HTTP:
return PSTR("HTTP");
case PType::ICMP:
return PSTR("ICMP");
case PType::IGMP:
return PSTR("IGMP");
case PType::IPv4:
return PSTR("IPv4");
case PType::IPv6:
return PSTR("IPv6");
case PType::UKNW:
return PSTR("UKNW");
default:
return PSTR("ERR");
};
}

View File

@ -15,7 +15,6 @@ namespace NetCapture
class PacketType
{
public:
enum PType : int
{
ARP,