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