Repository: dotnet/runtime
File: src/libraries/Native/Unix/System.Native/pal_networking.c (C)

Code before:

#if HAVE_SYS_PROCINFO_H
int32_t SystemNative_GetSocketType(intptr_t socket, int32_t* addressFamily, int32_t* socketType, int32_t* protocolType, int32_t* isListening)
{
    if (addressFamily == NULL || socketType == NULL || protocolType == NULL || isListening == NULL)
    {
        return Error_EFAULT;
    }

    int fd = ToFileDescriptor(socket);

    struct socket_fdinfo fdi;
    if (proc_pidfdinfo(getpid(), fd, PROC_PIDFDSOCKETINFO, &fdi, sizeof(fdi)) < sizeof(fdi))
    {
        return Error_EFAULT;
    }

    if (!TryConvertAddressFamilyPlatformToPal((sa_family_t)fdi.psi.soi_family, addressFamily))
    {
        *addressFamily = AddressFamily_AF_UNKNOWN;
    }

    if (!TryConvertSocketTypePlatformToPal(fdi.psi.soi_type, socketType))
    {
        *socketType = SocketType_UNKNOWN;
    }

    if (!TryConvertProtocolTypePlatformToPal(*addressFamily, fdi.psi.soi_protocol, protocolType))
    {
        *protocolType = ProtocolType_PT_UNKNOWN;
    }

    *isListening = (fdi.psi.soi_options & SO_ACCEPTCONN) != 0;

    return Error_SUCCESS;
}
#else
int32_t SystemNative_GetSocketType(intptr_t socket, int32_t* addressFamily, int32_t* socketType, int32_t* protocolType, int32_t* isListening)
{
    if (addressFamily == NULL || socketType == NULL || protocolType == NULL || isListening == NULL)
    {
        return Error_EFAULT;
    }

    int fd = ToFileDescriptor(socket);

#ifdef SO_DOMAIN
    int domainValue;
    socklen_t domainLength = sizeof(int);
    if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domainValue, &domainLength) != 0 ||
        !TryConvertAddressFamilyPlatformToPal((sa_family_t)domainValue, addressFamily))

Review comment (refactor):
The signature and these null checks should be before the ifdefs rather than being duplicated.

Code after:
    {
        *createdSocket = -1;
        return Error_EPROTONOSUPPORT;
    }

#ifdef SOCK_CLOEXEC
    platformSocketType |= SOCK_CLOEXEC;
#endif
    *createdSocket = socket(platformAddressFamily, platformSocketType, platformProtocolType);
    if (*createdSocket == -1)
    {
        return SystemNative_ConvertErrorPlatformToPal(errno);
    }

#ifndef SOCK_CLOEXEC
    fcntl(ToFileDescriptor(*createdSocket), F_SETFD, FD_CLOEXEC); // ignore any failures; this is best effort
#endif
    return Error_SUCCESS;
}

int32_t SystemNative_GetSocketType(intptr_t socket, int32_t* addressFamily, int32_t* socketType, int32_t* protocolType, int32_t* isListening)
{
    if (addressFamily == NULL || socketType == NULL || protocolType == NULL || isListening == NULL)
    {
        return Error_EFAULT;
    }

    int fd = ToFileDescriptor(socket);

#if HAVE_SYS_PROCINFO_H
    struct socket_fdinfo fdi;
    if (proc_pidfdinfo(getpid(), fd, PROC_PIDFDSOCKETINFO, &fdi, sizeof(fdi)) < sizeof(fdi))
    {
        return Error_EFAULT;
    }

    if (!TryConvertAddressFamilyPlatformToPal((sa_family_t)fdi.psi.soi_family, addressFamily))
    {
        *addressFamily = AddressFamily_AF_UNKNOWN;
    }

    if (!TryConvertSocketTypePlatformToPal(fdi.psi.soi_type, socketType))
    {
        *socketType = SocketType_UNKNOWN;
    }

    if (!TryConvertProtocolTypePlatformToPal(*addressFamily, fdi.psi.soi_protocol, protocolType))
    {
        *protocolType = ProtocolType_PT_UNKNOWN;
    }