/
xmpp_session.cc
318 lines (277 loc) · 9.94 KB
/
xmpp_session.cc
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "xmpp/xmpp_session.h"
#include "xmpp/xmpp_connection.h"
#include "xmpp/xmpp_log.h"
#include "xmpp/xmpp_proto.h"
#include "xmpp/xmpp_server.h"
#include "xmpp/xmpp_state_machine.h"
#include "sandesh/sandesh_types.h"
#include "sandesh/sandesh.h"
#include "sandesh/sandesh_trace.h"
#include "sandesh/xmpp_trace_sandesh_types.h"
using namespace std;
using boost::asio::mutable_buffer;
const boost::regex XmppSession::patt_(rXMPP_MESSAGE);
const boost::regex XmppSession::stream_patt_(rXMPP_STREAM_START);
const boost::regex XmppSession::stream_res_end_(rXMPP_STREAM_END);
const boost::regex XmppSession::whitespace_(sXMPP_WHITESPACE);
const boost::regex XmppSession::stream_features_patt_(rXMPP_STREAM_FEATURES);
const boost::regex XmppSession::starttls_patt_(rXMPP_STREAM_STARTTLS);
const boost::regex XmppSession::proceed_patt_(rXMPP_STREAM_PROCEED);
const boost::regex XmppSession::end_patt_(rXMPP_STREAM_STANZA_END);
XmppSession::XmppSession(XmppConnectionManager *manager, SslSocket *socket,
bool async_ready)
: SslSession(manager, socket, async_ready),
manager_(manager),
connection_(NULL),
tag_known_(0),
index_(-1),
stats_(XmppStanza::RESERVED_STANZA, XmppSession::StatsPair(0, 0)),
keepalive_probes_(kSessionKeepaliveProbes) {
buf_.reserve(kMaxMessageSize);
offset_ = buf_.begin();
stream_open_matched_ = false;
}
XmppSession::~XmppSession() {
set_observer(NULL);
connection_ = NULL;
}
void XmppSession::SetConnection(XmppConnection *connection) {
connection_ = connection;
index_ = connection_->GetIndex();
}
void XmppSession::ClearConnection() {
connection_ = NULL;
index_ = -1;
}
//
// Concurrency: called in the context of bgp::Config task.
//
// Process write ready callback.
//
void XmppSession::ProcessWriteReady() {
if (!connection_)
return;
connection_->WriteReady();
}
//
// Concurrency: called in the context of io thread.
//
// Handle write ready callback.
//
// Enqueue session to the XmppConnectionManager. The session is added to a
// WorkQueue gets processed in the context of bgp::Config task. Doing this
// ensures that we don't access the XmppConnection while the XmppConnection
// is trying to clear our back pointer to it.
//
// We can ignore any errors since the StateMachine will get informed of the
// TcpSession close independently and react to it.
//
void XmppSession::WriteReady(const boost::system::error_code &error) {
if (error)
return;
manager_->EnqueueSession(this);
}
XmppSession::StatsPair XmppSession::Stats(unsigned int type) const {
assert (type < (unsigned int)XmppStanza::RESERVED_STANZA);
return stats_[type];
}
void XmppSession::IncStats(unsigned int type, uint64_t bytes) {
assert (type < (unsigned int)XmppStanza::RESERVED_STANZA);
stats_[type].first++;
stats_[type].second += bytes;
}
boost::system::error_code XmppSession::EnableTcpKeepalive(int hold_time) {
if (hold_time <= 9) {
hold_time = 9; // min hold-time in secs.
}
hold_time = ((hold_time > 18)? hold_time/2 : hold_time);
keepalive_idle_time_ = hold_time/3;
keepalive_interval_ =
((hold_time - keepalive_idle_time_)/keepalive_probes_);
tcp_user_timeout_ = (hold_time * 1000); // msec
return (SetSocketKeepaliveOptions(keepalive_idle_time_,
keepalive_interval_,
keepalive_probes_,
tcp_user_timeout_));
}
boost::regex XmppSession::tag_to_pattern(const char *tag) {
std::string token("</");
token += ++tag;
token += "[\\s\\t\\r\\n]*>";
boost::regex exp(token.c_str());
return exp;
}
void XmppSession::SetBuf(const std::string &str) {
if (buf_.empty()) {
ReplaceBuf(str);
} else {
int pos = offset_ - buf_.begin();
buf_ += str;
offset_ = buf_.begin() + pos;
}
}
void XmppSession::ReplaceBuf(const std::string &str) {
buf_ = str;
buf_.reserve(kMaxMessageSize+8);
offset_ = buf_.begin();
}
bool XmppSession::LeftOver() const {
if (buf_.empty())
return false;
return (buf_.end() != offset_);
}
// Match a pattern in the buffer. Partially matched string is
// kept in buf_ for use in conjucntion with next buffer read.
int XmppSession::MatchRegex(const boost::regex &patt) {
std::string::const_iterator end = buf_.end();
if (regex_search(offset_, end, res_, patt,
boost::match_default | boost::match_partial) == 0) {
return -1;
}
if(res_[0].matched == false) {
// partial match
offset_ = res_[0].first;
return 1;
} else {
begin_tag_ = string(res_[0].first, res_[0].second);
offset_ = res_[0].second;
return 0;
}
}
bool XmppSession::Match(Buffer buffer, int *result, bool NewBuf) {
const XmppConnection *connection = this->Connection();
if (connection == NULL) {
return true;
}
xmsm::XmState state = connection->GetStateMcState();
xmsm::XmOpenConfirmState oc_state =
connection->GetStateMcOpenConfirmState();
if (NewBuf) {
const uint8_t *cp = BufferData(buffer);
// TODO Avoid this copy
std::string str(cp, cp + BufferSize(buffer));
XmppSession::SetBuf(str);
}
int m = -1;
*result = 0;
do {
if (!tag_known_) {
// check for whitespaces
size_t pos = buf_.find_first_not_of(sXMPP_VALIDWS);
if (pos != 0) {
if (pos == string::npos) pos = buf_.size();
offset_ = buf_.begin() + pos;
return false;
}
}
if (state == xmsm::ACTIVE || state == xmsm::IDLE) {
m = MatchRegex(tag_known_ ? stream_res_end_:stream_patt_);
} else if (state == xmsm::CONNECT || state == xmsm::OPENSENT) {
// Note, these are client only states
if (!stream_open_matched_) {
m = MatchRegex(tag_known_ ? stream_res_end_:stream_patt_);
if ((m == 0) && (tag_known_)) {
stream_open_matched_ = true;
}
} else {
m = MatchRegex(tag_known_ ? tag_to_pattern(begin_tag_.c_str()):
stream_features_patt_);
}
} else if ((state == xmsm::OPENCONFIRM) && !(IsSslDisabled())) {
if (connection->IsClient()) {
if (oc_state == xmsm::OPENCONFIRM_FEATURE_NEGOTIATION) {
m = MatchRegex(tag_known_ ? end_patt_: proceed_patt_);
if ((m == 0) && (tag_known_)) {
// set the flag, as we do not want OnRead function to
// read any more data from basic socket.
SetSslHandShakeInProgress(true);
}
} else if (oc_state == xmsm::OPENCONFIRM_FEATURE_SUCCESS) {
m = MatchRegex(tag_known_ ? stream_res_end_:stream_patt_);
} else {
m = MatchRegex(tag_known_ ? tag_to_pattern(begin_tag_.c_str()):
stream_features_patt_);
}
} else {
if (oc_state == xmsm::OPENCONFIRM_FEATURE_SUCCESS) {
m = MatchRegex(tag_known_ ? stream_res_end_:stream_patt_);
} else {
m = MatchRegex(tag_known_ ? end_patt_:starttls_patt_);
if ((m == 0) && (tag_known_)) {
SetSslHandShakeInProgress(true);
}
}
}
} else if (state == xmsm::OPENCONFIRM || state == xmsm::ESTABLISHED) {
m = MatchRegex(tag_known_ ? tag_to_pattern(begin_tag_.c_str()):patt_);
}
if (m == 0) { // full match
*result = 0;
tag_known_ ^= 1;
if (!tag_known_) {
// Found well formed xml
return false;
}
} else if (m == -1) { // no match
return true;
} else {
return true; // partial. read more
}
} while (true);
return true;
}
// Read the socket stream and send messages to the connection object.
// The buffer is copied to local string for regex match.
// TODO Code need to change st Match() is done on buffer itself.
void XmppSession::OnRead(Buffer buffer) {
if (this->Connection() == NULL || !connection_) {
// Connection is deleted. Session is being deleted as well
// Drop the packet.
ReleaseBuffer(buffer);
return;
}
if (connection_->disable_read()) {
ReleaseBuffer(buffer);
// Reset the hold timer as we did receive some thing from the peer
connection_->state_machine()->StartHoldTimer();
return;
}
int result = 0;
bool more = Match(buffer, &result, true);
do {
if (more == false) {
if (result < 0) {
// TODO generate error, close connection.
break;
}
// We got good match. Process the message
std::string::const_iterator st = buf_.begin();
std::string xml = string(st, offset_);
// Ensure we have not reached the end
if (buf_.begin() == offset_) { // xml.size() == 0
buf_.clear();
break;
}
connection_->ReceiveMsg(this, xml);
} else {
// Read more data. Either we have partial match
// or no match but in this state we need to keep
// reading data.
break;
}
if (LeftOver()) {
std::string::const_iterator st = buf_.end();
ReplaceBuf(string(offset_, st));
more = Match(buffer, &result, false);
} else {
// No more data in the Buffer
buf_.clear();
break;
}
} while (true);
ReleaseBuffer(buffer);
return;
}