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
322
323
324
325
326
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
#include <chrono>
#include <cstring>
#include "common/assert.h"
#include "common/logging.h"
#include "common/zstd_compression.h"
#include "core/internal_network/network.h"
#include "core/internal_network/network_interface.h"
#include "core/internal_network/socket_proxy.h"
#include "network/network.h"
#ifdef __unix__
#include <sys/socket.h>
#endif
namespace Network {
ProxySocket::ProxySocket() noexcept {}
ProxySocket::~ProxySocket() {
if (fd == INVALID_SOCKET) {
return;
}
fd = INVALID_SOCKET;
}
bool ProxySocket::MatchesPacket(const ProxyPacket& packet) const {
if (protocol != packet.protocol || local_endpoint.portno != packet.remote_endpoint.portno ||
closed) {
return false;
}
if (!broadcast && packet.broadcast) {
LOG_INFO(Network, "Received broadcast packet, but not configured for broadcast mode");
return false;
}
return true;
}
bool ProxySocket::AcceptsProxyPacket(const ProxyPacket& packet) {
std::lock_guard guard(packets_mutex);
return MatchesPacket(packet);
}
void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) {
{
// Runs on the room thread, while the game thread may bind, close or reconfigure the socket.
// Check again: the socket may have changed since AcceptsProxyPacket.
std::lock_guard guard(packets_mutex);
if (!MatchesPacket(packet)) {
return;
}
received_packets.push(packet);
}
// A MSG_PEEK reader leaves the packet queued, so every waiter must see it
packets_cv.notify_all();
}
template <typename T>
Errno ProxySocket::SetSockOpt(SOCKET fd_, int option, T value) {
LOG_DEBUG(Network, "(STUBBED) called");
return Errno::SUCCESS;
}
Errno ProxySocket::Initialize(Domain domain, Type type, Protocol socket_protocol) {
{
std::lock_guard guard(packets_mutex);
socket_type = type;
protocol = socket_protocol;
}
SetSockOpt(fd, SO_TYPE, type);
return Errno::SUCCESS;
}
std::pair<ProxySocket::AcceptResult, Errno> ProxySocket::Accept() {
LOG_WARNING(Network, "(STUBBED) called");
return {AcceptResult{}, Errno::SUCCESS};
}
Errno ProxySocket::Connect(SockAddrIn addr_in) {
LOG_WARNING(Network, "(STUBBED) called");
return Errno::SUCCESS;
}
std::pair<SockAddrIn, Errno> ProxySocket::GetPeerName() {
LOG_WARNING(Network, "(STUBBED) called");
return {SockAddrIn{}, Errno::SUCCESS};
}
std::pair<SockAddrIn, Errno> ProxySocket::GetSockName() {
LOG_WARNING(Network, "(STUBBED) called");
return {SockAddrIn{}, Errno::SUCCESS};
}
Errno ProxySocket::Bind(SockAddrIn addr) {
std::lock_guard guard(packets_mutex);
if (is_bound) {
LOG_WARNING(Network, "Rebinding Socket is unimplemented!");
return Errno::SUCCESS;
}
local_endpoint = addr;
is_bound = true;
return Errno::SUCCESS;
}
Errno ProxySocket::Listen(s32 backlog) {
LOG_WARNING(Network, "(STUBBED) called");
return Errno::SUCCESS;
}
Errno ProxySocket::Shutdown(ShutdownHow how) {
LOG_WARNING(Network, "(STUBBED) called");
return Errno::SUCCESS;
}
std::pair<s32, Errno> ProxySocket::Recv(int flags, std::span<u8> message) {
if (socket_type == Type::STREAM) {
// Room TCP sockets have no peer (Connect and Send are stubs). Report an empty queue as the
// end of the stream, as before, instead of blocking the bsd service until the timeout.
std::lock_guard guard(packets_mutex);
if (received_packets.empty()) {
return {0, Errno::SUCCESS};
}
}
return RecvFrom(flags, message, nullptr);
}
std::pair<s32, Errno> ProxySocket::RecvFrom(int flags, std::span<u8> message, SockAddrIn* addr) {
ASSERT((flags & ~FLAG_MSG_PEEK) == 0);
ASSERT(message.size() < static_cast<size_t>((std::numeric_limits<int>::max)()));
std::unique_lock lock(packets_mutex);
if (closed) {
return {-1, Errno::BADF};
}
if (received_packets.empty()) {
if (!blocking) {
return {-1, Errno::AGAIN};
}
// TODO (flTobi): Verify the timeout behavior and break when connection is lost
// When receive_timeout is set to zero, the socket is supposed to wait indefinitely until a
// packet arrives. In order to prevent lost packets from hanging the emulation thread, we set
// the timeout to 5s instead. An expired wait is reported like an SO_RCVTIMEO timeout.
const u32 timeout = receive_timeout == 0 ? 5000 : receive_timeout;
if (!packets_cv.wait_for(lock, std::chrono::milliseconds{timeout},
[this] { return closed || !received_packets.empty(); })) {
return {-1, Errno::AGAIN};
}
if (closed) {
return {-1, Errno::BADF};
}
}
return ReceivePacket(flags, message, addr, message.size());
}
std::pair<s32, Errno> ProxySocket::ReceivePacket(int flags, std::span<u8> message, SockAddrIn* addr,
std::size_t max_length) {
const ProxyPacket& packet = received_packets.front();
if (addr) {
addr->family = Domain::INET;
addr->ip = packet.local_endpoint.ip; // The senders ip address
addr->portno = packet.local_endpoint.portno; // The senders port number
}
const std::span<const u8> unread = std::span{packet.data}.subspan(front_read_offset);
const std::size_t read_bytes = (std::min)(unread.size(), max_length);
std::memcpy(message.data(), unread.data(), read_bytes);
if ((flags & FLAG_MSG_PEEK) != 0) {
return {static_cast<s32>(read_bytes), Errno::SUCCESS};
}
if (socket_type == Type::STREAM && read_bytes < unread.size()) {
// Keep the unread part of the stream for the next call
front_read_offset += read_bytes;
} else {
// Like BSD recv(), the rest of a datagram that does not fit is discarded
received_packets.pop();
front_read_offset = 0;
}
return {static_cast<s32>(read_bytes), Errno::SUCCESS};
}
std::pair<s32, Errno> ProxySocket::Send(std::span<const u8> message, int flags) {
LOG_WARNING(Network, "(STUBBED) called");
ASSERT(message.size() < static_cast<size_t>((std::numeric_limits<int>::max)()));
ASSERT(flags == 0);
return {static_cast<s32>(0), Errno::SUCCESS};
}
void ProxySocket::SendPacket(ProxyPacket& packet) {
if (auto room_member = Network::GetRoomMember().lock()) {
if (room_member->IsConnected()) {
packet.data = Common::Compression::CompressDataZSTDDefault(packet.data.data(),
packet.data.size());
room_member->SendProxyPacket(packet);
}
}
}
std::pair<s32, Errno> ProxySocket::SendTo(u32 flags, std::span<const u8> message,
const SockAddrIn* addr) {
ASSERT(flags == 0);
if (!is_bound) {
LOG_ERROR(Network, "ProxySocket is not bound!");
return {static_cast<s32>(message.size()), Errno::SUCCESS};
}
if (auto room_member = Network::GetRoomMember().lock()) {
if (!room_member->IsConnected()) {
return {static_cast<s32>(message.size()), Errno::SUCCESS};
}
}
ProxyPacket packet;
packet.local_endpoint = local_endpoint;
packet.remote_endpoint = *addr;
packet.protocol = protocol;
packet.broadcast = broadcast && packet.remote_endpoint.ip[3] == 255;
auto& ip = local_endpoint.ip;
auto ipv4 = Network::GetHostIPv4Address();
// If the ip is all zeroes (INADDR_ANY) or if it matches the hosts ip address,
// replace it with a "fake" routing address
if (std::all_of(ip.begin(), ip.end(), [](u8 i) { return i == 0; }) || (ipv4 && ipv4 == ip)) {
if (auto room_member = Network::GetRoomMember().lock()) {
packet.local_endpoint.ip = room_member->GetFakeIpAddress();
}
}
packet.data.clear();
std::copy(message.begin(), message.end(), std::back_inserter(packet.data));
SendPacket(packet);
return {static_cast<s32>(message.size()), Errno::SUCCESS};
}
Errno ProxySocket::Close() {
{
std::lock_guard guard(packets_mutex);
fd = INVALID_SOCKET;
closed = true;
received_packets = {};
front_read_offset = 0;
}
packets_cv.notify_all();
return Errno::SUCCESS;
}
Errno ProxySocket::SetLinger(bool enable, u32 linger) {
struct Linger {
u16 linger_enable;
u16 linger_time;
} values;
values.linger_enable = enable ? 1 : 0;
values.linger_time = static_cast<u16>(linger);
return SetSockOpt(fd, SO_LINGER, values);
}
Errno ProxySocket::SetReuseAddr(bool enable) {
return SetSockOpt<u32>(fd, SO_REUSEADDR, enable ? 1 : 0);
}
Errno ProxySocket::SetBroadcast(bool enable) {
{
std::lock_guard guard(packets_mutex);
broadcast = enable;
}
return SetSockOpt<u32>(fd, SO_BROADCAST, enable ? 1 : 0);
}
Errno ProxySocket::SetSndBuf(u32 value) {
return SetSockOpt(fd, SO_SNDBUF, value);
}
Errno ProxySocket::SetKeepAlive(bool enable) {
return Errno::SUCCESS;
}
Errno ProxySocket::SetRcvBuf(u32 value) {
return SetSockOpt(fd, SO_RCVBUF, value);
}
Errno ProxySocket::SetSndTimeo(u32 value) {
send_timeout = value;
return SetSockOpt(fd, SO_SNDTIMEO, static_cast<int>(value));
}
Errno ProxySocket::SetRcvTimeo(u32 value) {
{
std::lock_guard guard(packets_mutex);
receive_timeout = value;
}
return SetSockOpt(fd, SO_RCVTIMEO, static_cast<int>(value));
}
Errno ProxySocket::SetNonBlock(bool enable) {
std::lock_guard guard(packets_mutex);
blocking = !enable;
return Errno::SUCCESS;
}
std::pair<Errno, Errno> ProxySocket::GetPendingError() {
LOG_DEBUG(Network, "(STUBBED) called");
return {Errno::SUCCESS, Errno::SUCCESS};
}
bool ProxySocket::IsOpened() const {
return fd != INVALID_SOCKET;
}
} // namespace Network
|