Skip to content

Commit df83eab

Browse files
ZhiPeng Lumdroth
ZhiPeng Lu
authored andcommitted
qga: replace GetIfEntry with GetIfEntry2 for interface stats
The data obtained by GetIfEntry is 32 bits, and it may overflow. Thus using GetIfEntry2 instead of GetIfEntry. Signed-off-by: ZhiPeng Lu <[email protected]> *avoid CamelCase variable names *update field names for MIB_IFROW -> MIB_IF_ROW2 *dynamically probe for GetIfIndex2 to deal with older OSs *check return value from get_interface_index Signed-off-by: Michael Roth <[email protected]>
1 parent b2996bb commit df83eab

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

qga/commands-win32.c

+38-16
Original file line numberDiff line numberDiff line change
@@ -1169,24 +1169,46 @@ static DWORD get_interface_index(const char *guid)
11691169
return index;
11701170
}
11711171
}
1172+
1173+
typedef NETIOAPI_API (WINAPI *GetIfEntry2Func)(PMIB_IF_ROW2 Row);
1174+
11721175
static int guest_get_network_stats(const char *name,
1173-
GuestNetworkInterfaceStat *stats)
1176+
GuestNetworkInterfaceStat *stats)
11741177
{
1175-
DWORD if_index = 0;
1176-
MIB_IFROW a_mid_ifrow;
1177-
memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow));
1178-
if_index = get_interface_index(name);
1179-
a_mid_ifrow.dwIndex = if_index;
1180-
if (NO_ERROR == GetIfEntry(&a_mid_ifrow)) {
1181-
stats->rx_bytes = a_mid_ifrow.dwInOctets;
1182-
stats->rx_packets = a_mid_ifrow.dwInUcastPkts;
1183-
stats->rx_errs = a_mid_ifrow.dwInErrors;
1184-
stats->rx_dropped = a_mid_ifrow.dwInDiscards;
1185-
stats->tx_bytes = a_mid_ifrow.dwOutOctets;
1186-
stats->tx_packets = a_mid_ifrow.dwOutUcastPkts;
1187-
stats->tx_errs = a_mid_ifrow.dwOutErrors;
1188-
stats->tx_dropped = a_mid_ifrow.dwOutDiscards;
1189-
return 0;
1178+
OSVERSIONINFO os_ver;
1179+
1180+
os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1181+
GetVersionEx(&os_ver);
1182+
if (os_ver.dwMajorVersion >= 6) {
1183+
MIB_IF_ROW2 a_mid_ifrow;
1184+
GetIfEntry2Func getifentry2_ex;
1185+
DWORD if_index = 0;
1186+
HMODULE module = GetModuleHandle("iphlpapi");
1187+
PVOID func = GetProcAddress(module, "GetIfEntry2");
1188+
1189+
if (func == NULL) {
1190+
return -1;
1191+
}
1192+
1193+
getifentry2_ex = (GetIfEntry2Func)func;
1194+
if_index = get_interface_index(name);
1195+
if (if_index == (DWORD)~0) {
1196+
return -1;
1197+
}
1198+
1199+
memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow));
1200+
a_mid_ifrow.InterfaceIndex = if_index;
1201+
if (NO_ERROR == getifentry2_ex(&a_mid_ifrow)) {
1202+
stats->rx_bytes = a_mid_ifrow.InOctets;
1203+
stats->rx_packets = a_mid_ifrow.InUcastPkts;
1204+
stats->rx_errs = a_mid_ifrow.InErrors;
1205+
stats->rx_dropped = a_mid_ifrow.InDiscards;
1206+
stats->tx_bytes = a_mid_ifrow.OutOctets;
1207+
stats->tx_packets = a_mid_ifrow.OutUcastPkts;
1208+
stats->tx_errs = a_mid_ifrow.OutErrors;
1209+
stats->tx_dropped = a_mid_ifrow.OutDiscards;
1210+
return 0;
1211+
}
11901212
}
11911213
return -1;
11921214
}

0 commit comments

Comments
 (0)