forked from WerWolv/ImHex-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.hexpat
272 lines (224 loc) · 4.99 KB
/
ip.hexpat
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
#pragma author WerWolv
#pragma description Ethernet II Frames (IP Packets)
#pragma endian big
import std.sys;
import std.io;
struct MAC {
u8 octets[6];
} [[format("mac_formatter")]];
fn mac_formatter(MAC mac) {
return std::format("{0:02X}:{1:02X}:{2:02X}:{3:02X}:{4:02X}:{5:02X}",
mac.octets[0],
mac.octets[1],
mac.octets[2],
mac.octets[3],
mac.octets[4],
mac.octets[5]);
};
enum EtherType : u16 {
IPv4 = 0x0800,
ARP = 0x0806,
WakeOnLAN = 0x0842,
AVTP = 0x22F0,
IETF_TRILL = 0x22F3,
SRP = 0x22EA,
DEC_MOP_RC = 0x6002,
DECnet = 0x6003,
DEC_LAT = 0x6004,
RARP = 0x8035,
AppleTalk = 0x809B,
AppleTalk_AARP = 0x80F3,
VLANTaggedFrame = 0x8100,
SLPP = 0x8102,
VLACP = 0x8103,
IPX = 0x8137,
QNX_Qnet = 0x8204,
IPv6 = 0x860D,
EthernetFlowControl = 0x8808,
EthernetSlowProtocols = 0x8809,
CobraNet = 0x8819,
MPLS_Unicast = 0x8847,
MPLS_Multicast = 0x8848,
PPPoE_Discovery = 0x8863,
PPPoE_Session = 0x8864,
HomePlug = 0x887B,
EAPOverLAN = 0x888E,
PROFINET = 0x8892,
HyperSCSI = 0x889A,
ATAOverEthernet = 0x88A2,
EtherCAT = 0x88A4,
ServiceVLANTagIdentifier = 0x88A8,
EthernetPowerlink = 0x88AB,
GOOSE = 0x88B8,
GSE = 0x88B9,
SV = 0x88BA,
MikroTik_RoMON = 0x88BF,
LLDP = 0x88CC,
SERCOS_III = 0x88CD,
HomePlugGreenPHY = 0x88E1,
MRP = 0x88E3,
IEEE802_1AE = 0x88E5,
PBB = 0x88E7,
PTP = 0x88F7,
NC_SI = 0x88F8,
PRP = 0x88FB,
CFM = 0x8902,
FCoE = 0x8906,
FCoEIP = 0x8914,
RoCE = 0x8915,
TTE = 0x891D,
IEEE_1905_1 = 0x893A,
HSR = 0x892F,
ECTP = 0x9000,
RedundancyTag = 0xF1C1
};
namespace ip {
enum Protocol : u8 {
ICMP = 1,
IGMP = 2,
TCP = 6,
UDP = 17,
ENCAP = 41,
OSPF = 89,
SCTP = 132
};
namespace udp {
struct Packet {
u16 source_port;
u16 destination_port;
u16 length;
std::assert(length >= 8, "UDP Packet has invalid length");
u16 checksum;
u8 data[length - 8];
};
}
namespace tcp {
bitfield Flags {
data_offset : 4;
padding : 3;
ns : 1;
cwr : 1;
ece : 1;
urg : 1;
ack : 1;
psh : 1;
rst : 1;
syn : 1;
fin : 1;
};
struct Packet {
u16 source_port;
u16 destination_port;
u32 sequence_number;
u32 ack_number;
Flags flags [[inline]];
u16 window_size;
u16 checksum;
u16 urgent_pointer;
if (flags.data_offset > 5)
u8 options[(flags.data_offset - 5) * sizeof(u32)];
u8 data[parent.parent.header.total_length - parent.parent.header.ihl * 4 - flags.data_offset * 4];
};
}
struct Payload {
if (parent.protocol == ip::Protocol::UDP)
udp::Packet packet [[inline]];
else if (parent.protocol == ip::Protocol::TCP)
tcp::Packet packet [[inline]];
else
std::error(std::format("Unknown protocol: {}", parent.protocol));
};
}
namespace ipv4 {
struct Address {
u8 octets[4];
} [[format("ipv4::address_formatter")]];
fn address_formatter(Address addr) {
return std::format("{0}.{1}.{2}.{3}",
addr.octets[0],
addr.octets[1],
addr.octets[2],
addr.octets[3]);
};
bitfield Header {
version : 4;
ihl : 4;
tos : 8;
total_length : 16;
};
bitfield Flags {
reserved : 1;
df : 1;
mf : 1;
fragment_offset : 13;
};
struct Packet {
Header header [[inline]];
u16 identification;
Flags flags [[inline]];
u8 time_to_live;
ip::Protocol protocol;
u16 header_checksum;
Address source_ip_address;
Address destination_ip_address;
if (header.ihl > 5)
u8 options[(header.ihl - 5) * sizeof(u32)];
ip::Payload payload;
};
}
namespace ipv6 {
struct Address {
u16 segments[8];
} [[format("ipv6::address_formatter")]];
fn address_formatter(Address addr) {
return std::format("{0:04X}:{1:04X}:{2:04X}:{3:04X}:{4:04X}:{5:04X}:{6:04X}:{7:04X}",
addr.segments[0],
addr.segments[1],
addr.segments[2],
addr.segments[3],
addr.segments[4],
addr.segments[5],
addr.segments[6],
addr.segments[7]);
};
bitfield Header {
version : 4;
ds : 6;
ecn : 2;
flow_label : 20;
};
struct Packet {
Header header [[inline]];
u16 payload_length;
ip::Protocol next_header;
u8 hop_limit;
Address source_address;
Address destination_address;
ip::Payload payload;
};
}
struct Payload {
if (parent.type == EtherType::IPv4)
ipv4::Packet packet [[inline]];
else
std::error(std::format("Unknown payload type: 0x{:04X}", parent.type));
};
bitfield TCI {
pcp : 3;
dei : 1;
vid : 12;
};
struct EthernetIIFrame {
MAC destination_address;
MAC source_address;
u16 possible_tpid [[no_unique_address, hidden]];
if (possible_tpid == EtherType::VLANTaggedFrame) {
u16 tpid;
TCI tci [[inline]];
}
EtherType type;
Payload payload;
std::assert(sizeof(payload) >= 40 && sizeof(payload) <= 1500, std::format("Payload size out of range: {}", sizeof(payload)));
u32 frame_check_sequence;
};
EthernetIIFrame frame @ 0x00;