summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/renderer/p2p/ipc_socket_factory.cc35
-rw-r--r--chrome/renderer/p2p/socket_client.cc12
-rw-r--r--chrome/renderer/p2p/socket_client.h17
-rw-r--r--chrome/renderer/p2p/socket_dispatcher.cc4
-rw-r--r--chrome/renderer/p2p/socket_dispatcher.h7
-rw-r--r--content/browser/renderer_host/p2p_socket_host.h4
-rw-r--r--content/browser/renderer_host/p2p_socket_host_posix.cc75
-rw-r--r--content/browser/renderer_host/p2p_socket_host_posix.h2
-rw-r--r--content/browser/renderer_host/p2p_sockets_host.cc4
-rw-r--r--content/browser/renderer_host/p2p_sockets_host.h5
-rw-r--r--content/common/common_param_traits.cc19
-rw-r--r--content/common/common_param_traits.h9
-rw-r--r--content/common/p2p_messages.h16
-rw-r--r--content/common/p2p_sockets.cc11
-rw-r--r--content/common/p2p_sockets.h10
-rw-r--r--content/content_common.gypi1
-rw-r--r--net/base/ip_endpoint.cc11
-rw-r--r--net/base/ip_endpoint.h5
-rw-r--r--net/base/ip_endpoint_unittest.cc22
19 files changed, 138 insertions, 131 deletions
diff --git a/chrome/renderer/p2p/ipc_socket_factory.cc b/chrome/renderer/p2p/ipc_socket_factory.cc
index d9284f8..8e9b4b6 100644
--- a/chrome/renderer/p2p/ipc_socket_factory.cc
+++ b/chrome/renderer/p2p/ipc_socket_factory.cc
@@ -17,24 +17,25 @@ const size_t kIPv4AddressSize = 4;
// Chromium and libjingle represent socket addresses differently. The
// following two functions are used to convert addresses from one
// representation to another.
-bool ChromeToLibjingleSocketAddress(const P2PSocketAddress& address_chrome,
+bool ChromeToLibjingleSocketAddress(const net::IPEndPoint& address_chrome,
talk_base::SocketAddress* address_lj) {
- if (address_chrome.address.size() != kIPv4AddressSize) {
+ if (address_chrome.GetFamily() != AF_INET) {
LOG(ERROR) << "Only IPv4 addresses are supported.";
return false;
}
uint32 ip_as_int = ntohl(*reinterpret_cast<const uint32*>(
- &address_chrome.address[0]));
- *address_lj = talk_base::SocketAddress(ip_as_int, address_chrome.port);
+ &address_chrome.address()[0]));
+ *address_lj = talk_base::SocketAddress(ip_as_int, address_chrome.port());
return true;
}
-bool LibjingleToChromeSocketAddress(const talk_base::SocketAddress& address_lj,
- P2PSocketAddress* address_chrome) {
+bool LibjingleToIPEndPoint(const talk_base::SocketAddress& address_lj,
+ net::IPEndPoint* address_chrome) {
uint32 ip = htonl(address_lj.ip());
- address_chrome->address.resize(kIPv4AddressSize);
- memcpy(&address_chrome->address[0], &ip, kIPv4AddressSize);
- address_chrome->port = address_lj.port();
+ net::IPAddressNumber address;
+ address.resize(kIPv4AddressSize);
+ memcpy(&address[0], &ip, kIPv4AddressSize);
+ *address_chrome = net::IPEndPoint(address, address_lj.port());
return true;
}
@@ -64,9 +65,9 @@ class IpcPacketSocket : public talk_base::AsyncPacketSocket,
virtual void SetError(int error);
// P2PSocketClient::Delegate
- virtual void OnOpen(const P2PSocketAddress& address);
+ virtual void OnOpen(const net::IPEndPoint& address);
virtual void OnError();
- virtual void OnDataReceived(const P2PSocketAddress& address,
+ virtual void OnDataReceived(const net::IPEndPoint& address,
const std::vector<char>& data);
private:
@@ -124,8 +125,8 @@ bool IpcPacketSocket::Init(P2PSocketType type, P2PSocketClient* client,
remote_address_ = address;
state_ = STATE_OPENING;
- P2PSocketAddress address_chrome;
- if (!LibjingleToChromeSocketAddress(address, &address_chrome)) {
+ net::IPEndPoint address_chrome;
+ if (!LibjingleToIPEndPoint(address, &address_chrome)) {
return false;
}
@@ -177,8 +178,8 @@ int IpcPacketSocket::SendTo(const void *data, size_t data_size,
const char* data_char = reinterpret_cast<const char*>(data);
std::vector<char> data_vector(data_char, data_char + data_size);
- P2PSocketAddress address_chrome;
- if (!LibjingleToChromeSocketAddress(address, &address_chrome)) {
+ net::IPEndPoint address_chrome;
+ if (!LibjingleToIPEndPoint(address, &address_chrome)) {
// Just drop the packet if we failed to convert the address.
return 0;
}
@@ -244,7 +245,7 @@ void IpcPacketSocket::SetError(int error) {
error_ = error;
}
-void IpcPacketSocket::OnOpen(const P2PSocketAddress& address) {
+void IpcPacketSocket::OnOpen(const net::IPEndPoint& address) {
DCHECK_EQ(MessageLoop::current(), message_loop_);
if (!ChromeToLibjingleSocketAddress(address, &local_address_)) {
@@ -262,7 +263,7 @@ void IpcPacketSocket::OnError() {
error_ = ECONNABORTED;
}
-void IpcPacketSocket::OnDataReceived(const P2PSocketAddress& address,
+void IpcPacketSocket::OnDataReceived(const net::IPEndPoint& address,
const std::vector<char>& data) {
DCHECK_EQ(MessageLoop::current(), message_loop_);
diff --git a/chrome/renderer/p2p/socket_client.cc b/chrome/renderer/p2p/socket_client.cc
index 6d7b886..f799eea 100644
--- a/chrome/renderer/p2p/socket_client.cc
+++ b/chrome/renderer/p2p/socket_client.cc
@@ -22,7 +22,7 @@ P2PSocketClient::~P2PSocketClient() {
}
void P2PSocketClient::Init(
- P2PSocketType type, const P2PSocketAddress& address,
+ P2PSocketType type, const net::IPEndPoint& address,
P2PSocketClient::Delegate* delegate,
scoped_refptr<base::MessageLoopProxy> delegate_loop) {
if (!ipc_message_loop_->BelongsToCurrentThread()) {
@@ -41,7 +41,7 @@ void P2PSocketClient::Init(
new P2PHostMsg_CreateSocket(0, type, socket_id_, address));
}
-void P2PSocketClient::Send(const P2PSocketAddress& address,
+void P2PSocketClient::Send(const net::IPEndPoint& address,
const std::vector<char>& data) {
if (!ipc_message_loop_->BelongsToCurrentThread()) {
ipc_message_loop_->PostTask(
@@ -76,7 +76,7 @@ void P2PSocketClient::DoClose() {
state_ = STATE_CLOSED;
}
-void P2PSocketClient::OnSocketCreated(const P2PSocketAddress& address) {
+void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) {
DCHECK(ipc_message_loop_->BelongsToCurrentThread());
DCHECK_EQ(state_, STATE_OPENING);
state_ = STATE_OPEN;
@@ -85,7 +85,7 @@ void P2PSocketClient::OnSocketCreated(const P2PSocketAddress& address) {
this, &P2PSocketClient::DeliverOnSocketCreated, address));
}
-void P2PSocketClient::DeliverOnSocketCreated(const P2PSocketAddress& address) {
+void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) {
if (delegate_)
delegate_->OnOpen(address);
}
@@ -103,7 +103,7 @@ void P2PSocketClient::DeliverOnError() {
delegate_->OnError();
}
-void P2PSocketClient::OnDataReceived(const P2PSocketAddress& address,
+void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address,
const std::vector<char>& data) {
DCHECK(ipc_message_loop_->BelongsToCurrentThread());
DCHECK_EQ(STATE_OPEN, state_);
@@ -111,7 +111,7 @@ void P2PSocketClient::OnDataReceived(const P2PSocketAddress& address,
this, &P2PSocketClient::DeliverOnDataReceived, address, data));
}
-void P2PSocketClient::DeliverOnDataReceived(const P2PSocketAddress& address,
+void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address,
const std::vector<char>& data) {
if (delegate_)
delegate_->OnDataReceived(address, data);
diff --git a/chrome/renderer/p2p/socket_client.h b/chrome/renderer/p2p/socket_client.h
index 7c19fc2..17b38ca 100644
--- a/chrome/renderer/p2p/socket_client.h
+++ b/chrome/renderer/p2p/socket_client.h
@@ -9,6 +9,7 @@
#include "base/ref_counted.h"
#include "content/common/p2p_sockets.h"
+#include "net/base/ip_endpoint.h"
namespace base {
class MessageLoopProxy;
@@ -29,9 +30,9 @@ class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> {
public:
virtual ~Delegate() { }
- virtual void OnOpen(const P2PSocketAddress& address) = 0;
+ virtual void OnOpen(const net::IPEndPoint& address) = 0;
virtual void OnError() = 0;
- virtual void OnDataReceived(const P2PSocketAddress& address,
+ virtual void OnDataReceived(const net::IPEndPoint& address,
const std::vector<char>& data) = 0;
};
@@ -40,12 +41,12 @@ class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> {
// Initialize socket of the specified |type| and connected to the
// specified |address|. |address| matters only when |type| is set to
// P2P_SOCKET_TCP_CLIENT.
- void Init(P2PSocketType type, const P2PSocketAddress& address,
+ void Init(P2PSocketType type, const net::IPEndPoint& address,
Delegate* delegate,
scoped_refptr<base::MessageLoopProxy> delegate_loop);
// Send the |data| to the |address|.
- void Send(const P2PSocketAddress& address, const std::vector<char>& data);
+ void Send(const net::IPEndPoint& address, const std::vector<char>& data);
// Must be called before the socket is destroyed. The delegate may
// not be called after |closed_task| is executed.
@@ -70,15 +71,15 @@ class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> {
virtual ~P2PSocketClient();
// Message handlers that run on IPC thread.
- void OnSocketCreated(const P2PSocketAddress& address);
+ void OnSocketCreated(const net::IPEndPoint& address);
void OnError();
- void OnDataReceived(const P2PSocketAddress& address,
+ void OnDataReceived(const net::IPEndPoint& address,
const std::vector<char>& data);
// Proxy methods that deliver messages to the delegate thread.
- void DeliverOnSocketCreated(const P2PSocketAddress& address);
+ void DeliverOnSocketCreated(const net::IPEndPoint& address);
void DeliverOnError();
- void DeliverOnDataReceived(const P2PSocketAddress& address,
+ void DeliverOnDataReceived(const net::IPEndPoint& address,
const std::vector<char>& data);
// Scheduled on the IPC thread to finish closing the connection.
diff --git a/chrome/renderer/p2p/socket_dispatcher.cc b/chrome/renderer/p2p/socket_dispatcher.cc
index 6096f6c..221683d 100644
--- a/chrome/renderer/p2p/socket_dispatcher.cc
+++ b/chrome/renderer/p2p/socket_dispatcher.cc
@@ -50,7 +50,7 @@ base::MessageLoopProxy* P2PSocketDispatcher::message_loop() {
}
void P2PSocketDispatcher::OnSocketCreated(
- int socket_id, const P2PSocketAddress& address) {
+ int socket_id, const net::IPEndPoint& address) {
P2PSocketClient* client = GetClient(socket_id);
if (client) {
client->OnSocketCreated(address);
@@ -65,7 +65,7 @@ void P2PSocketDispatcher::OnError(int socket_id) {
}
void P2PSocketDispatcher::OnDataReceived(
- int socket_id, const P2PSocketAddress& address,
+ int socket_id, const net::IPEndPoint& address,
const std::vector<char>& data) {
P2PSocketClient* client = GetClient(socket_id);
if (client) {
diff --git a/chrome/renderer/p2p/socket_dispatcher.h b/chrome/renderer/p2p/socket_dispatcher.h
index 9f39387..076d051 100644
--- a/chrome/renderer/p2p/socket_dispatcher.h
+++ b/chrome/renderer/p2p/socket_dispatcher.h
@@ -38,7 +38,8 @@ class P2PSocketDispatcher : public RenderViewObserver {
explicit P2PSocketDispatcher(RenderView* render_view);
virtual ~P2PSocketDispatcher();
- P2PSocketClient* CreateSocket(P2PSocketType type, P2PSocketAddress address,
+ P2PSocketClient* CreateSocket(P2PSocketType type,
+ const net::IPEndPoint& address,
P2PSocketClient::Delegate* delegate);
// RenderViewObserver overrides.
@@ -54,9 +55,9 @@ class P2PSocketDispatcher : public RenderViewObserver {
base::MessageLoopProxy* message_loop();
// Incoming message handlers.
- void OnSocketCreated(int socket_id, const P2PSocketAddress& address);
+ void OnSocketCreated(int socket_id, const net::IPEndPoint& address);
void OnError(int socket_id);
- void OnDataReceived(int socket_id, const P2PSocketAddress& address,
+ void OnDataReceived(int socket_id, const net::IPEndPoint& address,
const std::vector<char>& data);
P2PSocketClient* GetClient(int socket_id);
diff --git a/content/browser/renderer_host/p2p_socket_host.h b/content/browser/renderer_host/p2p_socket_host.h
index 6d8656c..b7cd919 100644
--- a/content/browser/renderer_host/p2p_socket_host.h
+++ b/content/browser/renderer_host/p2p_socket_host.h
@@ -7,6 +7,8 @@
#include "content/common/p2p_sockets.h"
+#include "net/base/ip_endpoint.h"
+
class P2PSocketsHost;
// Base class for P2P sockets used by P2PSocketsHost.
@@ -23,7 +25,7 @@ class P2PSocketHost {
virtual bool Init() = 0;
// Sends |data| on the socket to |socket_address|.
- virtual void Send(const P2PSocketAddress& socket_address,
+ virtual void Send(const net::IPEndPoint& socket_address,
const std::vector<char>& data) = 0;
protected:
diff --git a/content/browser/renderer_host/p2p_socket_host_posix.cc b/content/browser/renderer_host/p2p_socket_host_posix.cc
index e7a804b..eb1f8b3 100644
--- a/content/browser/renderer_host/p2p_socket_host_posix.cc
+++ b/content/browser/renderer_host/p2p_socket_host_posix.cc
@@ -71,34 +71,6 @@ bool GetLocalAddress(sockaddr_in* addr) {
return found;
}
-bool SocketAddressToSockAddr(const 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(
@@ -161,16 +133,14 @@ bool P2PSocketHostPosix::Init() {
return false;
}
- P2PSocketAddress address;
- if (!SockAddrToSocketAddress(&addr, &address)) {
+ net::IPEndPoint address;
+ if (!address.FromSockAddr(reinterpret_cast<sockaddr*>(&addr), addrlen)) {
OnError();
return false;
}
VLOG(1) << "getsockname() returned "
- << net::NetAddressToString(
- reinterpret_cast<sockaddr*>(&addr), sizeof(addr))
- << ":" << address.port;
+ << address.ToString();
state_ = STATE_OPEN;
@@ -196,24 +166,24 @@ void P2PSocketHostPosix::DidCompleteRead() {
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);
+ sockaddr_storage addr_storage;
+ sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
+ socklen_t addr_len = sizeof(addr_storage);
+ int result = recvfrom(socket_, &data[0], data.size(), 0, 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.
+
+ net::IPEndPoint address;
+ if (!address.FromSockAddr(addr, addr_len) ||
+ address.GetFamily() != AF_INET) {
+ // We should never receive IPv6 packet on IPv4 packet.
NOTREACHED();
return;
}
+ VLOG(2) << "received " << result << " bytes from "
+ << address.ToString();
+
host_->Send(new P2PMsg_OnDataReceived(routing_id_, id_,
address, data));
} else if (result < 0) {
@@ -222,19 +192,18 @@ void P2PSocketHostPosix::DidCompleteRead() {
}
}
-void P2PSocketHostPosix::Send(const P2PSocketAddress& socket_address,
+void P2PSocketHostPosix::Send(const net::IPEndPoint& 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));
+ sockaddr_storage addr_storage;
+ sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
+ size_t addr_len = sizeof(addr_storage);
+ socket_address.ToSockAddr(addr, &addr_len);
+ int result = sendto(socket_, &data[0], data.size(), 0, addr, addr_len);
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);
+ << socket_address.ToString();
}
}
diff --git a/content/browser/renderer_host/p2p_socket_host_posix.h b/content/browser/renderer_host/p2p_socket_host_posix.h
index 236c24d..855d7ee 100644
--- a/content/browser/renderer_host/p2p_socket_host_posix.h
+++ b/content/browser/renderer_host/p2p_socket_host_posix.h
@@ -16,7 +16,7 @@ class P2PSocketHostPosix : public P2PSocketHost {
virtual ~P2PSocketHostPosix();
virtual bool Init();
- virtual void Send(const P2PSocketAddress& socket_address,
+ virtual void Send(const net::IPEndPoint& socket_address,
const std::vector<char>& data);
private:
diff --git a/content/browser/renderer_host/p2p_sockets_host.cc b/content/browser/renderer_host/p2p_sockets_host.cc
index bf1c880..8cc2843 100644
--- a/content/browser/renderer_host/p2p_sockets_host.cc
+++ b/content/browser/renderer_host/p2p_sockets_host.cc
@@ -41,7 +41,7 @@ bool P2PSocketsHost::OnMessageReceived(const IPC::Message& message,
void P2PSocketsHost::OnCreateSocket(
const IPC::Message& msg, P2PSocketType type, int socket_id,
- const P2PSocketAddress& remote_address) {
+ const net::IPEndPoint& remote_address) {
if (sockets_.Lookup(socket_id)) {
LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket "
"that already exists.";
@@ -67,7 +67,7 @@ void P2PSocketsHost::OnCreateSocket(
}
void P2PSocketsHost::OnSend(const IPC::Message& msg, int socket_id,
- const P2PSocketAddress& socket_address,
+ const net::IPEndPoint& socket_address,
const std::vector<char>& data) {
P2PSocketHost* socket = sockets_.Lookup(socket_id);
if (!socket) {
diff --git a/content/browser/renderer_host/p2p_sockets_host.h b/content/browser/renderer_host/p2p_sockets_host.h
index 37ec8fd..32757d0 100644
--- a/content/browser/renderer_host/p2p_sockets_host.h
+++ b/content/browser/renderer_host/p2p_sockets_host.h
@@ -8,6 +8,7 @@
#include "base/id_map.h"
#include "content/browser/browser_message_filter.h"
#include "content/common/p2p_sockets.h"
+#include "net/base/ip_endpoint.h"
class P2PSocketHost;
@@ -24,9 +25,9 @@ class P2PSocketsHost : public BrowserMessageFilter {
private:
void OnCreateSocket(const IPC::Message& msg, P2PSocketType type,
- int socket_id, const P2PSocketAddress& remote_address);
+ int socket_id, const net::IPEndPoint& remote_address);
void OnSend(const IPC::Message& msg, int socket_id,
- const P2PSocketAddress& socket_address,
+ const net::IPEndPoint& socket_address,
const std::vector<char>& data);
void OnDestroySocket(const IPC::Message& msg, int socket_id);
diff --git a/content/common/common_param_traits.cc b/content/common/common_param_traits.cc
index 948576b..4570559 100644
--- a/content/common/common_param_traits.cc
+++ b/content/common/common_param_traits.cc
@@ -316,6 +316,25 @@ void ParamTraits<scoped_refptr<net::HttpResponseHeaders> >::Log(
l->append("<HttpResponseHeaders>");
}
+void ParamTraits<net::IPEndPoint>::Write(Message* m, const param_type& p) {
+ WriteParam(m, p.address());
+ WriteParam(m, p.port());
+}
+
+bool ParamTraits<net::IPEndPoint>::Read(const Message* m, void** iter,
+ param_type* p) {
+ net::IPAddressNumber address;
+ int port;
+ if (!ReadParam(m, iter, &address) || !ReadParam(m, iter, &port))
+ return false;
+ *p = net::IPEndPoint(address, port);
+ return true;
+}
+
+void ParamTraits<net::IPEndPoint>::Log(const param_type& p, std::string* l) {
+ LogParam("IPEndPoint:" + p.ToString(), l);
+}
+
void ParamTraits<webkit_glue::ResourceLoadTimingInfo>::Write(
Message* m, const param_type& p) {
WriteParam(m, p.base_time.is_null());
diff --git a/content/common/common_param_traits.h b/content/common/common_param_traits.h
index 20c5be3..2448377 100644
--- a/content/common/common_param_traits.h
+++ b/content/common/common_param_traits.h
@@ -18,6 +18,7 @@
#include "base/ref_counted.h"
#include "googleurl/src/gurl.h"
#include "ipc/ipc_message_utils.h"
+#include "net/base/ip_endpoint.h"
#include "net/url_request/url_request_status.h"
// !!! WARNING: DO NOT ADD NEW WEBKIT DEPENDENCIES !!!
//
@@ -101,6 +102,14 @@ struct ParamTraits<scoped_refptr<net::HttpResponseHeaders> > {
};
template <>
+struct ParamTraits<net::IPEndPoint> {
+ typedef net::IPEndPoint param_type;
+ static void Write(Message* m, const param_type& p);
+ static bool Read(const Message* m, void** iter, param_type* p);
+ static void Log(const param_type& p, std::string* l);
+};
+
+template <>
struct ParamTraits<webkit_glue::ResourceLoadTimingInfo> {
typedef webkit_glue::ResourceLoadTimingInfo param_type;
static void Write(Message* m, const param_type& p);
diff --git a/content/common/p2p_messages.h b/content/common/p2p_messages.h
index 8424fe1..36935af 100644
--- a/content/common/p2p_messages.h
+++ b/content/common/p2p_messages.h
@@ -7,28 +7,24 @@
#include "content/common/p2p_sockets.h"
#include "ipc/ipc_message_macros.h"
+#include "net/base/ip_endpoint.h"
#define IPC_MESSAGE_START P2PMsgStart
-IPC_STRUCT_TRAITS_BEGIN(P2PSocketAddress)
- IPC_STRUCT_TRAITS_MEMBER(address)
- IPC_STRUCT_TRAITS_MEMBER(port)
-IPC_STRUCT_TRAITS_END()
-
IPC_ENUM_TRAITS(P2PSocketType)
// P2P Socket messages sent from the browser to the renderer.
IPC_MESSAGE_ROUTED2(P2PMsg_OnSocketCreated,
int /* socket_id */,
- P2PSocketAddress /* socket_address */)
+ net::IPEndPoint /* socket_address */)
IPC_MESSAGE_ROUTED1(P2PMsg_OnError,
int /* socket_id */)
IPC_MESSAGE_ROUTED3(P2PMsg_OnDataReceived,
int /* socket_id */,
- P2PSocketAddress /* socket_address */,
+ net::IPEndPoint /* socket_address */,
std::vector<char> /* data */)
// P2P Socket messages sent from the renderer to the browser.
@@ -36,13 +32,13 @@ IPC_MESSAGE_ROUTED3(P2PMsg_OnDataReceived,
IPC_MESSAGE_ROUTED3(P2PHostMsg_CreateSocket,
P2PSocketType /* type */,
int /* socket_id */,
- P2PSocketAddress /* remote_address */)
+ net::IPEndPoint /* remote_address */)
// TODO(sergeyu): Use shared memory to pass the data.
IPC_MESSAGE_ROUTED3(P2PHostMsg_Send,
int /* socket_id */,
- P2PSocketAddress /* socket_address */,
+ net::IPEndPoint /* socket_address */,
std::vector<char> /* data */)
IPC_MESSAGE_ROUTED1(P2PHostMsg_DestroySocket,
- int /* socket_id */)
+ int /* socket_id */)
diff --git a/content/common/p2p_sockets.cc b/content/common/p2p_sockets.cc
deleted file mode 100644
index 6223848..0000000
--- a/content/common/p2p_sockets.cc
+++ /dev/null
@@ -1,11 +0,0 @@
-// 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/common/p2p_sockets.h"
-
-P2PSocketAddress::P2PSocketAddress() : port(0) {
-}
-
-P2PSocketAddress::~P2PSocketAddress() {
-}
diff --git a/content/common/p2p_sockets.h b/content/common/p2p_sockets.h
index 3c44fd8..d775683 100644
--- a/content/common/p2p_sockets.h
+++ b/content/common/p2p_sockets.h
@@ -8,16 +8,6 @@
#ifndef CONTENT_COMMON_P2P_SOCKETS_H_
#define CONTENT_COMMON_P2P_SOCKETS_H_
-#include "net/base/net_util.h"
-
-// Pair of IP address and port number.
-struct P2PSocketAddress {
- P2PSocketAddress();
- ~P2PSocketAddress();
- net::IPAddressNumber address;
- int port;
-};
-
// Type of P2P Socket.
enum P2PSocketType {
P2P_SOCKET_UDP,
diff --git a/content/content_common.gypi b/content/content_common.gypi
index cd62bd3..cd1784b 100644
--- a/content/content_common.gypi
+++ b/content/content_common.gypi
@@ -73,7 +73,6 @@
'common/notification_source.h',
'common/notification_type.h',
'common/p2p_messages.h',
- 'common/p2p_sockets.cc',
'common/p2p_sockets.h',
'common/property_bag.cc',
'common/property_bag.h',
diff --git a/net/base/ip_endpoint.cc b/net/base/ip_endpoint.cc
index 626d12e..df90f1a 100644
--- a/net/base/ip_endpoint.cc
+++ b/net/base/ip_endpoint.cc
@@ -5,6 +5,7 @@
#include "net/base/ip_endpoint.h"
#include "base/logging.h"
+#include "base/string_number_conversions.h"
#if defined(OS_WIN)
#include <winsock2.h>
#elif defined(OS_POSIX)
@@ -108,6 +109,16 @@ bool IPEndPoint::FromSockAddr(const struct sockaddr* address,
return true;
}
+std::string IPEndPoint::ToString() const {
+ struct sockaddr_storage addr_storage;
+ size_t addr_len = sizeof(addr_storage);
+ struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
+ if (!ToSockAddr(addr, &addr_len)) {
+ return "";
+ }
+ return NetAddressToString(addr, addr_len) + ":" + base::IntToString(port_);
+}
+
bool IPEndPoint::operator<(const IPEndPoint& that) const {
return address_ < that.address_ || port_ < that.port_;
}
diff --git a/net/base/ip_endpoint.h b/net/base/ip_endpoint.h
index af5de87..f76a82a 100644
--- a/net/base/ip_endpoint.h
+++ b/net/base/ip_endpoint.h
@@ -44,6 +44,11 @@ class IPEndPoint {
// Returns true on success, false on failure.
bool FromSockAddr(const struct sockaddr* address, size_t address_length);
+ // Returns value as a string (e.g. "127.0.0.1:80"). Returns empty
+ // string if the address is invalid, and cannot not be converted to a
+ // string.
+ std::string ToString() const;
+
bool operator<(const IPEndPoint& that) const;
bool operator==(const IPEndPoint& that) const;
diff --git a/net/base/ip_endpoint_unittest.cc b/net/base/ip_endpoint_unittest.cc
index ae09ad3..17f6e0b 100644
--- a/net/base/ip_endpoint_unittest.cc
+++ b/net/base/ip_endpoint_unittest.cc
@@ -4,6 +4,7 @@
#include "net/base/ip_endpoint.h"
+#include "base/string_number_conversions.h"
#include "net/base/net_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@@ -19,13 +20,14 @@ namespace {
struct TestData {
std::string host;
+ std::string host_normalized;
bool ipv6;
IPAddressNumber ip_address;
} tests[] = {
- { "127.0.00.1", false},
- { "192.168.1.1", false },
- { "::1", true },
- { "2001:db8:0::42", true },
+ { "127.0.00.1", "127.0.0.1", false},
+ { "192.168.1.1", "192.168.1.1", false },
+ { "::1", "::1", true },
+ { "2001:db8:0::42", "2001:db8::42", true },
};
int test_count = ARRAYSIZE_UNSAFE(tests);
@@ -136,6 +138,18 @@ TEST_F(IPEndPointTest, LessThan) {
EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
}
+TEST_F(IPEndPointTest, ToString) {
+ IPEndPoint endpoint;
+ EXPECT_EQ(0, endpoint.port());
+
+ for (int index = 0; index < test_count; ++index) {
+ int port = 100 + index;
+ IPEndPoint endpoint(tests[index].ip_address, port);
+ EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port),
+ endpoint.ToString());
+ }
+}
+
} // namespace
} // namespace net