VirtualBox

Changeset 70393 in vbox for trunk/src


Ignore:
Timestamp:
Dec 30, 2017 12:11:56 PM (7 years ago)
Author:
vboxsync
Message:

IPRT/winsock: init fixes.

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/socket.cpp

    r70244 r70393  
    182182
    183183
     184/*********************************************************************************************************************************
     185*   Global Variables                                                                                                             *
     186*********************************************************************************************************************************/
     187#ifdef RT_OS_WINDOWS
     188/** Indicates that we've successfully initialized winsock.  */
     189static uint32_t volatile g_uWinSockInitedVersion = 0;
     190#endif
     191
     192
     193#ifdef RT_OS_WINDOWS
     194/**
     195 * Initializes winsock for the process.
     196 *
     197 * @returns IPRT status code.
     198 */
     199static int rtSocketInitWinsock(void)
     200{
     201    if (g_uWinSockInitedVersion != 0)
     202        return VINF_SUCCESS;
     203
     204    if (   !g_pfnWSAGetLastError
     205        || !g_pfnWSAStartup
     206        || !g_pfnsocket
     207        || !g_pfnclosesocket)
     208        return VERR_NET_INIT_FAILED;
     209
     210    /*
     211     * Initialize winsock. Try with 2.2 and back down till we get something that works.
     212     */
     213    static const WORD s_awVersions[] =
     214    {
     215        MAKEWORD(2, 2),
     216        MAKEWORD(2, 1),
     217        MAKEWORD(2, 0),
     218        MAKEWORD(1, 1),
     219        MAKEWORD(1, 0),
     220    };
     221    for (uint32_t i = 0; i < RT_ELEMENTS(s_awVersions); i++)
     222    {
     223        WSADATA     wsaData;
     224        RT_ZERO(wsaData);
     225        int rcWsa = g_pfnWSAStartup(s_awVersions[i], &wsaData);
     226        if (rcWsa == 0)
     227        {
     228            Assert(wsaData.wVersion >= s_awVersions[i]);
     229            ASMAtomicWriteU32(&g_uWinSockInitedVersion, s_awVersions[i]);
     230            return VINF_SUCCESS;
     231        }
     232        AssertLogRelMsg(rcWsa == WSAVERNOTSUPPORTED, ("rcWsa=%d (winsock version %#x)\n", rcWsa, s_awVersions[i]));
     233    }
     234    LogRel(("Failed to init winsock!\n"));
     235    return VERR_NET_INIT_FAILED;
     236}
     237#endif
     238
     239
    184240/**
    185241 * Get the last error as an iprt status code.
     
    363419{
    364420#ifdef RT_OS_WINDOWS
    365     AssertReturn(g_pfnioctlsocket, VERR_NOT_SUPPORTED);
     421    AssertReturn(g_pfnioctlsocket, VERR_NET_NOT_UNSUPPORTED);
    366422    u_long uBlocking = fBlocking ? 0 : 1;
    367423    if (g_pfnioctlsocket(pThis->hNative, FIONBIO, &uBlocking))
     
    458514{
    459515#ifdef RT_OS_WINDOWS
    460     AssertReturn(g_pfnsocket, VERR_NOT_SUPPORTED);
    461     AssertReturn(g_pfnclosesocket, VERR_NOT_SUPPORTED);
    462     AssertReturn(g_pfnWSAStartup, VERR_NOT_SUPPORTED);
     516    AssertReturn(g_pfnsocket, VERR_NET_NOT_UNSUPPORTED);
     517    AssertReturn(g_pfnclosesocket, VERR_NET_NOT_UNSUPPORTED);
    463518
    464519    /* Initialize WinSock. */
    465     WORD const  wVersionRequested = MAKEWORD(1, 1);
    466     WSADATA     wsaData;
    467     RT_ZERO(wsaData);
    468     int rcWsa = g_pfnWSAStartup(wVersionRequested, &wsaData);
    469     AssertMsgReturn(wsaData.wVersion >= wVersionRequested && rcWsa == 0,
    470                     ("Wrong winsock version %#x rcWsa=%#x (%d)\n", wsaData.wVersion, rcWsa, rcWsa),
    471                     VERR_NOT_SUPPORTED);
     520    int rc2 = rtSocketInitWinsock();
     521    if (RT_FAILURE(rc2))
     522        return rc2;
    472523#endif
    473524
     
    538589
    539590#ifdef RT_OS_WINDOWS
    540             AssertReturn(g_pfnclosesocket, VERR_NOT_SUPPORTED);
     591            AssertReturn(g_pfnclosesocket, VERR_NET_NOT_UNSUPPORTED);
    541592            if (g_pfnclosesocket(hNative))
    542593#else
     
    695746#ifdef RT_OS_WINDOWS
    696747    /* Initialize WinSock and check version before we call gethostbyname. */
    697     if (   !g_pfnWSAStartup
    698         || !g_pfngethostbyname)
    699         return VERR_NOT_SUPPORTED;
    700     WORD const  wVersionRequested = MAKEWORD(1, 1);
    701     WSADATA     wsaData;
    702     RT_ZERO(wsaData);
    703     int rcWsa = g_pfnWSAStartup(wVersionRequested, &wsaData);
    704     AssertMsgReturn(wsaData.wVersion >= wVersionRequested && rcWsa == 0,
    705                     ("Wrong winsock version %#x rcWsa=%#x (%d)\n", wsaData.wVersion, rcWsa, rcWsa),
    706                     VERR_NOT_SUPPORTED);
     748    if (!g_pfngethostbyname)
     749        return VERR_NET_NOT_UNSUPPORTED;
     750
     751    int rc2 = rtSocketInitWinsock();
     752    if (RT_FAILURE(rc2))
     753        return rc2;
     754
    707755# define gethostbyname g_pfngethostbyname
    708756#endif
     
    783831     * Winsock2 init
    784832     */
    785     if (   !g_pfnWSAStartup
    786         || !g_pfngetaddrinfo
     833    if (   !g_pfngetaddrinfo
    787834        || !g_pfnfreeaddrinfo)
    788         return VERR_NOT_SUPPORTED;
    789     /** @todo someone should check if we really need 2, 2 here */
    790     WORD const  wVersionRequested = MAKEWORD(2, 2);
    791     WSADATA     wsaData;
    792     RT_ZERO(wsaData);
    793     int rcWsa = g_pfnWSAStartup(wVersionRequested, &wsaData);
    794     AssertMsgReturn(wsaData.wVersion >= wVersionRequested && rcWsa == 0,
    795                     ("Wrong winsock version %#x rcWsa=%#x (%d)\n", wsaData.wVersion, rcWsa, rcWsa),
    796                     VERR_NOT_SUPPORTED);
     835        return VERR_NET_NOT_UNSUPPORTED;
     836
     837    int rc2 = rtSocketInitWinsock();
     838    if (RT_FAILURE(rc2))
     839        return rc2;
     840
    797841#  define getaddrinfo  g_pfngetaddrinfo
    798842#  define freeaddrinfo g_pfnfreeaddrinfo
     
    882926    AssertPtr(pvBuffer);
    883927#ifdef RT_OS_WINDOWS
    884     AssertReturn(g_pfnrecv, VERR_NOT_SUPPORTED);
     928    AssertReturn(g_pfnrecv, VERR_NET_NOT_UNSUPPORTED);
    885929# define recv g_pfnrecv
    886930#endif
     
    9581002    AssertPtr(pcbRead);
    9591003#ifdef RT_OS_WINDOWS
    960     AssertReturn(g_pfnrecvfrom, VERR_NOT_SUPPORTED);
     1004    AssertReturn(g_pfnrecvfrom, VERR_NET_NOT_UNSUPPORTED);
    9611005# define recvfrom g_pfnrecvfrom
    9621006#endif
     
    10161060    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    10171061#ifdef RT_OS_WINDOWS
    1018     AssertReturn(g_pfnsend, VERR_NOT_SUPPORTED);
     1062    AssertReturn(g_pfnsend, VERR_NET_NOT_UNSUPPORTED);
    10191063# define send g_pfnsend
    10201064#endif
     
    10921136    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    10931137#ifdef RT_OS_WINDOWS
    1094     AssertReturn(g_pfnsendto, VERR_NOT_SUPPORTED);
     1138    AssertReturn(g_pfnsendto, VERR_NET_NOT_UNSUPPORTED);
    10951139# define sendto g_pfnsendto
    10961140#endif
     
    11531197    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    11541198#ifdef RT_OS_WINDOWS
    1155     AssertReturn(g_pfnsendto, VERR_NOT_SUPPORTED);
     1199    AssertReturn(g_pfnsendto, VERR_NET_NOT_UNSUPPORTED);
    11561200# define sendto g_pfnsendto
    11571201#endif
     
    12771321    }
    12781322    else
    1279         rc = VERR_NOT_SUPPORTED;
     1323        rc = VERR_NET_NOT_UNSUPPORTED;
    12801324
    12811325#else  /* !RT_OS_WINDOWS */
     
    13561400    AssertPtrReturn(pcbRead, VERR_INVALID_PARAMETER);
    13571401#ifdef RT_OS_WINDOWS
    1358     AssertReturn(g_pfnrecv, VERR_NOT_SUPPORTED);
     1402    AssertReturn(g_pfnrecv, VERR_NET_NOT_UNSUPPORTED);
    13591403#endif
    13601404    AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
     
    14221466    AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
    14231467#ifdef RT_OS_WINDOWS
    1424     AssertReturn(g_pfnsend, VERR_NOT_SUPPORTED);
     1468    AssertReturn(g_pfnsend, VERR_NET_NOT_UNSUPPORTED);
    14251469#endif
    14261470    AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
     
    15451589    }
    15461590    else
    1547         rc = VERR_NOT_SUPPORTED;
     1591        rc = VERR_NET_NOT_UNSUPPORTED;
    15481592
    15491593#else  /* !RT_OS_WINDOWS */
     
    16181662    AssertReturn((RTSOCKETNATIVE)(fdMax - 1) == pThis->hNative, VERR_INTERNAL_ERROR_5);
    16191663#ifdef RT_OS_WINDOWS
    1620     AssertReturn(g_pfnselect, VERR_NOT_SUPPORTED);
     1664    AssertReturn(g_pfnselect, VERR_NET_NOT_UNSUPPORTED);
    16211665# define select g_pfnselect
    16221666#endif
     
    16791723    AssertReturn((RTSOCKETNATIVE)(fdMax - 1) == hNative, VERR_INTERNAL_ERROR_5);
    16801724#ifdef RT_OS_WINDOWS
    1681     AssertReturn(g_pfnselect, VERR_NOT_SUPPORTED);
    1682     AssertReturn(g_pfn__WSAFDIsSet, VERR_NOT_SUPPORTED);
     1725    AssertReturn(g_pfnselect, VERR_NET_NOT_UNSUPPORTED);
     1726    AssertReturn(g_pfn__WSAFDIsSet, VERR_NET_NOT_UNSUPPORTED);
    16831727# define select         g_pfnselect
    16841728# define __WSAFDIsSet   g_pfn__WSAFDIsSet
     
    17581802    AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER);
    17591803#ifdef RT_OS_WINDOWS
    1760     AssertReturn(g_pfnshutdown, VERR_NOT_SUPPORTED);
     1804    AssertReturn(g_pfnshutdown, VERR_NET_NOT_UNSUPPORTED);
    17611805# define shutdown g_pfnshutdown
    17621806#endif
     
    17931837    AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
    17941838#ifdef RT_OS_WINDOWS
    1795     AssertReturn(g_pfngetsockname, VERR_NOT_SUPPORTED);
     1839    AssertReturn(g_pfngetsockname, VERR_NET_NOT_UNSUPPORTED);
    17961840# define getsockname g_pfngetsockname
    17971841#endif
     
    18301874    AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
    18311875#ifdef RT_OS_WINDOWS
    1832     AssertReturn(g_pfngetpeername, VERR_NOT_SUPPORTED);
     1876    AssertReturn(g_pfngetpeername, VERR_NET_NOT_UNSUPPORTED);
    18331877# define getpeername g_pfngetpeername
    18341878#endif
     
    18951939    AssertPtrReturn(pvAddr, VERR_INVALID_POINTER);
    18961940#ifdef RT_OS_WINDOWS
    1897     AssertReturn(g_pfnbind, VERR_NOT_SUPPORTED);
     1941    AssertReturn(g_pfnbind, VERR_NET_NOT_UNSUPPORTED);
    18981942# define bind g_pfnbind
    18991943#endif
     
    19311975    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    19321976#ifdef RT_OS_WINDOWS
    1933     AssertReturn(g_pfnlisten, VERR_NOT_SUPPORTED);
     1977    AssertReturn(g_pfnlisten, VERR_NET_NOT_UNSUPPORTED);
    19341978# define listen g_pfnlisten
    19351979#endif
     
    19712015    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    19722016#ifdef RT_OS_WINDOWS
    1973     AssertReturn(g_pfnaccept, VERR_NOT_SUPPORTED);
    1974     AssertReturn(g_pfnclosesocket, VERR_NOT_SUPPORTED);
     2017    AssertReturn(g_pfnaccept, VERR_NET_NOT_UNSUPPORTED);
     2018    AssertReturn(g_pfnclosesocket, VERR_NET_NOT_UNSUPPORTED);
    19752019# define accept g_pfnaccept
    19762020#endif
     
    20362080    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    20372081#ifdef RT_OS_WINDOWS
    2038     AssertReturn(g_pfnconnect, VERR_NOT_SUPPORTED);
    2039     AssertReturn(g_pfnselect, VERR_NOT_SUPPORTED);
    2040     AssertReturn(g_pfngetsockopt, VERR_NOT_SUPPORTED);
     2082    AssertReturn(g_pfnconnect, VERR_NET_NOT_UNSUPPORTED);
     2083    AssertReturn(g_pfnselect, VERR_NET_NOT_UNSUPPORTED);
     2084    AssertReturn(g_pfngetsockopt, VERR_NET_NOT_UNSUPPORTED);
    20412085# define connect        g_pfnconnect
    20422086# define select         g_pfnselect
     
    21462190    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    21472191#ifdef RT_OS_WINDOWS
    2148     AssertReturn(g_pfnconnect, VERR_NOT_SUPPORTED);
     2192    AssertReturn(g_pfnconnect, VERR_NET_NOT_UNSUPPORTED);
    21492193# define connect        g_pfnconnect
    21502194#endif
     
    21842228    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    21852229#ifdef RT_OS_WINDOWS
    2186     AssertReturn(g_pfnsetsockopt, VERR_NOT_SUPPORTED);
     2230    AssertReturn(g_pfnsetsockopt, VERR_NET_NOT_UNSUPPORTED);
    21872231# define setsockopt g_pfnsetsockopt
    21882232#endif
     
    22302274    }
    22312275    else
    2232         rc = VERR_NOT_SUPPORTED;
     2276        rc = VERR_NET_NOT_UNSUPPORTED;
    22332277
    22342278    rtSocketUnlock(pThis);
     
    22822326        }
    22832327        else
    2284             rc = VERR_NOT_SUPPORTED;
     2328            rc = VERR_NET_NOT_UNSUPPORTED;
    22852329    }
    22862330    return rc;
     
    23172361    }
    23182362    else
    2319         rc = VERR_NOT_SUPPORTED;
     2363        rc = VERR_NET_NOT_UNSUPPORTED;
    23202364    return rc;
    23212365}
     
    23772421    }
    23782422    else if (RT_SUCCESS(rc))
    2379         rc = VERR_NOT_SUPPORTED;
     2423        rc = VERR_NET_NOT_UNSUPPORTED;
    23802424
    23812425    /* Fall back on select if we hit an error above. */
  • trunk/src/VBox/Runtime/win/RTErrConvertFromWin32.cpp

    r69793 r70393  
    389389        case WSAEALREADY:          return VERR_NET_ALREADY_IN_PROGRESS;
    390390        case WSAEINPROGRESS:       return VERR_NET_IN_PROGRESS;
     391        case WSAEPROVIDERFAILEDINIT: return VERR_NET_INIT_FAILED;
     392
    391393        //case WSAESTALE                116     /* Stale NFS file handle */
    392394        //case WSAEUCLEAN               117     /* Structure needs cleaning */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette