2018-01-18 19:35:03 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include "common/microprofile.h"
|
|
|
|
#include "common/thread.h"
|
2018-01-18 19:35:03 +00:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
#include "core/hle/kernel/thread.h"
|
2018-03-25 09:41:00 +00:00
|
|
|
#include "core/hle/service/sockets/bsd.h"
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
#include "core/hle/service/sockets/sockets_translate.h"
|
|
|
|
#include "core/network/network.h"
|
|
|
|
#include "core/network/sockets.h"
|
2018-01-18 19:35:03 +00:00
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
namespace Service::Sockets {
|
2018-01-18 19:35:03 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool IsConnectionBased(Type type) {
|
|
|
|
switch (type) {
|
|
|
|
case Type::STREAM:
|
|
|
|
return true;
|
|
|
|
case Type::DGRAM:
|
|
|
|
return false;
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented type={}", type);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
void BSD::PollWork::Execute(BSD* bsd) {
|
|
|
|
std::tie(ret, bsd_errno) = bsd->PollImpl(write_buffer, read_buffer, nfds, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::PollWork::Response(Kernel::HLERequestContext& ctx) {
|
|
|
|
ctx.WriteBuffer(write_buffer);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<s32>(ret);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::AcceptWork::Execute(BSD* bsd) {
|
|
|
|
std::tie(ret, bsd_errno) = bsd->AcceptImpl(fd, write_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::AcceptWork::Response(Kernel::HLERequestContext& ctx) {
|
|
|
|
ctx.WriteBuffer(write_buffer);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 5};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<s32>(ret);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
rb.Push<u32>(static_cast<u32>(write_buffer.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::ConnectWork::Execute(BSD* bsd) {
|
|
|
|
bsd_errno = bsd->ConnectImpl(fd, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::ConnectWork::Response(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<s32>(bsd_errno == Errno::SUCCESS ? 0 : -1);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::RecvWork::Execute(BSD* bsd) {
|
|
|
|
std::tie(ret, bsd_errno) = bsd->RecvImpl(fd, flags, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::RecvWork::Response(Kernel::HLERequestContext& ctx) {
|
|
|
|
ctx.WriteBuffer(message);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<s32>(ret);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::RecvFromWork::Execute(BSD* bsd) {
|
|
|
|
std::tie(ret, bsd_errno) = bsd->RecvFromImpl(fd, flags, message, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::RecvFromWork::Response(Kernel::HLERequestContext& ctx) {
|
|
|
|
ctx.WriteBuffer(message, 0);
|
|
|
|
if (!addr.empty()) {
|
|
|
|
ctx.WriteBuffer(addr, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 5};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<s32>(ret);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
rb.Push<u32>(static_cast<u32>(addr.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::SendWork::Execute(BSD* bsd) {
|
|
|
|
std::tie(ret, bsd_errno) = bsd->SendImpl(fd, flags, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::SendWork::Response(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<s32>(ret);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::SendToWork::Execute(BSD* bsd) {
|
|
|
|
std::tie(ret, bsd_errno) = bsd->SendToImpl(fd, flags, message, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::SendToWork::Response(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<s32>(ret);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
}
|
|
|
|
|
2018-03-25 09:41:00 +00:00
|
|
|
void BSD::RegisterClient(Kernel::HLERequestContext& ctx) {
|
2018-07-02 16:13:26 +00:00
|
|
|
LOG_WARNING(Service, "(STUBBED) called");
|
2018-01-18 19:35:03 +00:00
|
|
|
|
2018-01-24 00:52:18 +00:00
|
|
|
IPC::ResponseBuilder rb{ctx, 3};
|
2018-01-18 19:35:03 +00:00
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
rb.Push<s32>(0); // bsd errno
|
2018-01-18 19:35:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 09:41:00 +00:00
|
|
|
void BSD::StartMonitoring(Kernel::HLERequestContext& ctx) {
|
2018-07-02 16:13:26 +00:00
|
|
|
LOG_WARNING(Service, "(STUBBED) called");
|
2018-02-22 10:04:23 +00:00
|
|
|
|
2018-07-14 17:34:07 +00:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2018-02-22 10:04:23 +00:00
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-03-25 09:41:00 +00:00
|
|
|
void BSD::Socket(Kernel::HLERequestContext& ctx) {
|
2018-01-18 19:35:03 +00:00
|
|
|
IPC::RequestParser rp{ctx};
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
const u32 domain = rp.Pop<u32>();
|
|
|
|
const u32 type = rp.Pop<u32>();
|
|
|
|
const u32 protocol = rp.Pop<u32>();
|
2018-01-18 19:35:03 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_DEBUG(Service, "called. domain={} type={} protocol={}", domain, type, protocol);
|
2018-01-18 19:35:03 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
const auto [fd, bsd_errno] = SocketImpl(static_cast<Domain>(domain), static_cast<Type>(type),
|
|
|
|
static_cast<Protocol>(protocol));
|
2018-01-18 19:35:03 +00:00
|
|
|
|
2018-01-24 00:52:18 +00:00
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
2018-01-18 19:35:03 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
rb.Push<s32>(fd);
|
|
|
|
rb.PushEnum(bsd_errno);
|
2018-01-18 19:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 05:47:15 +00:00
|
|
|
void BSD::Select(Kernel::HLERequestContext& ctx) {
|
|
|
|
LOG_WARNING(Service, "(STUBBED) called");
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(0); // ret
|
|
|
|
rb.Push<u32>(0); // bsd errno
|
|
|
|
}
|
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
void BSD::Poll(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 nfds = rp.Pop<s32>();
|
|
|
|
const s32 timeout = rp.Pop<s32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. nfds={} timeout={}", nfds, timeout);
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
ExecuteWork(ctx, PollWork{
|
|
|
|
.nfds = nfds,
|
|
|
|
.timeout = timeout,
|
|
|
|
.read_buffer = ctx.ReadBuffer(),
|
|
|
|
.write_buffer = std::vector<u8>(ctx.GetWriteBufferSize()),
|
|
|
|
});
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::Accept(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={}", fd);
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
ExecuteWork(ctx, AcceptWork{
|
|
|
|
.fd = fd,
|
|
|
|
.write_buffer = std::vector<u8>(ctx.GetWriteBufferSize()),
|
|
|
|
});
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 05:47:15 +00:00
|
|
|
void BSD::Bind(Kernel::HLERequestContext& ctx) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
2020-01-25 05:47:15 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_DEBUG(Service, "called. fd={} addrlen={}", fd, ctx.GetReadBufferSize());
|
2020-01-25 05:47:15 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
BuildErrnoResponse(ctx, BindImpl(fd, ctx.ReadBuffer()));
|
2020-01-25 05:47:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 09:41:00 +00:00
|
|
|
void BSD::Connect(Kernel::HLERequestContext& ctx) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
2018-01-18 19:35:03 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_DEBUG(Service, "called. fd={} addrlen={}", fd, ctx.GetReadBufferSize());
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
ExecuteWork(ctx, ConnectWork{
|
|
|
|
.fd = fd,
|
|
|
|
.addr = ctx.ReadBuffer(),
|
|
|
|
});
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::GetPeerName(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={}", fd);
|
2018-01-18 19:35:03 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
std::vector<u8> write_buffer(ctx.GetWriteBufferSize());
|
|
|
|
const Errno bsd_errno = GetPeerNameImpl(fd, write_buffer);
|
|
|
|
|
|
|
|
ctx.WriteBuffer(write_buffer);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 5};
|
2018-01-18 19:35:03 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
rb.Push<s32>(bsd_errno != Errno::SUCCESS ? -1 : 0);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
rb.Push<u32>(static_cast<u32>(write_buffer.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::GetSockName(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={}", fd);
|
|
|
|
|
|
|
|
std::vector<u8> write_buffer(ctx.GetWriteBufferSize());
|
|
|
|
const Errno bsd_errno = GetSockNameImpl(fd, write_buffer);
|
|
|
|
|
|
|
|
ctx.WriteBuffer(write_buffer);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 5};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<s32>(bsd_errno != Errno::SUCCESS ? -1 : 0);
|
|
|
|
rb.PushEnum(bsd_errno);
|
|
|
|
rb.Push<u32>(static_cast<u32>(write_buffer.size()));
|
2018-01-18 19:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 05:47:15 +00:00
|
|
|
void BSD::Listen(Kernel::HLERequestContext& ctx) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
const s32 backlog = rp.Pop<s32>();
|
2020-01-25 05:47:15 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_DEBUG(Service, "called. fd={} backlog={}", fd, backlog);
|
|
|
|
|
|
|
|
BuildErrnoResponse(ctx, ListenImpl(fd, backlog));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::Fcntl(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
const s32 cmd = rp.Pop<s32>();
|
|
|
|
const s32 arg = rp.Pop<s32>();
|
2020-01-25 05:47:15 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_DEBUG(Service, "called. fd={} cmd={} arg={}", fd, cmd, arg);
|
|
|
|
|
|
|
|
const auto [ret, bsd_errno] = FcntlImpl(fd, static_cast<FcntlCmd>(cmd), arg);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
2020-01-25 05:47:15 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
rb.Push<s32>(ret);
|
|
|
|
rb.PushEnum(bsd_errno);
|
2020-01-25 05:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::SetSockOpt(Kernel::HLERequestContext& ctx) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
IPC::RequestParser rp{ctx};
|
2020-01-25 05:47:15 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
const u32 level = rp.Pop<u32>();
|
|
|
|
const OptName optname = static_cast<OptName>(rp.Pop<u32>());
|
2020-01-25 05:47:15 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
const std::vector<u8> buffer = ctx.ReadBuffer();
|
|
|
|
const u8* optval = buffer.empty() ? nullptr : buffer.data();
|
|
|
|
size_t optlen = buffer.size();
|
|
|
|
|
|
|
|
std::array<u64, 2> values;
|
|
|
|
if ((optname == OptName::SNDTIMEO || optname == OptName::RCVTIMEO) && buffer.size() == 8) {
|
|
|
|
std::memcpy(values.data(), buffer.data(), sizeof(values));
|
|
|
|
optlen = sizeof(values);
|
|
|
|
optval = reinterpret_cast<const u8*>(values.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={} level={} optname=0x{:x} optlen={}", fd, level,
|
|
|
|
static_cast<u32>(optname), optlen);
|
|
|
|
|
|
|
|
BuildErrnoResponse(ctx, SetSockOptImpl(fd, level, optname, optlen, optval));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::Shutdown(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
const s32 how = rp.Pop<s32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={} how={}", fd, how);
|
|
|
|
|
|
|
|
BuildErrnoResponse(ctx, ShutdownImpl(fd, how));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::Recv(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
const u32 flags = rp.Pop<u32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={} flags=0x{:x} len={}", fd, flags, ctx.GetWriteBufferSize());
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
ExecuteWork(ctx, RecvWork{
|
|
|
|
.fd = fd,
|
|
|
|
.flags = flags,
|
|
|
|
.message = std::vector<u8>(ctx.GetWriteBufferSize()),
|
|
|
|
});
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::RecvFrom(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
const u32 flags = rp.Pop<u32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={} flags=0x{:x} len={} addrlen={}", fd, flags,
|
|
|
|
ctx.GetWriteBufferSize(0), ctx.GetWriteBufferSize(1));
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
ExecuteWork(ctx, RecvFromWork{
|
|
|
|
.fd = fd,
|
|
|
|
.flags = flags,
|
|
|
|
.message = std::vector<u8>(ctx.GetWriteBufferSize(0)),
|
|
|
|
.addr = std::vector<u8>(ctx.GetWriteBufferSize(1)),
|
|
|
|
});
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::Send(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
const u32 flags = rp.Pop<u32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={} flags=0x{:x} len={}", fd, flags, ctx.GetReadBufferSize());
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
ExecuteWork(ctx, SendWork{
|
|
|
|
.fd = fd,
|
|
|
|
.flags = flags,
|
|
|
|
.message = ctx.ReadBuffer(),
|
|
|
|
});
|
2020-01-25 05:47:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 09:41:00 +00:00
|
|
|
void BSD::SendTo(Kernel::HLERequestContext& ctx) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
const u32 flags = rp.Pop<u32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={} flags=0x{} len={} addrlen={}", fd, flags,
|
|
|
|
ctx.GetReadBufferSize(0), ctx.GetReadBufferSize(1));
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
ExecuteWork(ctx, SendToWork{
|
|
|
|
.fd = fd,
|
|
|
|
.flags = flags,
|
|
|
|
.message = ctx.ReadBuffer(0),
|
|
|
|
.addr = ctx.ReadBuffer(1),
|
|
|
|
});
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
2018-01-18 19:35:03 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
void BSD::Write(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
2018-01-18 19:35:03 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_DEBUG(Service, "called. fd={} len={}", fd, ctx.GetReadBufferSize());
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
ExecuteWork(ctx, SendWork{
|
|
|
|
.fd = fd,
|
|
|
|
.flags = 0,
|
|
|
|
.message = ctx.ReadBuffer(),
|
|
|
|
});
|
2018-01-18 19:35:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 09:41:00 +00:00
|
|
|
void BSD::Close(Kernel::HLERequestContext& ctx) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const s32 fd = rp.Pop<s32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service, "called. fd={}", fd);
|
|
|
|
|
|
|
|
BuildErrnoResponse(ctx, CloseImpl(fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Work>
|
2020-12-12 00:44:27 +00:00
|
|
|
void BSD::ExecuteWork(Kernel::HLERequestContext& ctx, Work work) {
|
|
|
|
work.Execute(this);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
work.Response(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> BSD::SocketImpl(Domain domain, Type type, Protocol protocol) {
|
|
|
|
if (type == Type::SEQPACKET) {
|
|
|
|
UNIMPLEMENTED_MSG("SOCK_SEQPACKET errno management");
|
|
|
|
} else if (type == Type::RAW && (domain != Domain::INET || protocol != Protocol::ICMP)) {
|
|
|
|
UNIMPLEMENTED_MSG("SOCK_RAW errno management");
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
[[maybe_unused]] const bool unk_flag = (static_cast<u32>(type) & 0x20000000) != 0;
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
UNIMPLEMENTED_IF_MSG(unk_flag, "Unknown flag in type");
|
2020-10-21 02:07:39 +00:00
|
|
|
type = static_cast<Type>(static_cast<u32>(type) & ~0x20000000);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
|
|
|
|
const s32 fd = FindFreeFileDescriptorHandle();
|
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service, "No more file descriptors available");
|
|
|
|
return {-1, Errno::MFILE};
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
FileDescriptor& descriptor = file_descriptors[fd].emplace();
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
// ENONMEM might be thrown here
|
|
|
|
|
|
|
|
LOG_INFO(Service, "New socket fd={}", fd);
|
|
|
|
|
|
|
|
descriptor.socket = std::make_unique<Network::Socket>();
|
|
|
|
descriptor.socket->Initialize(Translate(domain), Translate(type), Translate(type, protocol));
|
|
|
|
descriptor.is_connection_based = IsConnectionBased(type);
|
|
|
|
|
|
|
|
return {fd, Errno::SUCCESS};
|
|
|
|
}
|
2018-01-30 06:29:47 +00:00
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
std::pair<s32, Errno> BSD::PollImpl(std::vector<u8>& write_buffer, std::vector<u8> read_buffer,
|
|
|
|
s32 nfds, s32 timeout) {
|
2020-10-21 02:07:39 +00:00
|
|
|
if (write_buffer.size() < nfds * sizeof(PollFD)) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return {-1, Errno::INVAL};
|
|
|
|
}
|
|
|
|
|
2020-07-28 04:51:44 +00:00
|
|
|
if (nfds == 0) {
|
|
|
|
// When no entries are provided, -1 is returned with errno zero
|
|
|
|
return {-1, Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
const size_t length = std::min(read_buffer.size(), write_buffer.size());
|
2020-10-21 02:07:39 +00:00
|
|
|
std::vector<PollFD> fds(nfds);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
std::memcpy(fds.data(), read_buffer.data(), length);
|
|
|
|
|
|
|
|
if (timeout >= 0) {
|
|
|
|
const s64 seconds = timeout / 1000;
|
|
|
|
const u64 nanoseconds = 1'000'000 * (static_cast<u64>(timeout) % 1000);
|
|
|
|
|
|
|
|
if (seconds < 0) {
|
|
|
|
return {-1, Errno::INVAL};
|
|
|
|
}
|
|
|
|
if (nanoseconds > 999'999'999) {
|
|
|
|
return {-1, Errno::INVAL};
|
|
|
|
}
|
|
|
|
} else if (timeout != -1) {
|
|
|
|
return {-1, Errno::INVAL};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PollFD& pollfd : fds) {
|
network, sockets: Replace `POLL_IN`, `POLL_OUT`, etc. constants with an `enum class PollEvents`
Actually, two enum classes, since for some reason there are two separate
yet identical `PollFD` types used in the codebase. I get that one is
ABI-compatible with the Switch while the other is an abstract type used
for the host, but why not use `WSAPOLLFD` directly for the latter?
Anyway, why make this change? Because on Apple platforms, `POLL_IN`,
`POLL_OUT`, etc. (with an underscore) are defined as macros in
<sys/signal.h>. (This is inherited from FreeBSD.) So defining
a variable with the same name causes a compile error.
I could just rename the variables, but while I was at it I thought I
might as well switch to an enum for stronger typing.
Also, change the type used for values copied directly to/from the
`events` and `revents` fields of the host *native*
`pollfd`/`WSASPOLLFD`, from `u32` to `short`, as `short` is the correct
canonical type on both Unix and Windows.
2020-08-31 14:20:44 +00:00
|
|
|
ASSERT(False(pollfd.revents));
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
|
2020-09-07 04:57:39 +00:00
|
|
|
if (pollfd.fd > static_cast<s32>(MAX_FD) || pollfd.fd < 0) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_ERROR(Service, "File descriptor handle={} is invalid", pollfd.fd);
|
network, sockets: Replace `POLL_IN`, `POLL_OUT`, etc. constants with an `enum class PollEvents`
Actually, two enum classes, since for some reason there are two separate
yet identical `PollFD` types used in the codebase. I get that one is
ABI-compatible with the Switch while the other is an abstract type used
for the host, but why not use `WSAPOLLFD` directly for the latter?
Anyway, why make this change? Because on Apple platforms, `POLL_IN`,
`POLL_OUT`, etc. (with an underscore) are defined as macros in
<sys/signal.h>. (This is inherited from FreeBSD.) So defining
a variable with the same name causes a compile error.
I could just rename the variables, but while I was at it I thought I
might as well switch to an enum for stronger typing.
Also, change the type used for values copied directly to/from the
`events` and `revents` fields of the host *native*
`pollfd`/`WSASPOLLFD`, from `u32` to `short`, as `short` is the correct
canonical type on both Unix and Windows.
2020-08-31 14:20:44 +00:00
|
|
|
pollfd.revents = PollEvents{};
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return {0, Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
const std::optional<FileDescriptor>& descriptor = file_descriptors[pollfd.fd];
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
if (!descriptor) {
|
|
|
|
LOG_ERROR(Service, "File descriptor handle={} is not allocated", pollfd.fd);
|
network, sockets: Replace `POLL_IN`, `POLL_OUT`, etc. constants with an `enum class PollEvents`
Actually, two enum classes, since for some reason there are two separate
yet identical `PollFD` types used in the codebase. I get that one is
ABI-compatible with the Switch while the other is an abstract type used
for the host, but why not use `WSAPOLLFD` directly for the latter?
Anyway, why make this change? Because on Apple platforms, `POLL_IN`,
`POLL_OUT`, etc. (with an underscore) are defined as macros in
<sys/signal.h>. (This is inherited from FreeBSD.) So defining
a variable with the same name causes a compile error.
I could just rename the variables, but while I was at it I thought I
might as well switch to an enum for stronger typing.
Also, change the type used for values copied directly to/from the
`events` and `revents` fields of the host *native*
`pollfd`/`WSASPOLLFD`, from `u32` to `short`, as `short` is the correct
canonical type on both Unix and Windows.
2020-08-31 14:20:44 +00:00
|
|
|
pollfd.revents = PollEvents::Nval;
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return {0, Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Network::PollFD> host_pollfds(fds.size());
|
|
|
|
std::transform(fds.begin(), fds.end(), host_pollfds.begin(), [this](PollFD pollfd) {
|
|
|
|
Network::PollFD result;
|
2020-10-21 02:07:39 +00:00
|
|
|
result.socket = file_descriptors[pollfd.fd]->socket.get();
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
result.events = TranslatePollEventsToHost(pollfd.events);
|
network, sockets: Replace `POLL_IN`, `POLL_OUT`, etc. constants with an `enum class PollEvents`
Actually, two enum classes, since for some reason there are two separate
yet identical `PollFD` types used in the codebase. I get that one is
ABI-compatible with the Switch while the other is an abstract type used
for the host, but why not use `WSAPOLLFD` directly for the latter?
Anyway, why make this change? Because on Apple platforms, `POLL_IN`,
`POLL_OUT`, etc. (with an underscore) are defined as macros in
<sys/signal.h>. (This is inherited from FreeBSD.) So defining
a variable with the same name causes a compile error.
I could just rename the variables, but while I was at it I thought I
might as well switch to an enum for stronger typing.
Also, change the type used for values copied directly to/from the
`events` and `revents` fields of the host *native*
`pollfd`/`WSASPOLLFD`, from `u32` to `short`, as `short` is the correct
canonical type on both Unix and Windows.
2020-08-31 14:20:44 +00:00
|
|
|
result.revents = Network::PollEvents{};
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto result = Network::Poll(host_pollfds, timeout);
|
|
|
|
|
|
|
|
const size_t num = host_pollfds.size();
|
|
|
|
for (size_t i = 0; i < num; ++i) {
|
|
|
|
fds[i].revents = TranslatePollEventsToGuest(host_pollfds[i].revents);
|
|
|
|
}
|
|
|
|
std::memcpy(write_buffer.data(), fds.data(), length);
|
|
|
|
|
|
|
|
return Translate(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> BSD::AcceptImpl(s32 fd, std::vector<u8>& write_buffer) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return {-1, Errno::BADF};
|
|
|
|
}
|
|
|
|
|
|
|
|
const s32 new_fd = FindFreeFileDescriptorHandle();
|
|
|
|
if (new_fd < 0) {
|
|
|
|
LOG_ERROR(Service, "No more file descriptors available");
|
|
|
|
return {-1, Errno::MFILE};
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
FileDescriptor& descriptor = *file_descriptors[fd];
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
auto [result, bsd_errno] = descriptor.socket->Accept();
|
|
|
|
if (bsd_errno != Network::Errno::SUCCESS) {
|
|
|
|
return {-1, Translate(bsd_errno)};
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
FileDescriptor& new_descriptor = file_descriptors[new_fd].emplace();
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
new_descriptor.socket = std::move(result.socket);
|
|
|
|
new_descriptor.is_connection_based = descriptor.is_connection_based;
|
|
|
|
|
|
|
|
ASSERT(write_buffer.size() == sizeof(SockAddrIn));
|
|
|
|
const SockAddrIn guest_addr_in = Translate(result.sockaddr_in);
|
|
|
|
std::memcpy(write_buffer.data(), &guest_addr_in, sizeof(guest_addr_in));
|
|
|
|
|
|
|
|
return {new_fd, Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno BSD::BindImpl(s32 fd, const std::vector<u8>& addr) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return Errno::BADF;
|
|
|
|
}
|
|
|
|
ASSERT(addr.size() == sizeof(SockAddrIn));
|
|
|
|
SockAddrIn addr_in;
|
|
|
|
std::memcpy(&addr_in, addr.data(), sizeof(addr_in));
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
return Translate(file_descriptors[fd]->socket->Bind(Translate(addr_in)));
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Errno BSD::ConnectImpl(s32 fd, const std::vector<u8>& addr) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return Errno::BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNIMPLEMENTED_IF(addr.size() != sizeof(SockAddrIn));
|
|
|
|
SockAddrIn addr_in;
|
|
|
|
std::memcpy(&addr_in, addr.data(), sizeof(addr_in));
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
return Translate(file_descriptors[fd]->socket->Connect(Translate(addr_in)));
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Errno BSD::GetPeerNameImpl(s32 fd, std::vector<u8>& write_buffer) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return Errno::BADF;
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
const auto [addr_in, bsd_errno] = file_descriptors[fd]->socket->GetPeerName();
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
if (bsd_errno != Network::Errno::SUCCESS) {
|
|
|
|
return Translate(bsd_errno);
|
|
|
|
}
|
|
|
|
const SockAddrIn guest_addrin = Translate(addr_in);
|
|
|
|
|
|
|
|
ASSERT(write_buffer.size() == sizeof(guest_addrin));
|
|
|
|
std::memcpy(write_buffer.data(), &guest_addrin, sizeof(guest_addrin));
|
|
|
|
return Translate(bsd_errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno BSD::GetSockNameImpl(s32 fd, std::vector<u8>& write_buffer) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return Errno::BADF;
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
const auto [addr_in, bsd_errno] = file_descriptors[fd]->socket->GetSockName();
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
if (bsd_errno != Network::Errno::SUCCESS) {
|
|
|
|
return Translate(bsd_errno);
|
|
|
|
}
|
|
|
|
const SockAddrIn guest_addrin = Translate(addr_in);
|
|
|
|
|
|
|
|
ASSERT(write_buffer.size() == sizeof(guest_addrin));
|
|
|
|
std::memcpy(write_buffer.data(), &guest_addrin, sizeof(guest_addrin));
|
|
|
|
return Translate(bsd_errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno BSD::ListenImpl(s32 fd, s32 backlog) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return Errno::BADF;
|
|
|
|
}
|
2020-10-21 02:07:39 +00:00
|
|
|
return Translate(file_descriptors[fd]->socket->Listen(backlog));
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> BSD::FcntlImpl(s32 fd, FcntlCmd cmd, s32 arg) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return {-1, Errno::BADF};
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
FileDescriptor& descriptor = *file_descriptors[fd];
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FcntlCmd::GETFL:
|
|
|
|
ASSERT(arg == 0);
|
|
|
|
return {descriptor.flags, Errno::SUCCESS};
|
|
|
|
case FcntlCmd::SETFL: {
|
2020-10-21 02:07:39 +00:00
|
|
|
const bool enable = (arg & FLAG_O_NONBLOCK) != 0;
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
const Errno bsd_errno = Translate(descriptor.socket->SetNonBlock(enable));
|
|
|
|
if (bsd_errno != Errno::SUCCESS) {
|
|
|
|
return {-1, bsd_errno};
|
|
|
|
}
|
|
|
|
descriptor.flags = arg;
|
|
|
|
return {0, Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented cmd={}", cmd);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return {-1, Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno BSD::SetSockOptImpl(s32 fd, u32 level, OptName optname, size_t optlen, const void* optval) {
|
|
|
|
UNIMPLEMENTED_IF(level != 0xffff); // SOL_SOCKET
|
|
|
|
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return Errno::BADF;
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
Network::Socket* const socket = file_descriptors[fd]->socket.get();
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
|
|
|
|
if (optname == OptName::LINGER) {
|
|
|
|
ASSERT(optlen == sizeof(Linger));
|
|
|
|
Linger linger;
|
|
|
|
std::memcpy(&linger, optval, sizeof(linger));
|
|
|
|
ASSERT(linger.onoff == 0 || linger.onoff == 1);
|
|
|
|
|
|
|
|
return Translate(socket->SetLinger(linger.onoff != 0, linger.linger));
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(optlen == sizeof(u32));
|
|
|
|
u32 value;
|
|
|
|
std::memcpy(&value, optval, sizeof(value));
|
|
|
|
|
|
|
|
switch (optname) {
|
|
|
|
case OptName::REUSEADDR:
|
|
|
|
ASSERT(value == 0 || value == 1);
|
|
|
|
return Translate(socket->SetReuseAddr(value != 0));
|
|
|
|
case OptName::BROADCAST:
|
|
|
|
ASSERT(value == 0 || value == 1);
|
|
|
|
return Translate(socket->SetBroadcast(value != 0));
|
|
|
|
case OptName::SNDBUF:
|
|
|
|
return Translate(socket->SetSndBuf(value));
|
|
|
|
case OptName::RCVBUF:
|
|
|
|
return Translate(socket->SetRcvBuf(value));
|
|
|
|
case OptName::SNDTIMEO:
|
|
|
|
return Translate(socket->SetSndTimeo(value));
|
|
|
|
case OptName::RCVTIMEO:
|
|
|
|
return Translate(socket->SetRcvTimeo(value));
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented optname={}", optname);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno BSD::ShutdownImpl(s32 fd, s32 how) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return Errno::BADF;
|
|
|
|
}
|
|
|
|
const Network::ShutdownHow host_how = Translate(static_cast<ShutdownHow>(how));
|
2020-10-21 02:07:39 +00:00
|
|
|
return Translate(file_descriptors[fd]->socket->Shutdown(host_how));
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> BSD::RecvImpl(s32 fd, u32 flags, std::vector<u8>& message) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return {-1, Errno::BADF};
|
|
|
|
}
|
2020-10-21 02:07:39 +00:00
|
|
|
return Translate(file_descriptors[fd]->socket->Recv(flags, message));
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> BSD::RecvFromImpl(s32 fd, u32 flags, std::vector<u8>& message,
|
|
|
|
std::vector<u8>& addr) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return {-1, Errno::BADF};
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
FileDescriptor& descriptor = *file_descriptors[fd];
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
|
|
|
|
Network::SockAddrIn addr_in{};
|
|
|
|
Network::SockAddrIn* p_addr_in = nullptr;
|
|
|
|
if (descriptor.is_connection_based) {
|
|
|
|
// Connection based file descriptors (e.g. TCP) zero addr
|
|
|
|
addr.clear();
|
|
|
|
} else {
|
|
|
|
p_addr_in = &addr_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply flags
|
|
|
|
if ((flags & FLAG_MSG_DONTWAIT) != 0) {
|
|
|
|
flags &= ~FLAG_MSG_DONTWAIT;
|
2020-10-21 02:07:39 +00:00
|
|
|
if ((descriptor.flags & FLAG_O_NONBLOCK) == 0) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
descriptor.socket->SetNonBlock(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto [ret, bsd_errno] = Translate(descriptor.socket->RecvFrom(flags, message, p_addr_in));
|
|
|
|
|
|
|
|
// Restore original state
|
2020-10-21 02:07:39 +00:00
|
|
|
if ((descriptor.flags & FLAG_O_NONBLOCK) == 0) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
descriptor.socket->SetNonBlock(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_addr_in) {
|
|
|
|
if (ret < 0) {
|
|
|
|
addr.clear();
|
|
|
|
} else {
|
|
|
|
ASSERT(addr.size() == sizeof(SockAddrIn));
|
|
|
|
const SockAddrIn result = Translate(addr_in);
|
|
|
|
std::memcpy(addr.data(), &result, sizeof(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {ret, bsd_errno};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> BSD::SendImpl(s32 fd, u32 flags, const std::vector<u8>& message) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return {-1, Errno::BADF};
|
|
|
|
}
|
2020-10-21 02:07:39 +00:00
|
|
|
return Translate(file_descriptors[fd]->socket->Send(message, flags));
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> BSD::SendToImpl(s32 fd, u32 flags, const std::vector<u8>& message,
|
|
|
|
const std::vector<u8>& addr) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return {-1, Errno::BADF};
|
|
|
|
}
|
|
|
|
|
|
|
|
Network::SockAddrIn addr_in;
|
|
|
|
Network::SockAddrIn* p_addr_in = nullptr;
|
|
|
|
if (!addr.empty()) {
|
|
|
|
ASSERT(addr.size() == sizeof(SockAddrIn));
|
|
|
|
SockAddrIn guest_addr_in;
|
|
|
|
std::memcpy(&guest_addr_in, addr.data(), sizeof(guest_addr_in));
|
|
|
|
addr_in = Translate(guest_addr_in);
|
2020-09-07 05:04:36 +00:00
|
|
|
p_addr_in = &addr_in;
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
return Translate(file_descriptors[fd]->socket->SendTo(flags, message, p_addr_in));
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Errno BSD::CloseImpl(s32 fd) {
|
|
|
|
if (!IsFileDescriptorValid(fd)) {
|
|
|
|
return Errno::BADF;
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
const Errno bsd_errno = Translate(file_descriptors[fd]->socket->Close());
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
if (bsd_errno != Errno::SUCCESS) {
|
|
|
|
return bsd_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_INFO(Service, "Close socket fd={}", fd);
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
file_descriptors[fd].reset();
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return bsd_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 BSD::FindFreeFileDescriptorHandle() noexcept {
|
|
|
|
for (s32 fd = 0; fd < static_cast<s32>(file_descriptors.size()); ++fd) {
|
2020-10-21 02:07:39 +00:00
|
|
|
if (!file_descriptors[fd]) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BSD::IsFileDescriptorValid(s32 fd) const noexcept {
|
2020-09-07 04:57:39 +00:00
|
|
|
if (fd > static_cast<s32>(MAX_FD) || fd < 0) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_ERROR(Service, "Invalid file descriptor handle={}", fd);
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-21 02:07:39 +00:00
|
|
|
if (!file_descriptors[fd]) {
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
LOG_ERROR(Service, "File descriptor handle={} is not allocated", fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BSD::BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) const noexcept {
|
2018-01-30 06:29:47 +00:00
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
rb.Push<s32>(bsd_errno == Errno::SUCCESS ? 0 : -1);
|
|
|
|
rb.PushEnum(bsd_errno);
|
2018-01-30 06:29:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 00:44:27 +00:00
|
|
|
BSD::BSD(Core::System& system_, const char* name) : ServiceFramework{system_, name} {
|
2019-04-10 18:48:37 +00:00
|
|
|
// clang-format off
|
2018-03-25 09:41:00 +00:00
|
|
|
static const FunctionInfo functions[] = {
|
|
|
|
{0, &BSD::RegisterClient, "RegisterClient"},
|
|
|
|
{1, &BSD::StartMonitoring, "StartMonitoring"},
|
|
|
|
{2, &BSD::Socket, "Socket"},
|
2018-04-17 15:37:43 +00:00
|
|
|
{3, nullptr, "SocketExempt"},
|
|
|
|
{4, nullptr, "Open"},
|
2020-01-25 05:47:15 +00:00
|
|
|
{5, &BSD::Select, "Select"},
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
{6, &BSD::Poll, "Poll"},
|
2018-04-17 15:37:43 +00:00
|
|
|
{7, nullptr, "Sysctl"},
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
{8, &BSD::Recv, "Recv"},
|
|
|
|
{9, &BSD::RecvFrom, "RecvFrom"},
|
|
|
|
{10, &BSD::Send, "Send"},
|
2018-03-25 09:41:00 +00:00
|
|
|
{11, &BSD::SendTo, "SendTo"},
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
{12, &BSD::Accept, "Accept"},
|
2020-01-25 05:47:15 +00:00
|
|
|
{13, &BSD::Bind, "Bind"},
|
2018-03-25 09:41:00 +00:00
|
|
|
{14, &BSD::Connect, "Connect"},
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
{15, &BSD::GetPeerName, "GetPeerName"},
|
|
|
|
{16, &BSD::GetSockName, "GetSockName"},
|
2018-04-17 15:37:43 +00:00
|
|
|
{17, nullptr, "GetSockOpt"},
|
2020-01-25 05:47:15 +00:00
|
|
|
{18, &BSD::Listen, "Listen"},
|
2018-04-17 15:37:43 +00:00
|
|
|
{19, nullptr, "Ioctl"},
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
{20, &BSD::Fcntl, "Fcntl"},
|
2020-01-25 05:47:15 +00:00
|
|
|
{21, &BSD::SetSockOpt, "SetSockOpt"},
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
{22, &BSD::Shutdown, "Shutdown"},
|
2018-04-17 15:37:43 +00:00
|
|
|
{23, nullptr, "ShutdownAllSockets"},
|
services/bsd: Implement most of bsd:s
This implements: Socket, Poll, Accept, Bind, Connect, GetPeerName,
GetSockName, Listen, Fcntl, SetSockOpt, Shutdown, Recv, RecvFrom,
Send, SendTo, Write, and Close
The implementation was done referencing: SwIPC, switchbrew, testing
with libnx and inspecting its code, general information about bsd
sockets online, and analysing official software.
Not everything from these service calls is implemented, but everything
that is not implemented will be logged in some way.
2020-07-12 01:37:47 +00:00
|
|
|
{24, &BSD::Write, "Write"},
|
2018-04-17 15:37:43 +00:00
|
|
|
{25, nullptr, "Read"},
|
2018-03-25 09:41:00 +00:00
|
|
|
{26, &BSD::Close, "Close"},
|
2018-04-17 15:37:43 +00:00
|
|
|
{27, nullptr, "DuplicateSocket"},
|
|
|
|
{28, nullptr, "GetResourceStatistics"},
|
|
|
|
{29, nullptr, "RecvMMsg"},
|
|
|
|
{30, nullptr, "SendMMsg"},
|
2019-04-10 18:48:37 +00:00
|
|
|
{31, nullptr, "EventFd"},
|
|
|
|
{32, nullptr, "RegisterResourceStatisticsName"},
|
2020-04-20 19:18:23 +00:00
|
|
|
{33, nullptr, "Initialize2"},
|
2018-03-25 09:41:00 +00:00
|
|
|
};
|
2019-04-10 18:48:37 +00:00
|
|
|
// clang-format on
|
|
|
|
|
2018-01-18 19:35:03 +00:00
|
|
|
RegisterHandlers(functions);
|
|
|
|
}
|
|
|
|
|
hle/service: Default constructors and destructors in the cpp file where applicable
When a destructor isn't defaulted into a cpp file, it can cause the use
of forward declarations to seemingly fail to compile for non-obvious
reasons. It also allows inlining of the construction/destruction logic
all over the place where a constructor or destructor is invoked, which
can lead to code bloat. This isn't so much a worry here, given the
services won't be created and destroyed frequently.
The cause of the above mentioned non-obvious errors can be demonstrated
as follows:
------- Demonstrative example, if you know how the described error happens, skip forwards -------
Assume we have the following in the header, which we'll call "thing.h":
\#include <memory>
// Forward declaration. For example purposes, assume the definition
// of Object is in some header named "object.h"
class Object;
class Thing {
public:
// assume no constructors or destructors are specified here,
// or the constructors/destructors are defined as:
//
// Thing() = default;
// ~Thing() = default;
//
// ... Some interface member functions would be defined here
private:
std::shared_ptr<Object> obj;
};
If this header is included in a cpp file, (which we'll call "main.cpp"),
this will result in a compilation error, because even though no
destructor is specified, the destructor will still need to be generated by
the compiler because std::shared_ptr's destructor is *not* trivial (in
other words, it does something other than nothing), as std::shared_ptr's
destructor needs to do two things:
1. Decrement the shared reference count of the object being pointed to,
and if the reference count decrements to zero,
2. Free the Object instance's memory (aka deallocate the memory it's
pointing to).
And so the compiler generates the code for the destructor doing this inside main.cpp.
Now, keep in mind, the Object forward declaration is not a complete type. All it
does is tell the compiler "a type named Object exists" and allows us to
use the name in certain situations to avoid a header dependency. So the
compiler needs to generate destruction code for Object, but the compiler
doesn't know *how* to destruct it. A forward declaration doesn't tell
the compiler anything about Object's constructor or destructor. So, the
compiler will issue an error in this case because it's undefined
behavior to try and deallocate (or construct) an incomplete type and
std::shared_ptr and std::unique_ptr make sure this isn't the case
internally.
Now, if we had defaulted the destructor in "thing.cpp", where we also
include "object.h", this would never be an issue, as the destructor
would only have its code generated in one place, and it would be in a
place where the full class definition of Object would be visible to the
compiler.
---------------------- End example ----------------------------
Given these service classes are more than certainly going to change in
the future, this defaults the constructors and destructors into the
relevant cpp files to make the construction and destruction of all of
the services consistent and unlikely to run into cases where forward
declarations are indirectly causing compilation errors. It also has the
plus of avoiding the need to rebuild several services if destruction
logic changes, since it would only be necessary to recompile the single
cpp file.
2018-09-11 01:20:52 +00:00
|
|
|
BSD::~BSD() = default;
|
|
|
|
|
2020-11-26 20:19:08 +00:00
|
|
|
BSDCFG::BSDCFG(Core::System& system_) : ServiceFramework{system_, "bsdcfg"} {
|
2018-07-26 04:55:33 +00:00
|
|
|
// clang-format off
|
|
|
|
static const FunctionInfo functions[] = {
|
|
|
|
{0, nullptr, "SetIfUp"},
|
|
|
|
{1, nullptr, "SetIfUpWithEvent"},
|
|
|
|
{2, nullptr, "CancelIf"},
|
|
|
|
{3, nullptr, "SetIfDown"},
|
|
|
|
{4, nullptr, "GetIfState"},
|
|
|
|
{5, nullptr, "DhcpRenew"},
|
|
|
|
{6, nullptr, "AddStaticArpEntry"},
|
|
|
|
{7, nullptr, "RemoveArpEntry"},
|
|
|
|
{8, nullptr, "LookupArpEntry"},
|
|
|
|
{9, nullptr, "LookupArpEntry2"},
|
|
|
|
{10, nullptr, "ClearArpEntries"},
|
|
|
|
{11, nullptr, "ClearArpEntries2"},
|
|
|
|
{12, nullptr, "PrintArpEntries"},
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
RegisterHandlers(functions);
|
|
|
|
}
|
|
|
|
|
hle/service: Default constructors and destructors in the cpp file where applicable
When a destructor isn't defaulted into a cpp file, it can cause the use
of forward declarations to seemingly fail to compile for non-obvious
reasons. It also allows inlining of the construction/destruction logic
all over the place where a constructor or destructor is invoked, which
can lead to code bloat. This isn't so much a worry here, given the
services won't be created and destroyed frequently.
The cause of the above mentioned non-obvious errors can be demonstrated
as follows:
------- Demonstrative example, if you know how the described error happens, skip forwards -------
Assume we have the following in the header, which we'll call "thing.h":
\#include <memory>
// Forward declaration. For example purposes, assume the definition
// of Object is in some header named "object.h"
class Object;
class Thing {
public:
// assume no constructors or destructors are specified here,
// or the constructors/destructors are defined as:
//
// Thing() = default;
// ~Thing() = default;
//
// ... Some interface member functions would be defined here
private:
std::shared_ptr<Object> obj;
};
If this header is included in a cpp file, (which we'll call "main.cpp"),
this will result in a compilation error, because even though no
destructor is specified, the destructor will still need to be generated by
the compiler because std::shared_ptr's destructor is *not* trivial (in
other words, it does something other than nothing), as std::shared_ptr's
destructor needs to do two things:
1. Decrement the shared reference count of the object being pointed to,
and if the reference count decrements to zero,
2. Free the Object instance's memory (aka deallocate the memory it's
pointing to).
And so the compiler generates the code for the destructor doing this inside main.cpp.
Now, keep in mind, the Object forward declaration is not a complete type. All it
does is tell the compiler "a type named Object exists" and allows us to
use the name in certain situations to avoid a header dependency. So the
compiler needs to generate destruction code for Object, but the compiler
doesn't know *how* to destruct it. A forward declaration doesn't tell
the compiler anything about Object's constructor or destructor. So, the
compiler will issue an error in this case because it's undefined
behavior to try and deallocate (or construct) an incomplete type and
std::shared_ptr and std::unique_ptr make sure this isn't the case
internally.
Now, if we had defaulted the destructor in "thing.cpp", where we also
include "object.h", this would never be an issue, as the destructor
would only have its code generated in one place, and it would be in a
place where the full class definition of Object would be visible to the
compiler.
---------------------- End example ----------------------------
Given these service classes are more than certainly going to change in
the future, this defaults the constructors and destructors into the
relevant cpp files to make the construction and destruction of all of
the services consistent and unlikely to run into cases where forward
declarations are indirectly causing compilation errors. It also has the
plus of avoiding the need to rebuild several services if destruction
logic changes, since it would only be necessary to recompile the single
cpp file.
2018-09-11 01:20:52 +00:00
|
|
|
BSDCFG::~BSDCFG() = default;
|
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
} // namespace Service::Sockets
|