mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Fixed the crash when checking IP addresses
PiperOrigin-RevId: 503048520
This commit is contained in:
committed by
Copybara-Service
parent
a29be214a3
commit
70412b8e3c
@@ -14,11 +14,7 @@
|
||||
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
|
||||
// Windows headers
|
||||
#include <inaddr.h>
|
||||
#include <stdlib.h>
|
||||
#include <strsafe.h>
|
||||
#include <winsock.h>
|
||||
#include <windows.h>
|
||||
|
||||
// Standard C/C++ headers
|
||||
#include <codecvt>
|
||||
@@ -37,8 +33,9 @@
|
||||
#include "internal/platform/bluetooth_utils.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/implementation/crypto.h"
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.Collections.h"
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Connectivity.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "winrt/Windows.Foundation.Collections.h"
|
||||
#include "winrt/Windows.Networking.Connectivity.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
@@ -110,13 +107,65 @@ std::string wstring_to_string(std::wstring wstr) {
|
||||
|
||||
std::vector<std::string> GetIpv4Addresses() {
|
||||
std::vector<std::string> result;
|
||||
auto host_names = NetworkInformation::GetHostNames();
|
||||
for (const auto& host_name : host_names) {
|
||||
if (host_name.IPInformation() != nullptr &&
|
||||
host_name.IPInformation().NetworkAdapter() != nullptr &&
|
||||
host_name.Type() == HostNameType::Ipv4) {
|
||||
result.push_back(winrt::to_string(host_name.ToString()));
|
||||
try {
|
||||
auto host_names = NetworkInformation::GetHostNames();
|
||||
for (const auto& host_name : host_names) {
|
||||
if (host_name.IPInformation() != nullptr &&
|
||||
host_name.IPInformation().NetworkAdapter() != nullptr &&
|
||||
host_name.Type() == HostNameType::Ipv4) {
|
||||
result.push_back(winrt::to_string(host_name.ToString()));
|
||||
}
|
||||
}
|
||||
} catch (std::exception exception) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Cannot get IPv4 addresses. Exception : "
|
||||
<< exception.what();
|
||||
} catch (const winrt::hresult_error& error) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Cannot get IPv4 addresses. WinRT exception: "
|
||||
<< error.code() << ": "
|
||||
<< winrt::to_string(error.message());
|
||||
} catch (...) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exeption.";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> Get4BytesIpv4Addresses() {
|
||||
std::vector<std::string> result;
|
||||
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());
|
||||
// Converts IP address from x.x.x.x to 4 bytes format.
|
||||
in_addr address;
|
||||
address.S_un.S_addr = inet_addr(ipv4_s.c_str());
|
||||
char ipv4_b[5];
|
||||
ipv4_b[0] = address.S_un.S_un_b.s_b1;
|
||||
ipv4_b[1] = address.S_un.S_un_b.s_b2;
|
||||
ipv4_b[2] = address.S_un.S_un_b.s_b3;
|
||||
ipv4_b[3] = address.S_un.S_un_b.s_b4;
|
||||
ipv4_b[4] = 0;
|
||||
std::string ipv4_b_s = std::string(ipv4_b, 4);
|
||||
|
||||
result.push_back(ipv4_b_s);
|
||||
}
|
||||
}
|
||||
} catch (std::exception exception) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Cannot get IPv4 addresses. Exception : "
|
||||
<< exception.what();
|
||||
} catch (const winrt::hresult_error& error) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Cannot get IPv4 addresses. WinRT exception: "
|
||||
<< error.code() << ": "
|
||||
<< winrt::to_string(error.message());
|
||||
} catch (...) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exeption.";
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
#ifndef PLATFORM_IMPL_WINDOWS_UTILS_H_
|
||||
#define PLATFORM_IMPL_WINDOWS_UTILS_H_
|
||||
|
||||
#include <Windows.h>
|
||||
#include <stdio.h>
|
||||
#include <windows.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
@@ -24,8 +23,8 @@
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h"
|
||||
#include "internal/platform/implementation/windows/generated/winrt/base.h"
|
||||
#include "winrt/Windows.Foundation.h"
|
||||
#include "winrt/base.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
@@ -45,6 +44,7 @@ ByteArray Sha256(absl::string_view input, size_t size);
|
||||
|
||||
// Reads the IPv4 addresses
|
||||
std::vector<std::string> GetIpv4Addresses();
|
||||
std::vector<std::string> Get4BytesIpv4Addresses();
|
||||
|
||||
namespace Constants {
|
||||
// The Id of the Service Name SDP attribute
|
||||
|
||||
@@ -227,20 +227,30 @@ fire_and_forget WifiHotspotServerSocket::Listener_ConnectionReceived(
|
||||
}
|
||||
|
||||
std::vector<std::string> WifiHotspotServerSocket::GetIpAddresses() const {
|
||||
std::vector<std::string> result{};
|
||||
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());
|
||||
std::vector<std::string> result;
|
||||
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());
|
||||
|
||||
if (absl::EndsWith(ipv4_s, ".1")) {
|
||||
NEARBY_LOGS(INFO) << "Found Hotspot IP: " << ipv4_s;
|
||||
result.push_back(ipv4_s);
|
||||
if (absl::EndsWith(ipv4_s, ".1")) {
|
||||
NEARBY_LOGS(INFO) << "Found Hotspot IP: " << ipv4_s;
|
||||
result.push_back(ipv4_s);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (std::exception exception) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
|
||||
} catch (const winrt::hresult_error &error) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
|
||||
<< ": " << winrt::to_string(error.message());
|
||||
} catch (...) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exeption.";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -201,9 +201,6 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
|
||||
StreamSocketListener listener,
|
||||
StreamSocketListenerConnectionReceivedEventArgs const& args);
|
||||
|
||||
// Retrieves IP addresses from local machine
|
||||
std::vector<std::string> GetIpAddresses() const;
|
||||
|
||||
mutable absl::Mutex mutex_;
|
||||
absl::CondVar cond_;
|
||||
std::deque<StreamSocket> pending_sockets_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
@@ -40,16 +40,16 @@ WifiLanServerSocket::~WifiLanServerSocket() { Close(); }
|
||||
// Returns the first IP address.
|
||||
std::string WifiLanServerSocket::GetIPAddress() const {
|
||||
if (stream_socket_listener_ == nullptr) {
|
||||
return {};
|
||||
NEARBY_LOGS(ERROR) << "Failed to get IP address due to no server socket.";
|
||||
return "";
|
||||
}
|
||||
|
||||
if (ip_addresses_.empty()) {
|
||||
auto ip_addr = GetIpAddresses();
|
||||
if (ip_addr.empty()) {
|
||||
return {};
|
||||
}
|
||||
return ip_addr.front();
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "Failed to get IP address due to no avaible IP addresses.";
|
||||
return "";
|
||||
}
|
||||
|
||||
return ip_addresses_.front();
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ Exception WifiLanServerSocket::Close() {
|
||||
|
||||
bool WifiLanServerSocket::listen() {
|
||||
// Get current IP addresses of the device.
|
||||
ip_addresses_ = GetIpAddresses();
|
||||
ip_addresses_ = Get4BytesIpv4Addresses();
|
||||
|
||||
if (ip_addresses_.empty()) {
|
||||
NEARBY_LOGS(WARNING) << "failed to start accepting connection without IP "
|
||||
@@ -219,31 +219,5 @@ fire_and_forget WifiLanServerSocket::Listener_ConnectionReceived(
|
||||
return fire_and_forget{};
|
||||
}
|
||||
|
||||
// Retrieves IP addresses from local machine.
|
||||
std::vector<std::string> WifiLanServerSocket::GetIpAddresses() const {
|
||||
std::vector<std::string> result{};
|
||||
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());
|
||||
// Converts ip address from x.x.x.x to 4 bytes format.
|
||||
in_addr address;
|
||||
address.S_un.S_addr = inet_addr(ipv4_s.c_str());
|
||||
char ipv4_b[5];
|
||||
ipv4_b[0] = address.S_un.S_un_b.s_b1;
|
||||
ipv4_b[1] = address.S_un.S_un_b.s_b2;
|
||||
ipv4_b[2] = address.S_un.S_un_b.s_b3;
|
||||
ipv4_b[3] = address.S_un.S_un_b.s_b4;
|
||||
ipv4_b[4] = 0;
|
||||
std::string ipv4_b_s = std::string(ipv4_b, 4);
|
||||
|
||||
result.push_back(ipv4_b_s);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
|
||||
Reference in New Issue
Block a user