summaryrefslogtreecommitdiffstats
path: root/content/browser
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-02 20:52:34 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-02 20:52:34 +0000
commit6c54e7e41109a4c5bc638d94d868d5f7c92bc68e (patch)
tree4cfbe30cd4b483e0d10630058fba5cf9100ba13b /content/browser
parent098a8eac76a314e4b26a7387e272d69fec518272 (diff)
downloadchromium_src-6c54e7e41109a4c5bc638d94d868d5f7c92bc68e.zip
chromium_src-6c54e7e41109a4c5bc638d94d868d5f7c92bc68e.tar.gz
chromium_src-6c54e7e41109a4c5bc638d94d868d5f7c92bc68e.tar.bz2
P2P Sockets host implementation.
Currently only UDP sockets on Mac and Linux are supported. Also added --enable-p2papi flag. The flags just enables IPC for P2P. BUG=None TEST=None Review URL: http://codereview.chromium.org/6598053 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@76604 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser')
-rw-r--r--content/browser/renderer_host/p2p_socket_host.cc11
-rw-r--r--content/browser/renderer_host/p2p_socket_host.h39
-rw-r--r--content/browser/renderer_host/p2p_socket_host_posix.cc260
-rw-r--r--content/browser/renderer_host/p2p_socket_host_posix.h58
-rw-r--r--content/browser/renderer_host/p2p_socket_host_win.cc12
-rw-r--r--content/browser/renderer_host/p2p_socket_host_win.h12
-rw-r--r--content/browser/renderer_host/p2p_sockets_host.cc81
-rw-r--r--content/browser/renderer_host/p2p_sockets_host.h38
8 files changed, 511 insertions, 0 deletions
diff --git a/content/browser/renderer_host/p2p_socket_host.cc b/content/browser/renderer_host/p2p_socket_host.cc
new file mode 100644
index 0000000..0645e7b
--- /dev/null
+++ b/content/browser/renderer_host/p2p_socket_host.cc
@@ -0,0 +1,11 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/renderer_host/p2p_socket_host.h"
+
+P2PSocketHost::P2PSocketHost(P2PSocketsHost* host, int routing_id, int id)
+ : host_(host), routing_id_(routing_id), id_(id) {
+}
+
+P2PSocketHost::~P2PSocketHost() { }
diff --git a/content/browser/renderer_host/p2p_socket_host.h b/content/browser/renderer_host/p2p_socket_host.h
new file mode 100644
index 0000000..8692888
--- /dev/null
+++ b/content/browser/renderer_host/p2p_socket_host.h
@@ -0,0 +1,39 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_H_
+#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_H_
+
+#include "content/common/p2p_sockets.h"
+
+class P2PSocketsHost;
+
+// Base class for P2P sockets used by P2PSocketsHost.
+class P2PSocketHost {
+ public:
+ // Creates P2PSocketHost for the current platform. Must be
+ // implemented in a platform-specific file.
+ static P2PSocketHost* Create(P2PSocketsHost* host, int routing_id, int id,
+ P2PSocketType type);
+
+ virtual ~P2PSocketHost();
+
+ // Initalizes the socket. Returns false when initiazations fails.
+ virtual bool Init() = 0;
+
+ // Sends |data| on the socket to |socket_address|.
+ virtual void Send(P2PSocketAddress socket_address,
+ const std::vector<char>& data) = 0;
+
+ protected:
+ P2PSocketHost(P2PSocketsHost* host, int routing_id, int id);
+
+ P2PSocketsHost* host_;
+ int routing_id_;
+ int id_;
+
+ DISALLOW_COPY_AND_ASSIGN(P2PSocketHost);
+};
+
+#endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_H_
diff --git a/content/browser/renderer_host/p2p_socket_host_posix.cc b/content/browser/renderer_host/p2p_socket_host_posix.cc
new file mode 100644
index 0000000..f9ae31f
--- /dev/null
+++ b/content/browser/renderer_host/p2p_socket_host_posix.cc
@@ -0,0 +1,260 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/renderer_host/p2p_socket_host_posix.h"
+
+#include <errno.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <unistd.h>
+
+#include "content/browser/renderer_host/p2p_sockets_host.h"
+#include "content/common/p2p_messages.h"
+#include "net/base/net_util.h"
+
+namespace {
+
+// This method returns address of the first IPv4 enabled network
+// interface it finds ignoring the loopback interface. 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(sockaddr_in* addr) {
+ int fd;
+ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+ LOG(ERROR) << "socket() failed: " << fd;
+ return false;
+ }
+
+ struct ifconf ifc;
+ ifc.ifc_len = 64 * sizeof(struct ifreq);
+ scoped_array<char> ifc_buffer(new char[ifc.ifc_len]);
+ ifc.ifc_buf = ifc_buffer.get();
+
+ int result = ioctl(fd, SIOCGIFCONF, &ifc);
+ if (result < 0) {
+ LOG(ERROR) << "GetLocalAddress: ioctl returned error:" << result;
+ close(fd);
+ return false;
+ }
+ CHECK_LT(ifc.ifc_len, static_cast<int>(64 * sizeof(struct ifreq)));
+
+ struct ifreq* ptr = reinterpret_cast<struct ifreq*>(ifc.ifc_buf);
+ struct ifreq* end =
+ reinterpret_cast<struct ifreq*>(ifc.ifc_buf + ifc.ifc_len);
+
+ bool found = false;
+ while (ptr < end) {
+ struct sockaddr_in* inaddr =
+ reinterpret_cast<struct sockaddr_in*>(&ptr->ifr_ifru.ifru_addr);
+ if (inaddr->sin_family == AF_INET &&
+ strncmp(ptr->ifr_name, "lo", 2) != 0) {
+ memcpy(addr, inaddr, sizeof(sockaddr_in));
+ found = true;
+ break;
+ }
+
+ ptr++;
+ }
+
+ close(fd);
+
+ return found;
+}
+
+bool SocketAddressToSockAddr(P2PSocketAddress address, sockaddr_in* addr) {
+ // TODO(sergeyu): Add IPv6 support.
+ if (address.address.size() != 4) {
+ return false;
+ }
+
+ addr->sin_family = AF_INET;
+ memcpy(&addr->sin_addr, &address.address[0], 4);
+ addr->sin_port = htons(address.port);
+ return true;
+}
+
+bool SockAddrToSocketAddress(sockaddr_in* addr,
+ P2PSocketAddress* address) {
+ if (addr->sin_family != AF_INET) {
+ LOG(ERROR) << "SockAddrToSocketAddress: only IPv4 addresses are supported";
+ // TODO(sergeyu): Add IPv6 support.
+ return false;
+ }
+
+ address->address.resize(4);
+ memcpy(&address->address[0], &addr->sin_addr, 4);
+ address->port = ntohs(addr->sin_port);
+
+ return true;
+}
+
+} // namespace
+
+P2PSocketHostPosix::P2PSocketHostPosix(
+ P2PSocketsHost* host, int routing_id, int id)
+ : P2PSocketHost(host, routing_id, id),
+ state_(STATE_UNINITIALIZED), socket_(0), read_watcher_(this) {
+}
+
+P2PSocketHostPosix::~P2PSocketHostPosix() {
+ if (state_ == STATE_OPEN) {
+ DCHECK_NE(socket_, 0);
+ close(socket_);
+ }
+}
+
+bool P2PSocketHostPosix::Init() {
+ socket_ = socket(AF_INET, SOCK_DGRAM, 0);
+ if (socket_ < 0) {
+ LOG(ERROR) << "Failed to create socket: " << socket_;
+ OnError();
+ return false;
+ }
+
+ int result = net::SetNonBlocking(socket_);
+ if (result < 0) {
+ LOG(ERROR) << "Failed to set O_NONBLOCK flag: " << result;
+ OnError();
+ return false;
+ }
+
+ sockaddr_in addr;
+ if (!GetLocalAddress(&addr)) {
+ LOG(ERROR) << "Failed to get local network address.";
+ OnError();
+ return false;
+ }
+
+ result = bind(socket_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
+ if (result < 0) {
+ LOG(ERROR) << "bind() failed: " << result;
+ OnError();
+ return false;
+ }
+
+ if (!MessageLoopForIO::current()->WatchFileDescriptor(
+ socket_, true, MessageLoopForIO::WATCH_READ,
+ &read_socket_watcher_, &read_watcher_)) {
+ LOG(ERROR) << "WatchFileDescriptor failed on read, errno: " << errno;
+ OnError();
+ return false;
+ }
+
+ socklen_t addrlen = sizeof(addr);
+ result = getsockname(socket_, reinterpret_cast<sockaddr*>(&addr),
+ &addrlen);
+ if (result < 0) {
+ LOG(ERROR) << "P2PSocket::Init(): unable to get local addr: "
+ << result;
+ OnError();
+ return false;
+ }
+
+ P2PSocketAddress address;
+ if (!SockAddrToSocketAddress(&addr, &address)) {
+ OnError();
+ return false;
+ }
+
+ VLOG(1) << "getsockname() returned "
+ << net::NetAddressToString(
+ reinterpret_cast<sockaddr*>(&addr), sizeof(addr))
+ << ":" << address.port;
+
+ state_ = STATE_OPEN;
+
+ host_->Send(new P2PMsg_OnSocketCreated(routing_id_, id_, address));
+
+ return true;
+}
+
+void P2PSocketHostPosix::OnError() {
+ if (socket_ != 0) {
+ close(socket_);
+ socket_ = 0;
+ }
+
+ if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) {
+ host_->Send(new P2PMsg_OnError(routing_id_, id_));
+ }
+
+ state_ = STATE_ERROR;
+}
+
+void P2PSocketHostPosix::DidCompleteRead() {
+ if (state_ != STATE_OPEN) {
+ return;
+ }
+
+ std::vector<char> data;
+ data.resize(4096);
+ sockaddr_in addr;
+ socklen_t addr_len = sizeof(addr);
+ int result = recvfrom(socket_, &data[0], data.size(), 0,
+ reinterpret_cast<sockaddr*>(&addr), &addr_len);
+ if (result > 0) {
+ data.resize(result);
+ VLOG(2) << "received " << result << " bytes from "
+ << net::NetAddressToString(
+ reinterpret_cast<sockaddr*>(&addr), sizeof(addr))
+ << ":" << ntohs(addr.sin_port);
+ P2PSocketAddress address;
+ if (!SockAddrToSocketAddress(&addr, &address)) {
+ // Address conversion fails only if we receive a non-IPv4
+ // packet, which should never happen because the socket is IPv4.
+ NOTREACHED();
+ return;
+ }
+
+ host_->Send(new P2PMsg_OnDataReceived(routing_id_, id_,
+ address, data));
+ } else if (result < 0) {
+ LOG(ERROR) << "recvfrom() returned error: " << result;
+ OnError();
+ }
+}
+
+void P2PSocketHostPosix::Send(P2PSocketAddress socket_address,
+ const std::vector<char>& data) {
+ sockaddr_in addr;
+ SocketAddressToSockAddr(socket_address, &addr);
+ int result = sendto(socket_, &data[0], data.size(), 0,
+ reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
+ if (result < 0) {
+ LOG(ERROR) << "Send failed.";
+ } else {
+ VLOG(2) << "Sent " << result << " bytes to "
+ << net::NetAddressToString(
+ reinterpret_cast<sockaddr*>(&addr), sizeof(addr))
+ << ":" << ntohs(addr.sin_port);
+ }
+}
+
+// static
+P2PSocketHost* P2PSocketHost::Create(
+ P2PSocketsHost* host, int routing_id, int id, P2PSocketType type) {
+ switch (type) {
+ case P2P_SOCKET_UDP:
+ return new P2PSocketHostPosix(host, routing_id, id);
+
+ case P2P_SOCKET_TCP_SERVER:
+ // TODO(sergeyu): Implement TCP sockets support.
+ return NULL;
+
+ case P2P_SOCKET_TCP_CLIENT:
+ return NULL;
+ }
+
+ NOTREACHED();
+ return NULL;
+}
diff --git a/content/browser/renderer_host/p2p_socket_host_posix.h b/content/browser/renderer_host/p2p_socket_host_posix.h
new file mode 100644
index 0000000..14dbc87
--- /dev/null
+++ b/content/browser/renderer_host/p2p_socket_host_posix.h
@@ -0,0 +1,58 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_POSIX_H_
+#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_POSIX_H_
+
+#include "content/common/p2p_sockets.h"
+
+#include "base/message_loop.h"
+#include "content/browser/renderer_host/p2p_socket_host.h"
+
+class P2PSocketHostPosix : public P2PSocketHost {
+ public:
+ P2PSocketHostPosix(P2PSocketsHost* host, int routing_id, int id);
+ virtual ~P2PSocketHostPosix();
+
+ virtual bool Init();
+ virtual void Send(P2PSocketAddress socket_address,
+ const std::vector<char>& data);
+
+ private:
+ enum State {
+ STATE_UNINITIALIZED,
+ STATE_OPEN,
+ STATE_ERROR,
+ };
+
+ // MessageLoopForIO::Watcher implementation used to catch read
+ // events from the socket.
+ class ReadWatcher : public MessageLoopForIO::Watcher {
+ public:
+ explicit ReadWatcher(P2PSocketHostPosix* socket) : socket_(socket) { }
+
+ // MessageLoopForIO::Watcher methods
+ virtual void OnFileCanReadWithoutBlocking(int /* fd */) {
+ socket_->DidCompleteRead();
+ }
+ virtual void OnFileCanWriteWithoutBlocking(int /* fd */) {}
+
+ private:
+ P2PSocketHostPosix* socket_;
+
+ DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
+ };
+
+ void DidCompleteRead();
+ void OnError();
+
+ State state_;
+ int socket_;
+ MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
+ ReadWatcher read_watcher_;
+
+ DISALLOW_COPY_AND_ASSIGN(P2PSocketHostPosix);
+};
+
+#endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_POSIX_H_
diff --git a/content/browser/renderer_host/p2p_socket_host_win.cc b/content/browser/renderer_host/p2p_socket_host_win.cc
new file mode 100644
index 0000000..6516935
--- /dev/null
+++ b/content/browser/renderer_host/p2p_socket_host_win.cc
@@ -0,0 +1,12 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/renderer_host/p2p_socket_host_win.h"
+
+// static
+P2PSocketHost* P2PSocketHost::Create(
+ P2PSocketsHost* host, int routing_id, int id, P2PSocketType type) {
+ // TODO(sergeyu): Implement P2PSocketHost on windows.
+ return NULL;
+}
diff --git a/content/browser/renderer_host/p2p_socket_host_win.h b/content/browser/renderer_host/p2p_socket_host_win.h
new file mode 100644
index 0000000..e9f0f95
--- /dev/null
+++ b/content/browser/renderer_host/p2p_socket_host_win.h
@@ -0,0 +1,12 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_WIN_H_
+#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_WIN_H_
+
+#include "content/browser/renderer_host/p2p_socket_host.h"
+
+// TODO(sergeyu): Implement P2PSocketHost on Windows.
+
+#endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_WIN_H_
diff --git a/content/browser/renderer_host/p2p_sockets_host.cc b/content/browser/renderer_host/p2p_sockets_host.cc
new file mode 100644
index 0000000..310bd1d
--- /dev/null
+++ b/content/browser/renderer_host/p2p_sockets_host.cc
@@ -0,0 +1,81 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/renderer_host/p2p_sockets_host.h"
+
+#include "content/browser/renderer_host/p2p_socket_host.h"
+#include "content/common/p2p_messages.h"
+
+P2PSocketsHost::P2PSocketsHost() {
+}
+
+P2PSocketsHost::~P2PSocketsHost() {
+}
+
+void P2PSocketsHost::OnChannelClosing() {
+ BrowserMessageFilter::OnChannelClosing();
+
+ // Since the IPC channel is gone, close pending connections.
+ for (IDMap<P2PSocketHost>::iterator i(&sockets_); !i.IsAtEnd(); i.Advance()) {
+ sockets_.Remove(i.GetCurrentKey());
+ }
+}
+
+void P2PSocketsHost::OnDestruct() const {
+ BrowserThread::DeleteOnIOThread::Destruct(this);
+}
+
+bool P2PSocketsHost::OnMessageReceived(const IPC::Message& message,
+ bool* message_was_ok) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketsHost, message, *message_was_ok)
+ IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket)
+ IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend)
+ IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP_EX()
+ return handled;
+}
+
+void P2PSocketsHost::OnCreateSocket(
+ const IPC::Message& msg, P2PSocketType type, int socket_id,
+ P2PSocketAddress remote_address) {
+ if (sockets_.Lookup(socket_id)) {
+ LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket "
+ "that already exists.";
+ return;
+ }
+
+ if (type != P2P_SOCKET_UDP) {
+ Send(new P2PMsg_OnError(msg.routing_id(), socket_id));
+ return;
+ }
+
+ scoped_ptr<P2PSocketHost> socket(
+ P2PSocketHost::Create(this, msg.routing_id(), socket_id, type));
+
+ if (!socket.get()) {
+ Send(new P2PMsg_OnError(msg.routing_id(), socket_id));
+ return;
+ }
+
+ if (socket->Init()) {
+ sockets_.AddWithID(socket.release(), socket_id);
+ }
+}
+
+void P2PSocketsHost::OnSend(const IPC::Message& msg, int socket_id,
+ P2PSocketAddress socket_address,
+ const std::vector<char>& data) {
+ P2PSocketHost* socket = sockets_.Lookup(socket_id);
+ if (!socket) {
+ LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id.";
+ return;
+ }
+ socket->Send(socket_address, data);
+}
+
+void P2PSocketsHost::OnDestroySocket(const IPC::Message& msg, int socket_id) {
+ sockets_.Remove(socket_id);
+}
diff --git a/content/browser/renderer_host/p2p_sockets_host.h b/content/browser/renderer_host/p2p_sockets_host.h
new file mode 100644
index 0000000..9d965eb
--- /dev/null
+++ b/content/browser/renderer_host/p2p_sockets_host.h
@@ -0,0 +1,38 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKETS_HOST_H_
+#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKETS_HOST_H_
+
+#include "base/id_map.h"
+#include "content/browser/browser_message_filter.h"
+#include "content/common/p2p_sockets.h"
+
+class P2PSocketHost;
+
+class P2PSocketsHost : public BrowserMessageFilter {
+ public:
+ P2PSocketsHost();
+ virtual ~P2PSocketsHost();
+
+ // BrowserMessageFilter overrides.
+ virtual void OnChannelClosing();
+ virtual void OnDestruct() const;
+ virtual bool OnMessageReceived(const IPC::Message& message,
+ bool* message_was_ok);
+
+ private:
+ void OnCreateSocket(const IPC::Message& msg, P2PSocketType type,
+ int socket_id, P2PSocketAddress remote_address);
+ void OnSend(const IPC::Message& msg, int socket_id,
+ P2PSocketAddress socket_address,
+ const std::vector<char>& data);
+ void OnDestroySocket(const IPC::Message& msg, int socket_id);
+
+ IDMap<P2PSocketHost> sockets_;
+
+ DISALLOW_COPY_AND_ASSIGN(P2PSocketsHost);
+};
+
+#endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKETS_HOST_H_