forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetdumpPacket.h
More file actions
314 lines (288 loc) · 7.89 KB
/
NetdumpPacket.h
File metadata and controls
314 lines (288 loc) · 7.89 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
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
*/
#ifndef __NETDUMP_PACKET_H
#define __NETDUMP_PACKET_H
#include <lwipopts.h>
#include <IPAddress.h>
#include <StreamString.h>
#include "NetdumpIP.h"
#include "PacketType.h"
#include <vector>
namespace NetCapture
{
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)
{
setPacketTypes();
};
enum class PacketDetail
{
NONE,
FULL,
CHAR,
RAW
};
const char* rawData() const
{
return data;
}
int getInOut() const
{
return out;
}
time_t getTime() const
{
return packetTime;
}
uint32_t getPacketSize() const
{
return packetLength;
}
uint16_t ntoh16(uint16_t idx) const
{
return data[idx + 1] | (((uint16_t)data[idx]) << 8);
};
uint32_t ntoh32(uint16_t idx) const
{
return ntoh16(idx + 2) | (((uint32_t)ntoh16(idx)) << 16);
};
uint8_t byteData(uint16_t idx) const
{
return data[idx];
}
const char* byteIdx(uint16_t idx) const
{
return &data[idx];
};
uint16_t ethType() const
{
return ntoh16(12);
};
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
}
uint16_t getIpTotalLen() const
{
return isIP() ? isIPv4() ? ntoh16(ETH_HDR_LEN + 2) : (packetLength - ETH_HDR_LEN) : 0;
}
uint32_t getTcpSeq() const
{
return isTCP() ? ntoh32(ETH_HDR_LEN + getIpHdrLen() + 4) : 0;
}
uint32_t getTcpAck() const
{
return isTCP() ? ntoh32(ETH_HDR_LEN + getIpHdrLen() + 8) : 0;
}
uint16_t getTcpFlags() const
{
return isTCP() ? ntoh16(ETH_HDR_LEN + getIpHdrLen() + 12) : 0;
}
uint16_t getTcpWindow() const
{
return isTCP() ? ntoh16(ETH_HDR_LEN + getIpHdrLen() + 14) : 0;
}
uint8_t getTcpHdrLen() const
{
return isTCP() ? (data[ETH_HDR_LEN + getIpHdrLen() + 12] >> 4) * 4 : 0;
};//Header len is in multiple of 4 bytes
uint16_t getTcpLen() const
{
return isTCP() ? getIpTotalLen() - getIpHdrLen() - getTcpHdrLen() : 0 ;
};
uint8_t getIcmpType() const
{
return isICMP() ? data[ETH_HDR_LEN + getIpHdrLen() + 0] : 0;
}
uint8_t getIgmpType() const
{
return isIGMP() ? data[ETH_HDR_LEN + getIpHdrLen() + 0] : 0;
}
uint8_t getARPType() const
{
return isARP() ? data[ETH_HDR_LEN + 7] : 0;
}
bool is_ARP_who() const
{
return (getARPType() == 1);
}
bool is_ARP_is() const
{
return (getARPType() == 2);
}
uint8_t getUdpHdrLen() const
{
return isUDP() ? 8 : 0;
};
uint16_t getUdpLen() const
{
return isUDP() ? ntoh16(ETH_HDR_LEN + getIpHdrLen() + 4) : 0;
};
bool isARP() const
{
return (ethType() == 0x0806);
};
bool isIPv4() const
{
return (ethType() == 0x0800);
};
bool isIPv6() const
{
return (ethType() == 0x86dd);
};
bool isIP() const
{
return (isIPv4() || isIPv6());
};
bool isICMP() const
{
return (isIP() && ((ipType() == 1) || (ipType() == 58)));
};
bool isIGMP() const
{
return (isIP() && (ipType() == 2));
};
bool isTCP() const
{
return (isIP() && (ipType() == 6));
};
bool isUDP() const
{
return (isIP() && ipType() == 17);
};
bool isMDNS() const
{
return (isUDP() && hasPort(5353));
};
bool isDNS() const
{
return (isUDP() && hasPort(53));
};
bool isSSDP() const
{
return (isUDP() && hasPort(1900));
};
bool isDHCP() const
{
return (isUDP() && ((hasPort(546) || hasPort(547) || hasPort(67) || hasPort(68))));
};
bool isWSDD() const
{
return (isUDP() && hasPort(3702));
};
bool isHTTP() const
{
return (isTCP() && hasPort(80));
};
bool isOTA() const
{
return (isUDP() && hasPort(8266));
}
bool isNETBIOS() const
{
return (isUDP() && (hasPort(137) || hasPort(138) || hasPort(139)));
}
bool isSMB() const
{
return (isUDP() && hasPort(445));
}
NetdumpIP getIP(uint16_t idx) const
{
return NetdumpIP(data[idx], data[idx + 1], data[idx + 2], data[idx + 3]);
};
NetdumpIP getIP6(uint16_t idx) const
{
return NetdumpIP((const uint8_t*)&data[idx], false);
};
NetdumpIP sourceIP() const
{
NetdumpIP ip;
if (isIPv4())
{
ip = getIP(ETH_HDR_LEN + 12);
}
else if (isIPv6())
{
ip = getIP6(ETH_HDR_LEN + 8);
}
return ip;
};
bool hasIP(NetdumpIP ip) const
{
return (isIP() && ((ip == sourceIP()) || (ip == destIP())));
}
NetdumpIP destIP() const
{
NetdumpIP ip;
if (isIPv4())
{
ip = getIP(ETH_HDR_LEN + 16);
}
else if (isIPv6())
{
ip = getIP6(ETH_HDR_LEN + 24);
}
return ip;
};
uint16_t getSrcPort() const
{
return isIP() ? ntoh16(ETH_HDR_LEN + getIpHdrLen() + 0) : 0;
}
uint16_t getDstPort() const
{
return isIP() ? ntoh16(ETH_HDR_LEN + getIpHdrLen() + 2) : 0;
}
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;
const PacketType packetType() const;
const std::vector<PacketType>& allPacketTypes() const;
private:
void setPacketType(PacketType);
void setPacketTypes();
void MACtoString(int dataIdx, StreamString& sstr) const;
void ARPtoString(PacketDetail netdumpDetail, StreamString& sstr) const;
void DNStoString(PacketDetail netdumpDetail, StreamString& sstr) const;
void UDPtoString(PacketDetail netdumpDetail, StreamString& sstr) const;
void TCPtoString(PacketDetail netdumpDetail, StreamString& sstr) const;
void ICMPtoString(PacketDetail netdumpDetail, StreamString& sstr) const;
void IGMPtoString(PacketDetail netdumpDetail, StreamString& sstr) const;
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;
std::vector<PacketType> thisAllPacketTypes;
};
} // namespace NetCapture
#endif /* __NETDUMP_PACKET_H */