2014-10-30 01:38:33 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-30 01:38:33 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-06-21 17:14:20 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
2016-03-21 05:21:48 +00:00
|
|
|
#include <vector>
|
2015-06-21 17:14:20 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/bit_field.h"
|
|
|
|
#include "common/common_types.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/logging/log.h"
|
2015-06-21 17:14:20 +00:00
|
|
|
#include "common/scope_exit.h"
|
2018-07-16 17:39:48 +00:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
|
|
|
#include "core/hle/kernel/shared_memory.h"
|
2015-06-21 17:14:20 +00:00
|
|
|
#include "core/hle/result.h"
|
2016-09-21 06:52:38 +00:00
|
|
|
#include "core/hle/service/soc_u.h"
|
2015-01-09 04:20:51 +00:00
|
|
|
|
2015-06-21 17:14:20 +00:00
|
|
|
#ifdef _WIN32
|
2016-09-18 00:38:01 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
|
|
|
|
// MinGW does not define several errno constants
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
#define EBADMSG 104
|
|
|
|
#define ENODATA 120
|
|
|
|
#define ENOMSG 122
|
|
|
|
#define ENOSR 124
|
|
|
|
#define ENOSTR 125
|
|
|
|
#define ETIME 137
|
|
|
|
#endif // _MSC_VER
|
2014-12-09 04:52:27 +00:00
|
|
|
#else
|
2016-09-18 00:38:01 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
2014-12-09 04:52:27 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-20 19:11:32 +00:00
|
|
|
#ifdef _WIN32
|
2016-09-18 00:38:01 +00:00
|
|
|
#define WSAEAGAIN WSAEWOULDBLOCK
|
|
|
|
#define WSAEMULTIHOP -1 // Invalid dummy value
|
|
|
|
#define ERRNO(x) WSA##x
|
|
|
|
#define GET_ERRNO WSAGetLastError()
|
|
|
|
#define poll(x, y, z) WSAPoll(x, y, z);
|
2014-12-09 04:52:27 +00:00
|
|
|
#else
|
2016-09-18 00:38:01 +00:00
|
|
|
#define ERRNO(x) x
|
|
|
|
#define GET_ERRNO errno
|
|
|
|
#define closesocket(x) close(x)
|
2014-12-09 04:52:27 +00:00
|
|
|
#endif
|
|
|
|
|
2016-12-10 12:51:50 +00:00
|
|
|
namespace Service {
|
|
|
|
namespace SOC {
|
2014-10-30 01:38:33 +00:00
|
|
|
|
2016-12-10 12:51:50 +00:00
|
|
|
const s32 SOCKET_ERROR_VALUE = -1;
|
2014-10-30 01:38:33 +00:00
|
|
|
|
2014-12-09 04:52:27 +00:00
|
|
|
/// Holds the translation from system network errors to 3DS network errors
|
2016-09-19 01:01:46 +00:00
|
|
|
static const std::unordered_map<int, int> error_map = {{
|
|
|
|
{E2BIG, 1},
|
|
|
|
{ERRNO(EACCES), 2},
|
|
|
|
{ERRNO(EADDRINUSE), 3},
|
|
|
|
{ERRNO(EADDRNOTAVAIL), 4},
|
|
|
|
{ERRNO(EAFNOSUPPORT), 5},
|
|
|
|
{ERRNO(EAGAIN), 6},
|
|
|
|
{ERRNO(EALREADY), 7},
|
|
|
|
{ERRNO(EBADF), 8},
|
|
|
|
{EBADMSG, 9},
|
|
|
|
{EBUSY, 10},
|
|
|
|
{ECANCELED, 11},
|
|
|
|
{ECHILD, 12},
|
|
|
|
{ERRNO(ECONNABORTED), 13},
|
|
|
|
{ERRNO(ECONNREFUSED), 14},
|
|
|
|
{ERRNO(ECONNRESET), 15},
|
|
|
|
{EDEADLK, 16},
|
|
|
|
{ERRNO(EDESTADDRREQ), 17},
|
|
|
|
{EDOM, 18},
|
|
|
|
{ERRNO(EDQUOT), 19},
|
|
|
|
{EEXIST, 20},
|
|
|
|
{ERRNO(EFAULT), 21},
|
|
|
|
{EFBIG, 22},
|
|
|
|
{ERRNO(EHOSTUNREACH), 23},
|
|
|
|
{EIDRM, 24},
|
|
|
|
{EILSEQ, 25},
|
|
|
|
{ERRNO(EINPROGRESS), 26},
|
|
|
|
{ERRNO(EINTR), 27},
|
|
|
|
{ERRNO(EINVAL), 28},
|
|
|
|
{EIO, 29},
|
|
|
|
{ERRNO(EISCONN), 30},
|
|
|
|
{EISDIR, 31},
|
|
|
|
{ERRNO(ELOOP), 32},
|
|
|
|
{ERRNO(EMFILE), 33},
|
|
|
|
{EMLINK, 34},
|
|
|
|
{ERRNO(EMSGSIZE), 35},
|
2018-02-19 23:42:51 +00:00
|
|
|
#ifdef EMULTIHOP
|
2016-09-19 01:01:46 +00:00
|
|
|
{ERRNO(EMULTIHOP), 36},
|
2018-02-19 23:42:51 +00:00
|
|
|
#endif
|
2016-09-19 01:01:46 +00:00
|
|
|
{ERRNO(ENAMETOOLONG), 37},
|
|
|
|
{ERRNO(ENETDOWN), 38},
|
|
|
|
{ERRNO(ENETRESET), 39},
|
|
|
|
{ERRNO(ENETUNREACH), 40},
|
|
|
|
{ENFILE, 41},
|
|
|
|
{ERRNO(ENOBUFS), 42},
|
2016-10-10 16:36:55 +00:00
|
|
|
#ifdef ENODATA
|
2016-09-19 01:01:46 +00:00
|
|
|
{ENODATA, 43},
|
2016-10-10 16:36:55 +00:00
|
|
|
#endif
|
2016-09-19 01:01:46 +00:00
|
|
|
{ENODEV, 44},
|
|
|
|
{ENOENT, 45},
|
|
|
|
{ENOEXEC, 46},
|
|
|
|
{ENOLCK, 47},
|
|
|
|
{ENOLINK, 48},
|
|
|
|
{ENOMEM, 49},
|
|
|
|
{ENOMSG, 50},
|
|
|
|
{ERRNO(ENOPROTOOPT), 51},
|
|
|
|
{ENOSPC, 52},
|
2016-10-10 16:36:55 +00:00
|
|
|
#ifdef ENOSR
|
2016-09-19 01:01:46 +00:00
|
|
|
{ENOSR, 53},
|
2016-10-10 16:36:55 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENOSTR
|
2016-09-19 01:01:46 +00:00
|
|
|
{ENOSTR, 54},
|
2016-10-10 16:36:55 +00:00
|
|
|
#endif
|
2016-09-19 01:01:46 +00:00
|
|
|
{ENOSYS, 55},
|
|
|
|
{ERRNO(ENOTCONN), 56},
|
|
|
|
{ENOTDIR, 57},
|
|
|
|
{ERRNO(ENOTEMPTY), 58},
|
|
|
|
{ERRNO(ENOTSOCK), 59},
|
|
|
|
{ENOTSUP, 60},
|
|
|
|
{ENOTTY, 61},
|
|
|
|
{ENXIO, 62},
|
|
|
|
{ERRNO(EOPNOTSUPP), 63},
|
|
|
|
{EOVERFLOW, 64},
|
|
|
|
{EPERM, 65},
|
|
|
|
{EPIPE, 66},
|
|
|
|
{EPROTO, 67},
|
|
|
|
{ERRNO(EPROTONOSUPPORT), 68},
|
|
|
|
{ERRNO(EPROTOTYPE), 69},
|
|
|
|
{ERANGE, 70},
|
|
|
|
{EROFS, 71},
|
|
|
|
{ESPIPE, 72},
|
|
|
|
{ESRCH, 73},
|
|
|
|
{ERRNO(ESTALE), 74},
|
2016-10-10 16:36:55 +00:00
|
|
|
#ifdef ETIME
|
2016-09-19 01:01:46 +00:00
|
|
|
{ETIME, 75},
|
2016-10-10 16:36:55 +00:00
|
|
|
#endif
|
2016-09-19 01:01:46 +00:00
|
|
|
{ERRNO(ETIMEDOUT), 76},
|
|
|
|
}};
|
2014-12-09 04:52:27 +00:00
|
|
|
|
|
|
|
/// Converts a network error from platform-specific to 3ds-specific
|
|
|
|
static int TranslateError(int error) {
|
|
|
|
auto found = error_map.find(error);
|
|
|
|
if (found != error_map.end())
|
|
|
|
return -found->second;
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2014-12-09 04:52:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2016-04-02 05:19:21 +00:00
|
|
|
/// Holds the translation from system network socket options to 3DS network socket options
|
|
|
|
/// Note: -1 = No effect/unavailable
|
2016-09-18 00:38:01 +00:00
|
|
|
static const std::unordered_map<int, int> sockopt_map = {{
|
|
|
|
{0x0004, SO_REUSEADDR},
|
|
|
|
{0x0080, -1},
|
|
|
|
{0x0100, -1},
|
|
|
|
{0x1001, SO_SNDBUF},
|
|
|
|
{0x1002, SO_RCVBUF},
|
|
|
|
{0x1003, -1},
|
2016-04-02 05:19:21 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/// Unsupported in WinSock2
|
2016-09-18 00:38:01 +00:00
|
|
|
{0x1004, -1},
|
2016-04-02 05:19:21 +00:00
|
|
|
#else
|
2016-09-18 00:38:01 +00:00
|
|
|
{0x1004, SO_RCVLOWAT},
|
2016-04-02 05:19:21 +00:00
|
|
|
#endif
|
2016-09-18 00:38:01 +00:00
|
|
|
{0x1008, SO_TYPE},
|
|
|
|
{0x1009, SO_ERROR},
|
2016-04-02 05:19:21 +00:00
|
|
|
}};
|
|
|
|
|
|
|
|
/// Converts a socket option from 3ds-specific to platform-specific
|
|
|
|
static int TranslateSockOpt(int console_opt_name) {
|
|
|
|
auto found = sockopt_map.find(console_opt_name);
|
|
|
|
if (found != sockopt_map.end()) {
|
|
|
|
return found->second;
|
|
|
|
}
|
|
|
|
return console_opt_name;
|
|
|
|
}
|
|
|
|
|
2014-12-09 04:52:27 +00:00
|
|
|
/// Structure to represent the 3ds' pollfd structure, which is different than most implementations
|
|
|
|
struct CTRPollFD {
|
|
|
|
u32 fd; ///< Socket handle
|
|
|
|
|
|
|
|
union Events {
|
|
|
|
u32 hex; ///< The complete value formed by the flags
|
|
|
|
BitField<0, 1, u32> pollin;
|
|
|
|
BitField<1, 1, u32> pollpri;
|
|
|
|
BitField<2, 1, u32> pollhup;
|
|
|
|
BitField<3, 1, u32> pollerr;
|
|
|
|
BitField<4, 1, u32> pollout;
|
|
|
|
BitField<5, 1, u32> pollnval;
|
|
|
|
|
|
|
|
Events& operator=(const Events& other) {
|
|
|
|
hex = other.hex;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
/// Translates the resulting events of a Poll operation from platform-specific to 3ds
|
|
|
|
/// specific
|
2014-12-09 04:52:27 +00:00
|
|
|
static Events TranslateTo3DS(u32 input_event) {
|
|
|
|
Events ev = {};
|
|
|
|
if (input_event & POLLIN)
|
2016-02-11 17:41:15 +00:00
|
|
|
ev.pollin.Assign(1);
|
2014-12-09 04:52:27 +00:00
|
|
|
if (input_event & POLLPRI)
|
2016-02-11 17:41:15 +00:00
|
|
|
ev.pollpri.Assign(1);
|
2014-12-09 04:52:27 +00:00
|
|
|
if (input_event & POLLHUP)
|
2016-02-11 17:41:15 +00:00
|
|
|
ev.pollhup.Assign(1);
|
2014-12-09 04:52:27 +00:00
|
|
|
if (input_event & POLLERR)
|
2016-02-11 17:41:15 +00:00
|
|
|
ev.pollerr.Assign(1);
|
2014-12-09 04:52:27 +00:00
|
|
|
if (input_event & POLLOUT)
|
2016-02-11 17:41:15 +00:00
|
|
|
ev.pollout.Assign(1);
|
2014-12-09 04:52:27 +00:00
|
|
|
if (input_event & POLLNVAL)
|
2016-02-11 17:41:15 +00:00
|
|
|
ev.pollnval.Assign(1);
|
2014-12-09 04:52:27 +00:00
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
/// Translates the resulting events of a Poll operation from 3ds specific to platform
|
|
|
|
/// specific
|
2014-12-09 04:52:27 +00:00
|
|
|
static u32 TranslateToPlatform(Events input_event) {
|
|
|
|
u32 ret = 0;
|
|
|
|
if (input_event.pollin)
|
|
|
|
ret |= POLLIN;
|
|
|
|
if (input_event.pollpri)
|
|
|
|
ret |= POLLPRI;
|
|
|
|
if (input_event.pollhup)
|
|
|
|
ret |= POLLHUP;
|
|
|
|
if (input_event.pollerr)
|
|
|
|
ret |= POLLERR;
|
|
|
|
if (input_event.pollout)
|
|
|
|
ret |= POLLOUT;
|
|
|
|
if (input_event.pollnval)
|
|
|
|
ret |= POLLNVAL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
2016-09-18 00:38:01 +00:00
|
|
|
Events events; ///< Events to poll for (input)
|
2014-12-09 04:52:27 +00:00
|
|
|
Events revents; ///< Events received (output)
|
|
|
|
|
|
|
|
/// Converts a platform-specific pollfd to a 3ds specific structure
|
|
|
|
static CTRPollFD FromPlatform(pollfd const& fd) {
|
|
|
|
CTRPollFD result;
|
|
|
|
result.events.hex = Events::TranslateTo3DS(fd.events).hex;
|
|
|
|
result.revents.hex = Events::TranslateTo3DS(fd.revents).hex;
|
|
|
|
result.fd = static_cast<u32>(fd.fd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a 3ds specific pollfd to a platform-specific structure
|
|
|
|
static pollfd ToPlatform(CTRPollFD const& fd) {
|
|
|
|
pollfd result;
|
|
|
|
result.events = Events::TranslateToPlatform(fd.events);
|
|
|
|
result.revents = Events::TranslateToPlatform(fd.revents);
|
|
|
|
result.fd = fd.fd;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Union to represent the 3ds' sockaddr structure
|
|
|
|
union CTRSockAddr {
|
|
|
|
/// Structure to represent a raw sockaddr
|
|
|
|
struct {
|
2016-09-18 00:38:01 +00:00
|
|
|
u8 len; ///< The length of the entire structure, only the set fields count
|
|
|
|
u8 sa_family; ///< The address family of the sockaddr
|
2014-12-09 04:52:27 +00:00
|
|
|
u8 sa_data[0x1A]; ///< The extra data, this varies, depending on the address family
|
|
|
|
} raw;
|
|
|
|
|
|
|
|
/// Structure to represent the 3ds' sockaddr_in structure
|
|
|
|
struct CTRSockAddrIn {
|
2016-09-18 00:38:01 +00:00
|
|
|
u8 len; ///< The length of the entire structure
|
2014-12-09 04:52:27 +00:00
|
|
|
u8 sin_family; ///< The address family of the sockaddr_in
|
2016-09-18 00:38:01 +00:00
|
|
|
u16 sin_port; ///< The port associated with this sockaddr_in
|
|
|
|
u32 sin_addr; ///< The actual address of the sockaddr_in
|
2014-12-09 04:52:27 +00:00
|
|
|
} in;
|
|
|
|
|
|
|
|
/// Convert a 3DS CTRSockAddr to a platform-specific sockaddr
|
|
|
|
static sockaddr ToPlatform(CTRSockAddr const& ctr_addr) {
|
|
|
|
sockaddr result;
|
|
|
|
result.sa_family = ctr_addr.raw.sa_family;
|
|
|
|
memset(result.sa_data, 0, sizeof(result.sa_data));
|
|
|
|
|
|
|
|
// We can not guarantee ABI compatibility between platforms so we copy the fields manually
|
|
|
|
switch (result.sa_family) {
|
2016-09-18 00:38:01 +00:00
|
|
|
case AF_INET: {
|
2014-12-09 04:52:27 +00:00
|
|
|
sockaddr_in* result_in = reinterpret_cast<sockaddr_in*>(&result);
|
|
|
|
result_in->sin_port = ctr_addr.in.sin_port;
|
|
|
|
result_in->sin_addr.s_addr = ctr_addr.in.sin_addr;
|
|
|
|
memset(result_in->sin_zero, 0, sizeof(result_in->sin_zero));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2015-01-21 01:16:47 +00:00
|
|
|
ASSERT_MSG(false, "Unhandled address family (sa_family) in CTRSockAddr::ToPlatform");
|
2014-12-09 04:52:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a platform-specific sockaddr to a 3DS CTRSockAddr
|
|
|
|
static CTRSockAddr FromPlatform(sockaddr const& addr) {
|
|
|
|
CTRSockAddr result;
|
|
|
|
result.raw.sa_family = static_cast<u8>(addr.sa_family);
|
|
|
|
// We can not guarantee ABI compatibility between platforms so we copy the fields manually
|
|
|
|
switch (result.raw.sa_family) {
|
2016-09-18 00:38:01 +00:00
|
|
|
case AF_INET: {
|
2014-12-09 04:52:27 +00:00
|
|
|
sockaddr_in const* addr_in = reinterpret_cast<sockaddr_in const*>(&addr);
|
|
|
|
result.raw.len = sizeof(CTRSockAddrIn);
|
|
|
|
result.in.sin_port = addr_in->sin_port;
|
|
|
|
result.in.sin_addr = addr_in->sin_addr.s_addr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2015-01-21 01:16:47 +00:00
|
|
|
ASSERT_MSG(false, "Unhandled address family (sa_family) in CTRSockAddr::ToPlatform");
|
2014-12-09 04:52:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::CleanupSockets() {
|
2014-12-09 04:52:27 +00:00
|
|
|
for (auto sock : open_sockets)
|
|
|
|
closesocket(sock.second.socket_fd);
|
|
|
|
open_sockets.clear();
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Socket(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x02, 3, 2);
|
|
|
|
u32 domain = rp.Pop<u32>(); // Address family
|
|
|
|
u32 type = rp.Pop<u32>();
|
|
|
|
u32 protocol = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
|
|
|
|
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
|
|
|
// Only 0 is allowed according to 3dbrew, using 0 will let the OS decide which protocol to use
|
|
|
|
if (protocol != 0) {
|
2018-07-16 17:39:48 +00:00
|
|
|
rb.Push(UnimplementedFunction(ErrorModule::SOC)); // TODO(Subv): Correct error code
|
|
|
|
rb.Skip(1, false);
|
2014-12-09 04:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domain != AF_INET) {
|
2018-07-16 17:39:48 +00:00
|
|
|
rb.Push(UnimplementedFunction(ErrorModule::SOC)); // TODO(Subv): Correct error code
|
|
|
|
rb.Skip(1, false);
|
2014-12-09 04:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != SOCK_DGRAM && type != SOCK_STREAM) {
|
2018-07-16 17:39:48 +00:00
|
|
|
rb.Push(UnimplementedFunction(ErrorModule::SOC)); // TODO(Subv): Correct error code
|
|
|
|
rb.Skip(1, false);
|
2014-12-09 04:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 04:43:26 +00:00
|
|
|
u32 ret = static_cast<u32>(::socket(domain, type, protocol));
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2017-03-11 04:43:26 +00:00
|
|
|
if ((s32)ret != SOCKET_ERROR_VALUE)
|
|
|
|
open_sockets[ret] = {ret, true};
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2017-03-11 04:43:26 +00:00
|
|
|
if ((s32)ret == SOCKET_ERROR_VALUE)
|
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Bind(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x05, 2, 4);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 len = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
|
|
|
auto sock_addr_buf = rp.PopStaticBuffer();
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2016-04-19 06:08:16 +00:00
|
|
|
CTRSockAddr ctr_sock_addr;
|
2018-07-16 17:39:48 +00:00
|
|
|
std::memcpy(&ctr_sock_addr, sock_addr_buf.data(), sizeof(CTRSockAddr));
|
2016-04-19 06:08:16 +00:00
|
|
|
|
|
|
|
sockaddr sock_addr = CTRSockAddr::ToPlatform(ctr_sock_addr);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = ::bind(socket_handle, &sock_addr, std::max<u32>(sizeof(sock_addr), len));
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2017-03-11 04:43:26 +00:00
|
|
|
if (ret != 0)
|
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Fcntl(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x13, 3, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 ctr_cmd = rp.Pop<u32>();
|
|
|
|
u32 ctr_arg = rp.Pop<u32>();
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2014-12-09 04:52:27 +00:00
|
|
|
u32 posix_ret = 0; // TODO: Check what hardware returns for F_SETFL (unspecified by POSIX)
|
|
|
|
SCOPE_EXIT({
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(posix_ret);
|
2014-12-09 04:52:27 +00:00
|
|
|
});
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2014-12-09 04:52:27 +00:00
|
|
|
if (ctr_cmd == 3) { // F_GETFL
|
2015-06-20 19:11:32 +00:00
|
|
|
#ifdef _WIN32
|
2014-12-09 04:52:27 +00:00
|
|
|
posix_ret = 0;
|
|
|
|
auto iter = open_sockets.find(socket_handle);
|
|
|
|
if (iter != open_sockets.end() && iter->second.blocking == false)
|
|
|
|
posix_ret |= 4; // O_NONBLOCK
|
|
|
|
#else
|
|
|
|
int ret = ::fcntl(socket_handle, F_GETFL, 0);
|
|
|
|
if (ret == SOCKET_ERROR_VALUE) {
|
2017-03-11 04:43:26 +00:00
|
|
|
posix_ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
posix_ret = 0;
|
|
|
|
if (ret & O_NONBLOCK)
|
|
|
|
posix_ret |= 4; // O_NONBLOCK
|
|
|
|
#endif
|
|
|
|
} else if (ctr_cmd == 4) { // F_SETFL
|
2015-06-20 19:11:32 +00:00
|
|
|
#ifdef _WIN32
|
2014-12-09 04:52:27 +00:00
|
|
|
unsigned long tmp = (ctr_arg & 4 /* O_NONBLOCK */) ? 1 : 0;
|
|
|
|
int ret = ioctlsocket(socket_handle, FIONBIO, &tmp);
|
|
|
|
if (ret == SOCKET_ERROR_VALUE) {
|
2017-03-11 04:43:26 +00:00
|
|
|
posix_ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto iter = open_sockets.find(socket_handle);
|
|
|
|
if (iter != open_sockets.end())
|
|
|
|
iter->second.blocking = (tmp == 0);
|
|
|
|
#else
|
|
|
|
int flags = ::fcntl(socket_handle, F_GETFL, 0);
|
|
|
|
if (flags == SOCKET_ERROR_VALUE) {
|
2017-03-11 04:43:26 +00:00
|
|
|
posix_ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2014-12-09 04:52:27 +00:00
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
if (ctr_arg & 4) // O_NONBLOCK
|
|
|
|
flags |= O_NONBLOCK;
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2014-12-09 04:52:27 +00:00
|
|
|
int ret = ::fcntl(socket_handle, F_SETFL, flags);
|
|
|
|
if (ret == SOCKET_ERROR_VALUE) {
|
2017-03-11 04:43:26 +00:00
|
|
|
posix_ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Service_SOC, "Unsupported command ({}) in fcntl call", ctr_cmd);
|
2017-03-11 04:43:26 +00:00
|
|
|
posix_ret = TranslateError(EINVAL); // TODO: Find the correct error
|
2014-12-09 04:52:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Listen(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x03, 2, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 backlog = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = ::listen(socket_handle, backlog);
|
2014-12-09 04:52:27 +00:00
|
|
|
if (ret != 0)
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Accept(Kernel::HLERequestContext& ctx) {
|
2015-05-25 18:34:09 +00:00
|
|
|
// TODO(Subv): Calling this function on a blocking socket will block the emu thread,
|
|
|
|
// preventing graceful shutdown when closing the emulator, this can be fixed by always
|
2014-12-09 04:52:27 +00:00
|
|
|
// performing nonblocking operations and spinlock until the data is available
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x04, 2, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
socklen_t max_addr_len = static_cast<socklen_t>(rp.Pop<u32>());
|
|
|
|
rp.PopPID();
|
2014-12-09 04:52:27 +00:00
|
|
|
sockaddr addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
|
|
|
u32 ret = static_cast<u32>(::accept(socket_handle, &addr, &addr_len));
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2014-12-12 17:21:58 +00:00
|
|
|
if ((s32)ret != SOCKET_ERROR_VALUE)
|
2016-09-18 00:38:01 +00:00
|
|
|
open_sockets[ret] = {ret, true};
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
CTRSockAddr ctr_addr;
|
|
|
|
std::vector<u8> ctr_addr_buf(sizeof(ctr_addr));
|
2014-12-12 17:21:58 +00:00
|
|
|
if ((s32)ret == SOCKET_ERROR_VALUE) {
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
} else {
|
2018-07-16 17:39:48 +00:00
|
|
|
ctr_addr = CTRSockAddr::FromPlatform(addr);
|
|
|
|
std::memcpy(ctr_addr_buf.data(), &ctr_addr, sizeof(ctr_addr));
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
|
|
|
rb.PushStaticBuffer(ctr_addr_buf, 0);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::GetHostId(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x16, 0, 0);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
|
|
|
char name[128];
|
|
|
|
gethostname(name, sizeof(name));
|
2015-06-30 23:59:27 +00:00
|
|
|
addrinfo hints = {};
|
|
|
|
addrinfo* res;
|
|
|
|
|
|
|
|
hints.ai_family = AF_INET;
|
2015-09-11 18:50:10 +00:00
|
|
|
getaddrinfo(name, nullptr, &hints, &res);
|
2015-06-30 23:59:27 +00:00
|
|
|
sockaddr_in* sock_addr = reinterpret_cast<sockaddr_in*>(res->ai_addr);
|
|
|
|
in_addr* addr = &sock_addr->sin_addr;
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(static_cast<u32>(addr->s_addr));
|
2015-06-30 23:59:27 +00:00
|
|
|
freeaddrinfo(res);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Close(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x0B, 1, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = 0;
|
2014-12-09 04:52:27 +00:00
|
|
|
open_sockets.erase(socket_handle);
|
|
|
|
|
|
|
|
ret = closesocket(socket_handle);
|
|
|
|
|
|
|
|
if (ret != 0)
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::SendTo(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x0A, 4, 6);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 len = rp.Pop<u32>();
|
|
|
|
u32 flags = rp.Pop<u32>();
|
|
|
|
u32 addr_len = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
|
|
|
auto input_buff = rp.PopStaticBuffer();
|
|
|
|
auto dest_addr_buff = rp.PopStaticBuffer();
|
2016-04-19 06:08:16 +00:00
|
|
|
|
|
|
|
CTRSockAddr ctr_dest_addr;
|
2018-07-16 17:39:48 +00:00
|
|
|
std::memcpy(&ctr_dest_addr, dest_addr_buff.data(), sizeof(ctr_dest_addr));
|
2016-04-19 06:08:16 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = -1;
|
2014-12-09 04:52:27 +00:00
|
|
|
if (addr_len > 0) {
|
2016-04-19 06:08:16 +00:00
|
|
|
sockaddr dest_addr = CTRSockAddr::ToPlatform(ctr_dest_addr);
|
2016-09-18 00:38:01 +00:00
|
|
|
ret = ::sendto(socket_handle, reinterpret_cast<const char*>(input_buff.data()), len, flags,
|
|
|
|
&dest_addr, sizeof(dest_addr));
|
2014-12-09 04:52:27 +00:00
|
|
|
} else {
|
2016-09-18 00:38:01 +00:00
|
|
|
ret = ::sendto(socket_handle, reinterpret_cast<const char*>(input_buff.data()), len, flags,
|
|
|
|
nullptr, 0);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == SOCKET_ERROR_VALUE)
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 06:40:11 +00:00
|
|
|
void SOC_U::RecvFromOther(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x7, 4, 4);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 len = rp.Pop<u32>();
|
|
|
|
u32 flags = rp.Pop<u32>();
|
|
|
|
u32 addr_len = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
|
|
|
auto& buffer = rp.PopMappedBuffer();
|
|
|
|
|
|
|
|
CTRSockAddr ctr_src_addr;
|
|
|
|
std::vector<u8> output_buff(len);
|
|
|
|
std::vector<u8> addr_buff(sizeof(ctr_src_addr));
|
|
|
|
sockaddr src_addr;
|
|
|
|
socklen_t src_addr_len = sizeof(src_addr);
|
2018-08-21 14:39:45 +00:00
|
|
|
|
|
|
|
s32 ret = -1;
|
|
|
|
if (addr_len > 0) {
|
|
|
|
ret = ::recvfrom(socket_handle, reinterpret_cast<char*>(output_buff.data()), len, flags,
|
2018-08-21 06:40:11 +00:00
|
|
|
&src_addr, &src_addr_len);
|
2018-08-21 14:39:45 +00:00
|
|
|
if (ret >= 0 && src_addr_len > 0) {
|
|
|
|
ctr_src_addr = CTRSockAddr::FromPlatform(src_addr);
|
|
|
|
std::memcpy(addr_buff.data(), &ctr_src_addr, sizeof(ctr_src_addr));
|
|
|
|
}
|
2018-08-21 06:40:11 +00:00
|
|
|
} else {
|
2018-08-21 14:39:45 +00:00
|
|
|
ret = ::recvfrom(socket_handle, reinterpret_cast<char*>(output_buff.data()), len, flags,
|
|
|
|
NULL, 0);
|
2018-08-21 06:40:11 +00:00
|
|
|
addr_buff.resize(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == SOCKET_ERROR_VALUE) {
|
|
|
|
ret = TranslateError(GET_ERRNO);
|
|
|
|
} else {
|
|
|
|
buffer.Write(output_buff.data(), 0, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 4);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
|
|
|
rb.PushStaticBuffer(addr_buff, 1);
|
|
|
|
rb.PushMappedBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::RecvFrom(Kernel::HLERequestContext& ctx) {
|
2015-05-25 18:34:09 +00:00
|
|
|
// TODO(Subv): Calling this function on a blocking socket will block the emu thread,
|
|
|
|
// preventing graceful shutdown when closing the emulator, this can be fixed by always
|
2014-12-09 04:52:27 +00:00
|
|
|
// performing nonblocking operations and spinlock until the data is available
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x08, 4, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 len = rp.Pop<u32>();
|
|
|
|
u32 flags = rp.Pop<u32>();
|
|
|
|
u32 addr_len = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
|
|
|
|
|
|
|
CTRSockAddr ctr_src_addr;
|
2016-04-19 06:08:16 +00:00
|
|
|
std::vector<u8> output_buff(len);
|
2018-07-16 17:39:48 +00:00
|
|
|
std::vector<u8> addr_buff(sizeof(ctr_src_addr));
|
2014-12-09 04:52:27 +00:00
|
|
|
sockaddr src_addr;
|
|
|
|
socklen_t src_addr_len = sizeof(src_addr);
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = ::recvfrom(socket_handle, reinterpret_cast<char*>(output_buff.data()), len, flags,
|
2016-09-18 00:38:01 +00:00
|
|
|
&src_addr, &src_addr_len);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
if (ret >= 0 && src_addr_len > 0) {
|
|
|
|
ctr_src_addr = CTRSockAddr::FromPlatform(src_addr);
|
|
|
|
std::memcpy(addr_buff.data(), &ctr_src_addr, sizeof(ctr_src_addr));
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 total_received = ret;
|
2014-12-09 04:52:27 +00:00
|
|
|
if (ret == SOCKET_ERROR_VALUE) {
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
total_received = 0;
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
// Write only the data we received to avoid overwriting parts of the buffer with zeros
|
|
|
|
output_buff.resize(total_received);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(3, 4);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
|
|
|
rb.Push(total_received);
|
|
|
|
rb.PushStaticBuffer(output_buff, 0);
|
|
|
|
rb.PushStaticBuffer(addr_buff, 1);
|
|
|
|
}
|
2016-04-19 06:08:16 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Poll(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x14, 2, 4);
|
|
|
|
u32 nfds = rp.Pop<u32>();
|
|
|
|
s32 timeout = rp.Pop<s32>();
|
|
|
|
rp.PopPID();
|
|
|
|
auto input_fds = rp.PopStaticBuffer();
|
2016-04-19 06:08:16 +00:00
|
|
|
|
|
|
|
std::vector<CTRPollFD> ctr_fds(nfds);
|
2018-07-16 17:39:48 +00:00
|
|
|
std::memcpy(ctr_fds.data(), input_fds.data(), nfds * sizeof(CTRPollFD));
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
// The 3ds_pollfd and the pollfd structures may be different (Windows/Linux have different
|
|
|
|
// sizes)
|
2014-12-09 04:52:27 +00:00
|
|
|
// so we have to copy the data
|
2016-03-21 05:21:48 +00:00
|
|
|
std::vector<pollfd> platform_pollfd(nfds);
|
2016-04-19 06:08:16 +00:00
|
|
|
std::transform(ctr_fds.begin(), ctr_fds.end(), platform_pollfd.begin(), CTRPollFD::ToPlatform);
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = ::poll(platform_pollfd.data(), nfds, timeout);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
|
|
|
// Now update the output pollfd structure
|
2016-09-18 00:38:01 +00:00
|
|
|
std::transform(platform_pollfd.begin(), platform_pollfd.end(), ctr_fds.begin(),
|
|
|
|
CTRPollFD::FromPlatform);
|
2016-04-19 06:08:16 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
std::vector<u8> output_fds(nfds * sizeof(CTRPollFD));
|
|
|
|
std::memcpy(output_fds.data(), ctr_fds.data(), nfds * sizeof(CTRPollFD));
|
2014-12-09 04:52:27 +00:00
|
|
|
|
|
|
|
if (ret == SOCKET_ERROR_VALUE)
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
|
|
|
rb.PushStaticBuffer(output_fds, 0);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::GetSockName(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x17, 2, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 max_addr_len = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
2014-12-09 04:52:27 +00:00
|
|
|
|
|
|
|
sockaddr dest_addr;
|
|
|
|
socklen_t dest_addr_len = sizeof(dest_addr);
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = ::getsockname(socket_handle, &dest_addr, &dest_addr_len);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
CTRSockAddr ctr_dest_addr = CTRSockAddr::FromPlatform(dest_addr);
|
|
|
|
std::vector<u8> dest_addr_buff(sizeof(ctr_dest_addr));
|
|
|
|
std::memcpy(dest_addr_buff.data(), &ctr_dest_addr, sizeof(ctr_dest_addr));
|
2014-12-09 04:52:27 +00:00
|
|
|
|
|
|
|
if (ret != 0)
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
|
|
|
rb.PushStaticBuffer(dest_addr_buff, 0);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Shutdown(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x0C, 2, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
s32 how = rp.Pop<s32>();
|
|
|
|
rp.PopPID();
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = ::shutdown(socket_handle, how);
|
2014-12-09 04:52:27 +00:00
|
|
|
if (ret != 0)
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::GetPeerName(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x18, 2, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 max_addr_len = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2014-12-09 04:52:27 +00:00
|
|
|
sockaddr dest_addr;
|
|
|
|
socklen_t dest_addr_len = sizeof(dest_addr);
|
|
|
|
int ret = ::getpeername(socket_handle, &dest_addr, &dest_addr_len);
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
CTRSockAddr ctr_dest_addr = CTRSockAddr::FromPlatform(dest_addr);
|
|
|
|
std::vector<u8> dest_addr_buff(sizeof(ctr_dest_addr));
|
|
|
|
std::memcpy(dest_addr_buff.data(), &ctr_dest_addr, sizeof(ctr_dest_addr));
|
2014-12-09 04:52:27 +00:00
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
if (ret != 0)
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
|
|
|
rb.PushStaticBuffer(dest_addr_buff, 0);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::Connect(Kernel::HLERequestContext& ctx) {
|
2015-05-25 18:34:09 +00:00
|
|
|
// TODO(Subv): Calling this function on a blocking socket will block the emu thread,
|
|
|
|
// preventing graceful shutdown when closing the emulator, this can be fixed by always
|
2014-12-09 04:52:27 +00:00
|
|
|
// performing nonblocking operations and spinlock until the data is available
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x06, 2, 4);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 input_addr_len = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
|
|
|
auto input_addr_buf = rp.PopStaticBuffer();
|
2014-12-09 04:52:27 +00:00
|
|
|
|
2016-04-19 06:08:16 +00:00
|
|
|
CTRSockAddr ctr_input_addr;
|
2018-07-16 17:39:48 +00:00
|
|
|
std::memcpy(&ctr_input_addr, input_addr_buf.data(), sizeof(ctr_input_addr));
|
2016-04-19 06:08:16 +00:00
|
|
|
|
|
|
|
sockaddr input_addr = CTRSockAddr::ToPlatform(ctr_input_addr);
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 ret = ::connect(socket_handle, &input_addr, sizeof(input_addr));
|
2014-12-09 04:52:27 +00:00
|
|
|
if (ret != 0)
|
2017-03-11 04:43:26 +00:00
|
|
|
ret = TranslateError(GET_ERRNO);
|
2015-06-25 17:55:27 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(ret);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::InitializeSockets(Kernel::HLERequestContext& ctx) {
|
2017-11-04 15:32:12 +00:00
|
|
|
// TODO(Subv): Implement
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x01, 1, 4);
|
|
|
|
u32 memory_block_size = rp.Pop<u32>();
|
|
|
|
rp.PopPID();
|
|
|
|
rp.PopObject<Kernel::SharedMemory>();
|
2017-11-04 15:41:20 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::ShutdownSockets(Kernel::HLERequestContext& ctx) {
|
2014-12-09 04:52:27 +00:00
|
|
|
// TODO(Subv): Implement
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x19, 0, 0);
|
2014-12-09 04:52:27 +00:00
|
|
|
CleanupSockets();
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::GetSockOpt(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x11, 4, 2);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 level = rp.Pop<u32>();
|
|
|
|
s32 optname = rp.Pop<s32>();
|
|
|
|
socklen_t optlen = static_cast<socklen_t>(rp.Pop<u32>());
|
|
|
|
rp.PopPID();
|
2016-03-29 06:34:34 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 err = 0;
|
|
|
|
|
|
|
|
std::vector<u8> optval(optlen);
|
2016-03-30 07:25:19 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
if (optname < 0) {
|
2016-03-30 07:25:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
err = WSAEINVAL;
|
|
|
|
#else
|
|
|
|
err = EINVAL;
|
|
|
|
#endif
|
|
|
|
} else {
|
2018-07-16 17:39:48 +00:00
|
|
|
char* optval_data = reinterpret_cast<char*>(optval.data());
|
|
|
|
err = ::getsockopt(socket_handle, level, optname, optval_data, &optlen);
|
2017-03-11 04:43:26 +00:00
|
|
|
if (err == SOCKET_ERROR_VALUE) {
|
2016-03-30 07:25:19 +00:00
|
|
|
err = TranslateError(GET_ERRNO);
|
|
|
|
}
|
2016-03-29 06:34:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(3, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(err);
|
|
|
|
rb.Push(static_cast<u32>(optlen));
|
|
|
|
rb.PushStaticBuffer(optval, 0);
|
2016-03-29 06:34:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void SOC_U::SetSockOpt(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x12, 4, 4);
|
|
|
|
u32 socket_handle = rp.Pop<u32>();
|
|
|
|
u32 level = rp.Pop<u32>();
|
|
|
|
s32 optname = rp.Pop<s32>();
|
|
|
|
socklen_t optlen = static_cast<socklen_t>(rp.Pop<u32>());
|
|
|
|
rp.PopPID();
|
|
|
|
auto optval = rp.PopStaticBuffer();
|
2016-03-29 06:34:34 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
s32 err = 0;
|
2016-03-30 07:25:19 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
if (optname < 0) {
|
2016-03-30 07:25:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
err = WSAEINVAL;
|
|
|
|
#else
|
|
|
|
err = EINVAL;
|
|
|
|
#endif
|
|
|
|
} else {
|
2018-07-16 17:39:48 +00:00
|
|
|
const char* optval_data = reinterpret_cast<const char*>(optval.data());
|
2018-07-23 21:08:14 +00:00
|
|
|
err = static_cast<u32>(::setsockopt(socket_handle, level, optname, optval_data,
|
|
|
|
static_cast<socklen_t>(optval.size())));
|
2017-03-11 04:43:26 +00:00
|
|
|
if (err == SOCKET_ERROR_VALUE) {
|
2016-03-30 07:25:19 +00:00
|
|
|
err = TranslateError(GET_ERRNO);
|
|
|
|
}
|
2016-03-29 06:34:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(err);
|
2016-03-29 06:34:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
SOC_U::SOC_U() : ServiceFramework("soc:U") {
|
|
|
|
static const FunctionInfo functions[] = {
|
|
|
|
{0x00010044, &SOC_U::InitializeSockets, "InitializeSockets"},
|
|
|
|
{0x000200C2, &SOC_U::Socket, "Socket"},
|
|
|
|
{0x00030082, &SOC_U::Listen, "Listen"},
|
|
|
|
{0x00040082, &SOC_U::Accept, "Accept"},
|
|
|
|
{0x00050084, &SOC_U::Bind, "Bind"},
|
|
|
|
{0x00060084, &SOC_U::Connect, "Connect"},
|
2018-08-21 06:40:11 +00:00
|
|
|
{0x00070104, &SOC_U::RecvFromOther, "recvfrom_other"},
|
2018-07-16 17:39:48 +00:00
|
|
|
{0x00080102, &SOC_U::RecvFrom, "RecvFrom"},
|
|
|
|
{0x00090106, nullptr, "sendto_other"},
|
|
|
|
{0x000A0106, &SOC_U::SendTo, "SendTo"},
|
|
|
|
{0x000B0042, &SOC_U::Close, "Close"},
|
|
|
|
{0x000C0082, &SOC_U::Shutdown, "Shutdown"},
|
|
|
|
{0x000D0082, nullptr, "GetHostByName"},
|
|
|
|
{0x000E00C2, nullptr, "GetHostByAddr"},
|
|
|
|
{0x000F0106, nullptr, "GetAddrInfo"},
|
|
|
|
{0x00100102, nullptr, "GetNameInfo"},
|
|
|
|
{0x00110102, &SOC_U::GetSockOpt, "GetSockOpt"},
|
|
|
|
{0x00120104, &SOC_U::SetSockOpt, "SetSockOpt"},
|
|
|
|
{0x001300C2, &SOC_U::Fcntl, "Fcntl"},
|
|
|
|
{0x00140084, &SOC_U::Poll, "Poll"},
|
|
|
|
{0x00150042, nullptr, "SockAtMark"},
|
|
|
|
{0x00160000, &SOC_U::GetHostId, "GetHostId"},
|
|
|
|
{0x00170082, &SOC_U::GetSockName, "GetSockName"},
|
|
|
|
{0x00180082, &SOC_U::GetPeerName, "GetPeerName"},
|
|
|
|
{0x00190000, &SOC_U::ShutdownSockets, "ShutdownSockets"},
|
|
|
|
{0x001A00C0, nullptr, "GetNetworkOpt"},
|
|
|
|
{0x001B0040, nullptr, "ICMPSocket"},
|
|
|
|
{0x001C0104, nullptr, "ICMPPing"},
|
|
|
|
{0x001D0040, nullptr, "ICMPCancel"},
|
|
|
|
{0x001E0040, nullptr, "ICMPClose"},
|
|
|
|
{0x001F0040, nullptr, "GetResolverInfo"},
|
|
|
|
{0x00210002, nullptr, "CloseSockets"},
|
|
|
|
{0x00230040, nullptr, "AddGlobalSocket"},
|
|
|
|
};
|
2014-10-30 01:38:33 +00:00
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
RegisterHandlers(functions);
|
2017-11-04 14:30:48 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA data;
|
|
|
|
WSAStartup(MAKEWORD(2, 2), &data);
|
|
|
|
#endif
|
2014-10-30 01:38:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 12:51:50 +00:00
|
|
|
SOC_U::~SOC_U() {
|
2014-12-09 04:52:27 +00:00
|
|
|
CleanupSockets();
|
2017-11-04 14:30:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
2014-12-09 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:39:48 +00:00
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager) {
|
|
|
|
std::make_shared<SOC_U>()->InstallAsService(service_manager);
|
|
|
|
}
|
|
|
|
|
2016-12-10 12:51:50 +00:00
|
|
|
} // namespace SOC
|
|
|
|
} // namespace Service
|