Fixed the crash to get network information

PiperOrigin-RevId: 488995745
This commit is contained in:
Guogang Li
2022-11-16 11:40:44 -08:00
committed by Copybara-Service
parent 384d144d6e
commit 656f888e5a
@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <exception>
// #include <cstddef>
#include "absl/strings/str_format.h"
#include "internal/platform/implementation/windows/utils.h"
@@ -101,6 +103,7 @@ void WifiMedium::InitCapability() {
WlanCloseHandle(client_handle, NULL);
}
// TODO(b/259414512): the return type should be optional.
api::WifiInformation& WifiMedium::GetInformation() {
HANDLE client_handle = NULL;
PWLAN_AVAILABLE_NETWORK_LIST pWLAN_AVAILABLE_NETWORK_LIST = NULL;
@@ -192,39 +195,53 @@ api::WifiInformation& WifiMedium::GetInformation() {
WlanCloseHandle(client_handle, NULL);
wifi_information_.ip_address_dot_decimal = GetIpAddress();
wifi_information_.ip_address_4_bytes = ipaddr_dotdecimal_to_4bytes_string(
wifi_information_.ip_address_dot_decimal);
if (!wifi_information_.ip_address_dot_decimal.empty()) {
wifi_information_.ip_address_4_bytes = ipaddr_dotdecimal_to_4bytes_string(
wifi_information_.ip_address_dot_decimal);
}
return wifi_information_;
}
std::string WifiMedium::GetIpAddress() {
auto host_names = NetworkInformation::GetHostNames();
for (auto host_name : host_names) {
if (host_name.IPInformation() != nullptr &&
host_name.IPInformation().NetworkAdapter() != nullptr &&
host_name.Type() == HostNameType::Ipv4) {
std::string ipv4_s = winrt::to_string(host_name.ToString());
NEARBY_LOGS(INFO) << "Found IP: " << ipv4_s;
try {
auto host_names = NetworkInformation::GetHostNames();
for (auto host_name : host_names) {
if (host_name.IPInformation() != nullptr &&
host_name.IPInformation().NetworkAdapter() != nullptr &&
host_name.Type() == HostNameType::Ipv4) {
std::string ipv4_s = winrt::to_string(host_name.ToString());
NEARBY_LOGS(INFO) << "Found IP: " << ipv4_s;
auto profile = host_name.IPInformation()
.NetworkAdapter()
.GetConnectedProfileAsync()
.get();
if (profile != nullptr && profile.IsWlanConnectionProfile()) {
auto profile_details = profile.WlanConnectionProfileDetails();
if (profile_details != nullptr &&
wifi_information_.ssid ==
winrt::to_string(profile_details.GetConnectedSsid())) {
NEARBY_LOGS(INFO)
<< "SSID of this IP matches with this WiFi interface's SSID:"
<< wifi_information_.ssid << ", return this IP";
return ipv4_s;
auto profile = host_name.IPInformation()
.NetworkAdapter()
.GetConnectedProfileAsync()
.get();
if (profile != nullptr && profile.IsWlanConnectionProfile()) {
auto profile_details = profile.WlanConnectionProfileDetails();
if (profile_details != nullptr &&
wifi_information_.ssid ==
winrt::to_string(profile_details.GetConnectedSsid())) {
NEARBY_LOGS(INFO)
<< "SSID of this IP matches with this WiFi interface's SSID:"
<< wifi_information_.ssid << ", return this IP";
return ipv4_s;
}
}
}
}
return {};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {};
} catch (...) {
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exeption.";
return {};
}
return {};
}
} // namespace windows