summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--content/browser/renderer_host/p2p/socket_dispatcher_host.cc83
-rw-r--r--content/browser/renderer_host/p2p/socket_dispatcher_host.h16
-rw-r--r--content/common/p2p_messages.h11
-rw-r--r--content/renderer/p2p/ipc_network_manager.cc23
-rw-r--r--content/renderer/p2p/socket_dispatcher.cc10
-rw-r--r--content/renderer/p2p/socket_dispatcher.h17
-rw-r--r--net/base/net_util.h2
7 files changed, 70 insertions, 92 deletions
diff --git a/content/browser/renderer_host/p2p/socket_dispatcher_host.cc b/content/browser/renderer_host/p2p/socket_dispatcher_host.cc
index 0747d5d..ade3411 100644
--- a/content/browser/renderer_host/p2p/socket_dispatcher_host.cc
+++ b/content/browser/renderer_host/p2p/socket_dispatcher_host.cc
@@ -8,34 +8,6 @@
#include "content/browser/renderer_host/p2p/socket_host.h"
#include "content/common/p2p_messages.h"
-namespace {
-
-// This function returns address of the first IPv4 enabled network
-// interface it finds. This address is used for all sockets.
-//
-// TODO(sergeyu): This approach works only in the simplest case when
-// host has only one network connection. Instead of binding all
-// connections to this interface we must provide list of interfaces to
-// the renderer, and let the PortAllocater in the renderer process
-// choose local address.
-bool GetLocalAddress(net::IPEndPoint* addr) {
- net::NetworkInterfaceList networks;
- if (!GetNetworkList(&networks))
- return false;
-
- for (net::NetworkInterfaceList::iterator it = networks.begin();
- it != networks.end(); ++it) {
- if (it->address.size() == net::kIPv4AddressSize) {
- *addr = net::IPEndPoint(it->address, 0);
- return true;
- }
- }
-
- return false;
-}
-
-} // namespace
-
P2PSocketDispatcherHost::P2PSocketDispatcherHost() {
}
@@ -55,9 +27,10 @@ void P2PSocketDispatcherHost::OnDestruct() const {
}
bool P2PSocketDispatcherHost::OnMessageReceived(const IPC::Message& message,
- bool* message_was_ok) {
+ bool* message_was_ok) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketDispatcherHost, message, *message_was_ok)
+ IPC_MESSAGE_HANDLER(P2PHostMsg_GetNetworkList, OnGetNetworkList)
IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket)
IPC_MESSAGE_HANDLER(P2PHostMsg_AcceptIncomingTcpConnection,
OnAcceptIncomingTcpConnection)
@@ -78,60 +51,46 @@ P2PSocketHost* P2PSocketDispatcherHost::LookupSocket(
return it->second;
}
-void P2PSocketDispatcherHost::OnCreateSocket(
- const IPC::Message& msg, P2PSocketType type, int socket_id,
- const net::IPEndPoint& local_address,
- const net::IPEndPoint& remote_address) {
+void P2PSocketDispatcherHost::OnGetNetworkList(const IPC::Message& msg) {
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE, NewRunnableMethod(
- this, &P2PSocketDispatcherHost::GetLocalAddressAndCreateSocket,
- msg.routing_id(), type, socket_id, remote_address));
+ this, &P2PSocketDispatcherHost::DoGetNetworkList, msg.routing_id()));
}
-void P2PSocketDispatcherHost::GetLocalAddressAndCreateSocket(
- int32 routing_id, P2PSocketType type, int socket_id,
- const net::IPEndPoint& remote_address) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
-
- net::IPEndPoint local_address;
- if (!GetLocalAddress(&local_address)) {
- LOG(ERROR) << "Failed to get local network address.";
- BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableMethod(this, &P2PSocketDispatcherHost::Send,
- new P2PMsg_OnError(routing_id, socket_id)));
- return;
- }
-
+void P2PSocketDispatcherHost::DoGetNetworkList(int routing_id) {
+ net::NetworkInterfaceList list;
+ net::GetNetworkList(&list);
BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableMethod(this, &P2PSocketDispatcherHost::FinishCreateSocket,
- routing_id, local_address, type, socket_id,
- remote_address));
+ BrowserThread::IO, FROM_HERE, NewRunnableMethod(
+ this, &P2PSocketDispatcherHost::SendNetworkList, routing_id, list));
}
-void P2PSocketDispatcherHost::FinishCreateSocket(
- int32 routing_id, const net::IPEndPoint& local_address, P2PSocketType type,
- int socket_id, const net::IPEndPoint& remote_address) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+void P2PSocketDispatcherHost::SendNetworkList(
+ int routing_id, const net::NetworkInterfaceList& list) {
+ Send(new P2PMsg_NetworkList(routing_id, list));
+}
- if (LookupSocket(routing_id, socket_id)) {
+void P2PSocketDispatcherHost::OnCreateSocket(
+ const IPC::Message& msg, P2PSocketType type, int socket_id,
+ const net::IPEndPoint& local_address,
+ const net::IPEndPoint& remote_address) {
+ if (LookupSocket(msg.routing_id(), socket_id)) {
LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket "
"that already exists.";
return;
}
scoped_ptr<P2PSocketHost> socket(
- P2PSocketHost::Create(this, routing_id, socket_id, type));
+ P2PSocketHost::Create(this, msg.routing_id(), socket_id, type));
if (!socket.get()) {
- Send(new P2PMsg_OnError(routing_id, socket_id));
+ Send(new P2PMsg_OnError(msg.routing_id(), socket_id));
return;
}
if (socket->Init(local_address, remote_address)) {
sockets_.insert(std::pair<ExtendedSocketId, P2PSocketHost*>(
- ExtendedSocketId(routing_id, socket_id), socket.release()));
+ ExtendedSocketId(msg.routing_id(), socket_id), socket.release()));
}
}
diff --git a/content/browser/renderer_host/p2p/socket_dispatcher_host.h b/content/browser/renderer_host/p2p/socket_dispatcher_host.h
index f2d041a..dffc111 100644
--- a/content/browser/renderer_host/p2p/socket_dispatcher_host.h
+++ b/content/browser/renderer_host/p2p/socket_dispatcher_host.h
@@ -31,6 +31,7 @@ class P2PSocketDispatcherHost : public BrowserMessageFilter {
P2PSocketHost* LookupSocket(int32 routing_id, int socket_id);
// Handlers for the messages coming from the renderer.
+ void OnGetNetworkList(const IPC::Message& msg);
void OnCreateSocket(const IPC::Message& msg,
P2PSocketType type,
int socket_id,
@@ -45,19 +46,8 @@ class P2PSocketDispatcherHost : public BrowserMessageFilter {
const std::vector<char>& data);
void OnDestroySocket(const IPC::Message& msg, int socket_id);
- // Helpers for OnCreateSocket().
- //
- // TODO(sergeyu): Remove these methods. Use |local_address| passed
- // in OnCreateSocket().
- void GetLocalAddressAndCreateSocket(int32 routing_id,
- P2PSocketType type,
- int socket_id,
- const net::IPEndPoint& remote_address);
- void FinishCreateSocket(int32 routing_id,
- const net::IPEndPoint& local_address,
- P2PSocketType type,
- int socket_id,
- const net::IPEndPoint& remote_address);
+ void DoGetNetworkList(int routing_id);
+ void SendNetworkList(int routing_id, const net::NetworkInterfaceList& list);
SocketsMap sockets_;
diff --git a/content/common/p2p_messages.h b/content/common/p2p_messages.h
index 1d10e01..e5010dd 100644
--- a/content/common/p2p_messages.h
+++ b/content/common/p2p_messages.h
@@ -8,13 +8,22 @@
#include "content/common/p2p_sockets.h"
#include "ipc/ipc_message_macros.h"
#include "net/base/ip_endpoint.h"
+#include "net/base/net_util.h"
#define IPC_MESSAGE_START P2PMsgStart
IPC_ENUM_TRAITS(P2PSocketType)
+IPC_STRUCT_TRAITS_BEGIN(net::NetworkInterface)
+ IPC_STRUCT_TRAITS_MEMBER(name)
+ IPC_STRUCT_TRAITS_MEMBER(address)
+IPC_STRUCT_TRAITS_END()
+
// P2P Socket messages sent from the browser to the renderer.
+IPC_MESSAGE_ROUTED1(P2PMsg_NetworkList,
+ net::NetworkInterfaceList /* networks */)
+
IPC_MESSAGE_ROUTED2(P2PMsg_OnSocketCreated,
int /* socket_id */,
net::IPEndPoint /* socket_address */)
@@ -33,6 +42,8 @@ IPC_MESSAGE_ROUTED3(P2PMsg_OnDataReceived,
// P2P Socket messages sent from the renderer to the browser.
+IPC_MESSAGE_ROUTED0(P2PHostMsg_GetNetworkList)
+
IPC_MESSAGE_ROUTED4(P2PHostMsg_CreateSocket,
P2PSocketType /* type */,
int /* socket_id */,
diff --git a/content/renderer/p2p/ipc_network_manager.cc b/content/renderer/p2p/ipc_network_manager.cc
index 204a699..3cb7124 100644
--- a/content/renderer/p2p/ipc_network_manager.cc
+++ b/content/renderer/p2p/ipc_network_manager.cc
@@ -4,6 +4,10 @@
#include "content/renderer/p2p/ipc_network_manager.h"
+#include "net/base/net_util.h"
+#include "net/base/sys_byteorder.h"
+#include "content/renderer/p2p/socket_dispatcher.h"
+
IpcNetworkManager::IpcNetworkManager(P2PSocketDispatcher* socket_dispatcher)
: socket_dispatcher_(socket_dispatcher) {
}
@@ -11,15 +15,18 @@ IpcNetworkManager::IpcNetworkManager(P2PSocketDispatcher* socket_dispatcher)
IpcNetworkManager::~IpcNetworkManager() {
}
-// TODO(sergeyu): Currently this method just adds one fake network in
-// the list. This doesn't prevent PortAllocator from allocating ports:
-// browser process chooses first IPv4-enabled interface. But this
-// approach will not work in case when there is more than one active
-// network interface. Implement this properly: get list of networks
-// from the browser.
bool IpcNetworkManager::EnumNetworks(
bool include_ignored, std::vector<talk_base::Network*>* networks) {
- networks->push_back(new talk_base::Network(
- "chrome", "Chrome virtual network", 0, 0));
+ socket_dispatcher_->RequestNetworks();
+ const net::NetworkInterfaceList& list = socket_dispatcher_->networks();
+ for (net::NetworkInterfaceList::const_iterator it = list.begin();
+ it != list.end(); it++) {
+ uint32 address;
+ if (it->address.size() != net::kIPv4AddressSize)
+ continue;
+ memcpy(&address, &it->address[0], sizeof(uint32));
+ address = ntohl(address);
+ networks->push_back(new talk_base::Network(it->name, it->name, address, 0));
+ }
return true;
}
diff --git a/content/renderer/p2p/socket_dispatcher.cc b/content/renderer/p2p/socket_dispatcher.cc
index c69d057..065bd23 100644
--- a/content/renderer/p2p/socket_dispatcher.cc
+++ b/content/renderer/p2p/socket_dispatcher.cc
@@ -19,9 +19,14 @@ P2PSocketDispatcher::~P2PSocketDispatcher() {
}
}
+void P2PSocketDispatcher::RequestNetworks() {
+ Send(new P2PHostMsg_GetNetworkList(routing_id()));
+}
+
bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(P2PSocketDispatcher, message)
+ IPC_MESSAGE_HANDLER(P2PMsg_NetworkList, OnNetworkList)
IPC_MESSAGE_HANDLER(P2PMsg_OnSocketCreated, OnSocketCreated)
IPC_MESSAGE_HANDLER(P2PMsg_OnIncomingTcpConnection, OnIncomingTcpConnection)
IPC_MESSAGE_HANDLER(P2PMsg_OnError, OnError)
@@ -48,6 +53,11 @@ base::MessageLoopProxy* P2PSocketDispatcher::message_loop() {
return message_loop_;
}
+void P2PSocketDispatcher::OnNetworkList(
+ const net::NetworkInterfaceList& networks) {
+ networks_ = networks;
+}
+
void P2PSocketDispatcher::OnSocketCreated(
int socket_id, const net::IPEndPoint& address) {
P2PSocketClient* client = GetClient(socket_id);
diff --git a/content/renderer/p2p/socket_dispatcher.h b/content/renderer/p2p/socket_dispatcher.h
index 8ff1e1c..4d0c273 100644
--- a/content/renderer/p2p/socket_dispatcher.h
+++ b/content/renderer/p2p/socket_dispatcher.h
@@ -9,11 +9,11 @@
//
// Relationship of classes.
//
-// P2PSocketHost P2PSocketClient
-// ^ ^
-// | |
-// v IPC v
-// P2PSocketsHost <---------> P2PSocketDispatcher
+// P2PSocketHost P2PSocketClient
+// ^ ^
+// | |
+// v IPC v
+// P2PSocketDispatcherHost <---------> P2PSocketDispatcher
//
#ifndef CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_
@@ -38,9 +38,8 @@ class P2PSocketDispatcher : public RenderViewObserver {
explicit P2PSocketDispatcher(RenderView* render_view);
virtual ~P2PSocketDispatcher();
- P2PSocketClient* CreateSocket(P2PSocketType type,
- const net::IPEndPoint& address,
- P2PSocketClient::Delegate* delegate);
+ void RequestNetworks();
+ const net::NetworkInterfaceList& networks() const { return networks_; }
// RenderViewObserver overrides.
virtual bool OnMessageReceived(const IPC::Message& message);
@@ -55,6 +54,7 @@ class P2PSocketDispatcher : public RenderViewObserver {
base::MessageLoopProxy* message_loop();
// Incoming message handlers.
+ void OnNetworkList(const net::NetworkInterfaceList& networks);
void OnSocketCreated(int socket_id, const net::IPEndPoint& address);
void OnIncomingTcpConnection(int socket_id, const net::IPEndPoint& address);
void OnError(int socket_id);
@@ -65,6 +65,7 @@ class P2PSocketDispatcher : public RenderViewObserver {
scoped_refptr<base::MessageLoopProxy> message_loop_;
IDMap<P2PSocketClient> clients_;
+ net::NetworkInterfaceList networks_;
DISALLOW_COPY_AND_ASSIGN(P2PSocketDispatcher);
};
diff --git a/net/base/net_util.h b/net/base/net_util.h
index 0ff3369..7b3e7ec 100644
--- a/net/base/net_util.h
+++ b/net/base/net_util.h
@@ -473,7 +473,7 @@ struct NetworkInterface {
IPAddressNumber address;
};
-typedef std::list<NetworkInterface> NetworkInterfaceList;
+typedef std::vector<NetworkInterface> NetworkInterfaceList;
// Returns list of network interfaces except loopback interface. If an
// interface has more than one address, a separate entry is added to