VirtualBox

Changeset 96598 in vbox


Ignore:
Timestamp:
Sep 4, 2022 11:15:10 PM (2 years ago)
Author:
vboxsync
Message:

VBoxService/vminfo: Corrected API requirement checks in vgsvcVMInfoWriteNetwork - the code only requires the TCP/IP ones, not the GetAdaptersInfo one. Only allocate buffer for and try call GetAdaptersInfo if we've got a valid pointer to it (shuts up confusing/noisy messages on NT4). Did some futher cleanups, as per usual.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceVMInfo.cpp

    r96407 r96598  
    943943
    944944#ifdef RT_OS_WINDOWS
    945     /* */
    946     if (   !g_pfnGetAdaptersInfo
    947         && (   !g_pfnWSAIoctl
    948             || !g_pfnWSASocketA
    949             || !g_pfnWSAGetLastError
    950             || !g_pfninet_ntoa
    951             || !g_pfnclosesocket) )
    952            return VINF_SUCCESS;
    953 
    954     ULONG            cbAdpInfo = sizeof(IP_ADAPTER_INFO);
    955     IP_ADAPTER_INFO *pAdpInfo  = (IP_ADAPTER_INFO *)RTMemAllocZ(cbAdpInfo);
    956     if (!pAdpInfo)
    957     {
    958         VGSvcError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
    959         return VERR_NO_MEMORY;
    960     }
    961     DWORD dwRet = g_pfnGetAdaptersInfo ? g_pfnGetAdaptersInfo(pAdpInfo, &cbAdpInfo) : ERROR_NO_DATA;
    962     if (dwRet == ERROR_BUFFER_OVERFLOW)
    963     {
    964         IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
    965         if (pAdpInfoNew)
    966         {
    967             pAdpInfo = pAdpInfoNew;
    968             RT_BZERO(pAdpInfo, cbAdpInfo);
    969             dwRet = g_pfnGetAdaptersInfo(pAdpInfo, &cbAdpInfo);
    970         }
    971     }
    972     else if (dwRet == ERROR_NO_DATA)
    973     {
    974         VGSvcVerbose(3, "VMInfo/Network: No network adapters available\n");
    975 
    976         /* If no network adapters available / present in the
    977          * system we pretend success to not bail out too early. */
    978         RTMemFree(pAdpInfo);
    979         pAdpInfo  = NULL;
    980         cbAdpInfo = 0;
    981         dwRet     = ERROR_SUCCESS;
    982     }
    983     if (dwRet != ERROR_SUCCESS)
    984     {
    985         RTMemFree(pAdpInfo);
    986         VGSvcError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
    987         return RTErrConvertFromWin32(dwRet);
    988     }
    989 
     945    /*
     946     * Check that the APIs we need are present.
     947     */
     948    if (   !g_pfnWSAIoctl
     949        || !g_pfnWSASocketA
     950        || !g_pfnWSAGetLastError
     951        || !g_pfninet_ntoa
     952        || !g_pfnclosesocket)
     953        return VINF_SUCCESS;
     954
     955    /*
     956     * Query the IP adapter info first, if we have the API.
     957     */
     958    IP_ADAPTER_INFO *pAdpInfo  = NULL;
     959    if (g_pfnGetAdaptersInfo)
     960    {
     961        ULONG cbAdpInfo = RT_MAX(sizeof(IP_ADAPTER_INFO) * 2, _2K);
     962        pAdpInfo  = (IP_ADAPTER_INFO *)RTMemAllocZ(cbAdpInfo);
     963        if (!pAdpInfo)
     964        {
     965            VGSvcError("VMInfo/Network: Failed to allocate two IP_ADAPTER_INFO structures\n");
     966            return VERR_NO_MEMORY;
     967        }
     968
     969        DWORD dwRet = g_pfnGetAdaptersInfo(pAdpInfo, &cbAdpInfo);
     970        if (dwRet == ERROR_BUFFER_OVERFLOW)
     971        {
     972            IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
     973            if (pAdpInfoNew)
     974            {
     975                pAdpInfo = pAdpInfoNew;
     976                RT_BZERO(pAdpInfo, cbAdpInfo);
     977                dwRet = g_pfnGetAdaptersInfo(pAdpInfo, &cbAdpInfo);
     978            }
     979        }
     980        if (dwRet != NO_ERROR)
     981        {
     982            RTMemFree(pAdpInfo);
     983            pAdpInfo  = NULL;
     984            if (dwRet == ERROR_NO_DATA)
     985                /* If no network adapters available / present in the
     986                   system we pretend success to not bail out too early. */
     987                VGSvcVerbose(3, "VMInfo/Network: No network adapters present according to GetAdaptersInfo.\n");
     988            else
     989            {
     990                VGSvcError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
     991                return RTErrConvertFromWin32(dwRet);
     992            }
     993        }
     994    }
     995
     996    /*
     997     * Ask the TCP/IP stack for an interface list.
     998     */
    990999    SOCKET sd = g_pfnWSASocketA(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
    9911000    if (sd == SOCKET_ERROR) /* Socket invalid. */
    9921001    {
    993         int wsaErr = g_pfnWSAGetLastError();
     1002        int const wsaErr = g_pfnWSAGetLastError();
     1003        RTMemFree(pAdpInfo);
     1004
    9941005        /* Don't complain/bail out with an error if network stack is not up; can happen
    9951006         * on NT4 due to start up when not connected shares dialogs pop up. */
    996         if (WSAENETDOWN == wsaErr)
     1007        if (wsaErr == WSAENETDOWN)
    9971008        {
    9981009            VGSvcVerbose(0, "VMInfo/Network: Network is not up yet.\n");
    999             wsaErr = VINF_SUCCESS;
    1000         }
    1001         else
    1002             VGSvcError("VMInfo/Network: Failed to get a socket: Error %d\n", wsaErr);
    1003         if (pAdpInfo)
    1004             RTMemFree(pAdpInfo);
     1010            return VINF_SUCCESS;
     1011        }
     1012        VGSvcError("VMInfo/Network: Failed to get a socket: Error %d\n", wsaErr);
    10051013        return RTErrConvertFromWin32(wsaErr);
    10061014    }
     
    10381046    {
    10391047        VGSvcError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", g_pfnWSAGetLastError());
    1040         if (pAdpInfo)
    1041             RTMemFree(pAdpInfo);
     1048        RTMemFree(pAdpInfo);
    10421049        g_pfnclosesocket(sd);
    10431050        return RTErrConvertFromWin32(g_pfnWSAGetLastError());
     
    10461053    int cIfacesSystem = cbReturned / sizeof(INTERFACE_INFO);
    10471054
     1055    /*
     1056     * Iterate the inteface list we got back from the TCP/IP,
     1057     * using the pAdpInfo list to supply the MAC address.
     1058     */
    10481059    /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
    10491060    for (int i = 0; i < cIfacesSystem; ++i)
    10501061    {
    1051         sockaddr_in *pAddress;
    1052         u_long nFlags = 0;
    10531062        if (aInterfaces[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
    10541063            continue;
    1055         nFlags = aInterfaces[i].iiFlags;
    1056         pAddress = (sockaddr_in *)&(aInterfaces[i].iiAddress);
    1057         Assert(pAddress);
     1064        sockaddr_in *pAddress = &aInterfaces[i].iiAddress.AddressIn;
    10581065        char szIp[32];
    10591066        RTStrPrintf(szIp, sizeof(szIp), "%s", g_pfninet_ntoa(pAddress->sin_addr));
     
    10611068        VGSvcPropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
    10621069
    1063         pAddress = (sockaddr_in *) & (aInterfaces[i].iiBroadcastAddress);
     1070        pAddress = &aInterfaces[i].iiBroadcastAddress.AddressIn;
    10641071        RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Broadcast", cIfsReported);
    10651072        VGSvcPropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", g_pfninet_ntoa(pAddress->sin_addr));
    10661073
    1067         pAddress = (sockaddr_in *)&(aInterfaces[i].iiNetmask);
     1074        pAddress = (sockaddr_in *)&aInterfaces[i].iiNetmask;
    10681075        RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Netmask", cIfsReported);
    10691076        VGSvcPropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", g_pfninet_ntoa(pAddress->sin_addr));
    10701077
    10711078        RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/Status", cIfsReported);
    1072         VGSvcPropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
     1079        VGSvcPropCacheUpdate(&g_VMInfoPropCache, szPropPath, aInterfaces[i].iiFlags & IFF_UP ? "Up" : "Down");
    10731080
    10741081        if (pAdpInfo)
     
    10941101        cIfsReported++;
    10951102    }
    1096     if (pAdpInfo)
    1097         RTMemFree(pAdpInfo);
     1103
     1104    RTMemFree(pAdpInfo);
    10981105
    10991106#elif defined(RT_OS_HAIKU)
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