[email protected] | 6bad55c | 2012-01-24 20:50:27 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 5 | #include "remoting/protocol/jingle_session.h" |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/rand_util.h" |
| 9 | #include "base/stl_util.h" |
| 10 | #include "base/string_number_conversions.h" |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 11 | #include "base/time.h" |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 12 | #include "remoting/base/constants.h" |
[email protected] | b39e182 | 2011-11-04 01:00:49 | [diff] [blame] | 13 | #include "remoting/jingle_glue/iq_sender.h" |
[email protected] | f4453851 | 2011-11-30 04:43:17 | [diff] [blame] | 14 | #include "remoting/protocol/authenticator.h" |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 15 | #include "remoting/protocol/channel_authenticator.h" |
[email protected] | 3e9187d0 | 2012-08-18 04:17:23 | [diff] [blame] | 16 | #include "remoting/protocol/channel_multiplexer.h" |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 17 | #include "remoting/protocol/content_description.h" |
| 18 | #include "remoting/protocol/jingle_messages.h" |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 19 | #include "remoting/protocol/jingle_session_manager.h" |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 20 | #include "remoting/protocol/session_config.h" |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 21 | #include "third_party/libjingle/source/talk/p2p/base/candidate.h" |
| 22 | #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 23 | |
| 24 | using buzz::XmlElement; |
| 25 | |
| 26 | namespace remoting { |
| 27 | namespace protocol { |
| 28 | |
| 29 | namespace { |
| 30 | // Delay after candidate creation before sending transport-info |
| 31 | // message. This is neccessary to be able to pack multiple candidates |
| 32 | // into one transport-info messages. The value needs to be greater |
| 33 | // than zero because ports are opened asynchronously in the browser |
| 34 | // process. |
| 35 | const int kTransportInfoSendDelayMs = 2; |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 36 | |
[email protected] | 9eed82f | 2013-02-09 06:09:27 | [diff] [blame] | 37 | // How long we should wait for a response from the other end. This value is used |
| 38 | // for all requests except |transport-info|. |
| 39 | const int kDefaultMessageTimeout = 10; |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 40 | |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 41 | // Timeout for the transport-info messages. |
| 42 | const int kTransportInfoTimeout = 10 * 60; |
| 43 | |
[email protected] | 3e9187d0 | 2012-08-18 04:17:23 | [diff] [blame] | 44 | // Name of the multiplexed channel. |
| 45 | const char kMuxChannelName[] = "mux"; |
| 46 | |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 47 | ErrorCode AuthRejectionReasonToErrorCode( |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 48 | Authenticator::RejectionReason reason) { |
| 49 | switch (reason) { |
| 50 | case Authenticator::INVALID_CREDENTIALS: |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 51 | return AUTHENTICATION_FAILED; |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 52 | case Authenticator::PROTOCOL_ERROR: |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 53 | return INCOMPATIBLE_PROTOCOL; |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 54 | } |
| 55 | NOTREACHED(); |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 56 | return UNKNOWN_ERROR; |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 59 | } // namespace |
| 60 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 61 | JingleSession::JingleSession(JingleSessionManager* session_manager) |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 62 | : session_manager_(session_manager), |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 63 | event_handler_(NULL), |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 64 | state_(INITIALIZING), |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 65 | error_(OK), |
| 66 | config_is_set_(false) { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 69 | JingleSession::~JingleSession() { |
[email protected] | 3e9187d0 | 2012-08-18 04:17:23 | [diff] [blame] | 70 | channel_multiplexer_.reset(); |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 71 | STLDeleteContainerPointers(pending_requests_.begin(), |
| 72 | pending_requests_.end()); |
| 73 | STLDeleteContainerPointers(transport_info_requests_.begin(), |
| 74 | transport_info_requests_.end()); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 75 | STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); |
| 76 | session_manager_->SessionDestroyed(this); |
| 77 | } |
| 78 | |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 79 | void JingleSession::SetEventHandler(Session::EventHandler* event_handler) { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 80 | DCHECK(CalledOnValidThread()); |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 81 | DCHECK(event_handler); |
| 82 | event_handler_ = event_handler; |
[email protected] | 3cb6677 | 2012-01-25 00:30:55 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 85 | ErrorCode JingleSession::error() { |
[email protected] | e3f03d0 | 2011-09-27 20:10:51 | [diff] [blame] | 86 | DCHECK(CalledOnValidThread()); |
| 87 | return error_; |
| 88 | } |
| 89 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 90 | void JingleSession::StartConnection( |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 91 | const std::string& peer_jid, |
[email protected] | 5bf5231 | 2012-01-20 04:10:52 | [diff] [blame] | 92 | scoped_ptr<Authenticator> authenticator, |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 93 | scoped_ptr<CandidateSessionConfig> config) { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 94 | DCHECK(CalledOnValidThread()); |
[email protected] | 5bf5231 | 2012-01-20 04:10:52 | [diff] [blame] | 95 | DCHECK(authenticator.get()); |
[email protected] | f4453851 | 2011-11-30 04:43:17 | [diff] [blame] | 96 | DCHECK_EQ(authenticator->state(), Authenticator::MESSAGE_READY); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 97 | |
| 98 | peer_jid_ = peer_jid; |
[email protected] | 5bf5231 | 2012-01-20 04:10:52 | [diff] [blame] | 99 | authenticator_ = authenticator.Pass(); |
| 100 | candidate_config_ = config.Pass(); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 101 | |
| 102 | // Generate random session ID. There are usually not more than 1 |
| 103 | // concurrent session per host, so a random 64-bit integer provides |
| 104 | // enough entropy. In the worst case connection will fail when two |
| 105 | // clients generate the same session ID concurrently. |
| 106 | session_id_ = base::Int64ToString(base::RandGenerator(kint64max)); |
| 107 | |
| 108 | // Send session-initiate message. |
| 109 | JingleMessage message(peer_jid_, JingleMessage::SESSION_INITIATE, |
| 110 | session_id_); |
[email protected] | a05c0d82a | 2012-02-29 01:19:25 | [diff] [blame] | 111 | message.from = session_manager_->signal_strategy_->GetLocalJid(); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 112 | message.description.reset( |
[email protected] | f4453851 | 2011-11-30 04:43:17 | [diff] [blame] | 113 | new ContentDescription(candidate_config_->Clone(), |
| 114 | authenticator_->GetNextMessage())); |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 115 | SendMessage(message); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 116 | |
| 117 | SetState(CONNECTING); |
| 118 | } |
| 119 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 120 | void JingleSession::InitializeIncomingConnection( |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 121 | const JingleMessage& initiate_message, |
| 122 | scoped_ptr<Authenticator> authenticator) { |
| 123 | DCHECK(CalledOnValidThread()); |
| 124 | DCHECK(initiate_message.description.get()); |
| 125 | DCHECK(authenticator.get()); |
| 126 | DCHECK_EQ(authenticator->state(), Authenticator::WAITING_MESSAGE); |
| 127 | |
| 128 | peer_jid_ = initiate_message.from; |
| 129 | authenticator_ = authenticator.Pass(); |
| 130 | session_id_ = initiate_message.sid; |
| 131 | candidate_config_ = initiate_message.description->config()->Clone(); |
| 132 | |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 133 | SetState(ACCEPTING); |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 136 | void JingleSession::AcceptIncomingConnection( |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 137 | const JingleMessage& initiate_message) { |
| 138 | DCHECK(config_is_set_); |
| 139 | |
| 140 | // Process the first authentication message. |
| 141 | const buzz::XmlElement* first_auth_message = |
| 142 | initiate_message.description->authenticator_message(); |
| 143 | |
| 144 | if (!first_auth_message) { |
| 145 | CloseInternal(INCOMPATIBLE_PROTOCOL); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | DCHECK_EQ(authenticator_->state(), Authenticator::WAITING_MESSAGE); |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 150 | // |authenticator_| is owned, so Unretained() is safe here. |
| 151 | authenticator_->ProcessMessage(first_auth_message, base::Bind( |
| 152 | &JingleSession::ContinueAcceptIncomingConnection, |
| 153 | base::Unretained(this))); |
| 154 | } |
| 155 | |
| 156 | void JingleSession::ContinueAcceptIncomingConnection() { |
| 157 | DCHECK_NE(authenticator_->state(), Authenticator::PROCESSING_MESSAGE); |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 158 | if (authenticator_->state() == Authenticator::REJECTED) { |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 159 | CloseInternal(AuthRejectionReasonToErrorCode( |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 160 | authenticator_->rejection_reason())); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | // Send the session-accept message. |
| 165 | JingleMessage message(peer_jid_, JingleMessage::SESSION_ACCEPT, |
| 166 | session_id_); |
| 167 | |
| 168 | scoped_ptr<buzz::XmlElement> auth_message; |
| 169 | if (authenticator_->state() == Authenticator::MESSAGE_READY) |
| 170 | auth_message = authenticator_->GetNextMessage(); |
| 171 | |
| 172 | message.description.reset( |
| 173 | new ContentDescription(CandidateSessionConfig::CreateFrom(config_), |
| 174 | auth_message.Pass())); |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 175 | SendMessage(message); |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 176 | |
| 177 | // Update state. |
| 178 | SetState(CONNECTED); |
| 179 | |
| 180 | if (authenticator_->state() == Authenticator::ACCEPTED) { |
| 181 | SetState(AUTHENTICATED); |
| 182 | } else { |
| 183 | DCHECK_EQ(authenticator_->state(), Authenticator::WAITING_MESSAGE); |
| 184 | } |
| 185 | |
| 186 | return; |
| 187 | } |
| 188 | |
[email protected] | 3e9187d0 | 2012-08-18 04:17:23 | [diff] [blame] | 189 | const std::string& JingleSession::jid() { |
| 190 | DCHECK(CalledOnValidThread()); |
| 191 | return peer_jid_; |
| 192 | } |
| 193 | |
| 194 | const CandidateSessionConfig* JingleSession::candidate_config() { |
| 195 | DCHECK(CalledOnValidThread()); |
| 196 | return candidate_config_.get(); |
| 197 | } |
| 198 | |
| 199 | const SessionConfig& JingleSession::config() { |
| 200 | DCHECK(CalledOnValidThread()); |
| 201 | return config_; |
| 202 | } |
| 203 | |
| 204 | void JingleSession::set_config(const SessionConfig& config) { |
| 205 | DCHECK(CalledOnValidThread()); |
| 206 | DCHECK(!config_is_set_); |
| 207 | config_ = config; |
| 208 | config_is_set_ = true; |
| 209 | } |
| 210 | |
| 211 | ChannelFactory* JingleSession::GetTransportChannelFactory() { |
| 212 | DCHECK(CalledOnValidThread()); |
| 213 | return this; |
| 214 | } |
| 215 | |
| 216 | ChannelFactory* JingleSession::GetMultiplexedChannelFactory() { |
| 217 | DCHECK(CalledOnValidThread()); |
| 218 | if (!channel_multiplexer_.get()) |
| 219 | channel_multiplexer_.reset(new ChannelMultiplexer(this, kMuxChannelName)); |
| 220 | return channel_multiplexer_.get(); |
| 221 | } |
| 222 | |
| 223 | void JingleSession::Close() { |
| 224 | DCHECK(CalledOnValidThread()); |
| 225 | |
| 226 | CloseInternal(OK); |
| 227 | } |
| 228 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 229 | void JingleSession::CreateStreamChannel( |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 230 | const std::string& name, |
| 231 | const StreamChannelCallback& callback) { |
| 232 | DCHECK(!channels_[name]); |
| 233 | |
[email protected] | 5bf5231 | 2012-01-20 04:10:52 | [diff] [blame] | 234 | scoped_ptr<ChannelAuthenticator> channel_authenticator = |
[email protected] | f4453851 | 2011-11-30 04:43:17 | [diff] [blame] | 235 | authenticator_->CreateChannelAuthenticator(); |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 236 | scoped_ptr<StreamTransport> channel = |
| 237 | session_manager_->transport_factory_->CreateStreamTransport(); |
[email protected] | 5f4ed93d | 2012-04-30 23:41:59 | [diff] [blame] | 238 | channel->Initialize(name, this, channel_authenticator.Pass()); |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 239 | channel->Connect(callback); |
| 240 | channels_[name] = channel.release(); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 243 | void JingleSession::CreateDatagramChannel( |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 244 | const std::string& name, |
| 245 | const DatagramChannelCallback& callback) { |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 246 | DCHECK(!channels_[name]); |
| 247 | |
| 248 | scoped_ptr<ChannelAuthenticator> channel_authenticator = |
| 249 | authenticator_->CreateChannelAuthenticator(); |
| 250 | scoped_ptr<DatagramTransport> channel = |
| 251 | session_manager_->transport_factory_->CreateDatagramTransport(); |
[email protected] | 5f4ed93d | 2012-04-30 23:41:59 | [diff] [blame] | 252 | channel->Initialize(name, this, channel_authenticator.Pass()); |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 253 | channel->Connect(callback); |
| 254 | channels_[name] = channel.release(); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 257 | void JingleSession::CancelChannelCreation(const std::string& name) { |
[email protected] | f2d6a003 | 2011-11-17 00:48:12 | [diff] [blame] | 258 | ChannelsMap::iterator it = channels_.find(name); |
| 259 | if (it != channels_.end() && !it->second->is_connected()) { |
| 260 | delete it->second; |
| 261 | DCHECK(!channels_[name]); |
| 262 | } |
| 263 | } |
| 264 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 265 | void JingleSession::OnTransportCandidate(Transport* transport, |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 266 | const cricket::Candidate& candidate) { |
[email protected] | 8bb74637 | 2012-04-26 04:20:12 | [diff] [blame] | 267 | pending_candidates_.push_back(JingleMessage::NamedCandidate( |
| 268 | transport->name(), candidate)); |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 269 | |
| 270 | if (!transport_infos_timer_.IsRunning()) { |
| 271 | // Delay sending the new candidates in case we get more candidates |
| 272 | // that we can send in one message. |
| 273 | transport_infos_timer_.Start( |
| 274 | FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 275 | this, &JingleSession::SendTransportInfo); |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 279 | void JingleSession::OnTransportRouteChange(Transport* transport, |
[email protected] | 0b52323 | 2012-02-21 23:23:11 | [diff] [blame] | 280 | const TransportRoute& route) { |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 281 | if (event_handler_) |
| 282 | event_handler_->OnSessionRouteChange(transport->name(), route); |
[email protected] | 0b52323 | 2012-02-21 23:23:11 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | a04494cb | 2012-07-23 05:00:32 | [diff] [blame] | 285 | void JingleSession::OnTransportReady(Transport* transport, bool ready) { |
| 286 | if (event_handler_) |
| 287 | event_handler_->OnSessionChannelReady(transport->name(), ready); |
| 288 | } |
| 289 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 290 | void JingleSession::OnTransportDeleted(Transport* transport) { |
[email protected] | e3c97a6 | 2012-02-09 03:52:20 | [diff] [blame] | 291 | ChannelsMap::iterator it = channels_.find(transport->name()); |
| 292 | DCHECK_EQ(it->second, transport); |
| 293 | channels_.erase(it); |
| 294 | } |
| 295 | |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 296 | void JingleSession::SendMessage(const JingleMessage& message) { |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 297 | scoped_ptr<IqRequest> request = session_manager_->iq_sender()->SendIq( |
| 298 | message.ToXml(), |
| 299 | base::Bind(&JingleSession::OnMessageResponse, |
| 300 | base::Unretained(this), message.action)); |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 301 | if (request) { |
| 302 | request->SetTimeout(base::TimeDelta::FromSeconds(kDefaultMessageTimeout)); |
| 303 | pending_requests_.insert(request.release()); |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 304 | } else { |
| 305 | LOG(ERROR) << "Failed to send a " |
| 306 | << JingleMessage::GetActionName(message.action) << " message"; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void JingleSession::OnMessageResponse( |
| 311 | JingleMessage::ActionType request_type, |
| 312 | IqRequest* request, |
| 313 | const buzz::XmlElement* response) { |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 314 | std::string type_str = JingleMessage::GetActionName(request_type); |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 315 | |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 316 | // Delete the request from the list of pending requests. |
| 317 | pending_requests_.erase(request); |
| 318 | delete request; |
| 319 | |
[email protected] | 9eed82f | 2013-02-09 06:09:27 | [diff] [blame] | 320 | // |response| will be NULL if the request timed out. |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 321 | if (!response) { |
| 322 | LOG(ERROR) << type_str << " request timed out."; |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 323 | CloseInternal(SIGNALING_TIMEOUT); |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 324 | return; |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 325 | } else { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 326 | const std::string& type = |
| 327 | response->Attr(buzz::QName(std::string(), "type")); |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 328 | if (type != "result") { |
| 329 | LOG(ERROR) << "Received error in response to " << type_str |
| 330 | << " message: \"" << response->Str() |
| 331 | << "\". Terminating the session."; |
| 332 | |
| 333 | switch (request_type) { |
| 334 | case JingleMessage::SESSION_INFO: |
| 335 | // session-info is used for the new authentication protocol, |
| 336 | // and wasn't previously supported. |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 337 | CloseInternal(INCOMPATIBLE_PROTOCOL); |
| 338 | break; |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 339 | |
| 340 | default: |
| 341 | // TODO(sergeyu): There may be different reasons for error |
| 342 | // here. Parse the response stanza to find failure reason. |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 343 | CloseInternal(PEER_IS_OFFLINE); |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 347 | } |
| 348 | |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 349 | void JingleSession::SendTransportInfo() { |
| 350 | JingleMessage message(peer_jid_, JingleMessage::TRANSPORT_INFO, session_id_); |
| 351 | message.candidates.swap(pending_candidates_); |
| 352 | |
| 353 | scoped_ptr<IqRequest> request = session_manager_->iq_sender()->SendIq( |
| 354 | message.ToXml(), |
| 355 | base::Bind(&JingleSession::OnTransportInfoResponse, |
| 356 | base::Unretained(this))); |
| 357 | if (request) { |
| 358 | request->SetTimeout(base::TimeDelta::FromSeconds(kTransportInfoTimeout)); |
| 359 | transport_info_requests_.push_back(request.release()); |
| 360 | } else { |
| 361 | LOG(ERROR) << "Failed to send a transport-info message"; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void JingleSession::OnTransportInfoResponse(IqRequest* request, |
| 366 | const buzz::XmlElement* response) { |
| 367 | DCHECK(!transport_info_requests_.empty()); |
| 368 | |
| 369 | // Consider transport-info requests sent before this one lost and delete |
| 370 | // corresponding IqRequest objects. |
| 371 | while (transport_info_requests_.front() != request) { |
| 372 | delete transport_info_requests_.front(); |
| 373 | transport_info_requests_.pop_front(); |
| 374 | } |
| 375 | |
| 376 | // Delete the |request| itself. |
| 377 | DCHECK_EQ(request, transport_info_requests_.front()); |
| 378 | delete request; |
| 379 | transport_info_requests_.pop_front(); |
| 380 | |
| 381 | // Ignore transport-info timeouts. |
| 382 | if (!response) { |
| 383 | LOG(ERROR) << "transport-info request has timed out."; |
| 384 | return; |
| 385 | } |
| 386 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 387 | const std::string& type = response->Attr(buzz::QName(std::string(), "type")); |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 388 | if (type != "result") { |
| 389 | LOG(ERROR) << "Received error in response to transport-info message: \"" |
| 390 | << response->Str() << "\". Terminating the session."; |
| 391 | CloseInternal(PEER_IS_OFFLINE); |
| 392 | } |
| 393 | } |
| 394 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 395 | void JingleSession::OnIncomingMessage(const JingleMessage& message, |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 396 | const ReplyCallback& reply_callback) { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 397 | DCHECK(CalledOnValidThread()); |
| 398 | |
| 399 | if (message.from != peer_jid_) { |
| 400 | // Ignore messages received from a different Jid. |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 401 | reply_callback.Run(JingleMessageReply::INVALID_SID); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | |
| 405 | switch (message.action) { |
| 406 | case JingleMessage::SESSION_ACCEPT: |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 407 | OnAccept(message, reply_callback); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 408 | break; |
| 409 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 410 | case JingleMessage::SESSION_INFO: |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 411 | OnSessionInfo(message, reply_callback); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 412 | break; |
| 413 | |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 414 | case JingleMessage::TRANSPORT_INFO: |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 415 | reply_callback.Run(JingleMessageReply::NONE); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 416 | ProcessTransportInfo(message); |
| 417 | break; |
| 418 | |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 419 | case JingleMessage::SESSION_TERMINATE: |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 420 | OnTerminate(message, reply_callback); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 421 | break; |
| 422 | |
| 423 | default: |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 424 | reply_callback.Run(JingleMessageReply::UNEXPECTED_REQUEST); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 428 | void JingleSession::OnAccept(const JingleMessage& message, |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 429 | const ReplyCallback& reply_callback) { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 430 | if (state_ != CONNECTING) { |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 431 | reply_callback.Run(JingleMessageReply::UNEXPECTED_REQUEST); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 435 | reply_callback.Run(JingleMessageReply::NONE); |
| 436 | |
[email protected] | f4453851 | 2011-11-30 04:43:17 | [diff] [blame] | 437 | const buzz::XmlElement* auth_message = |
| 438 | message.description->authenticator_message(); |
| 439 | if (!auth_message) { |
| 440 | DLOG(WARNING) << "Received session-accept without authentication message " |
| 441 | << auth_message->Str(); |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 442 | CloseInternal(INCOMPATIBLE_PROTOCOL); |
[email protected] | f4453851 | 2011-11-30 04:43:17 | [diff] [blame] | 443 | return; |
| 444 | } |
| 445 | |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 446 | if (!InitializeConfigFromDescription(message.description.get())) { |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 447 | CloseInternal(INCOMPATIBLE_PROTOCOL); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 451 | // In case there is transport information in the accept message. |
| 452 | ProcessTransportInfo(message); |
| 453 | |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 454 | SetState(CONNECTED); |
| 455 | |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 456 | DCHECK(authenticator_->state() == Authenticator::WAITING_MESSAGE); |
| 457 | authenticator_->ProcessMessage(auth_message, base::Bind( |
| 458 | &JingleSession::ProcessAuthenticationStep,base::Unretained(this))); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 459 | } |
[email protected] | 0de3700 | 2011-12-06 07:30:16 | [diff] [blame] | 460 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 461 | void JingleSession::OnSessionInfo(const JingleMessage& message, |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 462 | const ReplyCallback& reply_callback) { |
| 463 | if (!message.info.get() || |
| 464 | !Authenticator::IsAuthenticatorMessage(message.info.get())) { |
| 465 | reply_callback.Run(JingleMessageReply::UNSUPPORTED_INFO); |
| 466 | return; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 467 | } |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 468 | |
| 469 | if (state_ != CONNECTED || |
| 470 | authenticator_->state() != Authenticator::WAITING_MESSAGE) { |
| 471 | LOG(WARNING) << "Received unexpected authenticator message " |
| 472 | << message.info->Str(); |
| 473 | reply_callback.Run(JingleMessageReply::UNEXPECTED_REQUEST); |
| 474 | CloseInternal(INCOMPATIBLE_PROTOCOL); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | reply_callback.Run(JingleMessageReply::NONE); |
| 479 | |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 480 | authenticator_->ProcessMessage(message.info.get(), base::Bind( |
| 481 | &JingleSession::ProcessAuthenticationStep, base::Unretained(this))); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 482 | } |
| 483 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 484 | void JingleSession::ProcessTransportInfo(const JingleMessage& message) { |
[email protected] | 8bb74637 | 2012-04-26 04:20:12 | [diff] [blame] | 485 | for (std::list<JingleMessage::NamedCandidate>::const_iterator it = |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 486 | message.candidates.begin(); |
| 487 | it != message.candidates.end(); ++it) { |
[email protected] | 8bb74637 | 2012-04-26 04:20:12 | [diff] [blame] | 488 | ChannelsMap::iterator channel = channels_.find(it->name); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 489 | if (channel == channels_.end()) { |
[email protected] | 8bb74637 | 2012-04-26 04:20:12 | [diff] [blame] | 490 | LOG(WARNING) << "Received candidate for unknown channel " << it->name; |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 491 | continue; |
| 492 | } |
[email protected] | 8bb74637 | 2012-04-26 04:20:12 | [diff] [blame] | 493 | channel->second->AddRemoteCandidate(it->candidate); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 497 | void JingleSession::OnTerminate(const JingleMessage& message, |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 498 | const ReplyCallback& reply_callback) { |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 499 | if (state_ != CONNECTING && state_ != ACCEPTING && state_ != CONNECTED && |
| 500 | state_ != AUTHENTICATED) { |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 501 | LOG(WARNING) << "Received unexpected session-terminate message."; |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 502 | reply_callback.Run(JingleMessageReply::UNEXPECTED_REQUEST); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | |
[email protected] | b0fffb0 | 2012-02-17 21:59:43 | [diff] [blame] | 506 | reply_callback.Run(JingleMessageReply::NONE); |
| 507 | |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 508 | switch (message.reason) { |
| 509 | case JingleMessage::SUCCESS: |
| 510 | if (state_ == CONNECTING) { |
| 511 | error_ = SESSION_REJECTED; |
| 512 | } else { |
| 513 | error_ = OK; |
| 514 | } |
| 515 | break; |
| 516 | case JingleMessage::DECLINE: |
| 517 | error_ = AUTHENTICATION_FAILED; |
| 518 | break; |
[email protected] | a6b74c21 | 2012-03-27 02:38:11 | [diff] [blame] | 519 | case JingleMessage::CANCEL: |
[email protected] | d8216688 | 2012-03-28 04:38:53 | [diff] [blame] | 520 | error_ = HOST_OVERLOAD; |
[email protected] | a6b74c21 | 2012-03-27 02:38:11 | [diff] [blame] | 521 | break; |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 522 | case JingleMessage::GENERAL_ERROR: |
| 523 | error_ = CHANNEL_CONNECTION_ERROR; |
| 524 | break; |
| 525 | case JingleMessage::INCOMPATIBLE_PARAMETERS: |
| 526 | error_ = INCOMPATIBLE_PROTOCOL; |
| 527 | break; |
| 528 | default: |
| 529 | error_ = UNKNOWN_ERROR; |
[email protected] | 455a6168 | 2011-11-12 01:45:19 | [diff] [blame] | 530 | } |
| 531 | |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 532 | if (error_ != OK) { |
| 533 | SetState(FAILED); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 534 | } else { |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 535 | SetState(CLOSED); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 536 | } |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 537 | } |
| 538 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 539 | bool JingleSession::InitializeConfigFromDescription( |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 540 | const ContentDescription* description) { |
| 541 | DCHECK(description); |
| 542 | |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 543 | if (!description->config()->GetFinalConfig(&config_)) { |
| 544 | LOG(ERROR) << "session-accept does not specify configuration"; |
| 545 | return false; |
| 546 | } |
| 547 | if (!candidate_config()->IsSupported(config_)) { |
| 548 | LOG(ERROR) << "session-accept specifies an invalid configuration"; |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | return true; |
| 553 | } |
| 554 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 555 | void JingleSession::ProcessAuthenticationStep() { |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 556 | DCHECK(CalledOnValidThread()); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 557 | DCHECK_EQ(state_, CONNECTED); |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 558 | DCHECK_NE(authenticator_->state(), Authenticator::PROCESSING_MESSAGE); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 559 | |
| 560 | if (authenticator_->state() == Authenticator::MESSAGE_READY) { |
| 561 | JingleMessage message(peer_jid_, JingleMessage::SESSION_INFO, session_id_); |
[email protected] | 5bf5231 | 2012-01-20 04:10:52 | [diff] [blame] | 562 | message.info = authenticator_->GetNextMessage(); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 563 | DCHECK(message.info.get()); |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 564 | SendMessage(message); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 565 | } |
| 566 | DCHECK_NE(authenticator_->state(), Authenticator::MESSAGE_READY); |
| 567 | |
| 568 | if (authenticator_->state() == Authenticator::ACCEPTED) { |
| 569 | SetState(AUTHENTICATED); |
| 570 | } else if (authenticator_->state() == Authenticator::REJECTED) { |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 571 | CloseInternal(AuthRejectionReasonToErrorCode( |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 572 | authenticator_->rejection_reason())); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame] | 576 | void JingleSession::CloseInternal(ErrorCode error) { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 577 | DCHECK(CalledOnValidThread()); |
| 578 | |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 579 | if (state_ == CONNECTING || state_ == ACCEPTING || state_ == CONNECTED || |
| 580 | state_ == AUTHENTICATED) { |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 581 | // Send session-terminate message with the appropriate error code. |
| 582 | JingleMessage::Reason reason; |
| 583 | switch (error) { |
| 584 | case OK: |
| 585 | reason = JingleMessage::SUCCESS; |
| 586 | break; |
| 587 | case SESSION_REJECTED: |
| 588 | case AUTHENTICATION_FAILED: |
| 589 | reason = JingleMessage::DECLINE; |
| 590 | break; |
| 591 | case INCOMPATIBLE_PROTOCOL: |
| 592 | reason = JingleMessage::INCOMPATIBLE_PARAMETERS; |
| 593 | break; |
[email protected] | d8216688 | 2012-03-28 04:38:53 | [diff] [blame] | 594 | case HOST_OVERLOAD: |
[email protected] | a6b74c21 | 2012-03-27 02:38:11 | [diff] [blame] | 595 | reason = JingleMessage::CANCEL; |
| 596 | break; |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 597 | default: |
| 598 | reason = JingleMessage::GENERAL_ERROR; |
| 599 | } |
| 600 | |
| 601 | JingleMessage message(peer_jid_, JingleMessage::SESSION_TERMINATE, |
| 602 | session_id_); |
| 603 | message.reason = reason; |
[email protected] | 0dad65a | 2013-02-20 20:54:12 | [diff] [blame] | 604 | SendMessage(message); |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | error_ = error; |
| 608 | |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 609 | if (state_ != FAILED && state_ != CLOSED) { |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 610 | if (error != OK) { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 611 | SetState(FAILED); |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 612 | } else { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 613 | SetState(CLOSED); |
[email protected] | a209823 | 2012-02-09 06:19:33 | [diff] [blame] | 614 | } |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 618 | void JingleSession::SetState(State new_state) { |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 619 | DCHECK(CalledOnValidThread()); |
| 620 | |
| 621 | if (new_state != state_) { |
| 622 | DCHECK_NE(state_, CLOSED); |
| 623 | DCHECK_NE(state_, FAILED); |
| 624 | |
| 625 | state_ = new_state; |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 626 | if (event_handler_) |
| 627 | event_handler_->OnSessionStateChange(new_state); |
[email protected] | 22aae95 | 2011-09-12 23:47:51 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | } // namespace protocol |
| 632 | } // namespace remoting |