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
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <memory>
#include <mutex>
#include <span>
#include <vector>
#include <variant>
#include "common/common_types.h"
#include "common/socket_types.h"
#include "core/hle/service/service.h"
#include "core/hle/service/sockets/sockets.h"
#include "network/network.h"
namespace Core {
class System;
}
namespace Network {
class SocketBase;
class Socket;
} // namespace Network
namespace Service::Sockets {
class BSD_USA final : public ServiceFramework<BSD_USA> {
public:
explicit BSD_USA(Core::System& system_, const char* name, bool is_user);
~BSD_USA() override;
// These methods are called from SSL; the first two are also called from
// this class for the corresponding IPC methods.
// On the real device, the SSL service makes IPC calls to this service.
std::variant<s32, Errno> DuplicateSocketImpl(s32 fd);
Errno CloseImpl(s32 fd);
std::optional<std::shared_ptr<Network::SocketBase>> GetSocket(s32 fd);
private:
/// Maximum number of file descriptors
static constexpr size_t MAX_FD = 128;
struct FileDescriptor {
std::shared_ptr<Network::SocketBase> socket;
s32 flags = 0;
bool is_connection_based = false;
};
struct PollWork {
void Execute(BSD_USA* bsd);
void Response(HLERequestContext& ctx);
s32 nfds;
s32 timeout;
std::span<const u8> read_buffer;
std::vector<u8> write_buffer;
s32 ret{};
Errno bsd_errno{};
};
struct AcceptWork {
void Execute(BSD_USA* bsd);
void Response(HLERequestContext& ctx);
s32 fd;
std::vector<u8> write_buffer;
s32 ret{};
Errno bsd_errno{};
};
struct ConnectWork {
void Execute(BSD_USA* bsd);
void Response(HLERequestContext& ctx);
s32 fd;
std::span<const u8> addr;
Errno bsd_errno{};
};
struct RecvWork {
void Execute(BSD_USA* bsd);
void Response(HLERequestContext& ctx);
s32 fd;
u32 flags;
std::vector<u8> message;
s32 ret{};
Errno bsd_errno{};
};
struct RecvFromWork {
void Execute(BSD_USA* bsd);
void Response(HLERequestContext& ctx);
s32 fd;
u32 flags;
std::vector<u8> message;
std::vector<u8> addr;
s32 ret{};
Errno bsd_errno{};
};
struct SendWork {
void Execute(BSD_USA* bsd);
void Response(HLERequestContext& ctx);
s32 fd;
u32 flags;
std::span<const u8> message;
s32 ret{};
Errno bsd_errno{};
};
struct SendToWork {
void Execute(BSD_USA* bsd);
void Response(HLERequestContext& ctx);
s32 fd;
u32 flags;
std::span<const u8> message;
std::span<const u8> addr;
s32 ret{};
Errno bsd_errno{};
};
void RegisterClient(HLERequestContext& ctx);
void StartMonitoring(HLERequestContext& ctx);
void Socket(HLERequestContext& ctx);
void SocketExempt(HLERequestContext& ctx);
void Select(HLERequestContext& ctx);
void Poll(HLERequestContext& ctx);
void Accept(HLERequestContext& ctx);
void Bind(HLERequestContext& ctx);
void Connect(HLERequestContext& ctx);
void GetPeerName(HLERequestContext& ctx);
void GetSockName(HLERequestContext& ctx);
void GetSockOpt(HLERequestContext& ctx);
void Listen(HLERequestContext& ctx);
void Fcntl(HLERequestContext& ctx);
void SetSockOpt(HLERequestContext& ctx);
void Shutdown(HLERequestContext& ctx);
void Recv(HLERequestContext& ctx);
void RecvFrom(HLERequestContext& ctx);
void Send(HLERequestContext& ctx);
void SendTo(HLERequestContext& ctx);
void Write(HLERequestContext& ctx);
void Read(HLERequestContext& ctx);
void Close(HLERequestContext& ctx);
void DuplicateSocket(HLERequestContext& ctx);
void EventFd(HLERequestContext& ctx);
template <typename Work>
void ExecuteWork(HLERequestContext& ctx, Work work);
std::pair<s32, Errno> SocketImpl(Domain domain, Type type, Protocol protocol);
std::pair<s32, Errno> PollImpl(std::vector<u8>& write_buffer, std::span<const u8> read_buffer, s32 nfds, s32 timeout);
std::pair<s32, Errno> AcceptImpl(s32 fd, std::vector<u8>& write_buffer);
Errno BindImpl(s32 fd, std::span<const u8> addr);
Errno ConnectImpl(s32 fd, std::span<const u8> addr);
Errno GetPeerNameImpl(s32 fd, std::vector<u8>& write_buffer);
Errno GetSockNameImpl(s32 fd, std::vector<u8>& write_buffer);
Errno ListenImpl(s32 fd, s32 backlog);
std::pair<s32, Errno> FcntlImpl(s32 fd, FcntlCmd cmd, s32 arg);
Errno GetSockOptImpl(s32 fd, u32 level, OptName optname, std::vector<u8>& optval);
Errno SetSockOptImpl(s32 fd, u32 level, OptName optname, std::span<const u8> optval);
Errno ShutdownImpl(s32 fd, s32 how);
std::pair<s32, Errno> RecvImpl(s32 fd, u32 flags, std::vector<u8>& message);
std::pair<s32, Errno> RecvFromImpl(s32 fd, u32 flags, std::vector<u8>& message,
std::vector<u8>& addr);
std::pair<s32, Errno> SendImpl(s32 fd, u32 flags, std::span<const u8> message);
std::pair<s32, Errno> SendToImpl(s32 fd, u32 flags, std::span<const u8> message,
std::span<const u8> addr);
s32 FindFreeFileDescriptorHandle() noexcept;
bool IsFileDescriptorValid(s32 fd) const noexcept;
/// Copies a descriptor under file_descriptors_mutex; nullopt if fd has no socket
std::optional<FileDescriptor> CopyDescriptor(s32 fd);
void BuildErrnoResponse(HLERequestContext& ctx, Errno bsd_errno) const noexcept;
static inline std::array<std::optional<FileDescriptor>, MAX_FD> file_descriptors{};
/// Guards file_descriptors. Calls use CopyDescriptor, so sockets outlive a concurrent close.
static inline std::mutex file_descriptors_mutex;
/// Callback to parse and handle a received wifi packet.
static void OnProxyPacketReceived(const Network::ProxyPacket& packet);
// Callback identifier for the OnProxyPacketReceived event. bsd:s, bsd:u and bsd:a share
// file_descriptors, so they bind once; otherwise each packet is queued once per service.
static inline Network::RoomMember::CallbackHandle<Network::ProxyPacket> proxy_packet_received;
static inline std::mutex proxy_binding_mutex;
static inline size_t proxy_binding_count = 0;
protected:
std::unique_lock<std::mutex> LockService() noexcept override;
bool is_user = false;
};
class BSDCFG final : public ServiceFramework<BSDCFG> {
public:
explicit BSDCFG(Core::System& system_, const char *name);
~BSDCFG() override;
};
class BSD_NU final : public ServiceFramework<BSD_NU> {
public:
explicit BSD_NU(Core::System& system_);
~BSD_NU() override;
};
} // namespace Service::Sockets
|