UINT MeasureNet::GetBestInterfaceOrByName(const WCHAR* iface) { if (c_Table == nullptr) return 0; if (_wcsicmp(iface, L"BEST") == 0) { DWORD dwBestIndex; if (NO_ERROR == GetBestInterface(INADDR_ANY, &dwBestIndex)) { MIB_IF_ROW2* table = (MIB_IF_ROW2*)c_Table->Table; for (size_t i = 0; i < c_NumOfTables; ++i) { if (table[i].InterfaceIndex == (NET_IFINDEX)dwBestIndex) { if (GetRainmeter().GetDebug()) { LogDebugF(this, L"Using network interface: Number=(%i), Name=\"%s\"", i + 1, table[i].Description); } return (UINT)(i + 1); } } } } else { MIB_IF_ROW2* table = (MIB_IF_ROW2*)c_Table->Table; for (size_t i = 0; i < c_NumOfTables; ++i) { if (_wcsicmp(iface, table[i].Description) == 0) { return (UINT)(i + 1); } } } LogErrorF(this, L"Cannot find interface: \"%s\"", iface); return 0; }
UINT MeasureNet::GetBestInterfaceOrByName(const WCHAR* iface) { if (c_Table == nullptr) return 0; if (_wcsicmp(iface, L"BEST") == 0) { DWORD dwBestIndex; if (NO_ERROR == GetBestInterface(INADDR_ANY, &dwBestIndex)) { if (c_GetIfTable2) { MIB_IF_ROW2* table = (MIB_IF_ROW2*)((MIB_IF_TABLE2*)c_Table)->Table; for (size_t i = 0; i < c_NumOfTables; ++i) { if (table[i].InterfaceIndex == (NET_IFINDEX)dwBestIndex) { if (GetRainmeter().GetDebug()) { LogDebugF(this, L"Using network interface: Number=(%i), Name=\"%s\"", i + 1, table[i].Description); } return (i + 1); } } } else { MIB_IFROW* table = (MIB_IFROW*)((MIB_IFTABLE*)c_Table)->table; for (size_t i = 0; i < c_NumOfTables; ++i) { if (table[i].dwIndex == (NET_IFINDEX)dwBestIndex) { if (GetRainmeter().GetDebug()) { LogDebugF(this, L"Using network interface: Number=(%i), Name=\"%.*S\"", (int)i + 1, table[i].dwDescrLen, (char*)table[i].bDescr); } return (i + 1); } } } } } else { if (c_GetIfTable2) { MIB_IF_ROW2* table = (MIB_IF_ROW2*)((MIB_IF_TABLE2*)c_Table)->Table; for (size_t i = 0; i < c_NumOfTables; ++i) { if (_wcsicmp(iface, table[i].Description) == 0) { return (i + 1); } } } else { MIB_IFROW* table = (MIB_IFROW*)((MIB_IFTABLE*)c_Table)->table; for (size_t i = 0; i < c_NumOfTables; ++i) { if (_wcsicmp(iface, StringUtil::Widen((char*)table[i].bDescr).c_str()) == 0) { return (i + 1); } } } } LogErrorF(this, L"Cannot find interface: \"%s\"", iface); return 0; }
/* ** Reads the tables for all net interfaces ** */ void MeasureNet::UpdateIFTable() { bool logging = false; if (c_GetIfTable2) { if (c_Table) { c_FreeMibTable(c_Table); c_Table = nullptr; } if (c_GetIfTable2((MIB_IF_TABLE2**)&c_Table) == NO_ERROR) { MIB_IF_TABLE2* ifTable = (MIB_IF_TABLE2*)c_Table; if (c_NumOfTables != ifTable->NumEntries) { c_NumOfTables = ifTable->NumEntries; logging = true; } if (GetRainmeter().GetDebug() && logging) { LogDebug(L"------------------------------"); LogDebugF(L"* NETWORK-INTERFACE: Count=%i", c_NumOfTables); for (size_t i = 0; i < c_NumOfTables; ++i) { const WCHAR* type = L"Other"; switch (ifTable->Table[i].Type) { case IF_TYPE_ETHERNET_CSMACD: type = L"Ethernet"; break; case IF_TYPE_PPP: type = L"PPP"; break; case IF_TYPE_SOFTWARE_LOOPBACK: type = L"Loopback"; break; case IF_TYPE_IEEE80211: type = L"IEEE802.11"; break; case IF_TYPE_TUNNEL: type = L"Tunnel"; break; case IF_TYPE_IEEE1394: type = L"IEEE1394"; break; } LogDebugF(L"%i: %s", (int)i + 1, ifTable->Table[i].Description); LogDebugF(L" Alias: %s", ifTable->Table[i].Alias); LogDebugF(L" Type=%s(%i), Hardware=%s, Filter=%s", type, ifTable->Table[i].Type, (ifTable->Table[i].InterfaceAndOperStatusFlags.HardwareInterface == 1) ? L"Yes" : L"No", (ifTable->Table[i].InterfaceAndOperStatusFlags.FilterInterface == 1) ? L"Yes" : L"No"); } LogDebug(L"------------------------------"); } } else { // Something's wrong. Unable to get the table. c_Table = nullptr; c_NumOfTables = 0; } } else { DWORD ret, size = 0; MIB_IFTABLE* ifTable = (MIB_IFTABLE*)c_Table; if ((ret = GetIfTable(ifTable, &size, FALSE)) == ERROR_INSUFFICIENT_BUFFER) { delete [] c_Table; c_Table = new BYTE[size]; ifTable = (MIB_IFTABLE*)c_Table; ret = GetIfTable(ifTable, &size, FALSE); } if (ret == NO_ERROR) { if (c_NumOfTables != ifTable->dwNumEntries) { c_NumOfTables = ifTable->dwNumEntries; logging = true; } if (GetRainmeter().GetDebug() && logging) { LogDebug(L"------------------------------"); LogDebugF(L"* NETWORK-INTERFACE: Count=%i", c_NumOfTables); for (size_t i = 0; i < c_NumOfTables; ++i) { const WCHAR* type = L""; switch (ifTable->table[i].dwType) { case IF_TYPE_ETHERNET_CSMACD: type = L"Ethernet"; break; case IF_TYPE_PPP: type = L"PPP"; break; case IF_TYPE_SOFTWARE_LOOPBACK: type = L"Loopback"; break; case IF_TYPE_IEEE80211: type = L"IEEE802.11"; break; case IF_TYPE_TUNNEL: type = L"Tunnel"; break; case IF_TYPE_IEEE1394: type = L"IEEE1394"; break; default: type = L"Other"; break; } LogDebugF(L"%i: %.*S", (int)i + 1, ifTable->table[i].dwDescrLen, (char*)ifTable->table[i].bDescr); LogDebugF(L" Type=%s(%i)", type, ifTable->table[i].dwType); } LogDebug(L"------------------------------"); } } else { // Something's wrong. Unable to get the table. delete [] c_Table; c_Table = nullptr; c_NumOfTables = 0; } } }
void CommandHandler::DoWriteKeyValueBang(std::vector<std::wstring>& args, Skin* skin) { if (args.size() == 3 && skin) { // Add the skin file path to the args args.push_back(skin->GetFilePath()); } else if (args.size() < 4) { LogErrorF(skin, L"!WriteKeyValue: Invalid parameters"); return; } std::wstring& strIniFile = args[3]; if (skin) { skin->MakePathAbsolute(strIniFile); } const WCHAR* iniFile = strIniFile.c_str(); if (strIniFile.find(L"..\\") != std::wstring::npos || strIniFile.find(L"../") != std::wstring::npos) { LogErrorF(skin, L"!WriteKeyValue: Illegal path: %s", iniFile); return; } if (_wcsnicmp(iniFile, GetRainmeter().m_SkinPath.c_str(), GetRainmeter().m_SkinPath.size()) != 0 && _wcsnicmp(iniFile, GetRainmeter().m_SettingsPath.c_str(), GetRainmeter().m_SettingsPath.size()) != 0) { LogErrorF(skin, L"!WriteKeyValue: Illegal path: %s", iniFile); return; } // Verify whether the file exists. if (_waccess(iniFile, 0) == -1) { LogErrorF(skin, L"!WriteKeyValue: File not found: %s", iniFile); return; } // Verify whether the file is read-only. DWORD attr = GetFileAttributes(iniFile); if (attr == -1 || (attr & FILE_ATTRIBUTE_READONLY)) { LogWarningF(skin, L"!WriteKeyValue: File is read-only: %s", iniFile); return; } // Avoid "IniFileMapping" System::UpdateIniFileMappingList(); std::wstring strIniWrite = System::GetTemporaryFile(strIniFile); if (strIniWrite.size() == 1 && strIniWrite[0] == L'?') // error occurred { return; } bool temporary = !strIniWrite.empty(); if (temporary) { if (GetRainmeter().GetDebug()) { LogDebugF(skin, L"!WriteKeyValue: Writing to: %s (Temp: %s)", iniFile, strIniWrite.c_str()); } } else { if (GetRainmeter().GetDebug()) { LogDebugF(skin, L"!WriteKeyValue: Writing to: %s", iniFile); } strIniWrite = strIniFile; } const WCHAR* iniWrite = strIniWrite.c_str(); const WCHAR* section = args[0].c_str(); const WCHAR* key = args[1].c_str(); const std::wstring& strValue = args[2]; bool formula = false; BOOL write = 0; if (skin) { double value; formula = skin->GetParser().ParseFormula(strValue, &value); if (formula) { WCHAR buffer[256]; int len = _snwprintf_s(buffer, _TRUNCATE, L"%.5f", value); Measure::RemoveTrailingZero(buffer, len); write = WritePrivateProfileString(section, key, buffer, iniWrite); } } if (!formula) { write = WritePrivateProfileString(section, key, strValue.c_str(), iniWrite); } if (temporary) { if (write != 0) { WritePrivateProfileString(nullptr, nullptr, nullptr, iniWrite); // FLUSH // Copy the file back. if (!System::CopyFiles(strIniWrite, strIniFile)) { LogErrorF(skin, L"!WriteKeyValue: Failed to copy temporary file to original filepath: %s (Temp: %s)", iniFile, iniWrite); } } else // failed { LogErrorF(skin, L"!WriteKeyValue: Failed to write to: %s (Temp: %s)", iniFile, iniWrite); } // Remove the temporary file. System::RemoveFile(strIniWrite); } else { if (write == 0) // failed { LogErrorF(skin, L"!WriteKeyValue: Failed to write to: %s", iniFile); } } }
/* ** Reads the tables for all net interfaces ** */ void MeasureNet::UpdateIFTable() { bool logging = false; if (c_Table) { FreeMibTable(c_Table); c_Table = nullptr; } if (GetIfTable2(&c_Table) == NO_ERROR) { if (c_NumOfTables != c_Table->NumEntries) { c_NumOfTables = c_Table->NumEntries; logging = true; } if (GetRainmeter().GetDebug() && logging) { LogDebug(L"------------------------------"); LogDebugF(L"* NETWORK-INTERFACE: Count=%i", c_NumOfTables); for (size_t i = 0; i < c_NumOfTables; ++i) { const WCHAR* type = L"Other"; switch (c_Table->Table[i].Type) { case IF_TYPE_ETHERNET_CSMACD: type = L"Ethernet"; break; case IF_TYPE_PPP: type = L"PPP"; break; case IF_TYPE_SOFTWARE_LOOPBACK: type = L"Loopback"; break; case IF_TYPE_IEEE80211: type = L"IEEE802.11"; break; case IF_TYPE_TUNNEL: type = L"Tunnel"; break; case IF_TYPE_IEEE1394: type = L"IEEE1394"; break; } LogDebugF(L"%i: %s", (int)i + 1, c_Table->Table[i].Description); LogDebugF(L" Alias: %s", c_Table->Table[i].Alias); LogDebugF(L" Type=%s(%i), Hardware=%s, Filter=%s", type, c_Table->Table[i].Type, (c_Table->Table[i].InterfaceAndOperStatusFlags.HardwareInterface == 1) ? L"Yes" : L"No", (c_Table->Table[i].InterfaceAndOperStatusFlags.FilterInterface == 1) ? L"Yes" : L"No"); } LogDebug(L"------------------------------"); } } else { // Something's wrong. Unable to get the table. c_Table = nullptr; c_NumOfTables = 0; } }