VirtualBox

Changeset 29852 in vbox for trunk


Ignore:
Timestamp:
May 27, 2010 9:50:33 PM (15 years ago)
Author:
vboxsync
Message:

VBoxService/Windows: report the MAC address of the guest network interfaces to allow the GUI to show the guest IP in the network tooltop

File:
1 edited

Legend:

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

    r29764 r29852  
    2222*******************************************************************************/
    2323#ifdef RT_OS_WINDOWS
    24 #include <winsock2.h>
    25 #include <ws2tcpip.h>
    26 #include <windows.h>
    27 #include <Ntsecapi.h>
     24# include <winsock2.h>
     25# include <iphlpapi.h>
     26# include <ws2tcpip.h>
     27# include <windows.h>
     28# include <Ntsecapi.h>
    2829#else
    2930# define __STDC_LIMIT_MACROS
     
    242243    if (r != STATUS_SUCCESS)
    243244    {
    244         VBoxServiceError("LsaEnumerate failed %lu\n", LsaNtStatusToWinError(r));
     245        VBoxServiceError("VMInfo/Users: LsaEnumerate failed %lu\n", LsaNtStatusToWinError(r));
    245246        return RTErrConvertFromWin32(LsaNtStatusToWinError(r));
    246247    }
     
    290291# endif
    291292    {
    292         VBoxServiceError("Could not set UTMP file! Error: %ld\n", errno);
     293        VBoxServiceError("VMInfo/Users: Could not set UTMP file! Error: %ld\n", errno);
    293294    }
    294295    setutent();
     
    348349
    349350#ifdef RT_OS_WINDOWS
     351    IP_ADAPTER_INFO *pAdpInfo = NULL;
     352
     353# ifndef TARGET_NT4
     354    ULONG cbAdpInfo = sizeof(*pAdpInfo);
     355    pAdpInfo = (IP_ADAPTER_INFO*)RTMemAlloc(cbAdpInfo);
     356    if (!pAdpInfo)
     357    {
     358        VBoxServiceError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
     359        return VERR_NO_MEMORY;
     360    }
     361    DWORD dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
     362    if (dwRet == ERROR_BUFFER_OVERFLOW)
     363    {
     364        IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
     365        if (pAdpInfoNew)
     366        {
     367            pAdpInfo = pAdpInfoNew;
     368            dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
     369        }
     370    }
     371    if (dwRet != ERROR_SUCCESS)
     372    {
     373        if (pAdpInfo)
     374            RTMemFree(pAdpInfo);
     375        VBoxServiceError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
     376        return RTErrConvertFromWin32(dwRet);
     377    }
     378# endif
     379
    350380    SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
    351381    if (sd == SOCKET_ERROR) /* Socket invalid. */
    352382    {
    353         VBoxServiceError("Failed to get a socket: Error %d\n", WSAGetLastError());
     383        VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", WSAGetLastError());
     384        if (pAdpInfo)
     385            RTMemFree(pAdpInfo);
    354386        return RTErrConvertFromWin32(WSAGetLastError());
    355387    }
     
    367399                 0) ==  SOCKET_ERROR)
    368400    {
    369         VBoxServiceError("Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
     401        VBoxServiceError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
     402        if (pAdpInfo)
     403            RTMemFree(pAdpInfo);
    370404        return RTErrConvertFromWin32(WSAGetLastError());
    371405    }
     
    382416        pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
    383417        Assert(pAddress);
     418        char szIp[32];
     419        RTStrPrintf(szIp, sizeof(szIp), "%s", inet_ntoa(pAddress->sin_addr));
    384420        RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/IP", cIfacesReport);
    385         VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
     421        VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
    386422
    387423        pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
     
    395431        RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/Status", cIfacesReport);
    396432        VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
     433
     434# ifndef TARGET_NT4
     435        IP_ADAPTER_INFO *pAdp;
     436        for (pAdp = pAdpInfo; pAdp; pAdp = pAdp->Next)
     437            if (!strcmp(pAdp->IpAddressList.IpAddress.String, szIp))
     438                break;
     439
     440        RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/MAC", cIfacesReport);
     441        if (pAdp)
     442        {
     443            char szMac[32];
     444            RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
     445                        pAdp->Address[0], pAdp->Address[1], pAdp->Address[2],
     446                        pAdp->Address[3], pAdp->Address[4], pAdp->Address[5]);
     447            VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
     448        }
     449        else
     450            VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, NULL);
     451# endif
     452
    397453        cIfacesReport++;
    398454    }
     455    if (pAdpInfo)
     456        RTMemFree(pAdpInfo);
    399457    if (sd >= 0)
    400458        closesocket(sd);
     459
    401460#elif defined(RT_OS_FREEBSD)
    402461    int rc = 0;
     
    407466    if (rc < 0)
    408467    {
    409         VBoxServiceError("Failed to get all interfaces: Error %d\n", errno);
     468        VBoxServiceError("VMInfo/Network: Failed to get all interfaces: Error %d\n", errno);
    410469        return RTErrConvertFromErrno(errno);
    411470    }
     
    475534    if (sd < 0)
    476535    {
    477         VBoxServiceError("Failed to get a socket: Error %d\n", errno);
     536        VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", errno);
    478537        return RTErrConvertFromErrno(errno);
    479538    }
     
    485544    if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
    486545    {
    487         VBoxServiceError("Failed to ioctl(SIOCGIFCONF) on socket: Error %d\n", errno);
     546        VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFCONF) on socket: Error %d\n", errno);
    488547        return RTErrConvertFromErrno(errno);
    489548    }
     
    497556        if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
    498557        {
    499             VBoxServiceError("Failed to ioctl(SIOCGIFFLAGS) on socket: Error %d\n", errno);
     558            VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFFLAGS) on socket: Error %d\n", errno);
    500559            close(sd);
    501560            return RTErrConvertFromErrno(errno);
     
    512571        if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
    513572        {
    514             VBoxServiceError("Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
     573            VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
    515574            close(sd);
    516575            return RTErrConvertFromErrno(errno);
     
    522581        if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
    523582        {
    524             VBoxServiceError("Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
     583            VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
    525584            close(sd);
    526585            return RTErrConvertFromErrno(errno);
     
    541600 #endif
    542601        {
    543             VBoxServiceError("Failed to ioctl(SIOCGIFHWADDR) on socket: Error %d\n", errno);
     602            VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFHWADDR) on socket: Error %d\n", errno);
    544603            close(sd);
    545604            return RTErrConvertFromErrno(errno);
     
    596655    WSADATA wsaData;
    597656    if (WSAStartup(MAKEWORD(2, 2), &wsaData))
    598         VBoxServiceError("WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
     657        VBoxServiceError("VMInfo: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
    599658#endif /* RT_OS_WINDOWS */
    600659
     
    630689        if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
    631690        {
    632             VBoxServiceError("RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
     691            VBoxServiceError("VMInfo: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
    633692            rc = rc2;
    634693            break;
     
    679738        rc = VbglR3GuestPropDisconnect(g_uVMInfoGuestPropSvcClientID);
    680739        if (RT_FAILURE(rc))
    681             VBoxServiceError("Failed to disconnect from guest property service! Error: %Rrc\n", rc);
     740            VBoxServiceError("VMInfo: Failed to disconnect from guest property service! Error: %Rrc\n", rc);
    682741        g_uVMInfoGuestPropSvcClientID = 0;
    683742
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