-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathNetdump.cpp
210 lines (186 loc) · 6.06 KB
/
Netdump.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
NetDump library - tcpdump-like packet logger facility
Copyright (c) 2019 Herman Reintke. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Netdump.h"
#include <lwip/init.h>
#include "Schedule.h"
Netdump* Netdump::self;
void Netdump::setCallback(NetdumpCallback nc)
{
netDumpCallback = nc;
}
void Netdump::setCallback(NetdumpCallback nc, NetdumpFilter nf)
{
netDumpFilter = nf;
netDumpCallback = nc;
}
void Netdump::setFilter(NetdumpFilter nf)
{
netDumpFilter = nf;
}
void Netdump::reset()
{
setCallback(nullptr, nullptr);
}
void Netdump::printDump(Print& out, NetdumpPacket::PacketDetail ndd, NetdumpFilter nf)
{
out.printf("netDump starting\r\n");
// setCallback(std::bind(&Netdump::printDumpProcess, this, std::ref(out), ndd, std::placeholders::_1), nf);
setCallback([&out, ndd, this](NetdumpPacket & ndp)
{
printDumpProcess(out, ndd, ndp);
}, nf);
}
void Netdump::fileDump(File outfile, NetdumpFilter nf)
{
//char buf[24];
uint32_t buf[6];
/*
*(uint32_t*)&buf[0] = 0xa1b2c3d4;
*(uint32_t*)&buf[4] = 0x00040002;
*(uint32_t*)&buf[8] = 0;
*(uint32_t*)&buf[12] = 0;
*(uint32_t*)&buf[16] = 1024;
*(uint32_t*)&buf[20] = 1;
*/
buf[0] = 0xa1b2c3d4;
buf[1] = 0x00040002;
buf[2] = 0;
buf[3] = 0;
buf[4] = 1024;
buf[5] = 1;
outfile.write((uint8_t*)buf, 24);
// setCallback( std::bind(&Netdump::fileDumpProcess, this, outfile, std::placeholders::_1));
setCallback([outfile, this](NetdumpPacket & ndp)
{
fileDumpProcess(outfile, ndp);
}, nf);
}
void Netdump::tcpDump(WiFiServer &tcpDumpServer, NetdumpFilter nf)
{
// Get initialize code from netdumpout.cpp
if (packetBuffer)
{
delete packetBuffer;
}
packetBuffer = new char[2048];
bufferIndex = 0;
// schedule_function(std::bind(&Netdump::tcpDumpLoop,this,std::ref(tcpDumpServer)));
schedule_function([&tcpDumpServer, this]()
{
tcpDumpLoop(tcpDumpServer);
});
Serial.printf("scheduled\r\n");
}
void Netdump::capture(int netif_idx, const char* data, size_t len, int out, int success)
{
NetdumpPacket np(netif_idx, data, len, out, success);
if (self->netDumpCallback)
{
if (self->netDumpFilter && !self->netDumpFilter(np))
{
return;
}
self->netDumpCallback(np);
}
}
void Netdump::printDumpProcess(Print& out, NetdumpPacket::PacketDetail ndd, const NetdumpPacket& np)
{
out.printf("%8d %s", millis(), np.toString(ndd).c_str());
}
void Netdump::fileDumpProcess(File outfile, const NetdumpPacket& np)
{
size_t incl_len = np.len > 1024 ? 1024 : np.len;
char buf[16];
struct timeval tv;
gettimeofday(&tv, nullptr);
*(uint32_t*)&buf[0] = tv.tv_sec;
*(uint32_t*)&buf[4] = tv.tv_usec;
*(uint32_t*)&buf[8] = incl_len;
*(uint32_t*)&buf[12] = np.len;
outfile.write(buf, 16);
outfile.write(np.data, incl_len);
}
void Netdump::tcpDumpProcess(const NetdumpPacket& np)
{
// Get capture code from netdumpout.cpp
if (np.isIPv4() && np.isTCP()
&& ((np.out && np.getSrcPort() == tcpDumpClient.localPort())
|| (!np.out && np.getDstPort() == tcpDumpClient.localPort())
)
)
{
// skip myself
return;
}
size_t incl_len = np.len > 1024 ? 1024 : np.len;
struct timeval tv;
gettimeofday(&tv, nullptr);
*(uint32_t*)&packetBuffer[bufferIndex] = tv.tv_sec;
*(uint32_t*)&packetBuffer[bufferIndex + 4] = tv.tv_usec;
*(uint32_t*)&packetBuffer[bufferIndex + 8] = incl_len;
*(uint32_t*)&packetBuffer[bufferIndex + 12] = np.len;
bufferIndex += 16;
memcpy(&packetBuffer[bufferIndex], np.data, incl_len);
bufferIndex += incl_len;
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= bufferIndex)
{
tcpDumpClient.write(packetBuffer, bufferIndex);
bufferIndex = 0;
}
}
void Netdump::tcpDumpLoop(WiFiServer &tcpDumpServer)
{
if (tcpDumpServer.hasClient())
{
tcpDumpClient = tcpDumpServer.available();
//if (fastsend)
tcpDumpClient.setNoDelay(true);
// pcap-savefile(5) capture preamble
*(uint32_t*)&packetBuffer[0] = 0xa1b2c3d4;
*(uint32_t*)&packetBuffer[4] = 0x00040002;
*(uint32_t*)&packetBuffer[8] = 0;
*(uint32_t*)&packetBuffer[12] = 0;
*(uint32_t*)&packetBuffer[16] = 1024;
*(uint32_t*)&packetBuffer[20] = 1;
tcpDumpClient.write(packetBuffer, 24);
bufferIndex = 0;
// setCallback(std::bind(&Netdump::tcpDumpProcess,this,std::placeholders::_1));
setCallback([this](NetdumpPacket & ndp)
{
tcpDumpProcess(ndp);
});
Serial.printf("client started\r\n");
}
if (!tcpDumpClient || !tcpDumpClient.connected())
{
setCallback(nullptr);
}
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= bufferIndex)
{
Serial.printf("tcp write %d\r\n", bufferIndex);
tcpDumpClient.write(packetBuffer, bufferIndex);
bufferIndex = 0;
}
// schedule_function(std::bind(&Netdump::tcpDumpLoop,this,std::ref(tcpDumpServer)));
if (tcpDumpServer.status() != CLOSED)
{
schedule_function([&tcpDumpServer, this]()
{
tcpDumpLoop(tcpDumpServer);
});
}
}