[Bug] Socket API not consistent with nnsdk #660

Open
opened 2025-02-14 18:07:12 +00:00 by Martmists-GH · 2 comments
Martmists-GH commented 2025-02-14 18:07:12 +00:00 (Migrated from github.com)

Description of the issue

Opening a wrong socket crashes instead of returning an error code. (which I believe should be -91 in this case?)

00:00:34.952 |E| HLE.OsThread.1 Application : Unhandled exception caught: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.Net.Sockets.SocketException (91): Protocol wrong type for socket
   at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
   at Ryujinx.HLE.HOS.Services.Sockets.Bsd.Proxy.DefaultSocket..ctor(AddressFamily domain, SocketType type, ProtocolType protocol, String lanInterfaceId)
   at Ryujinx.HLE.HOS.Services.Sockets.Bsd.Proxy.SocketHelpers.CreateSocket(AddressFamily domain, SocketType type, ProtocolType protocol, String lanInterfaceId)
   at Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl.ManagedSocket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, String lanInterfaceId)
   at Ryujinx.HLE.HOS.Services.Sockets.Bsd.IClient.SocketInternal(ServiceCtx context, Boolean exempt)
   at Ryujinx.HLE.HOS.Services.Sockets.Bsd.IClient.Socket(ServiceCtx context)
   at InvokeStub_IClient.Socket(Object, Span`1)
   at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Ryujinx.HLE.HOS.Services.IpcService.CallCmifMethod(ServiceCtx context)
   at Ryujinx.HLE.HOS.Services.ServerBase.Process(Int32 serverSessionHandle, UInt64 recvListAddr)
   at Ryujinx.HLE.HOS.Services.ServerBase.ServerLoop()
   at Ryujinx.HLE.HOS.Services.ServerBase.Main()
   at Ryujinx.HLE.HOS.Kernel.Threading.KThread.ThreadStart()
   at System.Threading.Thread.StartCallback()

Reproduction steps

nnsocketSocket(2, 0, 0)

Log file

Ryujinx_1.2.81+5abd0290f8078665ff23ab4986240eb65691aded_2025-02-14_18-47-50.log

OS

Arch Linux

Ryujinx version

1.2.81

Game version

No response

CPU

No response

GPU

No response

RAM

No response

List of applied mods

A custom one that opens a socket as described above.

Additional context?

I'm looping over the results of nnsocketGetAddrInfo until a socket opens correctly.

/*
 * Initiate a TCP connection with host:port and the given protocol
 */
int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host,
                        const char *port, int proto)
{
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
    struct addrinfo hints, *addr_list, *cur;

    if ((ret = net_prepare()) != 0) {
        return ret;
    }

    /* Do name resolution with both IPv6 and IPv4 */
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
    hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;

    if (nnsocketGetAddrInfo(host, port, &hints, &addr_list) != 0) {
        return MBEDTLS_ERR_NET_UNKNOWN_HOST;
    }

    /* Try the sockaddrs until a connection succeeds */
    ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
    for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
        printf("nnsocketSocket(%d, %u, %d)\n", cur->ai_family, cur->ai_socktype, cur->ai_protocol);
        ctx->fd = nnsocketSocket(cur->ai_family, cur->ai_socktype,
                                 cur->ai_protocol);
        if (ctx->fd < 0) {
            ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
            continue;
        }

        if (nnsocketConnect(ctx->fd, cur->ai_addr, cur->ai_addrlen) == 0) {
            ret = 0;
            break;
        }

        nnsocketClose(ctx->fd);
        ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
    }

    nnsocketFreeAddrInfo(addr_list);

    return ret;
}
### Description of the issue Opening a wrong socket crashes instead of returning an error code. (which I believe should be -91 in this case?) ``` 00:00:34.952 |E| HLE.OsThread.1 Application : Unhandled exception caught: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.Sockets.SocketException (91): Protocol wrong type for socket at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) at Ryujinx.HLE.HOS.Services.Sockets.Bsd.Proxy.DefaultSocket..ctor(AddressFamily domain, SocketType type, ProtocolType protocol, String lanInterfaceId) at Ryujinx.HLE.HOS.Services.Sockets.Bsd.Proxy.SocketHelpers.CreateSocket(AddressFamily domain, SocketType type, ProtocolType protocol, String lanInterfaceId) at Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl.ManagedSocket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, String lanInterfaceId) at Ryujinx.HLE.HOS.Services.Sockets.Bsd.IClient.SocketInternal(ServiceCtx context, Boolean exempt) at Ryujinx.HLE.HOS.Services.Sockets.Bsd.IClient.Socket(ServiceCtx context) at InvokeStub_IClient.Socket(Object, Span`1) at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) --- End of inner exception stack trace --- at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Ryujinx.HLE.HOS.Services.IpcService.CallCmifMethod(ServiceCtx context) at Ryujinx.HLE.HOS.Services.ServerBase.Process(Int32 serverSessionHandle, UInt64 recvListAddr) at Ryujinx.HLE.HOS.Services.ServerBase.ServerLoop() at Ryujinx.HLE.HOS.Services.ServerBase.Main() at Ryujinx.HLE.HOS.Kernel.Threading.KThread.ThreadStart() at System.Threading.Thread.StartCallback() ``` ### Reproduction steps `nnsocketSocket(2, 0, 0)` ### Log file [Ryujinx_1.2.81+5abd0290f8078665ff23ab4986240eb65691aded_2025-02-14_18-47-50.log](https://github.com/user-attachments/files/18803133/Ryujinx_1.2.81%2B5abd0290f8078665ff23ab4986240eb65691aded_2025-02-14_18-47-50.log) ### OS Arch Linux ### Ryujinx version 1.2.81 ### Game version _No response_ ### CPU _No response_ ### GPU _No response_ ### RAM _No response_ ### List of applied mods A custom one that opens a socket as described above. ### Additional context? I'm looping over the results of nnsocketGetAddrInfo until a socket opens correctly. ```c /* * Initiate a TCP connection with host:port and the given protocol */ int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto) { int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; struct addrinfo hints, *addr_list, *cur; if ((ret = net_prepare()) != 0) { return ret; } /* Do name resolution with both IPv6 and IPv4 */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP; if (nnsocketGetAddrInfo(host, port, &hints, &addr_list) != 0) { return MBEDTLS_ERR_NET_UNKNOWN_HOST; } /* Try the sockaddrs until a connection succeeds */ ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; for (cur = addr_list; cur != NULL; cur = cur->ai_next) { printf("nnsocketSocket(%d, %u, %d)\n", cur->ai_family, cur->ai_socktype, cur->ai_protocol); ctx->fd = nnsocketSocket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); if (ctx->fd < 0) { ret = MBEDTLS_ERR_NET_SOCKET_FAILED; continue; } if (nnsocketConnect(ctx->fd, cur->ai_addr, cur->ai_addrlen) == 0) { ret = 0; break; } nnsocketClose(ctx->fd); ret = MBEDTLS_ERR_NET_CONNECT_FAILED; } nnsocketFreeAddrInfo(addr_list); return ret; } ```
FluffyOMC commented 2025-02-14 19:35:49 +00:00 (Migrated from github.com)

@Martmists-GH Mister RGB pfp on Discord, I have a build for you XD

@Martmists-GH Mister RGB pfp on Discord, I have a build for you XD
GreemDev commented 2025-02-16 08:27:42 +00:00 (Migrated from github.com)

@Martmists-GH Please test Canary 395 to see if this resolves the issue.

@Martmists-GH Please test [Canary 395](https://github.com/Ryubing/Canary-Releases/releases/tag/1.2.395) to see if this resolves the issue.
Sign in to join this conversation.
No Milestone
No project
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: MeloNX/Ryujinx-ryubing#660
No description provided.