You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1569 lines
52 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/p2p/base/session.h"
  28. #include "talk/base/common.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/base/helpers.h"
  31. #include "talk/base/scoped_ptr.h"
  32. #include "talk/base/sslstreamadapter.h"
  33. #include "talk/xmpp/constants.h"
  34. #include "talk/xmpp/jid.h"
  35. #include "talk/p2p/base/dtlstransport.h"
  36. #include "talk/p2p/base/p2ptransport.h"
  37. #include "talk/p2p/base/sessionclient.h"
  38. #include "talk/p2p/base/transport.h"
  39. #include "talk/p2p/base/transportchannelproxy.h"
  40. #include "talk/p2p/base/transportinfo.h"
  41. #include "talk/p2p/base/constants.h"
  42. namespace cricket {
  43. bool BadMessage(const buzz::QName type,
  44. const std::string& text,
  45. MessageError* err) {
  46. err->SetType(type);
  47. err->SetText(text);
  48. return false;
  49. }
  50. TransportProxy::~TransportProxy() {
  51. for (ChannelMap::iterator iter = channels_.begin();
  52. iter != channels_.end(); ++iter) {
  53. iter->second->SignalDestroyed(iter->second);
  54. delete iter->second;
  55. }
  56. }
  57. std::string TransportProxy::type() const {
  58. return transport_->get()->type();
  59. }
  60. TransportChannel* TransportProxy::GetChannel(int component) {
  61. return GetChannelProxy(component);
  62. }
  63. TransportChannel* TransportProxy::CreateChannel(
  64. const std::string& name, int component) {
  65. ASSERT(GetChannel(component) == NULL);
  66. ASSERT(!transport_->get()->HasChannel(component));
  67. // We always create a proxy in case we need to change out the transport later.
  68. TransportChannelProxy* channel =
  69. new TransportChannelProxy(content_name(), name, component);
  70. channels_[component] = channel;
  71. // If we're already negotiated, create an impl and hook it up to the proxy
  72. // channel. If we're connecting, create an impl but don't hook it up yet.
  73. if (negotiated_) {
  74. SetChannelProxyImpl(component, channel);
  75. } else if (connecting_) {
  76. GetOrCreateChannelProxyImpl(component);
  77. }
  78. return channel;
  79. }
  80. bool TransportProxy::HasChannel(int component) {
  81. return transport_->get()->HasChannel(component);
  82. }
  83. void TransportProxy::DestroyChannel(int component) {
  84. TransportChannel* channel = GetChannel(component);
  85. if (channel) {
  86. // If the state of TransportProxy is not NEGOTIATED
  87. // then TransportChannelProxy and its impl are not
  88. // connected. Both must be connected before
  89. // deletion.
  90. if (!negotiated_) {
  91. SetChannelProxyImpl(component, GetChannelProxy(component));
  92. }
  93. channels_.erase(component);
  94. channel->SignalDestroyed(channel);
  95. delete channel;
  96. }
  97. }
  98. void TransportProxy::ConnectChannels() {
  99. if (!connecting_) {
  100. if (!negotiated_) {
  101. for (ChannelMap::iterator iter = channels_.begin();
  102. iter != channels_.end(); ++iter) {
  103. GetOrCreateChannelProxyImpl(iter->first);
  104. }
  105. }
  106. connecting_ = true;
  107. }
  108. // TODO(juberti): Right now Transport::ConnectChannels doesn't work if we
  109. // don't have any channels yet, so we need to allow this method to be called
  110. // multiple times. Once we fix Transport, we can move this call inside the
  111. // if (!connecting_) block.
  112. transport_->get()->ConnectChannels();
  113. }
  114. void TransportProxy::CompleteNegotiation() {
  115. if (!negotiated_) {
  116. for (ChannelMap::iterator iter = channels_.begin();
  117. iter != channels_.end(); ++iter) {
  118. SetChannelProxyImpl(iter->first, iter->second);
  119. }
  120. negotiated_ = true;
  121. }
  122. }
  123. void TransportProxy::AddSentCandidates(const Candidates& candidates) {
  124. for (Candidates::const_iterator cand = candidates.begin();
  125. cand != candidates.end(); ++cand) {
  126. sent_candidates_.push_back(*cand);
  127. }
  128. }
  129. void TransportProxy::AddUnsentCandidates(const Candidates& candidates) {
  130. for (Candidates::const_iterator cand = candidates.begin();
  131. cand != candidates.end(); ++cand) {
  132. unsent_candidates_.push_back(*cand);
  133. }
  134. }
  135. bool TransportProxy::GetChannelNameFromComponent(
  136. int component, std::string* channel_name) const {
  137. const TransportChannelProxy* channel = GetChannelProxy(component);
  138. if (channel == NULL) {
  139. return false;
  140. }
  141. *channel_name = channel->name();
  142. return true;
  143. }
  144. bool TransportProxy::GetComponentFromChannelName(
  145. const std::string& channel_name, int* component) const {
  146. const TransportChannelProxy* channel = GetChannelProxyByName(channel_name);
  147. if (channel == NULL) {
  148. return false;
  149. }
  150. *component = channel->component();
  151. return true;
  152. }
  153. TransportChannelProxy* TransportProxy::GetChannelProxy(int component) const {
  154. ChannelMap::const_iterator iter = channels_.find(component);
  155. return (iter != channels_.end()) ? iter->second : NULL;
  156. }
  157. TransportChannelProxy* TransportProxy::GetChannelProxyByName(
  158. const std::string& name) const {
  159. for (ChannelMap::const_iterator iter = channels_.begin();
  160. iter != channels_.end();
  161. ++iter) {
  162. if (iter->second->name() == name) {
  163. return iter->second;
  164. }
  165. }
  166. return NULL;
  167. }
  168. TransportChannelImpl* TransportProxy::GetOrCreateChannelProxyImpl(
  169. int component) {
  170. TransportChannelImpl* impl = transport_->get()->GetChannel(component);
  171. if (impl == NULL) {
  172. impl = transport_->get()->CreateChannel(component);
  173. impl->SetSessionId(sid_);
  174. }
  175. return impl;
  176. }
  177. void TransportProxy::SetChannelProxyImpl(
  178. int component, TransportChannelProxy* transproxy) {
  179. TransportChannelImpl* impl = GetOrCreateChannelProxyImpl(component);
  180. ASSERT(impl != NULL);
  181. transproxy->SetImplementation(impl);
  182. }
  183. // This function muxes |this| onto |target| by repointing |this| at
  184. // |target|'s transport and setting our TransportChannelProxies
  185. // to point to |target|'s underlying implementations.
  186. bool TransportProxy::SetupMux(TransportProxy* target) {
  187. // Bail out if there's nothing to do.
  188. if (transport_ == target->transport_) {
  189. return true;
  190. }
  191. // Replace the impl for all the TransportProxyChannels with the channels
  192. // from |target|'s transport. Fail if there's not an exact match.
  193. for (ChannelMap::const_iterator iter = channels_.begin();
  194. iter != channels_.end(); ++iter) {
  195. if (!target->transport_->get()->HasChannel(iter->first)) {
  196. return false;
  197. }
  198. iter->second->SetImplementation(
  199. target->transport_->get()->CreateChannel(iter->first));
  200. }
  201. // Now replace our transport. Must happen afterwards because
  202. // it deletes all impls as a side effect.
  203. transport_ = target->transport_;
  204. transport_->get()->SignalCandidatesReady.connect(
  205. this, &TransportProxy::OnTransportCandidatesReady);
  206. set_candidates_allocated(target->candidates_allocated());
  207. return true;
  208. }
  209. void TransportProxy::SetRole(TransportRole role) {
  210. transport_->get()->SetRole(role);
  211. }
  212. bool TransportProxy::SetLocalTransportDescription(
  213. const TransportDescription& description, ContentAction action) {
  214. // If this is an answer, finalize the negotiation.
  215. if (action == CA_ANSWER) {
  216. CompleteNegotiation();
  217. }
  218. return transport_->get()->SetLocalTransportDescription(description, action);
  219. }
  220. bool TransportProxy::SetRemoteTransportDescription(
  221. const TransportDescription& description, ContentAction action) {
  222. // If this is an answer, finalize the negotiation.
  223. if (action == CA_ANSWER) {
  224. CompleteNegotiation();
  225. }
  226. return transport_->get()->SetRemoteTransportDescription(description, action);
  227. }
  228. void TransportProxy::OnSignalingReady() {
  229. // If we're starting a new allocation sequence, reset our state.
  230. set_candidates_allocated(false);
  231. transport_->get()->OnSignalingReady();
  232. }
  233. bool TransportProxy::OnRemoteCandidates(const Candidates& candidates,
  234. std::string* error) {
  235. // Ensure the transport is negotiated before handling candidates.
  236. // TODO(juberti): Remove this once everybody calls SetLocalTD.
  237. CompleteNegotiation();
  238. // Verify each candidate before passing down to transport layer.
  239. for (Candidates::const_iterator cand = candidates.begin();
  240. cand != candidates.end(); ++cand) {
  241. if (!transport_->get()->VerifyCandidate(*cand, error))
  242. return false;
  243. if (!HasChannel(cand->component())) {
  244. *error = "Candidate has unknown component: " + cand->ToString() +
  245. " for content: " + content_name_;
  246. return false;
  247. }
  248. }
  249. transport_->get()->OnRemoteCandidates(candidates);
  250. return true;
  251. }
  252. std::string BaseSession::StateToString(State state) {
  253. switch (state) {
  254. case Session::STATE_INIT:
  255. return "STATE_INIT";
  256. case Session::STATE_SENTINITIATE:
  257. return "STATE_SENTINITIATE";
  258. case Session::STATE_RECEIVEDINITIATE:
  259. return "STATE_RECEIVEDINITIATE";
  260. case Session::STATE_SENTACCEPT:
  261. return "STATE_SENTACCEPT";
  262. case Session::STATE_RECEIVEDACCEPT:
  263. return "STATE_RECEIVEDACCEPT";
  264. case Session::STATE_SENTMODIFY:
  265. return "STATE_SENTMODIFY";
  266. case Session::STATE_RECEIVEDMODIFY:
  267. return "STATE_RECEIVEDMODIFY";
  268. case Session::STATE_SENTREJECT:
  269. return "STATE_SENTREJECT";
  270. case Session::STATE_RECEIVEDREJECT:
  271. return "STATE_RECEIVEDREJECT";
  272. case Session::STATE_SENTREDIRECT:
  273. return "STATE_SENTREDIRECT";
  274. case Session::STATE_SENTTERMINATE:
  275. return "STATE_SENTTERMINATE";
  276. case Session::STATE_RECEIVEDTERMINATE:
  277. return "STATE_RECEIVEDTERMINATE";
  278. case Session::STATE_INPROGRESS:
  279. return "STATE_INPROGRESS";
  280. case Session::STATE_DEINIT:
  281. return "STATE_DEINIT";
  282. default:
  283. break;
  284. }
  285. return "STATE_" + talk_base::ToString(state);
  286. }
  287. BaseSession::BaseSession(talk_base::Thread* signaling_thread,
  288. talk_base::Thread* worker_thread,
  289. PortAllocator* port_allocator,
  290. const std::string& sid,
  291. const std::string& content_type,
  292. bool initiator)
  293. : state_(STATE_INIT),
  294. error_(ERROR_NONE),
  295. signaling_thread_(signaling_thread),
  296. worker_thread_(worker_thread),
  297. port_allocator_(port_allocator),
  298. sid_(sid),
  299. content_type_(content_type),
  300. transport_type_(NS_GINGLE_P2P),
  301. initiator_(initiator),
  302. identity_(NULL),
  303. local_description_(NULL),
  304. remote_description_(NULL),
  305. ice_tiebreaker_(talk_base::CreateRandomId64()),
  306. role_switch_(false) {
  307. ASSERT(signaling_thread->IsCurrent());
  308. }
  309. BaseSession::~BaseSession() {
  310. ASSERT(signaling_thread()->IsCurrent());
  311. ASSERT(state_ != STATE_DEINIT);
  312. LogState(state_, STATE_DEINIT);
  313. state_ = STATE_DEINIT;
  314. SignalState(this, state_);
  315. for (TransportMap::iterator iter = transports_.begin();
  316. iter != transports_.end(); ++iter) {
  317. delete iter->second;
  318. }
  319. delete remote_description_;
  320. delete local_description_;
  321. }
  322. bool BaseSession::PushdownTransportDescription(ContentSource source,
  323. ContentAction action) {
  324. if (source == CS_LOCAL) {
  325. return PushdownLocalTransportDescription(local_description_, action);
  326. }
  327. return PushdownRemoteTransportDescription(remote_description_, action);
  328. }
  329. bool BaseSession::PushdownLocalTransportDescription(
  330. const SessionDescription* sdesc,
  331. ContentAction action) {
  332. // Update the Transports with the right information, and trigger them to
  333. // start connecting.
  334. for (TransportMap::iterator iter = transports_.begin();
  335. iter != transports_.end(); ++iter) {
  336. // If no transport info was in this session description, ret == false
  337. // and we just skip this one.
  338. TransportDescription tdesc;
  339. bool ret = GetTransportDescription(
  340. sdesc, iter->second->content_name(), &tdesc);
  341. if (ret) {
  342. if (!iter->second->SetLocalTransportDescription(tdesc, action)) {
  343. return false;
  344. }
  345. iter->second->ConnectChannels();
  346. }
  347. }
  348. return true;
  349. }
  350. bool BaseSession::PushdownRemoteTransportDescription(
  351. const SessionDescription* sdesc,
  352. ContentAction action) {
  353. // Update the Transports with the right information.
  354. for (TransportMap::iterator iter = transports_.begin();
  355. iter != transports_.end(); ++iter) {
  356. TransportDescription tdesc;
  357. // If no transport info was in this session description, ret == false
  358. // and we just skip this one.
  359. bool ret = GetTransportDescription(
  360. sdesc, iter->second->content_name(), &tdesc);
  361. if (ret) {
  362. if (!iter->second->SetRemoteTransportDescription(tdesc, action)) {
  363. return false;
  364. }
  365. }
  366. }
  367. return true;
  368. }
  369. TransportChannel* BaseSession::CreateChannel(const std::string& content_name,
  370. const std::string& channel_name,
  371. int component) {
  372. // We create the proxy "on demand" here because we need to support
  373. // creating channels at any time, even before we send or receive
  374. // initiate messages, which is before we create the transports.
  375. TransportProxy* transproxy = GetOrCreateTransportProxy(content_name);
  376. return transproxy->CreateChannel(channel_name, component);
  377. }
  378. TransportChannel* BaseSession::GetChannel(const std::string& content_name,
  379. int component) {
  380. TransportProxy* transproxy = GetTransportProxy(content_name);
  381. if (transproxy == NULL)
  382. return NULL;
  383. else
  384. return transproxy->GetChannel(component);
  385. }
  386. void BaseSession::DestroyChannel(const std::string& content_name,
  387. int component) {
  388. TransportProxy* transproxy = GetTransportProxy(content_name);
  389. ASSERT(transproxy != NULL);
  390. transproxy->DestroyChannel(component);
  391. }
  392. TransportProxy* BaseSession::GetOrCreateTransportProxy(
  393. const std::string& content_name) {
  394. TransportProxy* transproxy = GetTransportProxy(content_name);
  395. if (transproxy)
  396. return transproxy;
  397. Transport* transport = CreateTransport(content_name);
  398. transport->SetRole(initiator_ ? ROLE_CONTROLLING : ROLE_CONTROLLED);
  399. transport->SetTiebreaker(ice_tiebreaker_);
  400. // TODO: Connect all the Transport signals to TransportProxy
  401. // then to the BaseSession.
  402. transport->SignalConnecting.connect(
  403. this, &BaseSession::OnTransportConnecting);
  404. transport->SignalWritableState.connect(
  405. this, &BaseSession::OnTransportWritable);
  406. transport->SignalRequestSignaling.connect(
  407. this, &BaseSession::OnTransportRequestSignaling);
  408. transport->SignalTransportError.connect(
  409. this, &BaseSession::OnTransportSendError);
  410. transport->SignalRouteChange.connect(
  411. this, &BaseSession::OnTransportRouteChange);
  412. transport->SignalCandidatesAllocationDone.connect(
  413. this, &BaseSession::OnTransportCandidatesAllocationDone);
  414. transport->SignalRoleConflict.connect(
  415. this, &BaseSession::OnRoleConflict);
  416. transproxy = new TransportProxy(sid_, content_name,
  417. new TransportWrapper(transport));
  418. transproxy->SignalCandidatesReady.connect(
  419. this, &BaseSession::OnTransportProxyCandidatesReady);
  420. transports_[content_name] = transproxy;
  421. return transproxy;
  422. }
  423. Transport* BaseSession::GetTransport(const std::string& content_name) {
  424. TransportProxy* transproxy = GetTransportProxy(content_name);
  425. if (transproxy == NULL)
  426. return NULL;
  427. return transproxy->impl();
  428. }
  429. TransportProxy* BaseSession::GetTransportProxy(
  430. const std::string& content_name) {
  431. TransportMap::iterator iter = transports_.find(content_name);
  432. return (iter != transports_.end()) ? iter->second : NULL;
  433. }
  434. TransportProxy* BaseSession::GetTransportProxy(const Transport* transport) {
  435. for (TransportMap::iterator iter = transports_.begin();
  436. iter != transports_.end(); ++iter) {
  437. TransportProxy* transproxy = iter->second;
  438. if (transproxy->impl() == transport) {
  439. return transproxy;
  440. }
  441. }
  442. return NULL;
  443. }
  444. TransportProxy* BaseSession::GetFirstTransportProxy() {
  445. if (transports_.empty())
  446. return NULL;
  447. return transports_.begin()->second;
  448. }
  449. void BaseSession::DestroyTransportProxy(
  450. const std::string& content_name) {
  451. TransportMap::iterator iter = transports_.find(content_name);
  452. if (iter != transports_.end()) {
  453. delete iter->second;
  454. transports_.erase(content_name);
  455. }
  456. }
  457. cricket::Transport* BaseSession::CreateTransport(
  458. const std::string& content_name) {
  459. ASSERT(transport_type_ == NS_GINGLE_P2P);
  460. return new cricket::DtlsTransport<P2PTransport>(
  461. signaling_thread(), worker_thread(), content_name,
  462. port_allocator(), identity_);
  463. }
  464. void BaseSession::SetState(State state) {
  465. ASSERT(signaling_thread_->IsCurrent());
  466. if (state != state_) {
  467. LogState(state_, state);
  468. state_ = state;
  469. SignalState(this, state_);
  470. signaling_thread_->Post(this, MSG_STATE);
  471. }
  472. }
  473. void BaseSession::SetError(Error error) {
  474. ASSERT(signaling_thread_->IsCurrent());
  475. if (error != error_) {
  476. error_ = error;
  477. SignalError(this, error);
  478. }
  479. }
  480. void BaseSession::OnSignalingReady() {
  481. ASSERT(signaling_thread()->IsCurrent());
  482. for (TransportMap::iterator iter = transports_.begin();
  483. iter != transports_.end(); ++iter) {
  484. iter->second->OnSignalingReady();
  485. }
  486. }
  487. // TODO(juberti): Since PushdownLocalTD now triggers the connection process to
  488. // start, remove this method once everyone calls PushdownLocalTD.
  489. void BaseSession::SpeculativelyConnectAllTransportChannels() {
  490. // Put all transports into the connecting state.
  491. for (TransportMap::iterator iter = transports_.begin();
  492. iter != transports_.end(); ++iter) {
  493. iter->second->ConnectChannels();
  494. }
  495. }
  496. bool BaseSession::OnRemoteCandidates(const std::string& content_name,
  497. const Candidates& candidates,
  498. std::string* error) {
  499. // Give candidates to the appropriate transport, and tell that transport
  500. // to start connecting, if it's not already doing so.
  501. TransportProxy* transproxy = GetTransportProxy(content_name);
  502. if (!transproxy) {
  503. *error = "Unknown content name " + content_name;
  504. return false;
  505. }
  506. if (!transproxy->OnRemoteCandidates(candidates, error)) {
  507. return false;
  508. }
  509. // TODO(juberti): Remove this call once we can be sure that we always have
  510. // a local transport description (which will trigger the connection).
  511. transproxy->ConnectChannels();
  512. return true;
  513. }
  514. bool BaseSession::MaybeEnableMuxingSupport() {
  515. // We need both a local and remote description to decide if we should mux.
  516. if ((state_ == STATE_SENTINITIATE ||
  517. state_ == STATE_RECEIVEDINITIATE) &&
  518. ((local_description_ == NULL) ||
  519. (remote_description_ == NULL))) {
  520. return false;
  521. }
  522. // In order to perform the multiplexing, we need all proxies to be in the
  523. // negotiated state, i.e. to have implementations underneath.
  524. // Ensure that this is the case, regardless of whether we are going to mux.
  525. for (TransportMap::iterator iter = transports_.begin();
  526. iter != transports_.end(); ++iter) {
  527. ASSERT(iter->second->negotiated());
  528. if (!iter->second->negotiated())
  529. return false;
  530. }
  531. // If both sides agree to BUNDLE, mux all the specified contents onto the
  532. // transport belonging to the first content name in the BUNDLE group.
  533. // If the contents are already muxed, this will be a no-op.
  534. // TODO(juberti): Should this check that local and remote have configured
  535. // BUNDLE the same way?
  536. bool candidates_allocated = IsCandidateAllocationDone();
  537. const ContentGroup* local_bundle_group =
  538. local_description()->GetGroupByName(GROUP_TYPE_BUNDLE);
  539. const ContentGroup* remote_bundle_group =
  540. remote_description()->GetGroupByName(GROUP_TYPE_BUNDLE);
  541. if (local_bundle_group && remote_bundle_group &&
  542. local_bundle_group->FirstContentName()) {
  543. const std::string* content_name = local_bundle_group->FirstContentName();
  544. const ContentInfo* content =
  545. local_description_->GetContentByName(*content_name);
  546. ASSERT(content != NULL);
  547. if (!SetSelectedProxy(content->name, local_bundle_group)) {
  548. LOG(LS_WARNING) << "Failed to set up BUNDLE";
  549. return false;
  550. }
  551. // If we weren't done gathering before, we might be done now, as a result
  552. // of enabling mux.
  553. LOG(LS_INFO) << "Enabling BUNDLE, bundling onto transport: "
  554. << *content_name;
  555. if (!candidates_allocated) {
  556. MaybeCandidateAllocationDone();
  557. }
  558. } else {
  559. LOG(LS_INFO) << "No BUNDLE information, not bundling.";
  560. }
  561. return true;
  562. }
  563. bool BaseSession::SetSelectedProxy(const std::string& content_name,
  564. const ContentGroup* muxed_group) {
  565. TransportProxy* selected_proxy = GetTransportProxy(content_name);
  566. if (!selected_proxy) {
  567. return false;
  568. }
  569. ASSERT(selected_proxy->negotiated());
  570. for (TransportMap::iterator iter = transports_.begin();
  571. iter != transports_.end(); ++iter) {
  572. // If content is part of the mux group, then repoint its proxy at the
  573. // transport object that we have chosen to mux onto. If the proxy
  574. // is already pointing at the right object, it will be a no-op.
  575. if (muxed_group->HasContentName(iter->first) &&
  576. !iter->second->SetupMux(selected_proxy)) {
  577. return false;
  578. }
  579. }
  580. return true;
  581. }
  582. void BaseSession::OnTransportCandidatesAllocationDone(Transport* transport) {
  583. // TODO(juberti): This is a clunky way of processing the done signal. Instead,
  584. // TransportProxy should receive the done signal directly, set its allocated
  585. // flag internally, and then reissue the done signal to Session.
  586. // Overall we should make TransportProxy receive *all* the signals from
  587. // Transport, since this removes the need to manually iterate over all
  588. // the transports, as is needed to make sure signals are handled properly
  589. // when BUNDLEing.
  590. ASSERT(!IsCandidateAllocationDone());
  591. for (TransportMap::iterator iter = transports_.begin();
  592. iter != transports_.end(); ++iter) {
  593. if (iter->second->impl() == transport) {
  594. iter->second->set_candidates_allocated(true);
  595. }
  596. }
  597. MaybeCandidateAllocationDone();
  598. }
  599. bool BaseSession::IsCandidateAllocationDone() const {
  600. for (TransportMap::const_iterator iter = transports_.begin();
  601. iter != transports_.end(); ++iter) {
  602. if (!iter->second->candidates_allocated())
  603. return false;
  604. }
  605. return true;
  606. }
  607. void BaseSession::MaybeCandidateAllocationDone() {
  608. if (IsCandidateAllocationDone()) {
  609. LOG(LS_INFO) << "Candidate gathering is complete.";
  610. OnCandidatesAllocationDone();
  611. }
  612. }
  613. void BaseSession::OnRoleConflict() {
  614. if (role_switch_) {
  615. LOG(LS_WARNING) << "Repeat of role conflict signal from Transport.";
  616. return;
  617. }
  618. role_switch_ = true;
  619. for (TransportMap::iterator iter = transports_.begin();
  620. iter != transports_.end(); ++iter) {
  621. // Role will be reverse of initial role setting.
  622. TransportRole role = initiator_ ? ROLE_CONTROLLED : ROLE_CONTROLLING;
  623. iter->second->SetRole(role);
  624. }
  625. }
  626. void BaseSession::LogState(State old_state, State new_state) {
  627. LOG(LS_INFO) << "Session:" << id()
  628. << " Old state:" << StateToString(old_state)
  629. << " New state:" << StateToString(new_state)
  630. << " Type:" << content_type()
  631. << " Transport:" << transport_type();
  632. }
  633. bool BaseSession::GetTransportDescription(const SessionDescription* description,
  634. const std::string& content_name,
  635. TransportDescription* tdesc) {
  636. if (!description || !tdesc) {
  637. return false;
  638. }
  639. const TransportInfo* transport_info =
  640. description->GetTransportInfoByName(content_name);
  641. if (!transport_info) {
  642. return false;
  643. }
  644. *tdesc = transport_info->description;
  645. return true;
  646. }
  647. void BaseSession::OnMessage(talk_base::Message *pmsg) {
  648. switch (pmsg->message_id) {
  649. case MSG_TIMEOUT:
  650. // Session timeout has occured.
  651. SetError(ERROR_TIME);
  652. break;
  653. case MSG_STATE:
  654. switch (state_) {
  655. case STATE_SENTACCEPT:
  656. case STATE_RECEIVEDACCEPT:
  657. SetState(STATE_INPROGRESS);
  658. break;
  659. default:
  660. // Explicitly ignoring some states here.
  661. break;
  662. }
  663. break;
  664. }
  665. }
  666. Session::Session(SessionManager* session_manager,
  667. const std::string& local_name,
  668. const std::string& initiator_name,
  669. const std::string& sid,
  670. const std::string& content_type,
  671. SessionClient* client)
  672. : BaseSession(session_manager->signaling_thread(),
  673. session_manager->worker_thread(),
  674. session_manager->port_allocator(),
  675. sid, content_type, initiator_name == local_name) {
  676. ASSERT(client != NULL);
  677. session_manager_ = session_manager;
  678. local_name_ = local_name;
  679. initiator_name_ = initiator_name;
  680. transport_parser_ = new P2PTransportParser();
  681. client_ = client;
  682. initiate_acked_ = false;
  683. current_protocol_ = PROTOCOL_HYBRID;
  684. }
  685. Session::~Session() {
  686. delete transport_parser_;
  687. }
  688. bool Session::Initiate(const std::string &to,
  689. const SessionDescription* sdesc) {
  690. ASSERT(signaling_thread()->IsCurrent());
  691. SessionError error;
  692. // Only from STATE_INIT
  693. if (state() != STATE_INIT)
  694. return false;
  695. // Setup for signaling.
  696. set_remote_name(to);
  697. set_local_description(sdesc);
  698. if (!CreateTransportProxies(GetEmptyTransportInfos(sdesc->contents()),
  699. &error)) {
  700. LOG(LS_ERROR) << "Could not create transports: " << error.text;
  701. return false;
  702. }
  703. if (!SendInitiateMessage(sdesc, &error)) {
  704. LOG(LS_ERROR) << "Could not send initiate message: " << error.text;
  705. return false;
  706. }
  707. // We need to connect transport proxy and impl here so that we can process
  708. // the TransportDescriptions.
  709. SpeculativelyConnectAllTransportChannels();
  710. PushdownTransportDescription(CS_LOCAL, CA_OFFER);
  711. SetState(Session::STATE_SENTINITIATE);
  712. return true;
  713. }
  714. bool Session::Accept(const SessionDescription* sdesc) {
  715. ASSERT(signaling_thread()->IsCurrent());
  716. // Only if just received initiate
  717. if (state() != STATE_RECEIVEDINITIATE)
  718. return false;
  719. // Setup for signaling.
  720. set_local_description(sdesc);
  721. SessionError error;
  722. if (!SendAcceptMessage(sdesc, &error)) {
  723. LOG(LS_ERROR) << "Could not send accept message: " << error.text;
  724. return false;
  725. }
  726. // TODO(juberti): Add BUNDLE support to transport-info messages.
  727. PushdownTransportDescription(CS_LOCAL, CA_ANSWER);
  728. MaybeEnableMuxingSupport(); // Enable transport channel mux if supported.
  729. SetState(Session::STATE_SENTACCEPT);
  730. return true;
  731. }
  732. bool Session::Reject(const std::string& reason) {
  733. ASSERT(signaling_thread()->IsCurrent());
  734. // Reject is sent in response to an initiate or modify, to reject the
  735. // request
  736. if (state() != STATE_RECEIVEDINITIATE && state() != STATE_RECEIVEDMODIFY)
  737. return false;
  738. SessionError error;
  739. if (!SendRejectMessage(reason, &error)) {
  740. LOG(LS_ERROR) << "Could not send reject message: " << error.text;
  741. return false;
  742. }
  743. SetState(STATE_SENTREJECT);
  744. return true;
  745. }
  746. bool Session::TerminateWithReason(const std::string& reason) {
  747. ASSERT(signaling_thread()->IsCurrent());
  748. // Either side can terminate, at any time.
  749. switch (state()) {
  750. case STATE_SENTTERMINATE:
  751. case STATE_RECEIVEDTERMINATE:
  752. return false;
  753. case STATE_SENTREJECT:
  754. case STATE_RECEIVEDREJECT:
  755. // We don't need to send terminate if we sent or received a reject...
  756. // it's implicit.
  757. break;
  758. default:
  759. SessionError error;
  760. if (!SendTerminateMessage(reason, &error)) {
  761. LOG(LS_ERROR) << "Could not send terminate message: " << error.text;
  762. return false;
  763. }
  764. break;
  765. }
  766. SetState(STATE_SENTTERMINATE);
  767. return true;
  768. }
  769. bool Session::SendInfoMessage(const XmlElements& elems) {
  770. ASSERT(signaling_thread()->IsCurrent());
  771. SessionError error;
  772. if (!SendMessage(ACTION_SESSION_INFO, elems, &error)) {
  773. LOG(LS_ERROR) << "Could not send info message " << error.text;
  774. return false;
  775. }
  776. return true;
  777. }
  778. bool Session::SendDescriptionInfoMessage(const ContentInfos& contents) {
  779. XmlElements elems;
  780. WriteError write_error;
  781. if (!WriteDescriptionInfo(current_protocol_,
  782. contents,
  783. GetContentParsers(),
  784. &elems, &write_error)) {
  785. LOG(LS_ERROR) << "Could not write description info message: "
  786. << write_error.text;
  787. return false;
  788. }
  789. SessionError error;
  790. if (!SendMessage(ACTION_DESCRIPTION_INFO, elems, &error)) {
  791. LOG(LS_ERROR) << "Could not send description info message: "
  792. << error.text;
  793. return false;
  794. }
  795. return true;
  796. }
  797. TransportInfos Session::GetEmptyTransportInfos(
  798. const ContentInfos& contents) const {
  799. TransportInfos tinfos;
  800. for (ContentInfos::const_iterator content = contents.begin();
  801. content != contents.end(); ++content) {
  802. tinfos.push_back(
  803. TransportInfo(content->name,
  804. TransportDescription(transport_type(), Candidates())));
  805. }
  806. return tinfos;
  807. }
  808. bool Session::OnRemoteCandidates(
  809. const TransportInfos& tinfos, ParseError* error) {
  810. for (TransportInfos::const_iterator tinfo = tinfos.begin();
  811. tinfo != tinfos.end(); ++tinfo) {
  812. std::string str_error;
  813. if (!BaseSession::OnRemoteCandidates(
  814. tinfo->content_name, tinfo->description.candidates, &str_error)) {
  815. return BadParse(str_error, error);
  816. }
  817. }
  818. return true;
  819. }
  820. bool Session::CreateTransportProxies(const TransportInfos& tinfos,
  821. SessionError* error) {
  822. for (TransportInfos::const_iterator tinfo = tinfos.begin();
  823. tinfo != tinfos.end(); ++tinfo) {
  824. if (tinfo->description.transport_type != transport_type()) {
  825. error->SetText("No supported transport in offer.");
  826. return false;
  827. }
  828. GetOrCreateTransportProxy(tinfo->content_name);
  829. }
  830. return true;
  831. }
  832. TransportParserMap Session::GetTransportParsers() {
  833. TransportParserMap parsers;
  834. parsers[transport_type()] = transport_parser_;
  835. return parsers;
  836. }
  837. CandidateTranslatorMap Session::GetCandidateTranslators() {
  838. CandidateTranslatorMap translators;
  839. // NOTE: This technique makes it impossible to parse G-ICE
  840. // candidates in session-initiate messages because the channels
  841. // aren't yet created at that point. Since we don't use candidates
  842. // in session-initiate messages, we should be OK. Once we switch to
  843. // ICE, this translation shouldn't be necessary.
  844. for (TransportMap::const_iterator iter = transport_proxies().begin();
  845. iter != transport_proxies().end(); ++iter) {
  846. translators[iter->first] = iter->second;
  847. }
  848. return translators;
  849. }
  850. ContentParserMap Session::GetContentParsers() {
  851. ContentParserMap parsers;
  852. parsers[content_type()] = client_;
  853. return parsers;
  854. }
  855. void Session::OnTransportRequestSignaling(Transport* transport) {
  856. ASSERT(signaling_thread()->IsCurrent());
  857. TransportProxy* transproxy = GetTransportProxy(transport);
  858. ASSERT(transproxy != NULL);
  859. if (transproxy) {
  860. // Reset candidate allocation status for the transport proxy.
  861. transproxy->set_candidates_allocated(false);
  862. }
  863. SignalRequestSignaling(this);
  864. }
  865. void Session::OnTransportConnecting(Transport* transport) {
  866. // This is an indication that we should begin watching the writability
  867. // state of the transport.
  868. OnTransportWritable(transport);
  869. }
  870. void Session::OnTransportWritable(Transport* transport) {
  871. ASSERT(signaling_thread()->IsCurrent());
  872. // If the transport is not writable, start a timer to make sure that it
  873. // becomes writable within a reasonable amount of time. If it does not, we
  874. // terminate since we can't actually send data. If the transport is writable,
  875. // cancel the timer. Note that writability transitions may occur repeatedly
  876. // during the lifetime of the session.
  877. signaling_thread()->Clear(this, MSG_TIMEOUT);
  878. if (transport->HasChannels() && !transport->writable()) {
  879. signaling_thread()->PostDelayed(
  880. session_manager_->session_timeout() * 1000, this, MSG_TIMEOUT);
  881. }
  882. }
  883. void Session::OnTransportProxyCandidatesReady(TransportProxy* transproxy,
  884. const Candidates& candidates) {
  885. ASSERT(signaling_thread()->IsCurrent());
  886. if (transproxy != NULL) {
  887. if (initiator() && !initiate_acked_) {
  888. // TODO: This is to work around server re-ordering
  889. // messages. We send the candidates once the session-initiate
  890. // is acked. Once we have fixed the server to guarantee message
  891. // order, we can remove this case.
  892. transproxy->AddUnsentCandidates(candidates);
  893. } else {
  894. if (!transproxy->negotiated()) {
  895. transproxy->AddSentCandidates(candidates);
  896. }
  897. SessionError error;
  898. if (!SendTransportInfoMessage(transproxy, candidates, &error)) {
  899. LOG(LS_ERROR) << "Could not send transport info message: "
  900. << error.text;
  901. return;
  902. }
  903. }
  904. }
  905. }
  906. void Session::OnTransportSendError(Transport* transport,
  907. const buzz::XmlElement* stanza,
  908. const buzz::QName& name,
  909. const std::string& type,
  910. const std::string& text,
  911. const buzz::XmlElement* extra_info) {
  912. ASSERT(signaling_thread()->IsCurrent());
  913. SignalErrorMessage(this, stanza, name, type, text, extra_info);
  914. }
  915. void Session::OnIncomingMessage(const SessionMessage& msg) {
  916. ASSERT(signaling_thread()->IsCurrent());
  917. ASSERT(state() == STATE_INIT || msg.from == remote_name());
  918. if (current_protocol_== PROTOCOL_HYBRID) {
  919. if (msg.protocol == PROTOCOL_GINGLE) {
  920. current_protocol_ = PROTOCOL_GINGLE;
  921. } else {
  922. current_protocol_ = PROTOCOL_JINGLE;
  923. }
  924. }
  925. bool valid = false;
  926. MessageError error;
  927. switch (msg.type) {
  928. case ACTION_SESSION_INITIATE:
  929. valid = OnInitiateMessage(msg, &error);
  930. break;
  931. case ACTION_SESSION_INFO:
  932. valid = OnInfoMessage(msg);
  933. break;
  934. case ACTION_SESSION_ACCEPT:
  935. valid = OnAcceptMessage(msg, &error);
  936. break;
  937. case ACTION_SESSION_REJECT:
  938. valid = OnRejectMessage(msg, &error);
  939. break;
  940. case ACTION_SESSION_TERMINATE:
  941. valid = OnTerminateMessage(msg, &error);
  942. break;
  943. case ACTION_TRANSPORT_INFO:
  944. valid = OnTransportInfoMessage(msg, &error);
  945. break;
  946. case ACTION_TRANSPORT_ACCEPT:
  947. valid = OnTransportAcceptMessage(msg, &error);
  948. break;
  949. case ACTION_DESCRIPTION_INFO:
  950. valid = OnDescriptionInfoMessage(msg, &error);
  951. break;
  952. default:
  953. valid = BadMessage(buzz::QN_STANZA_BAD_REQUEST,
  954. "unknown session message type",
  955. &error);
  956. }
  957. if (valid) {
  958. SendAcknowledgementMessage(msg.stanza);
  959. } else {
  960. SignalErrorMessage(this, msg.stanza, error.type,
  961. "modify", error.text, NULL);
  962. }
  963. }
  964. void Session::OnIncomingResponse(const buzz::XmlElement* orig_stanza,
  965. const buzz::XmlElement* response_stanza,
  966. const SessionMessage& msg) {
  967. ASSERT(signaling_thread()->IsCurrent());
  968. if (msg.type == ACTION_SESSION_INITIATE) {
  969. OnInitiateAcked();
  970. }
  971. }
  972. void Session::OnInitiateAcked() {
  973. // TODO: This is to work around server re-ordering
  974. // messages. We send the candidates once the session-initiate
  975. // is acked. Once we have fixed the server to guarantee message
  976. // order, we can remove this case.
  977. if (!initiate_acked_) {
  978. initiate_acked_ = true;
  979. SessionError error;
  980. SendAllUnsentTransportInfoMessages(&error);
  981. }
  982. }
  983. void Session::OnFailedSend(const buzz::XmlElement* orig_stanza,
  984. const buzz::XmlElement* error_stanza) {
  985. ASSERT(signaling_thread()->IsCurrent());
  986. SessionMessage msg;
  987. ParseError parse_error;
  988. if (!ParseSessionMessage(orig_stanza, &msg, &parse_error)) {
  989. LOG(LS_ERROR) << "Error parsing failed send: " << parse_error.text
  990. << ":" << orig_stanza;
  991. return;
  992. }
  993. // If the error is a session redirect, call OnRedirectError, which will
  994. // continue the session with a new remote JID.
  995. SessionRedirect redirect;
  996. if (FindSessionRedirect(error_stanza, &redirect)) {
  997. SessionError error;
  998. if (!OnRedirectError(redirect, &error)) {
  999. // TODO: Should we send a message back? The standard
  1000. // says nothing about it.
  1001. LOG(LS_ERROR) << "Failed to redirect: " << error.text;
  1002. SetError(ERROR_RESPONSE);
  1003. }
  1004. return;
  1005. }
  1006. std::string error_type = "cancel";
  1007. const buzz::XmlElement* error = error_stanza->FirstNamed(buzz::QN_ERROR);
  1008. if (error) {
  1009. error_type = error->Attr(buzz::QN_TYPE);
  1010. LOG(LS_ERROR) << "Session error:\n" << error->Str() << "\n"
  1011. << "in response to:\n" << orig_stanza->Str();
  1012. } else {
  1013. // don't crash if <error> is missing
  1014. LOG(LS_ERROR) << "Session error without <error/> element, ignoring";
  1015. return;
  1016. }
  1017. if (msg.type == ACTION_TRANSPORT_INFO) {
  1018. // Transport messages frequently generate errors because they are sent right
  1019. // when we detect a network failure. For that reason, we ignore such
  1020. // errors, because if we do not establish writability again, we will
  1021. // terminate anyway. The exceptions are transport-specific error tags,
  1022. // which we pass on to the respective transport.
  1023. } else if ((error_type != "continue") && (error_type != "wait")) {
  1024. // We do not set an error if the other side said it is okay to continue
  1025. // (possibly after waiting). These errors can be ignored.
  1026. SetError(ERROR_RESPONSE);
  1027. }
  1028. }
  1029. bool Session::OnInitiateMessage(const SessionMessage& msg,
  1030. MessageError* error) {
  1031. if (!CheckState(STATE_INIT, error))
  1032. return false;
  1033. SessionInitiate init;
  1034. if (!ParseSessionInitiate(msg.protocol, msg.action_elem,
  1035. GetContentParsers(), GetTransportParsers(),
  1036. GetCandidateTranslators(),
  1037. &init, error))
  1038. return false;
  1039. SessionError session_error;
  1040. if (!CreateTransportProxies(init.transports, &session_error)) {
  1041. return BadMessage(buzz::QN_STANZA_NOT_ACCEPTABLE,
  1042. session_error.text, error);
  1043. }
  1044. set_remote_name(msg.from);
  1045. set_initiator_name(msg.initiator);
  1046. set_remote_description(new SessionDescription(init.ClearContents(),
  1047. init.transports,
  1048. init.groups));
  1049. // Updating transport with TransportDescription.
  1050. PushdownTransportDescription(CS_REMOTE, CA_OFFER);
  1051. SetState(STATE_RECEIVEDINITIATE);
  1052. // Users of Session may listen to state change and call Reject().
  1053. if (state() != STATE_SENTREJECT) {
  1054. if (!OnRemoteCandidates(init.transports, error))
  1055. return false;
  1056. // TODO(juberti): Auto-generate and push down the local transport answer.
  1057. // This is necessary for trickling to work with RFC 5245 ICE.
  1058. }
  1059. return true;
  1060. }
  1061. bool Session::OnAcceptMessage(const SessionMessage& msg, MessageError* error) {
  1062. if (!CheckState(STATE_SENTINITIATE, error))
  1063. return false;
  1064. SessionAccept accept;
  1065. if (!ParseSessionAccept(msg.protocol, msg.action_elem,
  1066. GetContentParsers(), GetTransportParsers(),
  1067. GetCandidateTranslators(),
  1068. &accept, error)) {
  1069. return false;
  1070. }
  1071. // If we get an accept, we can assume the initiate has been
  1072. // received, even if we haven't gotten an IQ response.
  1073. OnInitiateAcked();
  1074. set_remote_description(new SessionDescription(accept.ClearContents(),
  1075. accept.transports,
  1076. accept.groups));
  1077. // Updating transport with TransportDescription.
  1078. PushdownTransportDescription(CS_REMOTE, CA_ANSWER);
  1079. MaybeEnableMuxingSupport(); // Enable transport channel mux if supported.
  1080. SetState(STATE_RECEIVEDACCEPT);
  1081. if (!OnRemoteCandidates(accept.transports, error))
  1082. return false;
  1083. return true;
  1084. }
  1085. bool Session::OnRejectMessage(const SessionMessage& msg, MessageError* error) {
  1086. if (!CheckState(STATE_SENTINITIATE, error))
  1087. return false;
  1088. SetState(STATE_RECEIVEDREJECT);
  1089. return true;
  1090. }
  1091. bool Session::OnInfoMessage(const SessionMessage& msg) {
  1092. SignalInfoMessage(this, msg.action_elem);
  1093. return true;
  1094. }
  1095. bool Session::OnTerminateMessage(const SessionMessage& msg,
  1096. MessageError* error) {
  1097. SessionTerminate term;
  1098. if (!ParseSessionTerminate(msg.protocol, msg.action_elem, &term, error))
  1099. return false;
  1100. SignalReceivedTerminateReason(this, term.reason);
  1101. if (term.debug_reason != buzz::STR_EMPTY) {
  1102. LOG(LS_VERBOSE) << "Received error on call: " << term.debug_reason;
  1103. }
  1104. SetState(STATE_RECEIVEDTERMINATE);
  1105. return true;
  1106. }
  1107. bool Session::OnTransportInfoMessage(const SessionMessage& msg,
  1108. MessageError* error) {
  1109. TransportInfos tinfos;
  1110. if (!ParseTransportInfos(msg.protocol, msg.action_elem,
  1111. initiator_description()->contents(),
  1112. GetTransportParsers(), GetCandidateTranslators(),
  1113. &tinfos, error))
  1114. return false;
  1115. if (!OnRemoteCandidates(tinfos, error))
  1116. return false;
  1117. return true;
  1118. }
  1119. bool Session::OnTransportAcceptMessage(const SessionMessage& msg,
  1120. MessageError* error) {
  1121. // TODO: Currently here only for compatibility with
  1122. // Gingle 1.1 clients (notably, Google Voice).
  1123. return true;
  1124. }
  1125. bool Session::OnDescriptionInfoMessage(const SessionMessage& msg,
  1126. MessageError* error) {
  1127. if (!CheckState(STATE_INPROGRESS, error))
  1128. return false;
  1129. DescriptionInfo description_info;
  1130. if (!ParseDescriptionInfo(msg.protocol, msg.action_elem,
  1131. GetContentParsers(), GetTransportParsers(),
  1132. GetCandidateTranslators(),
  1133. &description_info, error)) {
  1134. return false;
  1135. }
  1136. ContentInfos updated_contents = description_info.ClearContents();
  1137. // TODO: Currently, reflector sends back
  1138. // video stream updates even for an audio-only call, which causes
  1139. // this to fail. Put this back once reflector is fixed.
  1140. //
  1141. // ContentInfos::iterator it;
  1142. // First, ensure all updates are valid before modifying remote_description_.
  1143. // for (it = updated_contents.begin(); it != updated_contents.end(); ++it) {
  1144. // if (remote_description()->GetContentByName(it->name) == NULL) {
  1145. // return false;
  1146. // }
  1147. // }
  1148. // TODO: We used to replace contents from an update, but
  1149. // that no longer works with partial updates. We need to figure out
  1150. // a way to merge patial updates into contents. For now, users of
  1151. // Session should listen to SignalRemoteDescriptionUpdate and handle
  1152. // updates. They should not expect remote_description to be the
  1153. // latest value.
  1154. //
  1155. // for (it = updated_contents.begin(); it != updated_contents.end(); ++it) {
  1156. // remote_description()->RemoveContentByName(it->name);
  1157. // remote_description()->AddContent(it->name, it->type, it->description);
  1158. // }
  1159. // }
  1160. SignalRemoteDescriptionUpdate(this, updated_contents);
  1161. return true;
  1162. }
  1163. bool BareJidsEqual(const std::string& name1,
  1164. const std::string& name2) {
  1165. buzz::Jid jid1(name1);
  1166. buzz::Jid jid2(name2);
  1167. return jid1.IsValid() && jid2.IsValid() && jid1.BareEquals(jid2);
  1168. }
  1169. bool Session::OnRedirectError(const SessionRedirect& redirect,
  1170. SessionError* error) {
  1171. MessageError message_error;
  1172. if (!CheckState(STATE_SENTINITIATE, &message_error)) {
  1173. return BadWrite(message_error.text, error);
  1174. }
  1175. if (!BareJidsEqual(remote_name(), redirect.target))
  1176. return BadWrite("Redirection not allowed: must be the same bare jid.",
  1177. error);
  1178. // When we receive a redirect, we point the session at the new JID
  1179. // and resend the candidates.
  1180. set_remote_name(redirect.target);
  1181. return (SendInitiateMessage(local_description(), error) &&
  1182. ResendAllTransportInfoMessages(error));
  1183. }
  1184. bool Session::CheckState(State expected, MessageError* error) {
  1185. if (state() != expected) {
  1186. return BadMessage(buzz::QN_STANZA_NOT_ALLOWED,
  1187. "message not allowed in current state",
  1188. error);
  1189. }
  1190. return true;
  1191. }
  1192. void Session::SetError(Error error) {
  1193. BaseSession::SetError(error);
  1194. if (error != ERROR_NONE)
  1195. signaling_thread()->Post(this, MSG_ERROR);
  1196. }
  1197. void Session::OnMessage(talk_base::Message* pmsg) {
  1198. // preserve this because BaseSession::OnMessage may modify it
  1199. State orig_state = state();
  1200. BaseSession::OnMessage(pmsg);
  1201. switch (pmsg->message_id) {
  1202. case MSG_ERROR:
  1203. TerminateWithReason(STR_TERMINATE_ERROR);
  1204. break;
  1205. case MSG_STATE:
  1206. switch (orig_state) {
  1207. case STATE_SENTREJECT:
  1208. case STATE_RECEIVEDREJECT:
  1209. // Assume clean termination.
  1210. Terminate();
  1211. break;
  1212. case STATE_SENTTERMINATE:
  1213. case STATE_RECEIVEDTERMINATE:
  1214. session_manager_->DestroySession(this);
  1215. break;
  1216. default:
  1217. // Explicitly ignoring some states here.
  1218. break;
  1219. }
  1220. break;
  1221. }
  1222. }
  1223. bool Session::SendInitiateMessage(const SessionDescription* sdesc,
  1224. SessionError* error) {
  1225. SessionInitiate init;
  1226. init.contents = sdesc->contents();
  1227. init.transports = GetEmptyTransportInfos(init.contents);
  1228. init.groups = sdesc->groups();
  1229. return SendMessage(ACTION_SESSION_INITIATE, init, error);
  1230. }
  1231. bool Session::WriteSessionAction(
  1232. SignalingProtocol protocol, const SessionInitiate& init,
  1233. XmlElements* elems, WriteError* error) {
  1234. return WriteSessionInitiate(protocol, init.contents, init.transports,
  1235. GetContentParsers(), GetTransportParsers(),
  1236. GetCandidateTranslators(), init.groups,
  1237. elems, error);
  1238. }
  1239. bool Session::SendAcceptMessage(const SessionDescription* sdesc,
  1240. SessionError* error) {
  1241. XmlElements elems;
  1242. if (!WriteSessionAccept(current_protocol_,
  1243. sdesc->contents(),
  1244. GetEmptyTransportInfos(sdesc->contents()),
  1245. GetContentParsers(), GetTransportParsers(),
  1246. GetCandidateTranslators(), sdesc->groups(),
  1247. &elems, error)) {
  1248. return false;
  1249. }
  1250. return SendMessage(ACTION_SESSION_ACCEPT, elems, error);
  1251. }
  1252. bool Session::SendRejectMessage(const std::string& reason,
  1253. SessionError* error) {
  1254. SessionTerminate term(reason);
  1255. return SendMessage(ACTION_SESSION_REJECT, term, error);
  1256. }
  1257. bool Session::SendTerminateMessage(const std::string& reason,
  1258. SessionError* error) {
  1259. SessionTerminate term(reason);
  1260. return SendMessage(ACTION_SESSION_TERMINATE, term, error);
  1261. }
  1262. bool Session::WriteSessionAction(SignalingProtocol protocol,
  1263. const SessionTerminate& term,
  1264. XmlElements* elems, WriteError* error) {
  1265. WriteSessionTerminate(protocol, term, elems);
  1266. return true;
  1267. }
  1268. bool Session::SendTransportInfoMessage(const TransportInfo& tinfo,
  1269. SessionError* error) {
  1270. return SendMessage(ACTION_TRANSPORT_INFO, tinfo, error);
  1271. }
  1272. bool Session::SendTransportInfoMessage(const TransportProxy* transproxy,
  1273. const Candidates& candidates,
  1274. SessionError* error) {
  1275. return SendTransportInfoMessage(TransportInfo(transproxy->content_name(),
  1276. TransportDescription(transproxy->type(), candidates)), error);
  1277. }
  1278. bool Session::WriteSessionAction(SignalingProtocol protocol,
  1279. const TransportInfo& tinfo,
  1280. XmlElements* elems, WriteError* error) {
  1281. TransportInfos tinfos;
  1282. tinfos.push_back(tinfo);
  1283. return WriteTransportInfos(protocol, tinfos,
  1284. GetTransportParsers(), GetCandidateTranslators(),
  1285. elems, error);
  1286. }
  1287. bool Session::ResendAllTransportInfoMessages(SessionError* error) {
  1288. for (TransportMap::const_iterator iter = transport_proxies().begin();
  1289. iter != transport_proxies().end(); ++iter) {
  1290. TransportProxy* transproxy = iter->second;
  1291. if (transproxy->sent_candidates().size() > 0) {
  1292. if (!SendTransportInfoMessage(
  1293. transproxy, transproxy->sent_candidates(), error)) {
  1294. LOG(LS_ERROR) << "Could not resend transport info messages: "
  1295. << error->text;
  1296. return false;
  1297. }
  1298. transproxy->ClearSentCandidates();
  1299. }
  1300. }
  1301. return true;
  1302. }
  1303. bool Session::SendAllUnsentTransportInfoMessages(SessionError* error) {
  1304. for (TransportMap::const_iterator iter = transport_proxies().begin();
  1305. iter != transport_proxies().end(); ++iter) {
  1306. TransportProxy* transproxy = iter->second;
  1307. if (transproxy->unsent_candidates().size() > 0) {
  1308. if (!SendTransportInfoMessage(
  1309. transproxy, transproxy->unsent_candidates(), error)) {
  1310. LOG(LS_ERROR) << "Could not send unsent transport info messages: "
  1311. << error->text;
  1312. return false;
  1313. }
  1314. transproxy->ClearUnsentCandidates();
  1315. }
  1316. }
  1317. return true;
  1318. }
  1319. bool Session::SendMessage(ActionType type, const XmlElements& action_elems,
  1320. SessionError* error) {
  1321. talk_base::scoped_ptr<buzz::XmlElement> stanza(
  1322. new buzz::XmlElement(buzz::QN_IQ));
  1323. SessionMessage msg(current_protocol_, type, id(), initiator_name());
  1324. msg.to = remote_name();
  1325. WriteSessionMessage(msg, action_elems, stanza.get());
  1326. SignalOutgoingMessage(this, stanza.get());
  1327. return true;
  1328. }
  1329. template <typename Action>
  1330. bool Session::SendMessage(ActionType type, const Action& action,
  1331. SessionError* error) {
  1332. talk_base::scoped_ptr<buzz::XmlElement> stanza(
  1333. new buzz::XmlElement(buzz::QN_IQ));
  1334. if (!WriteActionMessage(type, action, stanza.get(), error))
  1335. return false;
  1336. SignalOutgoingMessage(this, stanza.get());
  1337. return true;
  1338. }
  1339. template <typename Action>
  1340. bool Session::WriteActionMessage(ActionType type, const Action& action,
  1341. buzz::XmlElement* stanza,
  1342. WriteError* error) {
  1343. if (current_protocol_ == PROTOCOL_HYBRID) {
  1344. if (!WriteActionMessage(PROTOCOL_JINGLE, type, action, stanza, error))
  1345. return false;
  1346. if (!WriteActionMessage(PROTOCOL_GINGLE, type, action, stanza, error))
  1347. return false;
  1348. } else {
  1349. if (!WriteActionMessage(current_protocol_, type, action, stanza, error))
  1350. return false;
  1351. }
  1352. return true;
  1353. }
  1354. template <typename Action>
  1355. bool Session::WriteActionMessage(SignalingProtocol protocol,
  1356. ActionType type, const Action& action,
  1357. buzz::XmlElement* stanza, WriteError* error) {
  1358. XmlElements action_elems;
  1359. if (!WriteSessionAction(protocol, action, &action_elems, error))
  1360. return false;
  1361. SessionMessage msg(protocol, type, id(), initiator_name());
  1362. msg.to = remote_name();
  1363. WriteSessionMessage(msg, action_elems, stanza);
  1364. return true;
  1365. }
  1366. void Session::SendAcknowledgementMessage(const buzz::XmlElement* stanza) {
  1367. talk_base::scoped_ptr<buzz::XmlElement> ack(
  1368. new buzz::XmlElement(buzz::QN_IQ));
  1369. ack->SetAttr(buzz::QN_TO, remote_name());
  1370. ack->SetAttr(buzz::QN_ID, stanza->Attr(buzz::QN_ID));
  1371. ack->SetAttr(buzz::QN_TYPE, "result");
  1372. SignalOutgoingMessage(this, ack.get());
  1373. }
  1374. } // namespace cricket