Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / p2p / base / port.cc
index ad692ce..3820154 100644 (file)
 #include <algorithm>
 #include <vector>
 
-#include "talk/base/base64.h"
-#include "talk/base/crc32.h"
-#include "talk/base/helpers.h"
-#include "talk/base/logging.h"
-#include "talk/base/messagedigest.h"
-#include "talk/base/scoped_ptr.h"
-#include "talk/base/stringencode.h"
-#include "talk/base/stringutils.h"
 #include "talk/p2p/base/common.h"
+#include "webrtc/base/base64.h"
+#include "webrtc/base/crc32.h"
+#include "webrtc/base/helpers.h"
+#include "webrtc/base/logging.h"
+#include "webrtc/base/messagedigest.h"
+#include "webrtc/base/scoped_ptr.h"
+#include "webrtc/base/stringencode.h"
+#include "webrtc/base/stringutils.h"
 
 namespace {
 
@@ -81,7 +81,7 @@ std::string GetRtcpUfragFromRtpUfrag(const std::string& rtp_ufrag) {
   }
   // Change the last character to the one next to it in the base64 table.
   char new_last_char;
-  if (!talk_base::Base64::GetNextBase64Char(rtp_ufrag[rtp_ufrag.size() - 1],
+  if (!rtc::Base64::GetNextBase64Char(rtp_ufrag[rtp_ufrag.size() - 1],
                                             &new_last_char)) {
     // Should not be here.
     ASSERT(false);
@@ -103,7 +103,7 @@ const uint32 DEFAULT_RTT = MAXIMUM_RTT;
 
 // Computes our estimate of the RTT given the current estimate.
 inline uint32 ConservativeRTTEstimate(uint32 rtt) {
-  return talk_base::_max(MINIMUM_RTT, talk_base::_min(MAXIMUM_RTT, 2 * rtt));
+  return rtc::_max(MINIMUM_RTT, rtc::_min(MAXIMUM_RTT, 2 * rtt));
 }
 
 // Weighting of the old rtt value to new data.
@@ -147,6 +147,12 @@ bool StringToProto(const char* value, ProtocolType* proto) {
   return false;
 }
 
+// RFC 6544, TCP candidate encoding rules.
+const int DISCARD_PORT = 9;
+const char TCPTYPE_ACTIVE_STR[] = "active";
+const char TCPTYPE_PASSIVE_STR[] = "passive";
+const char TCPTYPE_SIMOPEN_STR[] = "so";
+
 // Foundation:  An arbitrary string that is the same for two candidates
 //   that have the same type, base IP address, protocol (UDP, TCP,
 //   etc.), and STUN or TURN server.  If any of these are different,
@@ -156,14 +162,14 @@ bool StringToProto(const char* value, ProtocolType* proto) {
 static std::string ComputeFoundation(
     const std::string& type,
     const std::string& protocol,
-    const talk_base::SocketAddress& base_address) {
+    const rtc::SocketAddress& base_address) {
   std::ostringstream ost;
   ost << type << base_address.ipaddr().ToString() << protocol;
-  return talk_base::ToString<uint32>(talk_base::ComputeCrc32(ost.str()));
+  return rtc::ToString<uint32>(rtc::ComputeCrc32(ost.str()));
 }
 
-Port::Port(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
-           talk_base::Network* network, const talk_base::IPAddress& ip,
+Port::Port(rtc::Thread* thread, rtc::PacketSocketFactory* factory,
+           rtc::Network* network, const rtc::IPAddress& ip,
            const std::string& username_fragment, const std::string& password)
     : thread_(thread),
       factory_(factory),
@@ -185,9 +191,9 @@ Port::Port(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
   Construct();
 }
 
-Port::Port(talk_base::Thread* thread, const std::string& type,
-           talk_base::PacketSocketFactory* factory,
-           talk_base::Network* network, const talk_base::IPAddress& ip,
+Port::Port(rtc::Thread* thread, const std::string& type,
+           rtc::PacketSocketFactory* factory,
+           rtc::Network* network, const rtc::IPAddress& ip,
            int min_port, int max_port, const std::string& username_fragment,
            const std::string& password)
     : thread_(thread),
@@ -216,8 +222,8 @@ void Port::Construct() {
   // If the username_fragment and password are empty, we should just create one.
   if (ice_username_fragment_.empty()) {
     ASSERT(password_.empty());
-    ice_username_fragment_ = talk_base::CreateRandomString(ICE_UFRAG_LENGTH);
-    password_ = talk_base::CreateRandomString(ICE_PWD_LENGTH);
+    ice_username_fragment_ = rtc::CreateRandomString(ICE_UFRAG_LENGTH);
+    password_ = rtc::CreateRandomString(ICE_PWD_LENGTH);
   }
   LOG_J(LS_INFO, this) << "Port created";
 }
@@ -238,7 +244,7 @@ Port::~Port() {
     delete list[i];
 }
 
-Connection* Port::GetConnection(const talk_base::SocketAddress& remote_addr) {
+Connection* Port::GetConnection(const rtc::SocketAddress& remote_addr) {
   AddressMap::const_iterator iter = connections_.find(remote_addr);
   if (iter != connections_.end())
     return iter->second;
@@ -246,20 +252,28 @@ Connection* Port::GetConnection(const talk_base::SocketAddress& remote_addr) {
     return NULL;
 }
 
-void Port::AddAddress(const talk_base::SocketAddress& address,
-                      const talk_base::SocketAddress& base_address,
-                      const talk_base::SocketAddress& related_address,
+void Port::AddAddress(const rtc::SocketAddress& address,
+                      const rtc::SocketAddress& base_address,
+                      const rtc::SocketAddress& related_address,
                       const std::string& protocol,
+                      const std::string& tcptype,
                       const std::string& type,
                       uint32 type_preference,
+                      uint32 relay_preference,
                       bool final) {
+  if (protocol == TCP_PROTOCOL_NAME && type == LOCAL_PORT_TYPE) {
+    ASSERT(!tcptype.empty());
+  }
+
   Candidate c;
-  c.set_id(talk_base::CreateRandomString(8));
+  c.set_id(rtc::CreateRandomString(8));
   c.set_component(component_);
   c.set_type(type);
   c.set_protocol(protocol);
+  c.set_tcptype(tcptype);
   c.set_address(address);
-  c.set_priority(c.GetPriority(type_preference, network_->preference()));
+  c.set_priority(c.GetPriority(type_preference, network_->preference(),
+                               relay_preference));
   c.set_username(username_fragment());
   c.set_password(password_);
   c.set_network_name(network_->name());
@@ -281,7 +295,7 @@ void Port::AddConnection(Connection* conn) {
 }
 
 void Port::OnReadPacket(
-    const char* data, size_t size, const talk_base::SocketAddress& addr,
+    const char* data, size_t size, const rtc::SocketAddress& addr,
     ProtocolType proto) {
   // If the user has enabled port packets, just hand this over.
   if (enable_port_packets_) {
@@ -291,7 +305,7 @@ void Port::OnReadPacket(
 
   // If this is an authenticated STUN request, then signal unknown address and
   // send back a proper binding response.
-  talk_base::scoped_ptr<IceMessage> msg;
+  rtc::scoped_ptr<IceMessage> msg;
   std::string remote_username;
   if (!GetStunMessage(data, size, addr, msg.accept(), &remote_username)) {
     LOG_J(LS_ERROR, this) << "Received non-STUN packet from unknown address ("
@@ -345,7 +359,7 @@ bool Port::IsHybridIce() const {
 }
 
 bool Port::GetStunMessage(const char* data, size_t size,
-                          const talk_base::SocketAddress& addr,
+                          const rtc::SocketAddress& addr,
                           IceMessage** out_msg, std::string* out_username) {
   // NOTE: This could clearly be optimized to avoid allocating any memory.
   //       However, at the data rates we'll be looking at on the client side,
@@ -363,8 +377,8 @@ bool Port::GetStunMessage(const char* data, size_t size,
 
   // Parse the request message.  If the packet is not a complete and correct
   // STUN message, then ignore it.
-  talk_base::scoped_ptr<IceMessage> stun_msg(new IceMessage());
-  talk_base::ByteBuffer buf(data, size);
+  rtc::scoped_ptr<IceMessage> stun_msg(new IceMessage());
+  rtc::ByteBuffer buf(data, size);
   if (!stun_msg->Read(&buf) || (buf.Length() > 0)) {
     return false;
   }
@@ -452,7 +466,7 @@ bool Port::GetStunMessage(const char* data, size_t size,
   return true;
 }
 
-bool Port::IsCompatibleAddress(const talk_base::SocketAddress& addr) {
+bool Port::IsCompatibleAddress(const rtc::SocketAddress& addr) {
   int family = ip().family();
   // We use single-stack sockets, so families must match.
   if (addr.family() != family) {
@@ -511,7 +525,7 @@ bool Port::ParseStunUsername(const StunMessage* stun_msg,
 }
 
 bool Port::MaybeIceRoleConflict(
-    const talk_base::SocketAddress& addr, IceMessage* stun_msg,
+    const rtc::SocketAddress& addr, IceMessage* stun_msg,
     const std::string& remote_ufrag) {
   // Validate ICE_CONTROLLING or ICE_CONTROLLED attributes.
   bool ret = true;
@@ -583,7 +597,7 @@ void Port::CreateStunUsername(const std::string& remote_username,
 }
 
 void Port::SendBindingResponse(StunMessage* request,
-                               const talk_base::SocketAddress& addr) {
+                               const rtc::SocketAddress& addr) {
   ASSERT(request->type() == STUN_BINDING_REQUEST);
 
   // Retrieve the username from the request.
@@ -629,9 +643,9 @@ void Port::SendBindingResponse(StunMessage* request,
   }
 
   // Send the response message.
-  talk_base::ByteBuffer buf;
+  rtc::ByteBuffer buf;
   response.Write(&buf);
-  talk_base::PacketOptions options(DefaultDscpValue());
+  rtc::PacketOptions options(DefaultDscpValue());
   if (SendTo(buf.Data(), buf.Length(), addr, options, false) < 0) {
     LOG_J(LS_ERROR, this) << "Failed to send STUN ping response to "
                           << addr.ToSensitiveString();
@@ -646,7 +660,7 @@ void Port::SendBindingResponse(StunMessage* request,
 }
 
 void Port::SendBindingErrorResponse(StunMessage* request,
-                                    const talk_base::SocketAddress& addr,
+                                    const rtc::SocketAddress& addr,
                                     int error_code, const std::string& reason) {
   ASSERT(request->type() == STUN_BINDING_REQUEST);
 
@@ -684,15 +698,15 @@ void Port::SendBindingErrorResponse(StunMessage* request,
   }
 
   // Send the response message.
-  talk_base::ByteBuffer buf;
+  rtc::ByteBuffer buf;
   response.Write(&buf);
-  talk_base::PacketOptions options(DefaultDscpValue());
+  rtc::PacketOptions options(DefaultDscpValue());
   SendTo(buf.Data(), buf.Length(), addr, options, false);
   LOG_J(LS_INFO, this) << "Sending STUN binding error: reason=" << reason
                        << " to " << addr.ToSensitiveString();
 }
 
-void Port::OnMessage(talk_base::Message *pmsg) {
+void Port::OnMessage(rtc::Message *pmsg) {
   ASSERT(pmsg->message_id == MSG_CHECKTIMEOUT);
   CheckTimeout();
 }
@@ -886,9 +900,9 @@ uint64 Connection::priority() const {
       g = remote_candidate_.priority();
       d = local_candidate().priority();
     }
-    priority = talk_base::_min(g, d);
+    priority = rtc::_min(g, d);
     priority = priority << 32;
-    priority += 2 * talk_base::_max(g, d) + (g > d ? 1 : 0);
+    priority += 2 * rtc::_max(g, d) + (g > d ? 1 : 0);
   }
   return priority;
 }
@@ -935,7 +949,7 @@ void Connection::set_use_candidate_attr(bool enable) {
 
 void Connection::OnSendStunPacket(const void* data, size_t size,
                                   StunRequest* req) {
-  talk_base::PacketOptions options(port_->DefaultDscpValue());
+  rtc::PacketOptions options(port_->DefaultDscpValue());
   if (port_->SendTo(data, size, remote_candidate_.address(),
                     options, false) < 0) {
     LOG_J(LS_WARNING, this) << "Failed to send STUN ping " << req->id();
@@ -943,10 +957,10 @@ void Connection::OnSendStunPacket(const void* data, size_t size,
 }
 
 void Connection::OnReadPacket(
-  const char* data, size_t size, const talk_base::PacketTime& packet_time) {
-  talk_base::scoped_ptr<IceMessage> msg;
+  const char* data, size_t size, const rtc::PacketTime& packet_time) {
+  rtc::scoped_ptr<IceMessage> msg;
   std::string remote_ufrag;
-  const talk_base::SocketAddress& addr(remote_candidate_.address());
+  const rtc::SocketAddress& addr(remote_candidate_.address());
   if (!port_->GetStunMessage(data, size, addr, msg.accept(), &remote_ufrag)) {
     // The packet did not parse as a valid STUN message
 
@@ -955,7 +969,7 @@ void Connection::OnReadPacket(
       // readable means data from this address is acceptable
       // Send it on!
 
-      last_data_received_ = talk_base::Time();
+      last_data_received_ = rtc::Time();
       recv_rate_tracker_.Update(size);
       SignalReadPacket(this, data, size, packet_time);
 
@@ -1078,7 +1092,7 @@ void Connection::UpdateState(uint32 now) {
   std::string pings;
   for (size_t i = 0; i < pings_since_last_response_.size(); ++i) {
     char buf[32];
-    talk_base::sprintfn(buf, sizeof(buf), "%u",
+    rtc::sprintfn(buf, sizeof(buf), "%u",
         pings_since_last_response_[i]);
     pings.append(buf).append(" ");
   }
@@ -1163,7 +1177,7 @@ void Connection::Ping(uint32 now) {
 }
 
 void Connection::ReceivedPing() {
-  last_ping_received_ = talk_base::Time();
+  last_ping_received_ = rtc::Time();
   set_read_state(STATE_READABLE);
 }
 
@@ -1238,21 +1252,21 @@ void Connection::OnConnectionRequestResponse(ConnectionRequest* request,
   std::string pings;
   for (size_t i = 0; i < pings_since_last_response_.size(); ++i) {
     char buf[32];
-    talk_base::sprintfn(buf, sizeof(buf), "%u",
+    rtc::sprintfn(buf, sizeof(buf), "%u",
         pings_since_last_response_[i]);
     pings.append(buf).append(" ");
   }
 
-  talk_base::LoggingSeverity level =
+  rtc::LoggingSeverity level =
       (pings_since_last_response_.size() > CONNECTION_WRITE_CONNECT_FAILURES) ?
-          talk_base::LS_INFO : talk_base::LS_VERBOSE;
+          rtc::LS_INFO : rtc::LS_VERBOSE;
 
   LOG_JV(level, this) << "Received STUN ping response " << request->id()
                       << ", pings_since_last_response_=" << pings
                       << ", rtt=" << rtt;
 
   pings_since_last_response_.clear();
-  last_ping_response_received_ = talk_base::Time();
+  last_ping_response_received_ = rtc::Time();
   rtt_ = (RTT_RATIO * rtt_ + rtt) / (RTT_RATIO + 1);
 
   // Peer reflexive candidate is only for RFC 5245 ICE.
@@ -1294,8 +1308,8 @@ void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request,
 
 void Connection::OnConnectionRequestTimeout(ConnectionRequest* request) {
   // Log at LS_INFO if we miss a ping on a writable connection.
-  talk_base::LoggingSeverity sev = (write_state_ == STATE_WRITABLE) ?
-      talk_base::LS_INFO : talk_base::LS_VERBOSE;
+  rtc::LoggingSeverity sev = (write_state_ == STATE_WRITABLE) ?
+      rtc::LS_INFO : rtc::LS_VERBOSE;
   LOG_JV(sev, this) << "Timing-out STUN ping " << request->id()
                     << " after " << request->Elapsed() << " ms";
 }
@@ -1317,7 +1331,7 @@ void Connection::HandleRoleConflictFromPeer() {
   port_->SignalRoleConflict(port_);
 }
 
-void Connection::OnMessage(talk_base::Message *pmsg) {
+void Connection::OnMessage(rtc::Message *pmsg) {
   ASSERT(pmsg->message_id == MSG_DELETE);
 
   LOG_J(LS_INFO, this) << "Connection deleted";
@@ -1380,7 +1394,7 @@ void Connection::MaybeAddPrflxCandidate(ConnectionRequest* request,
     return;
   }
   const uint32 priority = priority_attr->value();
-  std::string id = talk_base::CreateRandomString(8);
+  std::string id = rtc::CreateRandomString(8);
 
   Candidate new_local_candidate;
   new_local_candidate.set_id(id);
@@ -1411,7 +1425,7 @@ ProxyConnection::ProxyConnection(Port* port, size_t index,
 }
 
 int ProxyConnection::Send(const void* data, size_t size,
-                          const talk_base::PacketOptions& options) {
+                          const rtc::PacketOptions& options) {
   if (write_state_ == STATE_WRITE_INIT || write_state_ == STATE_WRITE_TIMEOUT) {
     error_ = EWOULDBLOCK;
     return SOCKET_ERROR;