2020-07-12 00:42:56 +00:00
|
|
|
// Copyright 2020 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
#include <limits>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2021-09-08 18:36:20 +00:00
|
|
|
|
|
|
|
#include "common/error.h"
|
2020-07-12 00:42:56 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winsock2.h>
|
2021-08-12 19:32:53 +00:00
|
|
|
#include <ws2tcpip.h>
|
2020-08-31 14:09:06 +00:00
|
|
|
#elif YUZU_UNIX
|
2021-08-12 20:15:48 +00:00
|
|
|
#include <arpa/inet.h>
|
2020-07-12 00:42:56 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
2020-07-22 22:04:00 +00:00
|
|
|
#include <netinet/in.h>
|
2020-07-12 00:42:56 +00:00
|
|
|
#include <poll.h>
|
2020-07-22 22:04:00 +00:00
|
|
|
#include <sys/socket.h>
|
2020-07-12 00:42:56 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#error "Unimplemented platform"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/logging/log.h"
|
2021-08-12 19:32:53 +00:00
|
|
|
#include "common/settings.h"
|
2020-07-12 00:42:56 +00:00
|
|
|
#include "core/network/network.h"
|
2021-08-12 19:32:53 +00:00
|
|
|
#include "core/network/network_interface.h"
|
2020-07-12 00:42:56 +00:00
|
|
|
#include "core/network/sockets.h"
|
|
|
|
|
|
|
|
namespace Network {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
using socklen_t = int;
|
|
|
|
|
|
|
|
void Initialize() {
|
|
|
|
WSADATA wsa_data;
|
|
|
|
(void)WSAStartup(MAKEWORD(2, 2), &wsa_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Finalize() {
|
|
|
|
WSACleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr TranslateFromSockAddrIn(SockAddrIn input) {
|
|
|
|
sockaddr_in result;
|
|
|
|
|
2020-08-31 14:09:06 +00:00
|
|
|
#if YUZU_UNIX
|
2020-07-12 00:42:56 +00:00
|
|
|
result.sin_len = sizeof(result);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (static_cast<Domain>(input.family)) {
|
|
|
|
case Domain::INET:
|
|
|
|
result.sin_family = AF_INET;
|
|
|
|
break;
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled sockaddr family={}", input.family);
|
2020-07-12 00:42:56 +00:00
|
|
|
result.sin_family = AF_INET;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.sin_port = htons(input.portno);
|
|
|
|
|
|
|
|
auto& ip = result.sin_addr.S_un.S_un_b;
|
|
|
|
ip.s_b1 = input.ip[0];
|
|
|
|
ip.s_b2 = input.ip[1];
|
|
|
|
ip.s_b3 = input.ip[2];
|
|
|
|
ip.s_b4 = input.ip[3];
|
|
|
|
|
|
|
|
sockaddr addr;
|
|
|
|
std::memcpy(&addr, &result, sizeof(addr));
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LINGER MakeLinger(bool enable, u32 linger_value) {
|
|
|
|
ASSERT(linger_value <= std::numeric_limits<u_short>::max());
|
|
|
|
|
|
|
|
LINGER value;
|
|
|
|
value.l_onoff = enable ? 1 : 0;
|
|
|
|
value.l_linger = static_cast<u_short>(linger_value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EnableNonBlock(SOCKET fd, bool enable) {
|
|
|
|
u_long value = enable ? 1 : 0;
|
2020-10-21 02:07:39 +00:00
|
|
|
return ioctlsocket(fd, FIONBIO, &value) != SOCKET_ERROR;
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
Errno TranslateNativeError(int e) {
|
|
|
|
switch (e) {
|
|
|
|
case WSAEBADF:
|
|
|
|
return Errno::BADF;
|
|
|
|
case WSAEINVAL:
|
|
|
|
return Errno::INVAL;
|
|
|
|
case WSAEMFILE:
|
|
|
|
return Errno::MFILE;
|
|
|
|
case WSAENOTCONN:
|
|
|
|
return Errno::NOTCONN;
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
return Errno::AGAIN;
|
|
|
|
case WSAECONNREFUSED:
|
|
|
|
return Errno::CONNREFUSED;
|
|
|
|
case WSAEHOSTUNREACH:
|
|
|
|
return Errno::HOSTUNREACH;
|
|
|
|
case WSAENETDOWN:
|
|
|
|
return Errno::NETDOWN;
|
|
|
|
case WSAENETUNREACH:
|
|
|
|
return Errno::NETUNREACH;
|
|
|
|
default:
|
|
|
|
return Errno::OTHER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 14:09:06 +00:00
|
|
|
#elif YUZU_UNIX // ^ _WIN32 v YUZU_UNIX
|
2020-07-12 00:42:56 +00:00
|
|
|
|
|
|
|
using SOCKET = int;
|
|
|
|
using WSAPOLLFD = pollfd;
|
|
|
|
using ULONG = u64;
|
|
|
|
|
|
|
|
constexpr SOCKET INVALID_SOCKET = -1;
|
|
|
|
constexpr SOCKET SOCKET_ERROR = -1;
|
|
|
|
|
|
|
|
constexpr int SD_RECEIVE = SHUT_RD;
|
|
|
|
constexpr int SD_SEND = SHUT_WR;
|
|
|
|
constexpr int SD_BOTH = SHUT_RDWR;
|
|
|
|
|
|
|
|
void Initialize() {}
|
|
|
|
|
|
|
|
void Finalize() {}
|
|
|
|
|
|
|
|
sockaddr TranslateFromSockAddrIn(SockAddrIn input) {
|
|
|
|
sockaddr_in result;
|
|
|
|
|
|
|
|
switch (static_cast<Domain>(input.family)) {
|
|
|
|
case Domain::INET:
|
|
|
|
result.sin_family = AF_INET;
|
|
|
|
break;
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled sockaddr family={}", input.family);
|
2020-07-12 00:42:56 +00:00
|
|
|
result.sin_family = AF_INET;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.sin_port = htons(input.portno);
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
result.sin_addr.s_addr = input.ip[0] | input.ip[1] << 8 | input.ip[2] << 16 | input.ip[3] << 24;
|
2020-07-12 00:42:56 +00:00
|
|
|
|
|
|
|
sockaddr addr;
|
|
|
|
std::memcpy(&addr, &result, sizeof(addr));
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WSAPoll(WSAPOLLFD* fds, ULONG nfds, int timeout) {
|
2020-10-22 02:14:21 +00:00
|
|
|
return poll(fds, static_cast<nfds_t>(nfds), timeout);
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int closesocket(SOCKET fd) {
|
|
|
|
return close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
linger MakeLinger(bool enable, u32 linger_value) {
|
|
|
|
linger value;
|
|
|
|
value.l_onoff = enable ? 1 : 0;
|
2020-10-21 02:07:39 +00:00
|
|
|
value.l_linger = linger_value;
|
2020-07-12 00:42:56 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EnableNonBlock(int fd, bool enable) {
|
2021-08-06 19:08:31 +00:00
|
|
|
int flags = fcntl(fd, F_GETFL);
|
2020-07-12 00:42:56 +00:00
|
|
|
if (flags == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (enable) {
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
} else {
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
}
|
2021-08-06 19:08:31 +00:00
|
|
|
return fcntl(fd, F_SETFL, flags) == 0;
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
Errno TranslateNativeError(int e) {
|
|
|
|
switch (e) {
|
|
|
|
case EBADF:
|
|
|
|
return Errno::BADF;
|
|
|
|
case EINVAL:
|
|
|
|
return Errno::INVAL;
|
|
|
|
case EMFILE:
|
|
|
|
return Errno::MFILE;
|
|
|
|
case ENOTCONN:
|
|
|
|
return Errno::NOTCONN;
|
|
|
|
case EAGAIN:
|
|
|
|
return Errno::AGAIN;
|
|
|
|
case ECONNREFUSED:
|
|
|
|
return Errno::CONNREFUSED;
|
|
|
|
case EHOSTUNREACH:
|
|
|
|
return Errno::HOSTUNREACH;
|
|
|
|
case ENETDOWN:
|
|
|
|
return Errno::NETDOWN;
|
|
|
|
case ENETUNREACH:
|
|
|
|
return Errno::NETUNREACH;
|
|
|
|
default:
|
|
|
|
return Errno::OTHER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 00:42:56 +00:00
|
|
|
#endif
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
Errno GetAndLogLastError() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
int e = WSAGetLastError();
|
|
|
|
#else
|
|
|
|
int e = errno;
|
|
|
|
#endif
|
2021-08-07 00:54:25 +00:00
|
|
|
const Errno err = TranslateNativeError(e);
|
|
|
|
if (err == Errno::AGAIN) {
|
|
|
|
return err;
|
|
|
|
}
|
2021-09-08 18:36:20 +00:00
|
|
|
LOG_ERROR(Network, "Socket operation error: {}", Common::NativeErrorToString(e));
|
2021-08-07 00:54:25 +00:00
|
|
|
return err;
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 00:42:56 +00:00
|
|
|
int TranslateDomain(Domain domain) {
|
|
|
|
switch (domain) {
|
|
|
|
case Domain::INET:
|
|
|
|
return AF_INET;
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented domain={}", domain);
|
2020-07-12 00:42:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int TranslateType(Type type) {
|
|
|
|
switch (type) {
|
|
|
|
case Type::STREAM:
|
|
|
|
return SOCK_STREAM;
|
|
|
|
case Type::DGRAM:
|
|
|
|
return SOCK_DGRAM;
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented type={}", type);
|
2020-07-12 00:42:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int TranslateProtocol(Protocol protocol) {
|
|
|
|
switch (protocol) {
|
|
|
|
case Protocol::TCP:
|
|
|
|
return IPPROTO_TCP;
|
|
|
|
case Protocol::UDP:
|
|
|
|
return IPPROTO_UDP;
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented protocol={}", protocol);
|
2020-07-12 00:42:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddrIn TranslateToSockAddrIn(sockaddr input_) {
|
|
|
|
sockaddr_in input;
|
|
|
|
std::memcpy(&input, &input_, sizeof(input));
|
|
|
|
|
|
|
|
SockAddrIn result;
|
|
|
|
|
|
|
|
switch (input.sin_family) {
|
|
|
|
case AF_INET:
|
|
|
|
result.family = Domain::INET;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unhandled sockaddr family={}", input.sin_family);
|
|
|
|
result.family = Domain::INET;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.portno = ntohs(input.sin_port);
|
|
|
|
|
|
|
|
result.ip = TranslateIPv4(input.sin_addr);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
short TranslatePollEvents(PollEvents events) {
|
|
|
|
short result = 0;
|
2020-07-12 00:42:56 +00:00
|
|
|
|
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
|
|
|
if (True(events & PollEvents::In)) {
|
|
|
|
events &= ~PollEvents::In;
|
2020-07-12 00:42:56 +00:00
|
|
|
result |= POLLIN;
|
|
|
|
}
|
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
|
|
|
if (True(events & PollEvents::Pri)) {
|
|
|
|
events &= ~PollEvents::Pri;
|
2020-07-12 00:42:56 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LOG_WARNING(Service, "Winsock doesn't support POLLPRI");
|
|
|
|
#else
|
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 |= POLLPRI;
|
2020-07-12 00:42:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
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
|
|
|
if (True(events & PollEvents::Out)) {
|
|
|
|
events &= ~PollEvents::Out;
|
2020-07-12 00:42:56 +00:00
|
|
|
result |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
UNIMPLEMENTED_IF_MSG((u16)events != 0, "Unhandled guest events=0x{:x}", (u16)events);
|
2020-07-12 00:42:56 +00:00
|
|
|
|
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
|
|
|
return result;
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
PollEvents TranslatePollRevents(short revents) {
|
|
|
|
PollEvents result{};
|
|
|
|
const auto translate = [&result, &revents](short host, PollEvents guest) {
|
2020-10-13 12:10:50 +00:00
|
|
|
if ((revents & host) != 0) {
|
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
|
|
|
revents &= static_cast<short>(~host);
|
2020-07-12 00:42:56 +00:00
|
|
|
result |= guest;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
translate(POLLIN, PollEvents::In);
|
|
|
|
translate(POLLPRI, PollEvents::Pri);
|
|
|
|
translate(POLLOUT, PollEvents::Out);
|
|
|
|
translate(POLLERR, PollEvents::Err);
|
|
|
|
translate(POLLHUP, PollEvents::Hup);
|
2020-07-12 00:42:56 +00:00
|
|
|
|
|
|
|
UNIMPLEMENTED_IF_MSG(revents != 0, "Unhandled host revents=0x{:x}", revents);
|
|
|
|
|
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
|
|
|
return result;
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Errno SetSockOpt(SOCKET fd, int option, T value) {
|
|
|
|
const int result =
|
|
|
|
setsockopt(fd, SOL_SOCKET, option, reinterpret_cast<const char*>(&value), sizeof(value));
|
|
|
|
if (result != SOCKET_ERROR) {
|
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return GetAndLogLastError();
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
NetworkInstance::NetworkInstance() {
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkInstance::~NetworkInstance() {
|
|
|
|
Finalize();
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:32:53 +00:00
|
|
|
std::optional<IPv4Address> GetHostIPv4Address() {
|
|
|
|
const std::string& selected_network_interface = Settings::values.network_interface.GetValue();
|
|
|
|
const auto network_interfaces = Network::GetAvailableNetworkInterfaces();
|
2021-08-13 09:21:34 +00:00
|
|
|
if (network_interfaces.size() == 0) {
|
|
|
|
LOG_ERROR(Network, "GetAvailableNetworkInterfaces returned no interfaces");
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-12 00:42:56 +00:00
|
|
|
|
2021-08-13 09:23:50 +00:00
|
|
|
const auto res =
|
|
|
|
std::ranges::find_if(network_interfaces, [&selected_network_interface](const auto& iface) {
|
|
|
|
return iface.name == selected_network_interface;
|
|
|
|
});
|
2021-08-12 19:32:53 +00:00
|
|
|
|
|
|
|
if (res != network_interfaces.end()) {
|
2021-08-12 22:28:44 +00:00
|
|
|
char ip_addr[16] = {};
|
2021-08-12 19:32:53 +00:00
|
|
|
ASSERT(inet_ntop(AF_INET, &res->ip_address, ip_addr, sizeof(ip_addr)) != nullptr);
|
|
|
|
return TranslateIPv4(res->ip_address);
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Network, "Couldn't find selected interface \"{}\"", selected_network_interface);
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> Poll(std::vector<PollFD>& pollfds, s32 timeout) {
|
|
|
|
const size_t num = pollfds.size();
|
|
|
|
|
|
|
|
std::vector<WSAPOLLFD> host_pollfds(pollfds.size());
|
|
|
|
std::transform(pollfds.begin(), pollfds.end(), host_pollfds.begin(), [](PollFD fd) {
|
|
|
|
WSAPOLLFD result;
|
|
|
|
result.fd = fd.socket->fd;
|
2020-10-21 02:07:39 +00:00
|
|
|
result.events = TranslatePollEvents(fd.events);
|
2020-07-12 00:42:56 +00:00
|
|
|
result.revents = 0;
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
const int result = WSAPoll(host_pollfds.data(), static_cast<ULONG>(num), timeout);
|
|
|
|
if (result == 0) {
|
|
|
|
ASSERT(std::all_of(host_pollfds.begin(), host_pollfds.end(),
|
|
|
|
[](WSAPOLLFD fd) { return fd.revents == 0; }));
|
|
|
|
return {0, Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num; ++i) {
|
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
|
|
|
pollfds[i].revents = TranslatePollRevents(host_pollfds[i].revents);
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result > 0) {
|
|
|
|
return {result, Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(result == SOCKET_ERROR);
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return {-1, GetAndLogLastError()};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Socket::~Socket() {
|
|
|
|
if (fd == INVALID_SOCKET) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(void)closesocket(fd);
|
|
|
|
fd = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(Socket&& rhs) noexcept : fd{std::exchange(rhs.fd, INVALID_SOCKET)} {}
|
|
|
|
|
|
|
|
Errno Socket::Initialize(Domain domain, Type type, Protocol protocol) {
|
|
|
|
fd = socket(TranslateDomain(domain), TranslateType(type), TranslateProtocol(protocol));
|
|
|
|
if (fd != INVALID_SOCKET) {
|
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return GetAndLogLastError();
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Socket::AcceptResult, Errno> Socket::Accept() {
|
|
|
|
sockaddr addr;
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
const SOCKET new_socket = accept(fd, &addr, &addrlen);
|
|
|
|
|
|
|
|
if (new_socket == INVALID_SOCKET) {
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return {AcceptResult{}, GetAndLogLastError()};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AcceptResult result;
|
|
|
|
result.socket = std::make_unique<Socket>();
|
|
|
|
result.socket->fd = new_socket;
|
|
|
|
|
|
|
|
ASSERT(addrlen == sizeof(sockaddr_in));
|
|
|
|
result.sockaddr_in = TranslateToSockAddrIn(addr);
|
|
|
|
|
|
|
|
return {std::move(result), Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::Connect(SockAddrIn addr_in) {
|
|
|
|
const sockaddr host_addr_in = TranslateFromSockAddrIn(addr_in);
|
2020-10-13 12:10:50 +00:00
|
|
|
if (connect(fd, &host_addr_in, sizeof(host_addr_in)) != SOCKET_ERROR) {
|
2020-07-12 00:42:56 +00:00
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return GetAndLogLastError();
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<SockAddrIn, Errno> Socket::GetPeerName() {
|
|
|
|
sockaddr addr;
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
if (getpeername(fd, &addr, &addrlen) == SOCKET_ERROR) {
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return {SockAddrIn{}, GetAndLogLastError()};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(addrlen == sizeof(sockaddr_in));
|
|
|
|
return {TranslateToSockAddrIn(addr), Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<SockAddrIn, Errno> Socket::GetSockName() {
|
|
|
|
sockaddr addr;
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
if (getsockname(fd, &addr, &addrlen) == SOCKET_ERROR) {
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return {SockAddrIn{}, GetAndLogLastError()};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(addrlen == sizeof(sockaddr_in));
|
|
|
|
return {TranslateToSockAddrIn(addr), Errno::SUCCESS};
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::Bind(SockAddrIn addr) {
|
|
|
|
const sockaddr addr_in = TranslateFromSockAddrIn(addr);
|
|
|
|
if (bind(fd, &addr_in, sizeof(addr_in)) != SOCKET_ERROR) {
|
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return GetAndLogLastError();
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::Listen(s32 backlog) {
|
|
|
|
if (listen(fd, backlog) != SOCKET_ERROR) {
|
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return GetAndLogLastError();
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::Shutdown(ShutdownHow how) {
|
|
|
|
int host_how = 0;
|
|
|
|
switch (how) {
|
|
|
|
case ShutdownHow::RD:
|
|
|
|
host_how = SD_RECEIVE;
|
|
|
|
break;
|
|
|
|
case ShutdownHow::WR:
|
|
|
|
host_how = SD_SEND;
|
|
|
|
break;
|
|
|
|
case ShutdownHow::RDWR:
|
|
|
|
host_how = SD_BOTH;
|
|
|
|
break;
|
|
|
|
default:
|
2020-12-08 03:00:34 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented flag how={}", how);
|
2020-07-12 00:42:56 +00:00
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
|
|
|
if (shutdown(fd, host_how) != SOCKET_ERROR) {
|
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return GetAndLogLastError();
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
std::pair<s32, Errno> Socket::Recv(int flags, std::vector<u8>& message) {
|
2020-07-12 00:42:56 +00:00
|
|
|
ASSERT(flags == 0);
|
|
|
|
ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max()));
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
const auto result =
|
|
|
|
recv(fd, reinterpret_cast<char*>(message.data()), static_cast<int>(message.size()), 0);
|
2020-07-12 00:42:56 +00:00
|
|
|
if (result != SOCKET_ERROR) {
|
2020-10-13 12:10:50 +00:00
|
|
|
return {static_cast<s32>(result), Errno::SUCCESS};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return {-1, GetAndLogLastError()};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
std::pair<s32, Errno> Socket::RecvFrom(int flags, std::vector<u8>& message, SockAddrIn* addr) {
|
2020-07-12 00:42:56 +00:00
|
|
|
ASSERT(flags == 0);
|
|
|
|
ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max()));
|
|
|
|
|
|
|
|
sockaddr addr_in{};
|
|
|
|
socklen_t addrlen = sizeof(addr_in);
|
|
|
|
socklen_t* const p_addrlen = addr ? &addrlen : nullptr;
|
|
|
|
sockaddr* const p_addr_in = addr ? &addr_in : nullptr;
|
|
|
|
|
2020-10-13 12:10:50 +00:00
|
|
|
const auto result = recvfrom(fd, reinterpret_cast<char*>(message.data()),
|
2020-10-21 02:07:39 +00:00
|
|
|
static_cast<int>(message.size()), 0, p_addr_in, p_addrlen);
|
2020-07-12 00:42:56 +00:00
|
|
|
if (result != SOCKET_ERROR) {
|
|
|
|
if (addr) {
|
|
|
|
ASSERT(addrlen == sizeof(addr_in));
|
|
|
|
*addr = TranslateToSockAddrIn(addr_in);
|
|
|
|
}
|
2020-10-13 12:10:50 +00:00
|
|
|
return {static_cast<s32>(result), Errno::SUCCESS};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return {-1, GetAndLogLastError()};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
std::pair<s32, Errno> Socket::Send(const std::vector<u8>& message, int flags) {
|
2020-07-12 00:42:56 +00:00
|
|
|
ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max()));
|
|
|
|
ASSERT(flags == 0);
|
|
|
|
|
2020-10-13 12:10:50 +00:00
|
|
|
const auto result = send(fd, reinterpret_cast<const char*>(message.data()),
|
2020-10-21 02:07:39 +00:00
|
|
|
static_cast<int>(message.size()), 0);
|
2020-07-12 00:42:56 +00:00
|
|
|
if (result != SOCKET_ERROR) {
|
2020-10-13 12:10:50 +00:00
|
|
|
return {static_cast<s32>(result), Errno::SUCCESS};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return {-1, GetAndLogLastError()};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<s32, Errno> Socket::SendTo(u32 flags, const std::vector<u8>& message,
|
|
|
|
const SockAddrIn* addr) {
|
|
|
|
ASSERT(flags == 0);
|
|
|
|
|
|
|
|
const sockaddr* to = nullptr;
|
2021-08-02 18:12:12 +00:00
|
|
|
const int tolen = addr ? sizeof(sockaddr) : 0;
|
2020-07-12 00:42:56 +00:00
|
|
|
sockaddr host_addr_in;
|
|
|
|
|
|
|
|
if (addr) {
|
|
|
|
host_addr_in = TranslateFromSockAddrIn(*addr);
|
|
|
|
to = &host_addr_in;
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:07:39 +00:00
|
|
|
const auto result = sendto(fd, reinterpret_cast<const char*>(message.data()),
|
|
|
|
static_cast<int>(message.size()), 0, to, tolen);
|
2020-07-12 00:42:56 +00:00
|
|
|
if (result != SOCKET_ERROR) {
|
2020-10-13 12:10:50 +00:00
|
|
|
return {static_cast<s32>(result), Errno::SUCCESS};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return {-1, GetAndLogLastError()};
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::Close() {
|
|
|
|
[[maybe_unused]] const int result = closesocket(fd);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
fd = INVALID_SOCKET;
|
|
|
|
|
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::SetLinger(bool enable, u32 linger) {
|
|
|
|
return SetSockOpt(fd, SO_LINGER, MakeLinger(enable, linger));
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::SetReuseAddr(bool enable) {
|
|
|
|
return SetSockOpt<u32>(fd, SO_REUSEADDR, enable ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::SetBroadcast(bool enable) {
|
|
|
|
return SetSockOpt<u32>(fd, SO_BROADCAST, enable ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::SetSndBuf(u32 value) {
|
|
|
|
return SetSockOpt(fd, SO_SNDBUF, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::SetRcvBuf(u32 value) {
|
|
|
|
return SetSockOpt(fd, SO_RCVBUF, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::SetSndTimeo(u32 value) {
|
|
|
|
return SetSockOpt(fd, SO_SNDTIMEO, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::SetRcvTimeo(u32 value) {
|
|
|
|
return SetSockOpt(fd, SO_RCVTIMEO, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Errno Socket::SetNonBlock(bool enable) {
|
|
|
|
if (EnableNonBlock(fd, enable)) {
|
|
|
|
return Errno::SUCCESS;
|
|
|
|
}
|
[network] Error handling reform
`network.cpp` has several error paths which either:
- report "Unhandled host socket error=n" and return `SUCCESS`, or
- switch on a few possible errors, log them, and translate them to
Errno; the same switch statement is copied and pasted in multiple
places in the code
Convert these paths to use a helper function `GetAndLogLastError`, which
is roughly the equivalent of one of the switch statements, but:
- handling more cases (both ones that were already in `Errno`, and a few
more I added), and
- using OS functions to convert the error to a string when logging, so
it'll describe the error even if it's not one of the ones in the
switch statement.
- To handle this, refactor the logic in `GetLastErrorMsg` to expose a
new function `NativeErrorToString` which takes the error number
explicitly as an argument. And improve the Windows version a bit.
Also, add a test which exercises two random error paths.
2021-01-24 20:17:02 +00:00
|
|
|
return GetAndLogLastError();
|
2020-07-12 00:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::IsOpened() const {
|
|
|
|
return fd != INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Network
|