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
|
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Common/Network.h"
#include <algorithm>
#include <string_view>
#include <vector>
#ifndef _WIN32
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#else
#include <WinSock2.h>
#endif
#include <fmt/format.h>
#include "Common/Random.h"
#include "Common/StringUtil.h"
namespace Common
{
MACAddress GenerateMacAddress(const MACConsumer type)
{
constexpr std::array<u8, 3> oui_bba{{0x00, 0x09, 0xbf}};
constexpr std::array<u8, 3> oui_ios{{0x00, 0x17, 0xab}};
MACAddress mac{};
switch (type)
{
case MACConsumer::BBA:
std::copy(oui_bba.begin(), oui_bba.end(), mac.begin());
break;
case MACConsumer::IOS:
std::copy(oui_ios.begin(), oui_ios.end(), mac.begin());
break;
}
// Generate the 24-bit NIC-specific portion of the MAC address.
Random::Generate(&mac[3], 3);
return mac;
}
std::string MacAddressToString(const MACAddress& mac)
{
return fmt::format("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", mac[0], mac[1], mac[2], mac[3],
mac[4], mac[5]);
}
std::optional<MACAddress> StringToMacAddress(std::string_view mac_string)
{
if (mac_string.empty())
return std::nullopt;
int x = 0;
MACAddress mac{};
for (size_t i = 0; i < mac_string.size() && x < (MAC_ADDRESS_SIZE * 2); ++i)
{
char c = Common::ToLower(mac_string.at(i));
if (c >= '0' && c <= '9')
{
mac[x / 2] |= (c - '0') << ((x & 1) ? 0 : 4);
++x;
}
else if (c >= 'a' && c <= 'f')
{
mac[x / 2] |= (c - 'a' + 10) << ((x & 1) ? 0 : 4);
++x;
}
}
// A valid 48-bit MAC address consists of 6 octets, where each
// nibble is a character in the MAC address, making 12 characters
// in total.
if (x / 2 != MAC_ADDRESS_SIZE)
return std::nullopt;
return std::make_optional(mac);
}
EthernetHeader::EthernetHeader() = default;
EthernetHeader::EthernetHeader(u16 ether_type)
{
ethertype = htons(ether_type);
}
u16 EthernetHeader::Size() const
{
return static_cast<u16>(SIZE);
}
IPv4Header::IPv4Header() = default;
IPv4Header::IPv4Header(u16 data_size, u8 ip_proto, const sockaddr_in& from, const sockaddr_in& to)
{
version_ihl = 0x45;
total_len = htons(Size() + data_size);
flags_fragment_offset = htons(0x4000);
ttl = 0x40;
protocol = ip_proto;
std::memcpy(source_addr.data(), &from.sin_addr, IPV4_ADDR_LEN);
std::memcpy(destination_addr.data(), &to.sin_addr, IPV4_ADDR_LEN);
header_checksum = htons(ComputeNetworkChecksum(this, Size()));
}
u16 IPv4Header::Size() const
{
return static_cast<u16>(SIZE);
}
TCPHeader::TCPHeader() = default;
TCPHeader::TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, const u8* data,
u16 length)
{
std::memcpy(&source_port, &from.sin_port, 2);
std::memcpy(&destination_port, &to.sin_port, 2);
sequence_number = htonl(seq);
// TODO: Write flags
// Write data offset
std::memset(&properties, 0x50, 1);
window_size = 0xFFFF;
// Compute the TCP checksum with its pseudo header
const u32 source_addr = ntohl(from.sin_addr.s_addr);
const u32 destination_addr = ntohl(to.sin_addr.s_addr);
const u32 initial_value = (source_addr >> 16) + (source_addr & 0xFFFF) +
(destination_addr >> 16) + (destination_addr & 0xFFFF) + IPProto() +
Size() + length;
u32 tcp_checksum = ComputeNetworkChecksum(this, Size(), initial_value);
tcp_checksum += ComputeNetworkChecksum(data, length);
while (tcp_checksum > 0xFFFF)
tcp_checksum = (tcp_checksum >> 16) + (tcp_checksum & 0xFFFF);
checksum = htons(static_cast<u16>(tcp_checksum));
}
TCPHeader::TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, u32 ack, u16 flags)
{
source_port = from.sin_port;
destination_port = to.sin_port;
sequence_number = htonl(seq);
acknowledgement_number = htonl(ack);
properties = 0x50 | flags;
window_size = 0x7c;
checksum = 0;
}
u16 TCPHeader::Size() const
{
return static_cast<u16>(SIZE);
}
u8 TCPHeader::IPProto() const
{
return static_cast<u8>(IPPROTO_TCP);
}
UDPHeader::UDPHeader() = default;
UDPHeader::UDPHeader(const sockaddr_in& from, const sockaddr_in& to, u16 data_length)
{
std::memcpy(&source_port, &from.sin_port, 2);
std::memcpy(&destination_port, &to.sin_port, 2);
length = htons(Size() + data_length);
}
u16 UDPHeader::Size() const
{
return static_cast<u16>(SIZE);
}
u8 UDPHeader::IPProto() const
{
return static_cast<u8>(IPPROTO_UDP);
}
ARPHeader::ARPHeader() = default;
ARPHeader::ARPHeader(u32 from_ip, MACAddress from_mac, u32 to_ip, MACAddress to_mac)
{
hardware_type = htons(BBA_HARDWARE_TYPE);
protocol_type = IPV4_HEADER_TYPE;
hardware_size = MAC_ADDRESS_SIZE;
protocol_size = IPV4_ADDR_LEN;
opcode = 0x200;
sender_ip = from_ip;
target_ip = to_ip;
targer_address = to_mac;
sender_address = from_mac;
}
u16 ARPHeader::Size() const
{
return static_cast<u16>(SIZE);
}
DHCPBody::DHCPBody() = default;
DHCPBody::DHCPBody(u32 transaction, MACAddress client_address, u32 new_ip, u32 serv_ip)
{
transaction_id = transaction;
message_type = DHCPConst::MESSAGE_REPLY;
hardware_type = BBA_HARDWARE_TYPE;
hardware_addr = MAC_ADDRESS_SIZE;
client_mac = client_address;
your_ip = new_ip;
server_ip = serv_ip;
}
// Add an option to the DHCP Body
bool DHCPBody::AddDHCPOption(u8 size, u8 fnc, const std::vector<u8>& params)
{
int i = 0;
while (options[i] != 0)
{
i += options[i + 1] + 2;
if (i >= std::size(options))
{
return false;
}
}
options[i++] = fnc;
options[i++] = size;
for (auto val : params)
options[i++] = val;
return true;
}
// Compute the network checksum with a 32-bit accumulator using the
// "Normal" order, see RFC 1071 for more details.
u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value)
{
u32 checksum = initial_value;
std::size_t index = 0;
const std::string_view data_view{reinterpret_cast<const char*>(data), length};
for (u8 b : data_view)
{
const bool is_hi = index++ % 2 == 0;
checksum += is_hi ? b << 8 : b;
}
while (checksum > 0xFFFF)
checksum = (checksum >> 16) + (checksum & 0xFFFF);
return ~static_cast<u16>(checksum);
}
// Compute the TCP network checksum with a fake header
u16 ComputeTCPNetworkChecksum(const sockaddr_in& from, const sockaddr_in& to, const void* data,
u16 length, u8 protocol)
{
// Compute the TCP checksum with its pseudo header
const u32 source_addr = ntohl(from.sin_addr.s_addr);
const u32 destination_addr = ntohl(to.sin_addr.s_addr);
const u32 initial_value = (source_addr >> 16) + (source_addr & 0xFFFF) +
(destination_addr >> 16) + (destination_addr & 0xFFFF) + protocol +
length;
const u32 tcp_checksum = ComputeNetworkChecksum(data, length, initial_value);
return htons(static_cast<u16>(tcp_checksum));
}
NetworkErrorState SaveNetworkErrorState()
{
return {
errno,
#ifdef _WIN32
WSAGetLastError(),
#endif
};
}
void RestoreNetworkErrorState(const NetworkErrorState& state)
{
errno = state.error;
#ifdef _WIN32
WSASetLastError(state.wsa_error);
#endif
}
} // namespace Common
|