1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
// Copyright (c) 2012 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_dispatcher_host.h"
#include "base/bind.h"
#include "base/profiler/scoped_tracker.h"
#include "base/stl_util.h"
#include "content/browser/renderer_host/p2p/socket_host.h"
#include "content/common/p2p_messages.h"
#include "content/public/browser/resource_context.h"
#include "net/base/address_list.h"
#include "net/base/completion_callback.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/base/sys_addrinfo.h"
#include "net/dns/single_request_host_resolver.h"
#include "net/url_request/url_request_context_getter.h"
using content::BrowserMessageFilter;
using content::BrowserThread;
namespace content {
const size_t kMaximumPacketSize = 32768;
class P2PSocketDispatcherHost::DnsRequest {
public:
typedef base::Callback<void(const net::IPAddressList&)> DoneCallback;
DnsRequest(int32 request_id, net::HostResolver* host_resolver)
: request_id_(request_id),
resolver_(host_resolver) {
}
void Resolve(const std::string& host_name,
const DoneCallback& done_callback) {
DCHECK(!done_callback.is_null());
host_name_ = host_name;
done_callback_ = done_callback;
// Return an error if it's an empty string.
if (host_name_.empty()) {
net::IPAddressList address_list;
done_callback_.Run(address_list);
return;
}
// Add period at the end to make sure that we only resolve
// fully-qualified names.
if (host_name_.at(host_name_.size() - 1) != '.')
host_name_ = host_name_ + '.';
net::HostResolver::RequestInfo info(net::HostPortPair(host_name_, 0));
int result = resolver_.Resolve(
info,
net::DEFAULT_PRIORITY,
&addresses_,
base::Bind(&P2PSocketDispatcherHost::DnsRequest::OnDone,
base::Unretained(this)),
net::BoundNetLog());
if (result != net::ERR_IO_PENDING)
OnDone(result);
}
int32 request_id() { return request_id_; }
private:
void OnDone(int result) {
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
tracked_objects::ScopedTracker tracking_profile(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436634 P2PSocketDispatcherHost::DnsRequest::OnDone"));
net::IPAddressList list;
if (result != net::OK) {
LOG(ERROR) << "Failed to resolve address for " << host_name_
<< ", errorcode: " << result;
done_callback_.Run(list);
return;
}
DCHECK(!addresses_.empty());
for (net::AddressList::iterator iter = addresses_.begin();
iter != addresses_.end(); ++iter) {
list.push_back(iter->address());
}
done_callback_.Run(list);
}
int32 request_id_;
net::AddressList addresses_;
std::string host_name_;
net::SingleRequestHostResolver resolver_;
DoneCallback done_callback_;
};
P2PSocketDispatcherHost::P2PSocketDispatcherHost(
content::ResourceContext* resource_context,
net::URLRequestContextGetter* url_context)
: BrowserMessageFilter(P2PMsgStart),
resource_context_(resource_context),
url_context_(url_context),
monitoring_networks_(false),
dump_incoming_rtp_packet_(false),
dump_outgoing_rtp_packet_(false) {
}
void P2PSocketDispatcherHost::OnChannelClosing() {
// Since the IPC sender is gone, close pending connections.
STLDeleteContainerPairSecondPointers(sockets_.begin(), sockets_.end());
sockets_.clear();
STLDeleteContainerPointers(dns_requests_.begin(), dns_requests_.end());
dns_requests_.clear();
if (monitoring_networks_) {
net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
monitoring_networks_ = false;
}
}
void P2PSocketDispatcherHost::OnDestruct() const {
BrowserThread::DeleteOnIOThread::Destruct(this);
}
bool P2PSocketDispatcherHost::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(P2PSocketDispatcherHost, message)
IPC_MESSAGE_HANDLER(P2PHostMsg_StartNetworkNotifications,
OnStartNetworkNotifications)
IPC_MESSAGE_HANDLER(P2PHostMsg_StopNetworkNotifications,
OnStopNetworkNotifications)
IPC_MESSAGE_HANDLER(P2PHostMsg_GetHostAddress, OnGetHostAddress)
IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket)
IPC_MESSAGE_HANDLER(P2PHostMsg_AcceptIncomingTcpConnection,
OnAcceptIncomingTcpConnection)
IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend)
IPC_MESSAGE_HANDLER(P2PHostMsg_SetOption, OnSetOption)
IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void P2PSocketDispatcherHost::OnIPAddressChanged() {
// Notify the renderer about changes to list of network interfaces.
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE, base::Bind(
&P2PSocketDispatcherHost::DoGetNetworkList, this));
}
void P2PSocketDispatcherHost::StartRtpDump(
bool incoming,
bool outgoing,
const RenderProcessHost::WebRtcRtpPacketCallback& packet_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if ((!dump_incoming_rtp_packet_ && incoming) ||
(!dump_outgoing_rtp_packet_ && outgoing)) {
if (incoming)
dump_incoming_rtp_packet_ = true;
if (outgoing)
dump_outgoing_rtp_packet_ = true;
packet_callback_ = packet_callback;
for (SocketsMap::iterator it = sockets_.begin(); it != sockets_.end(); ++it)
it->second->StartRtpDump(incoming, outgoing, packet_callback);
}
}
void P2PSocketDispatcherHost::StopRtpDumpOnUIThread(bool incoming,
bool outgoing) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&P2PSocketDispatcherHost::StopRtpDumpOnIOThread,
this,
incoming,
outgoing));
}
P2PSocketDispatcherHost::~P2PSocketDispatcherHost() {
DCHECK(sockets_.empty());
DCHECK(dns_requests_.empty());
if (monitoring_networks_)
net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
}
P2PSocketHost* P2PSocketDispatcherHost::LookupSocket(int socket_id) {
SocketsMap::iterator it = sockets_.find(socket_id);
return (it == sockets_.end()) ? NULL : it->second;
}
void P2PSocketDispatcherHost::OnStartNetworkNotifications() {
if (!monitoring_networks_) {
net::NetworkChangeNotifier::AddIPAddressObserver(this);
monitoring_networks_ = true;
}
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE, base::Bind(
&P2PSocketDispatcherHost::DoGetNetworkList, this));
}
void P2PSocketDispatcherHost::OnStopNetworkNotifications() {
if (monitoring_networks_) {
net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
monitoring_networks_ = false;
}
}
void P2PSocketDispatcherHost::OnGetHostAddress(const std::string& host_name,
int32 request_id) {
DnsRequest* request = new DnsRequest(request_id,
resource_context_->GetHostResolver());
dns_requests_.insert(request);
request->Resolve(host_name, base::Bind(
&P2PSocketDispatcherHost::OnAddressResolved,
base::Unretained(this), request));
}
void P2PSocketDispatcherHost::OnCreateSocket(
P2PSocketType type, int socket_id,
const net::IPEndPoint& local_address,
const P2PHostAndIPEndPoint& remote_address) {
if (LookupSocket(socket_id)) {
LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket "
"that already exists.";
return;
}
scoped_ptr<P2PSocketHost> socket(P2PSocketHost::Create(
this, socket_id, type, url_context_.get(), &throttler_));
if (!socket) {
Send(new P2PMsg_OnError(socket_id));
return;
}
if (socket->Init(local_address, remote_address)) {
sockets_[socket_id] = socket.release();
if (dump_incoming_rtp_packet_ || dump_outgoing_rtp_packet_) {
sockets_[socket_id]->StartRtpDump(dump_incoming_rtp_packet_,
dump_outgoing_rtp_packet_,
packet_callback_);
}
}
}
void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection(
int listen_socket_id, const net::IPEndPoint& remote_address,
int connected_socket_id) {
P2PSocketHost* socket = LookupSocket(listen_socket_id);
if (!socket) {
LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection "
"for invalid listen_socket_id.";
return;
}
if (LookupSocket(connected_socket_id) != NULL) {
LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection "
"for duplicated connected_socket_id.";
return;
}
P2PSocketHost* accepted_connection =
socket->AcceptIncomingTcpConnection(remote_address, connected_socket_id);
if (accepted_connection) {
sockets_[connected_socket_id] = accepted_connection;
}
}
void P2PSocketDispatcherHost::OnSend(int socket_id,
const net::IPEndPoint& socket_address,
const std::vector<char>& data,
const rtc::PacketOptions& options,
uint64 packet_id) {
P2PSocketHost* socket = LookupSocket(socket_id);
if (!socket) {
LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id.";
return;
}
if (data.size() > kMaximumPacketSize) {
LOG(ERROR) << "Received P2PHostMsg_Send with a packet that is too big: "
<< data.size();
Send(new P2PMsg_OnError(socket_id));
delete socket;
sockets_.erase(socket_id);
return;
}
socket->Send(socket_address, data, options, packet_id);
}
void P2PSocketDispatcherHost::OnSetOption(int socket_id,
P2PSocketOption option,
int value) {
P2PSocketHost* socket = LookupSocket(socket_id);
if (!socket) {
LOG(ERROR) << "Received P2PHostMsg_SetOption for invalid socket_id.";
return;
}
socket->SetOption(option, value);
}
void P2PSocketDispatcherHost::OnDestroySocket(int socket_id) {
SocketsMap::iterator it = sockets_.find(socket_id);
if (it != sockets_.end()) {
delete it->second;
sockets_.erase(it);
} else {
LOG(ERROR) << "Received P2PHostMsg_DestroySocket for invalid socket_id.";
}
}
void P2PSocketDispatcherHost::DoGetNetworkList() {
net::NetworkInterfaceList list;
net::GetNetworkList(&list, net::EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE, base::Bind(
&P2PSocketDispatcherHost::SendNetworkList, this, list));
}
void P2PSocketDispatcherHost::SendNetworkList(
const net::NetworkInterfaceList& list) {
Send(new P2PMsg_NetworkListChanged(list));
}
void P2PSocketDispatcherHost::OnAddressResolved(
DnsRequest* request,
const net::IPAddressList& addresses) {
Send(new P2PMsg_GetHostAddressResult(request->request_id(), addresses));
dns_requests_.erase(request);
delete request;
}
void P2PSocketDispatcherHost::StopRtpDumpOnIOThread(bool incoming,
bool outgoing) {
if ((dump_incoming_rtp_packet_ && incoming) ||
(dump_outgoing_rtp_packet_ && outgoing)) {
if (incoming)
dump_incoming_rtp_packet_ = false;
if (outgoing)
dump_outgoing_rtp_packet_ = false;
if (!dump_incoming_rtp_packet_ && !dump_outgoing_rtp_packet_)
packet_callback_.Reset();
for (SocketsMap::iterator it = sockets_.begin(); it != sockets_.end(); ++it)
it->second->StopRtpDump(incoming, outgoing);
}
}
} // namespace content
|