mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Switch NetworkMonitor to use NetworkInfo.
PiperOrigin-RevId: 822135239
This commit is contained in:
committed by
Copybara-Service
parent
3878ce99c8
commit
cac20257eb
@@ -27,6 +27,7 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/base/no_destructor.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/implementation/windows/string_utils.h"
|
||||
@@ -59,6 +60,11 @@ void AddIpUnicastAddresses(IP_ADAPTER_UNICAST_ADDRESS* unicast_addresses,
|
||||
|
||||
} // namespace
|
||||
|
||||
NetworkInfo& NetworkInfo::GetNetworkInfo() {
|
||||
static absl::NoDestructor<NetworkInfo> kNetworkInfo;
|
||||
return *kNetworkInfo;
|
||||
}
|
||||
|
||||
bool NetworkInfo::Refresh() {
|
||||
static constexpr int kDefaultBufferSize = 15 * 1024; // default to 15K buffer
|
||||
static constexpr int kMaxBufferSize =
|
||||
@@ -94,8 +100,10 @@ bool NetworkInfo::Refresh() {
|
||||
IP_ADAPTER_ADDRESSES* next_address = addresses;
|
||||
while (next_address != nullptr) {
|
||||
if (next_address->OperStatus != IfOperStatusUp ||
|
||||
next_address->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
|
||||
// Skip down and loopback interfaces.
|
||||
next_address->IfType == IF_TYPE_SOFTWARE_LOOPBACK ||
|
||||
next_address->FirstUnicastAddress == nullptr) {
|
||||
// Skip down and loopback interfaces as well as interfaces without
|
||||
// addresses.
|
||||
next_address = next_address->Next;
|
||||
continue;
|
||||
}
|
||||
@@ -105,16 +113,19 @@ bool NetworkInfo::Refresh() {
|
||||
});
|
||||
if (next_address->IfType == IF_TYPE_ETHERNET_CSMACD) {
|
||||
it->type = InterfaceType::kEthernet;
|
||||
VLOG(1) << "Found ethernet interface: " << next_address->AdapterName
|
||||
<< " index: " << next_address->IfIndex;
|
||||
VLOG(1) << "Found ethernet interface: "
|
||||
<< string_utils::WideStringToString(next_address->Description)
|
||||
<< ", index: " << next_address->IfIndex;
|
||||
} else if (next_address->IfType == IF_TYPE_IEEE80211) {
|
||||
it->type = InterfaceType::kWifi;
|
||||
VLOG(1) << "Found wifi interface: " << next_address->AdapterName
|
||||
<< " index: " << next_address->IfIndex;
|
||||
VLOG(1) << "Found wifi interface: "
|
||||
<< string_utils::WideStringToString(next_address->Description)
|
||||
<< ", index: " << next_address->IfIndex;
|
||||
} else {
|
||||
it->type = InterfaceType::kOther;
|
||||
VLOG(1) << "Found other interface: " << next_address->AdapterName
|
||||
<< " index: " << next_address->IfIndex;
|
||||
VLOG(1) << "Found other interface: "
|
||||
<< string_utils::WideStringToString(next_address->Description)
|
||||
<< ", index: " << next_address->IfIndex;
|
||||
}
|
||||
AddIpUnicastAddresses(next_address->FirstUnicastAddress, *it);
|
||||
next_address = next_address->Next;
|
||||
|
||||
@@ -48,6 +48,9 @@ class NetworkInfo {
|
||||
std::vector<sockaddr_storage> ipv6_addresses;
|
||||
};
|
||||
|
||||
// Returns the singleton instance of this class.
|
||||
static NetworkInfo& GetNetworkInfo();
|
||||
|
||||
// Refreshes the network interfaces information keep by this class.
|
||||
// Returns true on success.
|
||||
bool Refresh();
|
||||
|
||||
@@ -62,9 +62,9 @@ void AddIpUnicastAddresses(IP_ADAPTER_UNICAST_ADDRESS* unicast_addresses,
|
||||
}
|
||||
}
|
||||
|
||||
void GetIpAddressesNative(int family, std::vector<std::string>& wifi_addresses,
|
||||
std::vector<std::string>& ethernet_addresses,
|
||||
std::vector<std::string>& other_addresses) {
|
||||
void GetIpAddresses(int family, std::vector<std::string>& wifi_addresses,
|
||||
std::vector<std::string>& ethernet_addresses,
|
||||
std::vector<std::string>& other_addresses) {
|
||||
static constexpr int kDefaultBufferSize = 15 * 1024; // default to 15K buffer
|
||||
static constexpr int kMaxBufferSize =
|
||||
45 * 1024; // Try to increase buffer 2 times.
|
||||
@@ -157,30 +157,12 @@ std::string ipaddr_dotdecimal_to_4bytes_string(std::string ipv4_s) {
|
||||
return std::string(ipv4_b, 4);
|
||||
}
|
||||
|
||||
void GetIpv4Addresses(std::vector<std::string>& wifi_addresses,
|
||||
std::vector<std::string>& ethernet_addresses,
|
||||
std::vector<std::string>& other_addresses) {
|
||||
GetIpAddressesNative(AF_INET, wifi_addresses, ethernet_addresses,
|
||||
other_addresses);
|
||||
}
|
||||
|
||||
std::vector<std::string> GetIpv4Addresses() {
|
||||
std::vector<std::string> result;
|
||||
GetIpv4Addresses(result, result, result);
|
||||
GetIpAddresses(AF_INET, result, result, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void GetConnectedNetworks(bool& is_wifi_connected, bool& is_ethernet_connected,
|
||||
bool& is_other_connected) {
|
||||
std::vector<std::string> wifi_addresses;
|
||||
std::vector<std::string> ethernet_addresses;
|
||||
std::vector<std::string> other_addresses;
|
||||
GetIpv4Addresses(wifi_addresses, ethernet_addresses, other_addresses);
|
||||
is_wifi_connected = !wifi_addresses.empty();
|
||||
is_ethernet_connected = !ethernet_addresses.empty();
|
||||
is_other_connected = !other_addresses.empty();
|
||||
}
|
||||
|
||||
Uuid winrt_guid_to_nearby_uuid(const ::winrt::guid& guid) {
|
||||
int64_t data1 = guid.Data1;
|
||||
int64_t data2 = guid.Data2;
|
||||
|
||||
@@ -42,13 +42,6 @@ ByteArray Sha256(absl::string_view input, size_t size);
|
||||
|
||||
// Reads the IPv4 addresses
|
||||
std::vector<std::string> GetIpv4Addresses();
|
||||
void GetIpv4Addresses(std::vector<std::string>& wifi_addresses,
|
||||
std::vector<std::string>& ethernet_addresses,
|
||||
std::vector<std::string>& other_addresses);
|
||||
|
||||
// Returns the connection state of the different network types.
|
||||
void GetConnectedNetworks(bool& is_wifi_connected, bool& is_ethernet_connected,
|
||||
bool& is_other_connected);
|
||||
|
||||
// Help methods to convert between Uuid and winrt::guid
|
||||
Uuid winrt_guid_to_nearby_uuid(const ::winrt::guid& guid);
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/implementation/windows/network_info.h"
|
||||
#include "internal/platform/implementation/windows/string_utils.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/wifi_credential.h"
|
||||
@@ -83,7 +84,8 @@ std::string ReasonCodeToString(DWORD reason_code) {
|
||||
|
||||
} // namespace
|
||||
|
||||
WifiHotspotNative::WifiHotspotNative() {
|
||||
WifiHotspotNative::WifiHotspotNative()
|
||||
: network_info_(NetworkInfo::GetNetworkInfo()) {
|
||||
// Open WLAN handle
|
||||
DWORD negotiated_version;
|
||||
DWORD result = WlanOpenHandle(/*dwClientVersion=*/2, /*pReserved=*/nullptr,
|
||||
@@ -539,8 +541,11 @@ bool WifiHotspotNative::RenewIpv4Address() const {
|
||||
return false;
|
||||
}
|
||||
NET_LUID luid;
|
||||
ConvertInterfaceGuidToLuid(&interface_guid, &luid);
|
||||
return network_info_.RenewIpv4Address(luid);
|
||||
if (ConvertInterfaceGuidToLuid(&interface_guid, &luid) == NO_ERROR) {
|
||||
return network_info_.RenewIpv4Address(luid);
|
||||
}
|
||||
LOG(ERROR) << "Failed to convert interface guid to luid.";
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace nearby::windows
|
||||
|
||||
@@ -90,6 +90,7 @@ class WifiHotspotNative {
|
||||
void TriggerConnected();
|
||||
void TriggerNetworkRefreshed();
|
||||
|
||||
NetworkInfo& network_info_;
|
||||
mutable absl::Mutex mutex_;
|
||||
|
||||
HANDLE wifi_ = nullptr;
|
||||
@@ -97,7 +98,6 @@ class WifiHotspotNative {
|
||||
std::unique_ptr<CountDownLatch> connect_latch_;
|
||||
std::unique_ptr<CountDownLatch> scan_latch_;
|
||||
std::wstring backup_profile_name_;
|
||||
NetworkInfo network_info_;
|
||||
};
|
||||
|
||||
} // namespace nearby::windows
|
||||
|
||||
Reference in New Issue
Block a user