OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/jingle_session.h" | 5 #include "remoting/protocol/jingle_session.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/rand_util.h" | |
14 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
15 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
16 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_split.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
18 #include "base/time/time.h" | 17 #include "base/time/time.h" |
19 #include "remoting/base/constants.h" | 18 #include "remoting/base/constants.h" |
20 #include "remoting/protocol/authenticator.h" | 19 #include "remoting/protocol/authenticator.h" |
21 #include "remoting/protocol/content_description.h" | 20 #include "remoting/protocol/content_description.h" |
22 #include "remoting/protocol/jingle_messages.h" | 21 #include "remoting/protocol/jingle_messages.h" |
23 #include "remoting/protocol/jingle_session_manager.h" | 22 #include "remoting/protocol/jingle_session_manager.h" |
24 #include "remoting/protocol/session_config.h" | 23 #include "remoting/protocol/session_config.h" |
25 #include "remoting/protocol/transport.h" | 24 #include "remoting/protocol/transport.h" |
26 #include "remoting/signaling/iq_sender.h" | 25 #include "remoting/signaling/iq_sender.h" |
27 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 26 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
27 #include "third_party/webrtc/libjingle/xmpp/constants.h" | |
28 #include "third_party/webrtc/p2p/base/candidate.h" | 28 #include "third_party/webrtc/p2p/base/candidate.h" |
29 | 29 |
30 using buzz::XmlElement; | 30 using buzz::XmlElement; |
31 | 31 |
32 namespace remoting { | 32 namespace remoting { |
33 namespace protocol { | 33 namespace protocol { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 // How long we should wait for a response from the other end. This value is used | 37 // How long we should wait for a response from the other end. This value is used |
38 // for all requests except |transport-info|. | 38 // for all requests except |transport-info|. |
39 const int kDefaultMessageTimeout = 10; | 39 const int kDefaultMessageTimeout = 10; |
40 | 40 |
41 // During a reconnection, it usually takes longer for the peer to respond due to | 41 // During a reconnection, it usually takes longer for the peer to respond due to |
42 // pending messages in the channel from the previous session. From experiment, | 42 // pending messages in the channel from the previous session. From experiment, |
43 // it can take up to 20s for the session to reconnect. To make it safe, setting | 43 // it can take up to 20s for the session to reconnect. To make it safe, setting |
44 // the timeout to 30s. | 44 // the timeout to 30s. |
45 const int kSessionInitiateAndAcceptTimeout = kDefaultMessageTimeout * 3; | 45 const int kSessionInitiateAndAcceptTimeout = kDefaultMessageTimeout * 3; |
46 | 46 |
47 // Timeout for the transport-info messages. | 47 // Timeout for the transport-info messages. |
48 const int kTransportInfoTimeout = 10 * 60; | 48 const int kTransportInfoTimeout = 10 * 60; |
49 | 49 |
50 // Special value for an invalid sequential ID for an incoming IQ. | |
51 const int kInvalid = -1; | |
52 | |
53 // Special value indicating that any sequential ID is valid for the next | |
54 // incoming IQ. | |
55 const int kAny = -1; | |
56 | |
50 ErrorCode AuthRejectionReasonToErrorCode( | 57 ErrorCode AuthRejectionReasonToErrorCode( |
51 Authenticator::RejectionReason reason) { | 58 Authenticator::RejectionReason reason) { |
52 switch (reason) { | 59 switch (reason) { |
53 case Authenticator::INVALID_CREDENTIALS: | 60 case Authenticator::INVALID_CREDENTIALS: |
54 return AUTHENTICATION_FAILED; | 61 return AUTHENTICATION_FAILED; |
55 case Authenticator::PROTOCOL_ERROR: | 62 case Authenticator::PROTOCOL_ERROR: |
56 return INCOMPATIBLE_PROTOCOL; | 63 return INCOMPATIBLE_PROTOCOL; |
57 case Authenticator::INVALID_ACCOUNT: | 64 case Authenticator::INVALID_ACCOUNT: |
58 return INVALID_ACCOUNT; | 65 return INVALID_ACCOUNT; |
59 case Authenticator::REJECTED_BY_USER: | 66 case Authenticator::REJECTED_BY_USER: |
60 return SESSION_REJECTED; | 67 return SESSION_REJECTED; |
61 } | 68 } |
62 NOTREACHED(); | 69 NOTREACHED(); |
63 return UNKNOWN_ERROR; | 70 return UNKNOWN_ERROR; |
64 } | 71 } |
65 | 72 |
73 // Extracts a sequential id from the id attribute of the IQ stanza. | |
74 int GetSequentialId(const std::string& id) { | |
75 std::vector<std::string> tokens = | |
76 SplitString(id, "_", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
77 // Legacy endpoints does not encode the IQ ordering in the ID attribute | |
78 if (tokens.size() != 2) { | |
79 return kInvalid; | |
80 } | |
81 | |
82 int result = kInvalid; | |
83 if (!base::StringToInt(tokens[1].c_str(), &result)) { | |
84 return kInvalid; | |
85 } | |
86 return result; | |
87 } | |
88 | |
66 } // namespace | 89 } // namespace |
67 | 90 |
91 // A Queue that sorts incoming messages and returns them in the ascending order | |
92 // of sequence ids. The sequence id can be extracted from the ID attribute of | |
93 // an IQ stanza, which have the following format <opaque_string>_<sequence_id>. | |
94 // | |
95 // Background: | |
96 // The chromoting signaling channel does not guarantee that the incoming IQs are | |
97 // delivered in the order that it is sent. | |
98 // | |
99 // This behavior leads to transient session setup failures. For instance, | |
100 // a <transport-info> that is sent after a <session-info> message is sometimes | |
101 // delivered to the client out of order, causing the client to close the | |
102 // session due to an unexpected request. | |
103 class JingleSession::OrderedMessageQueue { | |
104 public: | |
105 OrderedMessageQueue() {} | |
106 ~OrderedMessageQueue() {} | |
107 | |
108 // Returns the list of messages ordered by their sequential IDs. | |
109 std::vector<std::unique_ptr<PendingMessage>> OnIncomingMessage( | |
110 const std::string& id, | |
111 std::unique_ptr<PendingMessage>); | |
112 | |
113 private: | |
114 // Implements an ordered list by using map with the |sequence_id| as the key, | |
115 // so that |queue_| is always sorted by |sequence_id|. | |
116 std::map<int, std::unique_ptr<PendingMessage>> queue_; | |
117 | |
118 int next_incoming_ = kAny; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(OrderedMessageQueue); | |
121 }; | |
122 | |
123 std::vector<std::unique_ptr<JingleSession::PendingMessage>> | |
124 JingleSession::OrderedMessageQueue::OnIncomingMessage( | |
125 const std::string& id, | |
126 std::unique_ptr<JingleSession::PendingMessage> message) { | |
127 std::vector<std::unique_ptr<JingleSession::PendingMessage>> result; | |
128 int current = GetSequentialId(id); | |
129 // If there is no sequencing order encoded in the id, just return the | |
130 // message. | |
131 if (current == kInvalid) { | |
132 result.push_back(std::move(message)); | |
133 return result; | |
134 } | |
135 | |
136 if (next_incoming_ == kAny) { | |
137 next_incoming_ = current; | |
138 } | |
139 | |
140 // Ensure there are no duplicate sequence ids. | |
141 DCHECK(current >= next_incoming_); | |
Sergey Ulanov
2016/10/21 19:19:39
DCHECK_GE
kelvinp
2016/10/21 22:41:27
Done.
| |
142 DCHECK(queue_.find(current) == queue_.end()); | |
143 | |
144 queue_.insert(std::make_pair(current, std::move(message))); | |
145 | |
146 auto it = queue_.begin(); | |
147 while (it != queue_.end() && it->first == next_incoming_) { | |
148 result.push_back(std::move(it->second)); | |
149 it = queue_.erase(it); | |
150 next_incoming_++; | |
151 } | |
152 | |
153 if (current - next_incoming_ >= 3) { | |
154 LOG(WARNING) << "Multiple messages are missing: expected= " | |
155 << next_incoming_ << " current= " << current; | |
156 } | |
157 return result; | |
158 }; | |
159 | |
160 JingleSession::PendingMessage::PendingMessage( | |
161 std::unique_ptr<JingleMessage> message, | |
162 const ReplyCallback& reply_callback) | |
163 : message(std::move(message)), reply_callback(reply_callback) {} | |
164 | |
165 JingleSession::PendingMessage::~PendingMessage() {} | |
166 | |
68 JingleSession::JingleSession(JingleSessionManager* session_manager) | 167 JingleSession::JingleSession(JingleSessionManager* session_manager) |
69 : session_manager_(session_manager), | 168 : session_manager_(session_manager), |
70 event_handler_(nullptr), | 169 event_handler_(nullptr), |
71 state_(INITIALIZING), | 170 state_(INITIALIZING), |
72 error_(OK), | 171 error_(OK), |
73 weak_factory_(this) { | 172 message_queue_(new OrderedMessageQueue), |
74 } | 173 weak_factory_(this) {} |
75 | 174 |
76 JingleSession::~JingleSession() { | 175 JingleSession::~JingleSession() { |
77 session_manager_->SessionDestroyed(this); | 176 session_manager_->SessionDestroyed(this); |
78 } | 177 } |
79 | 178 |
80 void JingleSession::SetEventHandler(Session::EventHandler* event_handler) { | 179 void JingleSession::SetEventHandler(Session::EventHandler* event_handler) { |
81 DCHECK(thread_checker_.CalledOnValidThread()); | 180 DCHECK(thread_checker_.CalledOnValidThread()); |
82 DCHECK(event_handler); | 181 DCHECK(event_handler); |
83 event_handler_ = event_handler; | 182 event_handler_ = event_handler; |
84 } | 183 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 | 312 |
214 void JingleSession::SendTransportInfo( | 313 void JingleSession::SendTransportInfo( |
215 std::unique_ptr<buzz::XmlElement> transport_info) { | 314 std::unique_ptr<buzz::XmlElement> transport_info) { |
216 DCHECK(thread_checker_.CalledOnValidThread()); | 315 DCHECK(thread_checker_.CalledOnValidThread()); |
217 DCHECK_EQ(state_, AUTHENTICATED); | 316 DCHECK_EQ(state_, AUTHENTICATED); |
218 | 317 |
219 std::unique_ptr<JingleMessage> message(new JingleMessage( | 318 std::unique_ptr<JingleMessage> message(new JingleMessage( |
220 peer_address_, JingleMessage::TRANSPORT_INFO, session_id_)); | 319 peer_address_, JingleMessage::TRANSPORT_INFO, session_id_)); |
221 message->transport_info = std::move(transport_info); | 320 message->transport_info = std::move(transport_info); |
222 | 321 |
322 std::unique_ptr<buzz::XmlElement> stanza = message->ToXml(); | |
323 stanza->AddAttr(buzz::QN_ID, GetNextOutgoingId()); | |
324 | |
223 auto request = session_manager_->iq_sender()->SendIq( | 325 auto request = session_manager_->iq_sender()->SendIq( |
224 message->ToXml(), base::Bind(&JingleSession::OnTransportInfoResponse, | 326 std::move(stanza), base::Bind(&JingleSession::OnTransportInfoResponse, |
225 base::Unretained(this))); | 327 base::Unretained(this))); |
226 if (request) { | 328 if (request) { |
227 request->SetTimeout(base::TimeDelta::FromSeconds(kTransportInfoTimeout)); | 329 request->SetTimeout(base::TimeDelta::FromSeconds(kTransportInfoTimeout)); |
228 transport_info_requests_.push_back(std::move(request)); | 330 transport_info_requests_.push_back(std::move(request)); |
229 } else { | 331 } else { |
230 LOG(ERROR) << "Failed to send a transport-info message"; | 332 LOG(ERROR) << "Failed to send a transport-info message"; |
231 } | 333 } |
232 } | 334 } |
233 | 335 |
234 void JingleSession::Close(protocol::ErrorCode error) { | 336 void JingleSession::Close(protocol::ErrorCode error) { |
235 DCHECK(thread_checker_.CalledOnValidThread()); | 337 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 SetState(FAILED); | 378 SetState(FAILED); |
277 } else { | 379 } else { |
278 SetState(CLOSED); | 380 SetState(CLOSED); |
279 } | 381 } |
280 } | 382 } |
281 } | 383 } |
282 | 384 |
283 void JingleSession::SendMessage(std::unique_ptr<JingleMessage> message) { | 385 void JingleSession::SendMessage(std::unique_ptr<JingleMessage> message) { |
284 DCHECK(thread_checker_.CalledOnValidThread()); | 386 DCHECK(thread_checker_.CalledOnValidThread()); |
285 | 387 |
388 std::unique_ptr<buzz::XmlElement> stanza = message->ToXml(); | |
389 stanza->AddAttr(buzz::QN_ID, GetNextOutgoingId()); | |
390 | |
286 auto request = session_manager_->iq_sender()->SendIq( | 391 auto request = session_manager_->iq_sender()->SendIq( |
287 message->ToXml(), base::Bind(&JingleSession::OnMessageResponse, | 392 std::move(stanza), base::Bind(&JingleSession::OnMessageResponse, |
288 base::Unretained(this), message->action)); | 393 base::Unretained(this), message->action)); |
289 | 394 |
290 int timeout = kDefaultMessageTimeout; | 395 int timeout = kDefaultMessageTimeout; |
291 if (message->action == JingleMessage::SESSION_INITIATE || | 396 if (message->action == JingleMessage::SESSION_INITIATE || |
292 message->action == JingleMessage::SESSION_ACCEPT) { | 397 message->action == JingleMessage::SESSION_ACCEPT) { |
293 timeout = kSessionInitiateAndAcceptTimeout; | 398 timeout = kSessionInitiateAndAcceptTimeout; |
294 } | 399 } |
295 if (request) { | 400 if (request) { |
296 request->SetTimeout(base::TimeDelta::FromSeconds(timeout)); | 401 request->SetTimeout(base::TimeDelta::FromSeconds(timeout)); |
297 pending_requests_.insert(std::move(request)); | 402 pending_requests_.insert(std::move(request)); |
298 } else { | 403 } else { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
362 } | 467 } |
363 | 468 |
364 const std::string& type = response->Attr(buzz::QName(std::string(), "type")); | 469 const std::string& type = response->Attr(buzz::QName(std::string(), "type")); |
365 if (type != "result") { | 470 if (type != "result") { |
366 LOG(ERROR) << "Received error in response to transport-info message: \"" | 471 LOG(ERROR) << "Received error in response to transport-info message: \"" |
367 << response->Str() << "\". Terminating the session."; | 472 << response->Str() << "\". Terminating the session."; |
368 Close(PEER_IS_OFFLINE); | 473 Close(PEER_IS_OFFLINE); |
369 } | 474 } |
370 } | 475 } |
371 | 476 |
372 void JingleSession::OnIncomingMessage(std::unique_ptr<JingleMessage> message, | 477 void JingleSession::OnIncomingMessage(const std::string& id, |
478 std::unique_ptr<JingleMessage> message, | |
373 const ReplyCallback& reply_callback) { | 479 const ReplyCallback& reply_callback) { |
480 std::unique_ptr<PendingMessage> item( | |
481 new PendingMessage(std::move(message), reply_callback)); | |
482 std::vector<std::unique_ptr<PendingMessage>> ordered = | |
483 message_queue_->OnIncomingMessage(id, std::move(item)); | |
484 for (auto& message : ordered) { | |
485 ProcessIncomingMessage(std::move(message->message), | |
486 message->reply_callback); | |
487 } | |
488 } | |
489 | |
490 void JingleSession::ProcessIncomingMessage( | |
491 std::unique_ptr<JingleMessage> message, | |
492 const ReplyCallback& reply_callback) { | |
374 DCHECK(thread_checker_.CalledOnValidThread()); | 493 DCHECK(thread_checker_.CalledOnValidThread()); |
375 | 494 |
376 if (peer_address_ != message->from) { | 495 if (peer_address_ != message->from) { |
377 // Ignore messages received from a different Jid. | 496 // Ignore messages received from a different Jid. |
378 reply_callback.Run(JingleMessageReply::INVALID_SID); | 497 reply_callback.Run(JingleMessageReply::INVALID_SID); |
379 return; | 498 return; |
380 } | 499 } |
381 | 500 |
382 switch (message->action) { | 501 switch (message->action) { |
383 case JingleMessage::SESSION_ACCEPT: | 502 case JingleMessage::SESSION_ACCEPT: |
384 OnAccept(std::move(message), reply_callback); | 503 OnAccept(std::move(message), reply_callback); |
385 break; | 504 break; |
386 | 505 |
387 case JingleMessage::SESSION_INFO: | 506 case JingleMessage::SESSION_INFO: |
388 OnSessionInfo(std::move(message), reply_callback); | 507 OnSessionInfo(std::move(message), reply_callback); |
389 break; | 508 break; |
390 | 509 |
391 case JingleMessage::TRANSPORT_INFO: | 510 case JingleMessage::TRANSPORT_INFO: |
392 if (!transport_) { | 511 if (!transport_) { |
393 LOG(ERROR) << "Received unexpected transport-info message->"; | 512 LOG(ERROR) << "Received unexpected transport-info message->"; |
394 reply_callback.Run(JingleMessageReply::NONE); | 513 reply_callback.Run(JingleMessageReply::NONE); |
395 return; | 514 return; |
396 } | 515 } |
397 | 516 |
398 if (!message->transport_info || | 517 if (!message->transport_info || |
399 !transport_->ProcessTransportInfo( | 518 !transport_->ProcessTransportInfo(message->transport_info.get())) { |
400 message->transport_info.get())) { | |
401 reply_callback.Run(JingleMessageReply::BAD_REQUEST); | 519 reply_callback.Run(JingleMessageReply::BAD_REQUEST); |
402 return; | 520 return; |
403 } | 521 } |
404 | 522 |
405 reply_callback.Run(JingleMessageReply::NONE); | 523 reply_callback.Run(JingleMessageReply::NONE); |
406 break; | 524 break; |
407 | 525 |
408 case JingleMessage::SESSION_TERMINATE: | 526 case JingleMessage::SESSION_TERMINATE: |
409 OnTerminate(std::move(message), reply_callback); | 527 OnTerminate(std::move(message), reply_callback); |
410 break; | 528 break; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 if (event_handler_) | 720 if (event_handler_) |
603 event_handler_->OnSessionStateChange(new_state); | 721 event_handler_->OnSessionStateChange(new_state); |
604 } | 722 } |
605 } | 723 } |
606 | 724 |
607 bool JingleSession::is_session_active() { | 725 bool JingleSession::is_session_active() { |
608 return state_ == CONNECTING || state_ == ACCEPTING || state_ == ACCEPTED || | 726 return state_ == CONNECTING || state_ == ACCEPTING || state_ == ACCEPTED || |
609 state_ == AUTHENTICATING || state_ == AUTHENTICATED; | 727 state_ == AUTHENTICATING || state_ == AUTHENTICATED; |
610 } | 728 } |
611 | 729 |
730 std::string JingleSession::GetNextOutgoingId() { | |
731 return outgoing_id_prefix_ + "_" + base::IntToString(++next_outgoing_id_); | |
732 } | |
733 | |
612 } // namespace protocol | 734 } // namespace protocol |
613 } // namespace remoting | 735 } // namespace remoting |
OLD | NEW |