Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/libs/prx/libSceNet/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,21 @@ static int disconnect_datagram(const Sock& socket) {

#ifdef _WIN32
extern "C" void RefuseClosedLoopbackAtOnce_nid_no_patch(SOCKET socket, const sockaddr* address);

static void LoopbackForUnspecifiedInet(sockaddr_storage& native) {
if (native.ss_family == AF_INET) {
auto& inet = reinterpret_cast<sockaddr_in&>(native);
if (inet.sin_addr.s_addr == htonl(INADDR_ANY)) inet.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
return;
}
if (native.ss_family != AF_INET6) return;
auto& address = reinterpret_cast<sockaddr_in6&>(native).sin6_addr;
auto* bytes = address.s6_addr;
if (IN6_IS_ADDR_V4MAPPED(&address) && (bytes[12] | bytes[13] | bytes[14] | bytes[15]) == 0) {
bytes[12] = 127;
bytes[15] = 1;
}
}
#endif

#ifdef __APPLE__
Expand Down Expand Up @@ -1723,7 +1738,10 @@ int APS5_VABI sceNetConnect(int s, const void* addr, uint32_t addrlen) {
sockaddr_storage native{};
NativeLength native_length = 0;
if (!guest_to_native_address(address.data(), addrlen, native, native_length)) return fail(NET_EINVAL);
if (native.ss_family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&reinterpret_cast<sockaddr_in6&>(native).sin6_addr))
std::memcpy(&reinterpret_cast<sockaddr_in6&>(native).sin6_addr, sce_net_in6addr_loopback_nid_postfix, sizeof(sce_net_in6addr_loopback_nid_postfix));
#ifdef _WIN32
LoopbackForUnspecifiedInet(native);
if (socket.type == NET_SOCK_STREAM) RefuseClosedLoopbackAtOnce_nid_no_patch(socket.native->value, reinterpret_cast<const sockaddr*>(&native));
#endif
if (socket.listening) return fail(NET_EOPNOTSUPP);
Expand Down Expand Up @@ -2006,6 +2024,9 @@ int64_t APS5_VABI sceNetSendto(int s, const void* buf, size_t len, int flags, co
reinterpret_cast<const sockaddr*>(&destination), destination_length);
});
#else
#ifdef _WIN32
LoopbackForUnspecifiedInet(destination);
#endif
result = native_transfer(*socket.native, flags, true, [&](int offset) {
return ::sendto(socket.native->value, offset ? static_cast<const char*>(buf) + offset : static_cast<const char*>(buf), static_cast<int>(len) - offset, NATIVE_SEND_FLAGS | native_message_flags(flags),
reinterpret_cast<const sockaddr*>(&destination), destination_length);
Expand Down
54 changes: 54 additions & 0 deletions core/libs/tests/GuestSceNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,59 @@ static void CheckIpv6SocketsAreIpv6Only() {
Require(sceNetSocketClose(inet) == 0);
}

static void CheckConnectUnspecified() {
for (const int family : {2, 28}) {
if (family == 28 && ipv6Unavailable) continue;
#ifdef __APPLE__
if (family == 2) continue;
#endif
const std::uint32_t addressSize = family == 2 ? 16 : 28;
std::array<std::uint8_t, 28> loopback{};
loopback[0] = static_cast<std::uint8_t>(addressSize);
loopback[1] = static_cast<std::uint8_t>(family);
if (family == 2) {
loopback[4] = 127;
loopback[7] = 1;
} else {
loopback[23] = 1;
}
const int listener = sceNetSocket("unspecified-listener", family, 1, 6);
Require(listener >= 0 && sceNetBind_nid_postfix(listener, loopback.data(), addressSize) == 0 && sceNetListen(listener, 4) == 0);
std::uint32_t size = loopback.size();
Require(sceNetGetsockname(listener, loopback.data(), &size) == 0 && size == addressSize);
std::array<std::uint8_t, 28> unspecified{};
std::memcpy(unspecified.data(), loopback.data(), 4);
const int client = sceNetSocket("unspecified-client", family, 1, 6);
Require(client >= 0);
const int connected = sceNetConnect(client, unspecified.data(), addressSize);
std::array<std::uint8_t, 28> peer{};
size = peer.size();
const bool reachedLoopback = connected == 0 && sceNetGetpeername(client, peer.data(), &size) == 0 && size == addressSize &&
std::memcmp(peer.data(), loopback.data(), family == 2 ? 8 : 24) == 0;
const int accepted = connected == 0 ? sceNetAccept(listener, nullptr, nullptr) : -1;
for (const int descriptor : {accepted, client, listener}) {
if (descriptor >= 0) Require(sceNetSocketClose(descriptor) == 0);
}
Require(reachedLoopback && accepted >= 0);
}
#ifdef _WIN32
const int receiver = sceNetSocket("unspecified-receiver", 2, 2, 17);
const int sender = sceNetSocket("unspecified-sender", 2, 2, 17);
Require(receiver >= 0 && sender >= 0);
std::array<std::uint8_t, 16> address{16, 2, 0, 0, 127, 0, 0, 1};
Require(sceNetBind_nid_postfix(receiver, address.data(), address.size()) == 0);
std::uint32_t size = address.size();
Require(sceNetGetsockname(receiver, address.data(), &size) == 0);
std::array<std::uint8_t, 16> unspecified{16, 2, address[2], address[3], 0, 0, 0, 0};
const char payload[] = "unspecified";
char received[sizeof(payload)]{};
const auto sent = sceNetSendto(sender, payload, sizeof(payload), 0, unspecified.data(), unspecified.size());
const auto got = sent == sizeof(payload) ? sceNetRecvfrom(receiver, received, sizeof(received), 0, nullptr, nullptr) : -1;
for (const int descriptor : {sender, receiver}) Require(sceNetSocketClose(descriptor) == 0);
Require(sent == sizeof(payload) && got == sizeof(payload) && std::memcmp(received, payload, sizeof(payload)) == 0);
#endif
}

static void CheckSocketError() {
std::array<std::uint8_t, 16> address{16, 2, 0, 0, 127, 0, 0, 1};
const int listener = sceNetSocket(nullptr, 2, 1, 6);
Expand Down Expand Up @@ -3337,6 +3390,7 @@ int main(int argc, char** argv) {
CheckKernelSocketGetOption();
CheckSockInfo();
CheckSockInfo6();
CheckConnectUnspecified();

const int closed = sceNetSocket("closed-by-kernel", 2, 2, 17);
Require(closed >= 0);
Expand Down
5 changes: 5 additions & 0 deletions docs/dev/debt/changes/fix-libscenet-connect-unspecified.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libkernel

## change [connect to the unspecified address]

- [connect to the unspecified address](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - `sendto` and `sendmsg` to `::` on an `AF_INET6` socket go to the host unchanged and fail with `EADDRNOTAVAIL` on Windows, where FreeBSD 11.4's `udp6_output` leaves `::` as it is. `connect` on an `AF_INET6` socket replaces `::` with `::1`, as FreeBSD 11.4's `in6_pcbladdr` does, also on Linux, whose `tcp_v6_connect` and `ip6_datagram_connect` would use `::ffff:127.0.0.1` for a socket bound to a v4-mapped address. On Windows, `INADDR_ANY` and `::ffff:0.0.0.0` destinations become `127.0.0.1`, where FreeBSD 11.4's `in_pcbconnect_setup` uses the first interface address (the jail's in a jail), so the peer can differ; Linux and macOS leave them to the host. None of this was measured on a console. [sceNetConnect and sceNetSendto](../../../core/libs/prx/libSceNet/Export.cpp) do the same