Commit 247df259 authored by Bailey Berro's avatar Bailey Berro Committed by Commit Bot

Add NetBiosClient to NetBiosHostLocator

This continues the implementation of NetBiosHostLocator by adding
a list of NetBiosClients. Each NetBiosClient corresponds to one NetBios
Name Request query.

Bug: chromium:757625
Change-Id: Iadd6b3fe59fe54a4f4d38fb604af175779c0aff9
Reviewed-on: https://chromium-review.googlesource.com/1072415
Commit-Queue: Bailey Berro <baileyberro@chromium.org>
Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566042}
parent 24a00eb9
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/chromeos/smb_client/discovery/netbios_host_locator.h" #include "chrome/browser/chromeos/smb_client/discovery/netbios_host_locator.h"
#include <memory>
#include "net/base/network_change_notifier.h" #include "net/base/network_change_notifier.h"
namespace chromeos { namespace chromeos {
...@@ -31,20 +33,65 @@ bool ShouldUseInterface(const net::NetworkInterface& interface) { ...@@ -31,20 +33,65 @@ bool ShouldUseInterface(const net::NetworkInterface& interface) {
interface.type == net::NetworkChangeNotifier::CONNECTION_WIFI); interface.type == net::NetworkChangeNotifier::CONNECTION_WIFI);
} }
NetBiosHostLocator::NetBiosHostLocator(GetInterfacesFunction get_interfaces) NetBiosHostLocator::NetBiosHostLocator(GetInterfacesFunction get_interfaces,
: get_interfaces_(std::move(get_interfaces)) {} NetBiosClientFactory client_factory)
: get_interfaces_(std::move(get_interfaces)),
client_factory_(std::move(client_factory)) {}
NetBiosHostLocator::~NetBiosHostLocator() = default; NetBiosHostLocator::~NetBiosHostLocator() = default;
void NetBiosHostLocator::FindHosts(FindHostsCallback callback) { void NetBiosHostLocator::FindHosts(FindHostsCallback callback) {
DCHECK(!running_);
DCHECK(callback); DCHECK(callback);
callback_ = std::move(callback); callback_ = std::move(callback);
running_ = true;
net::NetworkInterfaceList network_interface_list = GetNetworkInterfaceList();
for (const auto& interface : network_interface_list) {
if (ShouldUseInterface(interface)) {
FindHostsOnInterface(interface);
}
}
if (netbios_clients_.empty()) {
// No NetBiosClients were created since there were either no interfaces or
// no valid interfaces.
running_ = false;
std::move(callback_).Run(false /* success */, results_);
}
} }
net::NetworkInterfaceList NetBiosHostLocator::GetNetworkInterfaceList() { net::NetworkInterfaceList NetBiosHostLocator::GetNetworkInterfaceList() {
return get_interfaces_.Run(); return get_interfaces_.Run();
} }
void NetBiosHostLocator::FindHostsOnInterface(
const net::NetworkInterface& interface) {
net::IPAddress broadcast_address = CalculateBroadcastAddress(interface);
netbios_clients_.push_back(CreateClient());
ExecuteNameRequest(broadcast_address);
}
std::unique_ptr<NetBiosClientInterface> NetBiosHostLocator::CreateClient()
const {
return client_factory_.Run();
}
void NetBiosHostLocator::ExecuteNameRequest(
const net::IPAddress& broadcast_address) {
netbios_clients_.back()->ExecuteNameRequest(
broadcast_address, transaction_id_++,
base::BindRepeating(&NetBiosHostLocator::PacketReceived,
base::Unretained(this)));
}
void NetBiosHostLocator::PacketReceived(const std::vector<uint8_t>& packet,
uint16_t transaction_id,
const net::IPEndPoint& sender_ip) {
NOTREACHED();
}
} // namespace smb_client } // namespace smb_client
} // namespace chromeos } // namespace chromeos
...@@ -5,11 +5,13 @@ ...@@ -5,11 +5,13 @@
#ifndef CHROME_BROWSER_CHROMEOS_SMB_CLIENT_DISCOVERY_NETBIOS_HOST_LOCATOR_H_ #ifndef CHROME_BROWSER_CHROMEOS_SMB_CLIENT_DISCOVERY_NETBIOS_HOST_LOCATOR_H_
#define CHROME_BROWSER_CHROMEOS_SMB_CLIENT_DISCOVERY_NETBIOS_HOST_LOCATOR_H_ #define CHROME_BROWSER_CHROMEOS_SMB_CLIENT_DISCOVERY_NETBIOS_HOST_LOCATOR_H_
#include <list>
#include <string> #include <string>
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/smb_client/discovery/host_locator.h" #include "chrome/browser/chromeos/smb_client/discovery/host_locator.h"
#include "chrome/browser/chromeos/smb_client/discovery/netbios_client_interface.h"
#include "net/base/network_interfaces.h" #include "net/base/network_interfaces.h"
namespace chromeos { namespace chromeos {
...@@ -28,8 +30,12 @@ class NetBiosHostLocator : public HostLocator, ...@@ -28,8 +30,12 @@ class NetBiosHostLocator : public HostLocator,
public: public:
using GetInterfacesFunction = using GetInterfacesFunction =
base::RepeatingCallback<net::NetworkInterfaceList()>; base::RepeatingCallback<net::NetworkInterfaceList()>;
using NetBiosClientFactory =
base::RepeatingCallback<std::unique_ptr<NetBiosClientInterface>()>;
NetBiosHostLocator(GetInterfacesFunction get_interfaces,
NetBiosClientFactory client_factory);
explicit NetBiosHostLocator(GetInterfacesFunction get_interfaces);
~NetBiosHostLocator() override; ~NetBiosHostLocator() override;
// HostLocator override. // HostLocator override.
...@@ -39,9 +45,32 @@ class NetBiosHostLocator : public HostLocator, ...@@ -39,9 +45,32 @@ class NetBiosHostLocator : public HostLocator,
// Returns a list of network interfaces on the device. // Returns a list of network interfaces on the device.
net::NetworkInterfaceList GetNetworkInterfaceList(); net::NetworkInterfaceList GetNetworkInterfaceList();
// Finds hosts on |interface| by constructing a NetBiosClient, performing a
// NetBios Name Request for the interface.
void FindHostsOnInterface(const net::NetworkInterface& interface);
// Creates a NetBiosClient using the |client_factory_|.
std::unique_ptr<NetBiosClientInterface> CreateClient() const;
// Executes a name request transaction for |broadcast_address| using the most
// recently added NetBiosClient in |netbios_clients_|.
void ExecuteNameRequest(const net::IPAddress& broadcast_address);
// Callback handler for packets received by the |netbios_clients_|.
void PacketReceived(const std::vector<uint8_t>& packet,
uint16_t transaction_id,
const net::IPEndPoint& sender_ip);
bool running_ = false;
GetInterfacesFunction get_interfaces_; GetInterfacesFunction get_interfaces_;
NetBiosClientFactory client_factory_;
FindHostsCallback callback_; FindHostsCallback callback_;
HostMap results_; HostMap results_;
uint16_t transaction_id_ = 0;
// |netbios_clients_| is a container for storing NetBios clients that are
// currently performing a NetBios Name Request so that they do not go out of
// scope. One NetBiosClient exists for each network interface on the device.
std::list<std::unique_ptr<NetBiosClientInterface>> netbios_clients_;
DISALLOW_COPY_AND_ASSIGN(NetBiosHostLocator); DISALLOW_COPY_AND_ASSIGN(NetBiosHostLocator);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment