1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/host_status_sender.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringize_macros.h"
#include "base/time/time.h"
#include "remoting/base/constants.h"
#include "remoting/host/server_log_entry.h"
#include "remoting/jingle_glue/iq_sender.h"
#include "remoting/jingle_glue/signal_strategy.h"
#include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
#include "third_party/libjingle/source/talk/xmpp/constants.h"
using buzz::QName;
using buzz::XmlElement;
namespace remoting {
namespace {
const char kHostStatusTag[] = "host-status";
const char kHostIdAttr[] = "hostid";
const char kExitCodeAttr[] = "exit-code";
const char kHostVersionTag[] = "host-version";
const char kSignatureTag[] = "signature";
const char kStatusAttr[] = "status";
const char kSignatureTimeAttr[] = "time";
} // namespace
const char* const HostStatusSender::host_status_strings_[] =
{"OFFLINE", "ONLINE"};
HostStatusSender::HostStatusSender(
const std::string& host_id,
SignalStrategy* signal_strategy,
scoped_refptr<RsaKeyPair> key_pair,
const std::string& directory_bot_jid)
: host_id_(host_id),
signal_strategy_(signal_strategy),
key_pair_(key_pair),
directory_bot_jid_(directory_bot_jid) {
DCHECK(signal_strategy_);
DCHECK(key_pair_.get());
signal_strategy_->AddListener(this);
}
HostStatusSender::~HostStatusSender() {
signal_strategy_->RemoveListener(this);
}
void HostStatusSender::OnSignalStrategyStateChange(
SignalStrategy::State state) {
if (state == SignalStrategy::CONNECTED)
iq_sender_.reset(new IqSender(signal_strategy_));
else if (state == SignalStrategy::DISCONNECTED)
iq_sender_.reset();
}
bool HostStatusSender::OnSignalStrategyIncomingStanza(
const XmlElement* stanza) {
return false;
}
void HostStatusSender::SendOfflineStatus(HostExitCodes exit_code) {
SendHostStatus(OFFLINE, exit_code);
}
void HostStatusSender::SendOnlineStatus() {
SendHostStatus(ONLINE, kSuccessExitCode);
}
void HostStatusSender::SendHostStatus(HostStatus status,
HostExitCodes exit_code) {
SignalStrategy::State state = signal_strategy_->GetState();
if (state == SignalStrategy::CONNECTED) {
LOG(INFO) << "Sending host status '"
<< HostStatusToString(status)
<< "' to "
<< directory_bot_jid_;
iq_sender_->SendIq(buzz::STR_SET,
directory_bot_jid_,
CreateHostStatusMessage(status, exit_code),
IqSender::ReplyCallback());
} else {
LOG(INFO) << "Cannot send host status to '"
<< directory_bot_jid_
<< " ' because the state of the SignalStrategy is "
<< state;
}
}
scoped_ptr<XmlElement> HostStatusSender::CreateHostStatusMessage(
HostStatus status, HostExitCodes exit_code) {
// Create host status stanza.
scoped_ptr<XmlElement> host_status(new XmlElement(
QName(kChromotingXmlNamespace, kHostStatusTag)));
host_status->AddAttr(
QName(kChromotingXmlNamespace, kHostIdAttr), host_id_);
host_status->AddAttr(
QName(kChromotingXmlNamespace, kStatusAttr), HostStatusToString(status));
if (status == OFFLINE) {
host_status->AddAttr(
QName(kChromotingXmlNamespace, kExitCodeAttr),
ExitCodeToString(exit_code));
}
host_status->AddElement(CreateSignature(status, exit_code).release());
// Append host version.
scoped_ptr<XmlElement> version_tag(new XmlElement(
QName(kChromotingXmlNamespace, kHostVersionTag)));
version_tag->AddText(STRINGIZE(VERSION));
host_status->AddElement(version_tag.release());
// Append log message (which isn't signed).
scoped_ptr<XmlElement> log(ServerLogEntry::MakeStanza());
scoped_ptr<ServerLogEntry> log_entry(
ServerLogEntry::MakeForHostStatus(status, exit_code));
log_entry->AddHostFields();
log->AddElement(log_entry->ToStanza().release());
host_status->AddElement(log.release());
return host_status.Pass();
}
scoped_ptr<XmlElement> HostStatusSender::CreateSignature(
HostStatus status, HostExitCodes exit_code) {
scoped_ptr<XmlElement> signature_tag(new XmlElement(
QName(kChromotingXmlNamespace, kSignatureTag)));
// Number of seconds since epoch (Jan 1, 1970).
int64 time = static_cast<int64>(base::Time::Now().ToDoubleT());
std::string time_str(base::Int64ToString(time));
signature_tag->AddAttr(
QName(kChromotingXmlNamespace, kSignatureTimeAttr), time_str);
// Add a time stamp to the signature to prevent replay attacks.
std::string message =
signal_strategy_->GetLocalJid() +
" " +
time_str +
" " +
HostStatusToString(status);
if (status == OFFLINE)
message += std::string(" ") + ExitCodeToString(exit_code);
std::string signature(key_pair_->SignMessage(message));
signature_tag->AddText(signature);
return signature_tag.Pass();
}
} // namespace remoting
|