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
315
316
317
318
319
320
321
|
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#if defined(__linux__) || defined(__HAIKU__)
#include <sys/socket.h>
#endif
#include "Common/CommonTypes.h"
struct sockaddr_in;
namespace Common
{
enum class MACConsumer
{
BBA,
IOS
};
enum
{
BBA_HARDWARE_TYPE = 1,
MAC_ADDRESS_SIZE = 6,
IPV4_HEADER_TYPE = 8
};
enum DHCPConst
{
MESSAGE_QUERY = 1,
MESSAGE_REPLY = 2
};
using MACAddress = std::array<u8, MAC_ADDRESS_SIZE>;
// Note: Bluetooth address display order is reverse of the storage order.
struct BluetoothAddress : std::array<u8, MAC_ADDRESS_SIZE>
{
};
constexpr std::size_t IPV4_ADDR_LEN = 4;
using IPAddress = std::array<u8, IPV4_ADDR_LEN>;
constexpr IPAddress IP_ADDR_ANY = {0, 0, 0, 0};
constexpr IPAddress IP_ADDR_BROADCAST = {255, 255, 255, 255};
constexpr IPAddress IP_ADDR_SSDP = {239, 255, 255, 250};
constexpr u16 SSDP_PORT = 1900;
constexpr u16 IPV4_ETHERTYPE = 0x800;
constexpr u16 ARP_ETHERTYPE = 0x806;
struct EthernetHeader
{
EthernetHeader();
explicit EthernetHeader(u16 ether_type);
EthernetHeader(const MACAddress& dest, const MACAddress& src, u16 ether_type);
u16 Size() const;
static constexpr std::size_t SIZE = 14;
MACAddress destination = {};
MACAddress source = {};
u16 ethertype = 0;
};
static_assert(sizeof(EthernetHeader) == EthernetHeader::SIZE);
static_assert(std::is_standard_layout_v<EthernetHeader>);
struct IPv4Header
{
IPv4Header();
IPv4Header(u16 data_size, u8 ip_proto, const sockaddr_in& from, const sockaddr_in& to);
u16 Size() const;
u8 DefinedSize() const;
static constexpr std::size_t SIZE = 20;
u8 version_ihl = 0;
u8 dscp_esn = 0;
u16 total_len = 0;
u16 identification = 0;
u16 flags_fragment_offset = 0;
u8 ttl = 0;
u8 protocol = 0;
u16 header_checksum = 0;
IPAddress source_addr{};
IPAddress destination_addr{};
};
static_assert(sizeof(IPv4Header) == IPv4Header::SIZE);
static_assert(std::is_standard_layout_v<IPv4Header>);
struct TCPHeader
{
TCPHeader();
TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, const u8* data, u16 length);
TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, u32 ack, u16 flags);
u8 GetHeaderSize() const;
u16 Size() const;
u8 IPProto() const;
static constexpr std::size_t SIZE = 20;
u16 source_port = 0;
u16 destination_port = 0;
u32 sequence_number = 0;
u32 acknowledgement_number = 0;
u16 properties = 0;
u16 window_size = 0;
u16 checksum = 0;
u16 urgent_pointer = 0;
};
static_assert(sizeof(TCPHeader) == TCPHeader::SIZE);
static_assert(std::is_standard_layout_v<TCPHeader>);
struct UDPHeader
{
UDPHeader();
UDPHeader(const sockaddr_in& from, const sockaddr_in& to, u16 data_length);
u16 Size() const;
u8 IPProto() const;
static constexpr std::size_t SIZE = 8;
u16 source_port = 0;
u16 destination_port = 0;
u16 length = 0;
u16 checksum = 0;
};
static_assert(sizeof(UDPHeader) == UDPHeader::SIZE);
static_assert(std::is_standard_layout_v<UDPHeader>);
#pragma pack(push, 1)
struct ARPHeader
{
ARPHeader();
ARPHeader(u32 from_ip, const MACAddress& from_mac, u32 to_ip, const MACAddress& to_mac);
u16 Size() const;
static constexpr std::size_t SIZE = 28;
u16 hardware_type = 0;
u16 protocol_type = 0;
u8 hardware_size = 0;
u8 protocol_size = 0;
u16 opcode = 0;
MACAddress sender_address{};
u32 sender_ip = 0;
MACAddress targer_address{};
u32 target_ip = 0;
};
static_assert(sizeof(ARPHeader) == ARPHeader::SIZE);
#pragma pack(pop)
struct DHCPBody
{
DHCPBody();
DHCPBody(u32 transaction, const MACAddress& client_address, u32 new_ip, u32 serv_ip);
static constexpr std::size_t SIZE = 240;
u8 message_type = 0;
u8 hardware_type = 0;
u8 hardware_addr = 0;
u8 hops = 0;
u32 transaction_id = 0;
u16 seconds = 0;
u16 boot_flag = 0;
u32 client_ip = 0;
u32 your_ip = 0;
u32 server_ip = 0;
u32 relay_ip = 0;
MACAddress client_mac{};
unsigned char padding[10]{};
unsigned char hostname[0x40]{};
unsigned char boot_file[0x80]{};
u8 magic_cookie[4] = {0x63, 0x82, 0x53, 0x63};
};
static_assert(sizeof(DHCPBody) == DHCPBody::SIZE);
struct DHCPPacket
{
DHCPPacket();
DHCPPacket(const std::vector<u8>& data);
void AddOption(u8 fnc, const std::vector<u8>& params);
std::vector<u8> Build() const;
DHCPBody body;
std::vector<std::vector<u8>> options;
};
// The compiler might add 2 bytes after EthernetHeader to enforce 16-bytes alignment
#pragma pack(push, 1)
struct ARPPacket
{
ARPPacket();
ARPPacket(const MACAddress& destination, const MACAddress& source);
std::vector<u8> Build() const;
u16 Size() const;
EthernetHeader eth_header;
ARPHeader arp_header;
static constexpr std::size_t SIZE = EthernetHeader::SIZE + ARPHeader::SIZE;
};
static_assert(sizeof(ARPPacket) == ARPPacket::SIZE);
#pragma pack(pop)
struct TCPPacket
{
TCPPacket();
TCPPacket(const MACAddress& destination, const MACAddress& source, const sockaddr_in& from,
const sockaddr_in& to, u32 seq, u32 ack, u16 flags);
std::vector<u8> Build() const;
u16 Size() const;
EthernetHeader eth_header;
IPv4Header ip_header;
TCPHeader tcp_header;
std::vector<u8> ipv4_options;
std::vector<u8> tcp_options;
std::vector<u8> data;
static constexpr std::size_t MIN_SIZE = EthernetHeader::SIZE + IPv4Header::SIZE + TCPHeader::SIZE;
};
struct UDPPacket
{
UDPPacket();
UDPPacket(const MACAddress& destination, const MACAddress& source, const sockaddr_in& from,
const sockaddr_in& to, const std::vector<u8>& payload);
std::vector<u8> Build() const;
u16 Size() const;
EthernetHeader eth_header;
IPv4Header ip_header;
UDPHeader udp_header;
std::vector<u8> ipv4_options;
std::vector<u8> data;
static constexpr std::size_t MIN_SIZE = EthernetHeader::SIZE + IPv4Header::SIZE + UDPHeader::SIZE;
};
class PacketView
{
public:
PacketView(const u8* ptr, std::size_t size);
std::optional<u16> GetEtherType() const;
std::optional<ARPPacket> GetARPPacket() const;
std::optional<u8> GetIPProto() const;
std::optional<TCPPacket> GetTCPPacket() const;
std::optional<UDPPacket> GetUDPPacket() const;
private:
const u8* m_ptr;
std::size_t m_size;
};
struct NetworkErrorState
{
int error;
#ifdef _WIN32
int wsa_error;
#endif
};
struct IPv4Port
{
IPAddress ip_address;
u16 port; // Network byte order.
// These convert to host byte order.
u32 GetIPAddressValue() const;
u16 GetPortValue() const;
};
struct IPv4PortRange
{
IPv4Port first;
IPv4Port last;
bool IsMatch(IPv4Port subject) const;
std::string ToString() const;
};
std::string IPAddressToString(IPAddress ip_address);
// Syntax is: first_ip[-last_ip|/network_prefix_length][:first_port[-last_port]]
std::optional<IPv4PortRange> StringToIPv4PortRange(std::string_view subject);
MACAddress GenerateMacAddress(MACConsumer type);
std::string MacAddressToString(const MACAddress& mac);
std::optional<MACAddress> StringToMacAddress(std::string_view mac_string);
std::string BluetoothAddressToString(BluetoothAddress bdaddr);
std::optional<BluetoothAddress> StringToBluetoothAddress(std::string_view str);
u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value = 0);
u16 ComputeTCPNetworkChecksum(const IPAddress& from, const IPAddress& to, const void* data,
u16 length, u8 protocol);
NetworkErrorState SaveNetworkErrorState();
void RestoreNetworkErrorState(const NetworkErrorState& state);
const char* DecodeNetworkError(s32 error_code);
const char* StrNetworkError();
// Sets SO_NOSIGPIPE when available.
bool SetPlatformSocketOptions(int fd);
// Pass this to all `send` calls to avoid SIGPIPE.
#if defined(__linux__) || defined(__HAIKU__)
static constexpr int SEND_FLAGS = MSG_NOSIGNAL;
#else
static constexpr int SEND_FLAGS = 0;
#endif
} // namespace Common
|