Changeset 96598 in vbox
- Timestamp:
- Sep 4, 2022 11:15:10 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceVMInfo.cpp
r96407 r96598 943 943 944 944 #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 */ 990 999 SOCKET sd = g_pfnWSASocketA(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); 991 1000 if (sd == SOCKET_ERROR) /* Socket invalid. */ 992 1001 { 993 int wsaErr = g_pfnWSAGetLastError(); 1002 int const wsaErr = g_pfnWSAGetLastError(); 1003 RTMemFree(pAdpInfo); 1004 994 1005 /* Don't complain/bail out with an error if network stack is not up; can happen 995 1006 * on NT4 due to start up when not connected shares dialogs pop up. */ 996 if ( WSAENETDOWN == wsaErr)1007 if (wsaErr == WSAENETDOWN) 997 1008 { 998 1009 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); 1005 1013 return RTErrConvertFromWin32(wsaErr); 1006 1014 } … … 1038 1046 { 1039 1047 VGSvcError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", g_pfnWSAGetLastError()); 1040 if (pAdpInfo) 1041 RTMemFree(pAdpInfo); 1048 RTMemFree(pAdpInfo); 1042 1049 g_pfnclosesocket(sd); 1043 1050 return RTErrConvertFromWin32(g_pfnWSAGetLastError()); … … 1046 1053 int cIfacesSystem = cbReturned / sizeof(INTERFACE_INFO); 1047 1054 1055 /* 1056 * Iterate the inteface list we got back from the TCP/IP, 1057 * using the pAdpInfo list to supply the MAC address. 1058 */ 1048 1059 /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */ 1049 1060 for (int i = 0; i < cIfacesSystem; ++i) 1050 1061 { 1051 sockaddr_in *pAddress;1052 u_long nFlags = 0;1053 1062 if (aInterfaces[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */ 1054 1063 continue; 1055 nFlags = aInterfaces[i].iiFlags; 1056 pAddress = (sockaddr_in *)&(aInterfaces[i].iiAddress); 1057 Assert(pAddress); 1064 sockaddr_in *pAddress = &aInterfaces[i].iiAddress.AddressIn; 1058 1065 char szIp[32]; 1059 1066 RTStrPrintf(szIp, sizeof(szIp), "%s", g_pfninet_ntoa(pAddress->sin_addr)); … … 1061 1068 VGSvcPropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp); 1062 1069 1063 pAddress = (sockaddr_in *) & (aInterfaces[i].iiBroadcastAddress);1070 pAddress = &aInterfaces[i].iiBroadcastAddress.AddressIn; 1064 1071 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Broadcast", cIfsReported); 1065 1072 VGSvcPropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", g_pfninet_ntoa(pAddress->sin_addr)); 1066 1073 1067 pAddress = (sockaddr_in *)& (aInterfaces[i].iiNetmask);1074 pAddress = (sockaddr_in *)&aInterfaces[i].iiNetmask; 1068 1075 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Netmask", cIfsReported); 1069 1076 VGSvcPropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", g_pfninet_ntoa(pAddress->sin_addr)); 1070 1077 1071 1078 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"); 1073 1080 1074 1081 if (pAdpInfo) … … 1094 1101 cIfsReported++; 1095 1102 } 1096 if (pAdpInfo) 1097 1103 1104 RTMemFree(pAdpInfo); 1098 1105 1099 1106 #elif defined(RT_OS_HAIKU)
Note:
See TracChangeset
for help on using the changeset viewer.