1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/p2p/socket_dispatcher_host.h"
8 #include "base/stl_util.h"
9 #include "content/browser/renderer_host/p2p/socket_host.h"
10 #include "content/common/p2p_messages.h"
11 #include "content/public/browser/resource_context.h"
12 #include "net/base/address_list.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/net_log.h"
16 #include "net/base/sys_addrinfo.h"
17 #include "net/dns/single_request_host_resolver.h"
18 #include "net/url_request/url_request_context_getter.h"
20 using content::BrowserMessageFilter
;
21 using content::BrowserThread
;
25 const size_t kMaximumPacketSize
= 32768;
27 class P2PSocketDispatcherHost::DnsRequest
{
29 typedef base::Callback
<void(const net::IPAddressList
&)> DoneCallback
;
31 DnsRequest(int32 request_id
, net::HostResolver
* host_resolver
)
32 : request_id_(request_id
),
33 resolver_(host_resolver
) {
36 void Resolve(const std::string
& host_name
,
37 const DoneCallback
& done_callback
) {
38 DCHECK(!done_callback
.is_null());
40 host_name_
= host_name
;
41 done_callback_
= done_callback
;
43 // Return an error if it's an empty string.
44 if (host_name_
.empty()) {
45 net::IPAddressList address_list
;
46 done_callback_
.Run(address_list
);
50 // Add period at the end to make sure that we only resolve
51 // fully-qualified names.
52 if (host_name_
.at(host_name_
.size() - 1) != '.')
53 host_name_
= host_name_
+ '.';
55 net::HostResolver::RequestInfo
info(net::HostPortPair(host_name_
, 0));
56 int result
= resolver_
.Resolve(
58 net::DEFAULT_PRIORITY
,
60 base::Bind(&P2PSocketDispatcherHost::DnsRequest::OnDone
,
61 base::Unretained(this)),
63 if (result
!= net::ERR_IO_PENDING
)
67 int32
request_id() { return request_id_
; }
70 void OnDone(int result
) {
71 net::IPAddressList list
;
72 if (result
!= net::OK
) {
73 LOG(ERROR
) << "Failed to resolve address for " << host_name_
74 << ", errorcode: " << result
;
75 done_callback_
.Run(list
);
79 DCHECK(!addresses_
.empty());
80 for (net::AddressList::iterator iter
= addresses_
.begin();
81 iter
!= addresses_
.end(); ++iter
) {
82 list
.push_back(iter
->address());
84 done_callback_
.Run(list
);
88 net::AddressList addresses_
;
90 std::string host_name_
;
91 net::SingleRequestHostResolver resolver_
;
93 DoneCallback done_callback_
;
96 P2PSocketDispatcherHost::P2PSocketDispatcherHost(
97 content::ResourceContext
* resource_context
,
98 net::URLRequestContextGetter
* url_context
)
99 : resource_context_(resource_context
),
100 url_context_(url_context
),
101 monitoring_networks_(false) {
104 void P2PSocketDispatcherHost::OnChannelClosing() {
105 // Since the IPC channel is gone, close pending connections.
106 STLDeleteContainerPairSecondPointers(sockets_
.begin(), sockets_
.end());
109 STLDeleteContainerPointers(dns_requests_
.begin(), dns_requests_
.end());
110 dns_requests_
.clear();
112 if (monitoring_networks_
) {
113 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
114 monitoring_networks_
= false;
118 void P2PSocketDispatcherHost::OnDestruct() const {
119 BrowserThread::DeleteOnIOThread::Destruct(this);
122 bool P2PSocketDispatcherHost::OnMessageReceived(const IPC::Message
& message
,
123 bool* message_was_ok
) {
125 IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketDispatcherHost
, message
, *message_was_ok
)
126 IPC_MESSAGE_HANDLER(P2PHostMsg_StartNetworkNotifications
,
127 OnStartNetworkNotifications
)
128 IPC_MESSAGE_HANDLER(P2PHostMsg_StopNetworkNotifications
,
129 OnStopNetworkNotifications
)
130 IPC_MESSAGE_HANDLER(P2PHostMsg_GetHostAddress
, OnGetHostAddress
)
131 IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket
, OnCreateSocket
)
132 IPC_MESSAGE_HANDLER(P2PHostMsg_AcceptIncomingTcpConnection
,
133 OnAcceptIncomingTcpConnection
)
134 IPC_MESSAGE_HANDLER(P2PHostMsg_Send
, OnSend
)
135 IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket
, OnDestroySocket
)
136 IPC_MESSAGE_UNHANDLED(handled
= false)
137 IPC_END_MESSAGE_MAP_EX()
141 void P2PSocketDispatcherHost::OnIPAddressChanged() {
142 // Notify the renderer about changes to list of network interfaces.
143 BrowserThread::PostTask(
144 BrowserThread::FILE, FROM_HERE
, base::Bind(
145 &P2PSocketDispatcherHost::DoGetNetworkList
, this));
148 P2PSocketDispatcherHost::~P2PSocketDispatcherHost() {
149 DCHECK(sockets_
.empty());
150 DCHECK(dns_requests_
.empty());
152 if (monitoring_networks_
)
153 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
156 P2PSocketHost
* P2PSocketDispatcherHost::LookupSocket(int socket_id
) {
157 SocketsMap::iterator it
= sockets_
.find(socket_id
);
158 return (it
== sockets_
.end()) ? NULL
: it
->second
;
161 void P2PSocketDispatcherHost::OnStartNetworkNotifications(
162 const IPC::Message
& msg
) {
163 if (!monitoring_networks_
) {
164 net::NetworkChangeNotifier::AddIPAddressObserver(this);
165 monitoring_networks_
= true;
168 BrowserThread::PostTask(
169 BrowserThread::FILE, FROM_HERE
, base::Bind(
170 &P2PSocketDispatcherHost::DoGetNetworkList
, this));
173 void P2PSocketDispatcherHost::OnStopNetworkNotifications(
174 const IPC::Message
& msg
) {
175 if (monitoring_networks_
) {
176 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
177 monitoring_networks_
= false;
181 void P2PSocketDispatcherHost::OnGetHostAddress(const std::string
& host_name
,
183 DnsRequest
* request
= new DnsRequest(request_id
,
184 resource_context_
->GetHostResolver());
185 dns_requests_
.insert(request
);
186 request
->Resolve(host_name
, base::Bind(
187 &P2PSocketDispatcherHost::OnAddressResolved
,
188 base::Unretained(this), request
));
191 void P2PSocketDispatcherHost::OnCreateSocket(
192 P2PSocketType type
, int socket_id
,
193 const net::IPEndPoint
& local_address
,
194 const net::IPEndPoint
& remote_address
) {
195 if (LookupSocket(socket_id
)) {
196 LOG(ERROR
) << "Received P2PHostMsg_CreateSocket for socket "
197 "that already exists.";
201 scoped_ptr
<P2PSocketHost
> socket(P2PSocketHost::Create(
202 this, socket_id
, type
, url_context_
.get(), &throttler_
));
205 Send(new P2PMsg_OnError(socket_id
));
209 if (socket
->Init(local_address
, remote_address
)) {
210 sockets_
[socket_id
] = socket
.release();
214 void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection(
215 int listen_socket_id
, const net::IPEndPoint
& remote_address
,
216 int connected_socket_id
) {
217 P2PSocketHost
* socket
= LookupSocket(listen_socket_id
);
219 LOG(ERROR
) << "Received P2PHostMsg_AcceptIncomingTcpConnection "
220 "for invalid socket_id.";
223 P2PSocketHost
* accepted_connection
=
224 socket
->AcceptIncomingTcpConnection(remote_address
, connected_socket_id
);
225 if (accepted_connection
) {
226 sockets_
[connected_socket_id
] = accepted_connection
;
230 void P2PSocketDispatcherHost::OnSend(int socket_id
,
231 const net::IPEndPoint
& socket_address
,
232 const std::vector
<char>& data
,
233 net::DiffServCodePoint dscp
,
235 P2PSocketHost
* socket
= LookupSocket(socket_id
);
237 LOG(ERROR
) << "Received P2PHostMsg_Send for invalid socket_id.";
241 if (data
.size() > kMaximumPacketSize
) {
242 LOG(ERROR
) << "Received P2PHostMsg_Send with a packet that is too big: "
244 Send(new P2PMsg_OnError(socket_id
));
246 sockets_
.erase(socket_id
);
250 socket
->Send(socket_address
, data
, dscp
, packet_id
);
253 void P2PSocketDispatcherHost::OnDestroySocket(int socket_id
) {
254 SocketsMap::iterator it
= sockets_
.find(socket_id
);
255 if (it
!= sockets_
.end()) {
259 LOG(ERROR
) << "Received P2PHostMsg_DestroySocket for invalid socket_id.";
263 void P2PSocketDispatcherHost::DoGetNetworkList() {
264 net::NetworkInterfaceList list
;
265 net::GetNetworkList(&list
, net::EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES
);
266 BrowserThread::PostTask(
267 BrowserThread::IO
, FROM_HERE
, base::Bind(
268 &P2PSocketDispatcherHost::SendNetworkList
, this, list
));
271 void P2PSocketDispatcherHost::SendNetworkList(
272 const net::NetworkInterfaceList
& list
) {
273 Send(new P2PMsg_NetworkListChanged(list
));
276 void P2PSocketDispatcherHost::OnAddressResolved(
278 const net::IPAddressList
& addresses
) {
279 Send(new P2PMsg_GetHostAddressResult(request
->request_id(), addresses
));
281 dns_requests_
.erase(request
);
285 } // namespace content