xmpp_register_support_host_request.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/host/xmpp_register_support_host_request.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/logging.h"
  10. #include "base/strings/string_number_conversions.h"
  11. #include "base/strings/stringize_macros.h"
  12. #include "base/time/time.h"
  13. #include "remoting/base/constants.h"
  14. #include "remoting/host/host_config.h"
  15. #include "remoting/host/host_details.h"
  16. #include "remoting/protocol/errors.h"
  17. #include "remoting/signaling/iq_sender.h"
  18. #include "remoting/signaling/signal_strategy.h"
  19. #include "remoting/signaling/signaling_address.h"
  20. #include "remoting/signaling/signaling_id_util.h"
  21. #include "remoting/signaling/xmpp_constants.h"
  22. #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
  23. using jingle_xmpp::QName;
  24. using jingle_xmpp::XmlElement;
  25. namespace remoting {
  26. using protocol::ErrorCode;
  27. namespace {
  28. // Strings used in the request message we send to the bot.
  29. const char kRegisterQueryTag[] = "register-support-host";
  30. const char kPublicKeyTag[] = "public-key";
  31. const char kSignatureTag[] = "signature";
  32. const char kSignatureTimeAttr[] = "time";
  33. const char kHostVersionTag[] = "host-version";
  34. const char kHostOsNameTag[] = "host-os-name";
  35. const char kHostOsVersionTag[] = "host-os-version";
  36. // Strings used to parse responses received from the bot.
  37. const char kRegisterQueryResultTag[] = "register-support-host-result";
  38. const char kSupportIdTag[] = "support-id";
  39. const char kSupportIdLifetimeTag[] = "support-id-lifetime";
  40. // The signaling timeout for register support host requests.
  41. constexpr int kRegisterRequestTimeoutInSeconds = 10;
  42. } // namespace
  43. XmppRegisterSupportHostRequest::XmppRegisterSupportHostRequest(
  44. const std::string& directory_bot_jid)
  45. : directory_bot_jid_(directory_bot_jid) {}
  46. XmppRegisterSupportHostRequest::~XmppRegisterSupportHostRequest() {
  47. if (signal_strategy_)
  48. signal_strategy_->RemoveListener(this);
  49. }
  50. void XmppRegisterSupportHostRequest::StartRequest(
  51. SignalStrategy* signal_strategy,
  52. scoped_refptr<RsaKeyPair> key_pair,
  53. RegisterCallback callback) {
  54. DCHECK(signal_strategy);
  55. DCHECK(key_pair.get());
  56. DCHECK(callback);
  57. signal_strategy_ = signal_strategy;
  58. key_pair_ = key_pair;
  59. callback_ = std::move(callback);
  60. signal_strategy_->AddListener(this);
  61. iq_sender_ = std::make_unique<IqSender>(signal_strategy_);
  62. }
  63. void XmppRegisterSupportHostRequest::OnSignalStrategyStateChange(
  64. SignalStrategy::State state) {
  65. if (state == SignalStrategy::CONNECTED) {
  66. DCHECK(!callback_.is_null());
  67. // The host_jid will be written to the SupportHostStore for lookup. Use id()
  68. // instead of jid() so that we can write the lcs address instead of the
  69. // remoting bot JID.
  70. std::string host_jid = signal_strategy_->GetLocalAddress().id();
  71. request_ = iq_sender_->SendIq(
  72. kIqTypeSet, directory_bot_jid_, CreateRegistrationRequest(host_jid),
  73. base::BindOnce(&XmppRegisterSupportHostRequest::ProcessResponse,
  74. base::Unretained(this)));
  75. if (!request_) {
  76. LOG(ERROR) << "Error sending the register-support-host request.";
  77. CallCallback(std::string(), base::TimeDelta(),
  78. ErrorCode::SIGNALING_ERROR);
  79. return;
  80. }
  81. request_->SetTimeout(base::Seconds(kRegisterRequestTimeoutInSeconds));
  82. } else if (state == SignalStrategy::DISCONNECTED) {
  83. // We will reach here if signaling fails to connect.
  84. LOG(ERROR) << "Signal strategy disconnected.";
  85. CallCallback(std::string(), base::TimeDelta(), ErrorCode::SIGNALING_ERROR);
  86. }
  87. }
  88. bool XmppRegisterSupportHostRequest::OnSignalStrategyIncomingStanza(
  89. const jingle_xmpp::XmlElement* stanza) {
  90. return false;
  91. }
  92. std::unique_ptr<XmlElement>
  93. XmppRegisterSupportHostRequest::CreateRegistrationRequest(
  94. const std::string& jid) {
  95. auto query = std::make_unique<XmlElement>(
  96. QName(kChromotingXmlNamespace, kRegisterQueryTag));
  97. auto public_key = std::make_unique<XmlElement>(
  98. QName(kChromotingXmlNamespace, kPublicKeyTag));
  99. public_key->AddText(key_pair_->GetPublicKey());
  100. query->AddElement(public_key.release());
  101. query->AddElement(CreateSignature(jid).release());
  102. // Add host version.
  103. auto host_version = std::make_unique<XmlElement>(
  104. QName(kChromotingXmlNamespace, kHostVersionTag));
  105. host_version->AddText(STRINGIZE(VERSION));
  106. query->AddElement(host_version.release());
  107. // Add host os name.
  108. auto host_os_name = std::make_unique<XmlElement>(
  109. QName(kChromotingXmlNamespace, kHostOsNameTag));
  110. host_os_name->AddText(GetHostOperatingSystemName());
  111. query->AddElement(host_os_name.release());
  112. // Add host os version.
  113. auto host_os_version = std::make_unique<XmlElement>(
  114. QName(kChromotingXmlNamespace, kHostOsVersionTag));
  115. host_os_version->AddText(GetHostOperatingSystemVersion());
  116. query->AddElement(host_os_version.release());
  117. return query;
  118. }
  119. std::unique_ptr<XmlElement> XmppRegisterSupportHostRequest::CreateSignature(
  120. const std::string& jid) {
  121. std::unique_ptr<XmlElement> signature_tag(
  122. new XmlElement(QName(kChromotingXmlNamespace, kSignatureTag)));
  123. int64_t time = static_cast<int64_t>(base::Time::Now().ToDoubleT());
  124. std::string time_str(base::NumberToString(time));
  125. signature_tag->AddAttr(QName(kChromotingXmlNamespace, kSignatureTimeAttr),
  126. time_str);
  127. std::string message = NormalizeSignalingId(jid) + ' ' + time_str;
  128. std::string signature(key_pair_->SignMessage(message));
  129. signature_tag->AddText(signature);
  130. return signature_tag;
  131. }
  132. void XmppRegisterSupportHostRequest::ParseResponse(const XmlElement* response,
  133. std::string* support_id,
  134. base::TimeDelta* lifetime,
  135. ErrorCode* error_code) {
  136. if (!response) {
  137. LOG(ERROR) << "register-support-host request timed out.";
  138. *error_code = ErrorCode::SIGNALING_TIMEOUT;
  139. return;
  140. }
  141. std::string type = response->Attr(kQNameType);
  142. if (type == kIqTypeError) {
  143. LOG(ERROR) << "Received error in response to heartbeat: "
  144. << response->Str();
  145. *error_code = ErrorCode::HOST_REGISTRATION_ERROR;
  146. return;
  147. }
  148. // This method must only be called for error or result stanzas.
  149. if (type != kIqTypeResult) {
  150. LOG(ERROR) << "Received unexpect stanza of type \"" << type << "\"";
  151. *error_code = ErrorCode::HOST_REGISTRATION_ERROR;
  152. return;
  153. }
  154. const XmlElement* result_element = response->FirstNamed(
  155. QName(kChromotingXmlNamespace, kRegisterQueryResultTag));
  156. if (!result_element) {
  157. LOG(ERROR) << "<" << kRegisterQueryResultTag
  158. << "> is missing in the host registration response: "
  159. << response->Str();
  160. *error_code = ErrorCode::HOST_REGISTRATION_ERROR;
  161. return;
  162. }
  163. const XmlElement* support_id_element =
  164. result_element->FirstNamed(QName(kChromotingXmlNamespace, kSupportIdTag));
  165. if (!support_id_element) {
  166. LOG(ERROR) << "<" << kSupportIdTag
  167. << "> is missing in the host registration response: "
  168. << response->Str();
  169. *error_code = ErrorCode::HOST_REGISTRATION_ERROR;
  170. return;
  171. }
  172. const XmlElement* lifetime_element = result_element->FirstNamed(
  173. QName(kChromotingXmlNamespace, kSupportIdLifetimeTag));
  174. if (!lifetime_element) {
  175. LOG(ERROR) << "<" << kSupportIdLifetimeTag
  176. << "> is missing in the host registration response: "
  177. << response->Str();
  178. *error_code = ErrorCode::HOST_REGISTRATION_ERROR;
  179. return;
  180. }
  181. int lifetime_int;
  182. if (!base::StringToInt(lifetime_element->BodyText(), &lifetime_int) ||
  183. lifetime_int <= 0) {
  184. LOG(ERROR) << "<" << kSupportIdLifetimeTag
  185. << "> is malformed in the host registration response: "
  186. << response->Str();
  187. *error_code = ErrorCode::HOST_REGISTRATION_ERROR;
  188. return;
  189. }
  190. *support_id = support_id_element->BodyText();
  191. *lifetime = base::Seconds(lifetime_int);
  192. return;
  193. }
  194. void XmppRegisterSupportHostRequest::ProcessResponse(
  195. IqRequest* request,
  196. const XmlElement* response) {
  197. std::string support_id;
  198. base::TimeDelta lifetime;
  199. ErrorCode error_code = ErrorCode::OK;
  200. ParseResponse(response, &support_id, &lifetime, &error_code);
  201. CallCallback(support_id, lifetime, error_code);
  202. }
  203. void XmppRegisterSupportHostRequest::CallCallback(const std::string& support_id,
  204. base::TimeDelta lifetime,
  205. ErrorCode error_code) {
  206. // Cleanup state before calling the callback.
  207. request_.reset();
  208. iq_sender_.reset();
  209. signal_strategy_->RemoveListener(this);
  210. signal_strategy_ = nullptr;
  211. std::move(callback_).Run(support_id, lifetime, error_code);
  212. }
  213. } // namespace remoting