Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / p2p / socket_host_tcp.cc
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.
4
5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h"
6
7 #include "base/sys_byteorder.h"
8 #include "content/common/p2p_messages.h"
9 #include "ipc/ipc_sender.h"
10 #include "jingle/glue/fake_ssl_client_socket.h"
11 #include "jingle/glue/proxy_resolving_client_socket.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "net/socket/client_socket_factory.h"
16 #include "net/socket/client_socket_handle.h"
17 #include "net/socket/ssl_client_socket.h"
18 #include "net/socket/tcp_client_socket.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "third_party/webrtc/base/asyncpacketsocket.h"
22
23 namespace {
24
25 typedef uint16 PacketLength;
26 const int kPacketHeaderSize = sizeof(PacketLength);
27 const int kReadBufferSize = 4096;
28 const int kPacketLengthOffset = 2;
29 const int kTurnChannelDataHeaderSize = 4;
30 const int kRecvSocketBufferSize = 128 * 1024;
31 const int kSendSocketBufferSize = 128 * 1024;
32
33 bool IsTlsClientSocket(content::P2PSocketType type) {
34   return (type == content::P2P_SOCKET_STUN_TLS_CLIENT ||
35           type == content::P2P_SOCKET_TLS_CLIENT);
36 }
37
38 bool IsPseudoTlsClientSocket(content::P2PSocketType type) {
39   return (type == content::P2P_SOCKET_SSLTCP_CLIENT ||
40           type == content::P2P_SOCKET_STUN_SSLTCP_CLIENT);
41 }
42
43 }  // namespace
44
45 namespace content {
46
47 P2PSocketHostTcpBase::P2PSocketHostTcpBase(
48     IPC::Sender* message_sender,
49     int socket_id,
50     P2PSocketType type,
51     net::URLRequestContextGetter* url_context)
52     : P2PSocketHost(message_sender, socket_id, P2PSocketHost::TCP),
53       write_pending_(false),
54       connected_(false),
55       type_(type),
56       url_context_(url_context) {
57 }
58
59 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() {
60   if (state_ == STATE_OPEN) {
61     DCHECK(socket_.get());
62     socket_.reset();
63   }
64 }
65
66 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address,
67                                         net::StreamSocket* socket) {
68   DCHECK(socket);
69   DCHECK_EQ(state_, STATE_UNINITIALIZED);
70
71   remote_address_.ip_address = remote_address;
72   // TODO(ronghuawu): Add FakeSSLServerSocket.
73   socket_.reset(socket);
74   state_ = STATE_OPEN;
75   DoRead();
76   return state_ != STATE_ERROR;
77 }
78
79 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address,
80                                 const P2PHostAndIPEndPoint& remote_address) {
81   DCHECK_EQ(state_, STATE_UNINITIALIZED);
82
83   remote_address_ = remote_address;
84   state_ = STATE_CONNECTING;
85
86   net::HostPortPair dest_host_port_pair;
87   // If there is no resolved address, let's try with domain name, assuming
88   // socket layer will do the DNS resolve.
89   if (remote_address.ip_address.address().empty()) {
90     DCHECK(!remote_address.hostname.empty());
91     dest_host_port_pair = net::HostPortPair(remote_address.hostname,
92                                             remote_address.ip_address.port());
93   } else {
94     dest_host_port_pair = net::HostPortPair::FromIPEndPoint(
95         remote_address.ip_address);
96   }
97
98   // TODO(mallinath) - We are ignoring local_address altogether. We should
99   // find a way to inject this into ProxyResolvingClientSocket. This could be
100   // a problem on multi-homed host.
101
102   // The default SSLConfig is good enough for us for now.
103   const net::SSLConfig ssl_config;
104   socket_.reset(new jingle_glue::ProxyResolvingClientSocket(
105                     NULL,     // Default socket pool provided by the net::Proxy.
106                     url_context_,
107                     ssl_config,
108                     dest_host_port_pair));
109
110   int status = socket_->Connect(
111       base::Bind(&P2PSocketHostTcpBase::OnConnected,
112                  base::Unretained(this)));
113   if (status != net::ERR_IO_PENDING) {
114     // We defer execution of ProcessConnectDone instead of calling it
115     // directly here as the caller may not expect an error/close to
116     // happen here.  This is okay, as from the caller's point of view,
117     // the connect always happens asynchronously.
118     base::MessageLoop* message_loop = base::MessageLoop::current();
119     CHECK(message_loop);
120     message_loop->PostTask(
121         FROM_HERE,
122         base::Bind(&P2PSocketHostTcpBase::OnConnected,
123                    base::Unretained(this), status));
124   }
125
126   return state_ != STATE_ERROR;
127 }
128
129 void P2PSocketHostTcpBase::OnError() {
130   socket_.reset();
131
132   if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING ||
133       state_ == STATE_TLS_CONNECTING || state_ == STATE_OPEN) {
134     message_sender_->Send(new P2PMsg_OnError(id_));
135   }
136
137   state_ = STATE_ERROR;
138 }
139
140 void P2PSocketHostTcpBase::OnConnected(int result) {
141   DCHECK_EQ(state_, STATE_CONNECTING);
142   DCHECK_NE(result, net::ERR_IO_PENDING);
143
144   if (result != net::OK) {
145     OnError();
146     return;
147   }
148
149   if (IsTlsClientSocket(type_)) {
150     state_ = STATE_TLS_CONNECTING;
151     StartTls();
152   } else if (IsPseudoTlsClientSocket(type_)) {
153     scoped_ptr<net::StreamSocket> transport_socket = socket_.Pass();
154     socket_.reset(
155         new jingle_glue::FakeSSLClientSocket(transport_socket.Pass()));
156     state_ = STATE_TLS_CONNECTING;
157     int status = socket_->Connect(
158         base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone,
159                    base::Unretained(this)));
160     if (status != net::ERR_IO_PENDING) {
161       ProcessTlsSslConnectDone(status);
162     }
163   } else {
164     // If we are not doing TLS, we are ready to send data now.
165     // In case of TLS, SignalConnect will be sent only after TLS handshake is
166     // successfull. So no buffering will be done at socket handlers if any
167     // packets sent before that by the application.
168     OnOpen();
169   }
170 }
171
172 void P2PSocketHostTcpBase::StartTls() {
173   DCHECK_EQ(state_, STATE_TLS_CONNECTING);
174   DCHECK(socket_.get());
175
176   scoped_ptr<net::ClientSocketHandle> socket_handle(
177       new net::ClientSocketHandle());
178   socket_handle->SetSocket(socket_.Pass());
179
180   net::SSLClientSocketContext context;
181   context.cert_verifier = url_context_->GetURLRequestContext()->cert_verifier();
182   context.transport_security_state =
183       url_context_->GetURLRequestContext()->transport_security_state();
184   DCHECK(context.transport_security_state);
185
186   // Default ssl config.
187   const net::SSLConfig ssl_config;
188   net::HostPortPair dest_host_port_pair;
189
190   // Calling net::HostPortPair::FromIPEndPoint will crash if the IP address is
191   // empty.
192   if (!remote_address_.ip_address.address().empty()) {
193       net::HostPortPair::FromIPEndPoint(remote_address_.ip_address);
194   } else {
195     dest_host_port_pair.set_port(remote_address_.ip_address.port());
196   }
197   if (!remote_address_.hostname.empty())
198     dest_host_port_pair.set_host(remote_address_.hostname);
199
200   net::ClientSocketFactory* socket_factory =
201       net::ClientSocketFactory::GetDefaultFactory();
202   DCHECK(socket_factory);
203
204   socket_ = socket_factory->CreateSSLClientSocket(
205       socket_handle.Pass(), dest_host_port_pair, ssl_config, context);
206   int status = socket_->Connect(
207       base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone,
208                  base::Unretained(this)));
209   if (status != net::ERR_IO_PENDING) {
210     ProcessTlsSslConnectDone(status);
211   }
212 }
213
214 void P2PSocketHostTcpBase::ProcessTlsSslConnectDone(int status) {
215   DCHECK_NE(status, net::ERR_IO_PENDING);
216   DCHECK_EQ(state_, STATE_TLS_CONNECTING);
217   if (status != net::OK) {
218     OnError();
219     return;
220   }
221   OnOpen();
222 }
223
224 void P2PSocketHostTcpBase::OnOpen() {
225   state_ = STATE_OPEN;
226   // Setting socket send and receive buffer size.
227   if (net::OK != socket_->SetReceiveBufferSize(kRecvSocketBufferSize)) {
228     LOG(WARNING) << "Failed to set socket receive buffer size to "
229                  << kRecvSocketBufferSize;
230   }
231
232   if (net::OK != socket_->SetSendBufferSize(kSendSocketBufferSize)) {
233     LOG(WARNING) << "Failed to set socket send buffer size to "
234                  << kSendSocketBufferSize;
235   }
236
237   if (!DoSendSocketCreateMsg())
238     return;
239
240   DCHECK_EQ(state_, STATE_OPEN);
241   DoRead();
242 }
243
244 bool P2PSocketHostTcpBase::DoSendSocketCreateMsg() {
245   DCHECK(socket_.get());
246
247   net::IPEndPoint local_address;
248   int result = socket_->GetLocalAddress(&local_address);
249   if (result < 0) {
250     LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get local"
251                << " address: " << result;
252     OnError();
253     return false;
254   }
255
256   VLOG(1) << "Local address: " << local_address.ToString();
257
258   net::IPEndPoint remote_address;
259   result = socket_->GetPeerAddress(&remote_address);
260   if (result < 0) {
261     LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get peer"
262                << " address: " << result;
263     OnError();
264     return false;
265   }
266   VLOG(1) << "Remote address: " << remote_address.ToString();
267   if (remote_address_.ip_address.address().empty()) {
268     // Save |remote_address| if address is empty.
269     remote_address_.ip_address = remote_address;
270   }
271
272   // If we are not doing TLS, we are ready to send data now.
273   // In case of TLS SignalConnect will be sent only after TLS handshake is
274   // successfull. So no buffering will be done at socket handlers if any
275   // packets sent before that by the application.
276   message_sender_->Send(new P2PMsg_OnSocketCreated(
277       id_, local_address, remote_address));
278   return true;
279 }
280
281 void P2PSocketHostTcpBase::DoRead() {
282   int result;
283   do {
284     if (!read_buffer_.get()) {
285       read_buffer_ = new net::GrowableIOBuffer();
286       read_buffer_->SetCapacity(kReadBufferSize);
287     } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) {
288       // Make sure that we always have at least kReadBufferSize of
289       // remaining capacity in the read buffer. Normally all packets
290       // are smaller than kReadBufferSize, so this is not really
291       // required.
292       read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize -
293                                 read_buffer_->RemainingCapacity());
294     }
295     result = socket_->Read(
296         read_buffer_.get(),
297         read_buffer_->RemainingCapacity(),
298         base::Bind(&P2PSocketHostTcp::OnRead, base::Unretained(this)));
299     DidCompleteRead(result);
300   } while (result > 0);
301 }
302
303 void P2PSocketHostTcpBase::OnRead(int result) {
304   DidCompleteRead(result);
305   if (state_ == STATE_OPEN) {
306     DoRead();
307   }
308 }
309
310 void P2PSocketHostTcpBase::OnPacket(const std::vector<char>& data) {
311   if (!connected_) {
312     P2PSocketHost::StunMessageType type;
313     bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
314     if (stun && IsRequestOrResponse(type)) {
315       connected_ = true;
316     } else if (!stun || type == STUN_DATA_INDICATION) {
317       LOG(ERROR) << "Received unexpected data packet from "
318                  << remote_address_.ip_address.ToString()
319                  << " before STUN binding is finished. "
320                  << "Terminating connection.";
321       OnError();
322       return;
323     }
324   }
325
326   message_sender_->Send(new P2PMsg_OnDataReceived(
327       id_, remote_address_.ip_address, data, base::TimeTicks::Now()));
328
329   if (dump_incoming_rtp_packet_)
330     DumpRtpPacket(&data[0], data.size(), true);
331 }
332
333 // Note: dscp is not actually used on TCP sockets as this point,
334 // but may be honored in the future.
335 void P2PSocketHostTcpBase::Send(const net::IPEndPoint& to,
336                                 const std::vector<char>& data,
337                                 const rtc::PacketOptions& options,
338                                 uint64 packet_id) {
339   if (!socket_) {
340     // The Send message may be sent after the an OnError message was
341     // sent by hasn't been processed the renderer.
342     return;
343   }
344
345   if (!(to == remote_address_.ip_address)) {
346     // Renderer should use this socket only to send data to |remote_address_|.
347     NOTREACHED();
348     OnError();
349     return;
350   }
351
352   if (!connected_) {
353     P2PSocketHost::StunMessageType type = P2PSocketHost::StunMessageType();
354     bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
355     if (!stun || type == STUN_DATA_INDICATION) {
356       LOG(ERROR) << "Page tried to send a data packet to " << to.ToString()
357                  << " before STUN binding is finished.";
358       OnError();
359       return;
360     }
361   }
362
363   DoSend(to, data, options);
364 }
365
366 void P2PSocketHostTcpBase::WriteOrQueue(
367     scoped_refptr<net::DrainableIOBuffer>& buffer) {
368   IncrementTotalSentPackets();
369   if (write_buffer_.get()) {
370     write_queue_.push(buffer);
371     IncrementDelayedPackets();
372     IncrementDelayedBytes(buffer->size());
373     return;
374   }
375
376   write_buffer_ = buffer;
377   DoWrite();
378 }
379
380 void P2PSocketHostTcpBase::DoWrite() {
381   while (write_buffer_.get() && state_ == STATE_OPEN && !write_pending_) {
382     int result = socket_->Write(
383         write_buffer_.get(),
384         write_buffer_->BytesRemaining(),
385         base::Bind(&P2PSocketHostTcp::OnWritten, base::Unretained(this)));
386     HandleWriteResult(result);
387   }
388 }
389
390 void P2PSocketHostTcpBase::OnWritten(int result) {
391   DCHECK(write_pending_);
392   DCHECK_NE(result, net::ERR_IO_PENDING);
393
394   write_pending_ = false;
395   HandleWriteResult(result);
396   DoWrite();
397 }
398
399 void P2PSocketHostTcpBase::HandleWriteResult(int result) {
400   DCHECK(write_buffer_.get());
401   if (result >= 0) {
402     write_buffer_->DidConsume(result);
403     if (write_buffer_->BytesRemaining() == 0) {
404       message_sender_->Send(new P2PMsg_OnSendComplete(id_));
405       if (write_queue_.empty()) {
406         write_buffer_ = NULL;
407       } else {
408         write_buffer_ = write_queue_.front();
409         write_queue_.pop();
410         // Update how many bytes are still waiting to be sent.
411         DecrementDelayedBytes(write_buffer_->size());
412       }
413     }
414   } else if (result == net::ERR_IO_PENDING) {
415     write_pending_ = true;
416   } else {
417     LOG(ERROR) << "Error when sending data in TCP socket: " << result;
418     OnError();
419   }
420 }
421
422 P2PSocketHost* P2PSocketHostTcpBase::AcceptIncomingTcpConnection(
423     const net::IPEndPoint& remote_address, int id) {
424   NOTREACHED();
425   OnError();
426   return NULL;
427 }
428
429 void P2PSocketHostTcpBase::DidCompleteRead(int result) {
430   DCHECK_EQ(state_, STATE_OPEN);
431
432   if (result == net::ERR_IO_PENDING) {
433     return;
434   } else if (result < 0) {
435     LOG(ERROR) << "Error when reading from TCP socket: " << result;
436     OnError();
437     return;
438   }
439
440   read_buffer_->set_offset(read_buffer_->offset() + result);
441   char* head = read_buffer_->StartOfBuffer();  // Purely a convenience.
442   int pos = 0;
443   while (pos <= read_buffer_->offset() && state_ == STATE_OPEN) {
444     int consumed = ProcessInput(head + pos, read_buffer_->offset() - pos);
445     if (!consumed)
446       break;
447     pos += consumed;
448   }
449   // We've consumed all complete packets from the buffer; now move any remaining
450   // bytes to the head of the buffer and set offset to reflect this.
451   if (pos && pos <= read_buffer_->offset()) {
452     memmove(head, head + pos, read_buffer_->offset() - pos);
453     read_buffer_->set_offset(read_buffer_->offset() - pos);
454   }
455 }
456
457 bool P2PSocketHostTcpBase::SetOption(P2PSocketOption option, int value) {
458   DCHECK_EQ(STATE_OPEN, state_);
459   switch (option) {
460     case P2P_SOCKET_OPT_RCVBUF:
461       return socket_->SetReceiveBufferSize(value) == net::OK;
462     case P2P_SOCKET_OPT_SNDBUF:
463       return socket_->SetSendBufferSize(value) == net::OK;
464     case P2P_SOCKET_OPT_DSCP:
465       return false;  // For TCP sockets DSCP setting is not available.
466     default:
467       NOTREACHED();
468       return false;
469   }
470 }
471
472 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender,
473                                    int socket_id,
474                                    P2PSocketType type,
475                                    net::URLRequestContextGetter* url_context)
476     : P2PSocketHostTcpBase(message_sender, socket_id, type, url_context) {
477   DCHECK(type == P2P_SOCKET_TCP_CLIENT ||
478          type == P2P_SOCKET_SSLTCP_CLIENT ||
479          type == P2P_SOCKET_TLS_CLIENT);
480 }
481
482 P2PSocketHostTcp::~P2PSocketHostTcp() {
483 }
484
485 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) {
486   if (input_len < kPacketHeaderSize)
487     return 0;
488   int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input));
489   if (input_len < packet_size + kPacketHeaderSize)
490     return 0;
491
492   int consumed = kPacketHeaderSize;
493   char* cur = input + consumed;
494   std::vector<char> data(cur, cur + packet_size);
495   OnPacket(data);
496   consumed += packet_size;
497   return consumed;
498 }
499
500 void P2PSocketHostTcp::DoSend(const net::IPEndPoint& to,
501                               const std::vector<char>& data,
502                               const rtc::PacketOptions& options) {
503   int size = kPacketHeaderSize + data.size();
504   scoped_refptr<net::DrainableIOBuffer> buffer =
505       new net::DrainableIOBuffer(new net::IOBuffer(size), size);
506   *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size());
507   memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size());
508
509   packet_processing_helpers::ApplyPacketOptions(
510       buffer->data() + kPacketHeaderSize,
511       buffer->BytesRemaining() - kPacketHeaderSize,
512       options, 0);
513
514   WriteOrQueue(buffer);
515 }
516
517 // P2PSocketHostStunTcp
518 P2PSocketHostStunTcp::P2PSocketHostStunTcp(
519     IPC::Sender* message_sender,
520     int socket_id,
521     P2PSocketType type,
522     net::URLRequestContextGetter* url_context)
523     : P2PSocketHostTcpBase(message_sender, socket_id, type, url_context) {
524   DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT ||
525          type == P2P_SOCKET_STUN_SSLTCP_CLIENT ||
526          type == P2P_SOCKET_STUN_TLS_CLIENT);
527 }
528
529 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() {
530 }
531
532 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) {
533   if (input_len < kPacketHeaderSize + kPacketLengthOffset)
534     return 0;
535
536   int pad_bytes;
537   int packet_size = GetExpectedPacketSize(
538       input, input_len, &pad_bytes);
539
540   if (input_len < packet_size + pad_bytes)
541     return 0;
542
543   // We have a complete packet. Read through it.
544   int consumed = 0;
545   char* cur = input;
546   std::vector<char> data(cur, cur + packet_size);
547   OnPacket(data);
548   consumed += packet_size;
549   consumed += pad_bytes;
550   return consumed;
551 }
552
553 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint& to,
554                                   const std::vector<char>& data,
555                                   const rtc::PacketOptions& options) {
556   // Each packet is expected to have header (STUN/TURN ChannelData), where
557   // header contains message type and and length of message.
558   if (data.size() < kPacketHeaderSize + kPacketLengthOffset) {
559     NOTREACHED();
560     OnError();
561     return;
562   }
563
564   int pad_bytes;
565   size_t expected_len = GetExpectedPacketSize(
566       &data[0], data.size(), &pad_bytes);
567
568   // Accepts only complete STUN/TURN packets.
569   if (data.size() != expected_len) {
570     NOTREACHED();
571     OnError();
572     return;
573   }
574
575   // Add any pad bytes to the total size.
576   int size = data.size() + pad_bytes;
577
578   scoped_refptr<net::DrainableIOBuffer> buffer =
579       new net::DrainableIOBuffer(new net::IOBuffer(size), size);
580   memcpy(buffer->data(), &data[0], data.size());
581
582   packet_processing_helpers::ApplyPacketOptions(
583       buffer->data(), data.size(), options, 0);
584
585   if (pad_bytes) {
586     char padding[4] = {0};
587     DCHECK_LE(pad_bytes, 4);
588     memcpy(buffer->data() + data.size(), padding, pad_bytes);
589   }
590   WriteOrQueue(buffer);
591
592   if (dump_outgoing_rtp_packet_)
593     DumpRtpPacket(buffer->data(), data.size(), false);
594 }
595
596 int P2PSocketHostStunTcp::GetExpectedPacketSize(
597     const char* data, int len, int* pad_bytes) {
598   DCHECK_LE(kTurnChannelDataHeaderSize, len);
599   // Both stun and turn had length at offset 2.
600   int packet_size = base::NetToHost16(*reinterpret_cast<const uint16*>(
601       data + kPacketLengthOffset));
602
603   // Get packet type (STUN or TURN).
604   uint16 msg_type = base::NetToHost16(*reinterpret_cast<const uint16*>(data));
605
606   *pad_bytes = 0;
607   // Add heder length to packet length.
608   if ((msg_type & 0xC000) == 0) {
609     packet_size += kStunHeaderSize;
610   } else {
611     packet_size += kTurnChannelDataHeaderSize;
612     // Calculate any padding if present.
613     if (packet_size % 4)
614       *pad_bytes = 4 - packet_size % 4;
615   }
616   return packet_size;
617 }
618
619 }  // namespace content