Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "xmpp/xmpp_state_machine.h"
7 :
8 : #include <atomic>
9 : #include <typeinfo>
10 : #include <boost/bind/bind.hpp>
11 : #include <boost/date_time/posix_time/posix_time.hpp>
12 : #include <boost/statechart/custom_reaction.hpp>
13 : #include <boost/statechart/event.hpp>
14 : #include <boost/statechart/simple_state.hpp>
15 : #include <boost/statechart/state.hpp>
16 : #include <boost/statechart/state_machine.hpp>
17 : #include <boost/statechart/transition.hpp>
18 :
19 : #include "base/logging.h"
20 : #include "base/task_annotations.h"
21 : #include "io/event_manager.h"
22 : #include "io/ssl_session.h"
23 : #include "sandesh/sandesh_trace.h"
24 : #include "sandesh/common/vns_types.h"
25 : #include "sandesh/common/vns_constants.h"
26 : #include "sandesh/xmpp_client_server_sandesh_types.h"
27 : #include "sandesh/xmpp_peer_info_types.h"
28 : #include "sandesh/xmpp_state_machine_sandesh_types.h"
29 : #include "sandesh/xmpp_trace_sandesh_types.h"
30 : #include "xmpp/xmpp_connection.h"
31 : #include "xmpp/xmpp_factory.h"
32 : #include "xmpp/xmpp_log.h"
33 : #include "xmpp/xmpp_server.h"
34 : #include "xmpp/xmpp_session.h"
35 :
36 : using namespace std;
37 : using namespace boost::placeholders;
38 :
39 : namespace mpl = boost::mpl;
40 : namespace sc = boost::statechart;
41 :
42 : #define SM_LOG(_sm, _msg) do { \
43 : XMPP_UTDEBUG(XmppStateMachineDebug, \
44 : (_sm)->connection() ? (_sm)->connection()->ToUVEKey() : "", \
45 : XMPP_PEER_DIR_NA, (_sm)->ChannelType(), _msg); \
46 : } while (false)
47 :
48 : namespace xmsm {
49 : // events
50 :
51 : struct EvStart : sc::event<EvStart> {
52 : static const char *Name() {
53 : return "EvStart";
54 : }
55 : };
56 :
57 : struct EvStop : sc::event<EvStop> {
58 130 : static const char *Name() {
59 130 : return "EvStop";
60 : }
61 : };
62 :
63 : struct EvAdminDown : sc::event<EvAdminDown> {
64 32 : static const char *Name() {
65 32 : return "EvAdminDown";
66 : }
67 : };
68 :
69 : struct EvConnectTimerExpired : sc::event<EvConnectTimerExpired> {
70 271 : static const char *Name() {
71 271 : return "EvConnectTimerExpired";
72 : }
73 : };
74 :
75 : struct EvOpenTimerExpired : sc::event<EvOpenTimerExpired> {
76 1 : static const char *Name() {
77 1 : return "EvOpenTimerExpired";
78 : }
79 : };
80 :
81 : struct EvHoldTimerExpired : sc::event<EvHoldTimerExpired> {
82 13 : static const char *Name() {
83 13 : return "EvHoldTimerExpired";
84 : }
85 : };
86 :
87 : struct EvTcpConnected : sc::event<EvTcpConnected> {
88 188 : EvTcpConnected(XmppSession *session) : session(session) { };
89 188 : static const char *Name() {
90 188 : return "EvTcpConnected";
91 : }
92 : XmppSession *session;
93 : } ;
94 :
95 : struct EvTcpConnectFail : sc::event<EvTcpConnectFail> {
96 78 : EvTcpConnectFail(XmppSession *session) : session(session) { };
97 78 : static const char *Name() {
98 78 : return "EvTcpConnectFail";
99 : }
100 : XmppSession *session;
101 : } ;
102 :
103 : struct EvTcpPassiveOpen : sc::event<EvTcpPassiveOpen> {
104 150 : EvTcpPassiveOpen(XmppSession *session) : session(session) { };
105 150 : static const char *Name() {
106 150 : return "EvTcpPassiveOpen";
107 : }
108 : XmppSession *session;
109 : };
110 :
111 : struct EvTcpClose : sc::event<EvTcpClose> {
112 163 : EvTcpClose(XmppSession *session) : session(session) { };
113 147 : static const char *Name() {
114 147 : return "EvTcpClose";
115 : }
116 : XmppSession *session;
117 : };
118 :
119 : struct EvTcpDeleteSession : sc::event<EvTcpDeleteSession> {
120 321 : explicit EvTcpDeleteSession(TcpSession *session) : session(session) { }
121 : static const char *Name() {
122 : return "EvTcpDeleteSession";
123 : }
124 : TcpSession *session;
125 : };
126 :
127 : struct EvXmppMessage : sc::event<EvXmppMessage> {
128 312020 : explicit EvXmppMessage(XmppSession *session,
129 312020 : const XmppStanza::XmppMessage *msg) : session(session), msg(msg) { }
130 : static const char *Name() {
131 : return "EvXmppMessage";
132 : }
133 : XmppSession *session;
134 : const XmppStanza::XmppMessage *msg;
135 : };
136 :
137 : struct EvXmppOpen : public sc::event<EvXmppOpen> {
138 494 : EvXmppOpen(XmppSession *session, const XmppStanza::XmppMessage *msg) :
139 494 : session(session),
140 494 : msg(static_cast<const XmppStanza::XmppStreamMessage *>(msg)) {
141 494 : }
142 493 : static const char *Name() {
143 493 : return "EvXmppOpen";
144 : }
145 : XmppSession *session;
146 : boost::shared_ptr<const XmppStanza::XmppStreamMessage> msg;
147 : };
148 :
149 : struct EvStreamFeatureRequest : sc::event<EvStreamFeatureRequest> {
150 83 : EvStreamFeatureRequest(XmppSession *session,
151 83 : const XmppStanza::XmppMessage *msg) :
152 83 : session(session),
153 83 : msg(static_cast<const XmppStanza::XmppStreamMessage *>(msg)) {
154 83 : }
155 83 : static const char *Name() {
156 83 : return "EvStreamFeatureRequest";
157 : }
158 : XmppSession *session;
159 : boost::shared_ptr<const XmppStanza::XmppMessage> msg;
160 : };
161 :
162 : struct EvStartTls : sc::event<EvStartTls> {
163 78 : EvStartTls(XmppSession *session,
164 78 : const XmppStanza::XmppMessage *msg) :
165 78 : session(session),
166 78 : msg(static_cast<const XmppStanza::XmppStreamMessage *>(msg)) {
167 78 : }
168 78 : static const char *Name() {
169 78 : return "EvStartTls";
170 : }
171 : XmppSession *session;
172 : boost::shared_ptr<const XmppStanza::XmppMessage> msg;
173 : };
174 :
175 : struct EvTlsProceed : sc::event<EvTlsProceed> {
176 81 : EvTlsProceed(XmppSession *session,
177 81 : const XmppStanza::XmppMessage *msg) :
178 81 : session(session),
179 81 : msg(static_cast<const XmppStanza::XmppStreamMessage *>(msg)) {
180 81 : }
181 81 : static const char *Name() {
182 81 : return "EvTlsProceed";
183 : }
184 : XmppSession *session;
185 : boost::shared_ptr<const XmppStanza::XmppMessage> msg;
186 : };
187 :
188 : struct EvTlsHandShakeSuccess : sc::event<EvTlsHandShakeSuccess> {
189 152 : explicit EvTlsHandShakeSuccess(XmppSession *session) :
190 152 : session(session) { }
191 152 : static const char *Name() {
192 152 : return "EvTlsHandShakeSuccess";
193 : }
194 : XmppSession *session;
195 : };
196 :
197 : struct EvTlsHandShakeFailure : sc::event<EvTlsHandShakeFailure> {
198 2 : explicit EvTlsHandShakeFailure(XmppSession *session) :
199 2 : session(session) { }
200 2 : static const char *Name() {
201 2 : return "EvTlsHandShakeFailure";
202 : }
203 : XmppSession *session;
204 : };
205 :
206 : struct EvXmppKeepalive : sc::event<EvXmppKeepalive> {
207 189875 : EvXmppKeepalive(XmppSession *session,
208 189875 : const XmppStanza::XmppMessage *msg) :
209 189875 : session(session), msg(msg) {
210 189875 : }
211 : static const char *Name() {
212 : return "EvXmppKeepalive";
213 : }
214 : XmppSession *session;
215 : boost::shared_ptr<const XmppStanza::XmppMessage> msg;
216 : };
217 :
218 : struct EvXmppMessageStanza : sc::event<EvXmppMessageStanza> {
219 120084 : EvXmppMessageStanza(XmppSession *session,
220 120084 : const XmppStanza::XmppMessage *msg) :
221 120084 : session(session), msg(msg) {
222 120084 : }
223 : static const char *Name() {
224 : return "EvXmppMessageStanza";
225 : }
226 : XmppSession *session;
227 : boost::shared_ptr<const XmppStanza::XmppMessage> msg;
228 : };
229 :
230 : struct EvXmppIqStanza : sc::event<EvXmppIqStanza> {
231 1325 : EvXmppIqStanza(XmppSession *session,
232 : const XmppStanza::XmppMessage *msg)
233 1325 : : session(session), msg(msg) {
234 1325 : }
235 : static const char *Name() {
236 : return "EvXmppIqStanza";
237 : }
238 : XmppSession *session;
239 : boost::shared_ptr<const XmppStanza::XmppMessage> msg;
240 : };
241 :
242 : struct EvXmppOpenReceive : sc::event<EvXmppOpenReceive> {
243 : EvXmppOpenReceive(XmppSession *session) : session(session) {
244 : }
245 : static const char *Name() {
246 : return "EvXmppOpenReceive";
247 : }
248 : XmppSession *session;
249 : };
250 :
251 : struct Idle : public sc::state<Idle, XmppStateMachine> {
252 : typedef sc::transition<EvStart, Active,
253 : XmppStateMachine, &XmppStateMachine::OnStart> reactions;
254 523 : Idle(my_context ctx) : my_base(ctx) {
255 523 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
256 523 : bool flap = (state_machine->get_state() == ESTABLISHED);
257 523 : state_machine->set_state(IDLE);
258 523 : state_machine->SendConnectionInfo("Start", "Active");
259 523 : if (flap) {
260 186 : state_machine->connection()->increment_flap_count();
261 : }
262 523 : }
263 : };
264 :
265 : struct Active : public sc::state<Active, XmppStateMachine> {
266 : typedef mpl::list<
267 : sc::custom_reaction<EvAdminDown>,
268 : sc::custom_reaction<EvConnectTimerExpired>,
269 : sc::custom_reaction<EvOpenTimerExpired>,
270 : sc::custom_reaction<EvTcpPassiveOpen>,
271 : sc::custom_reaction<EvTcpClose>,
272 : sc::custom_reaction<EvXmppOpen>,
273 : sc::custom_reaction<EvStop>
274 : > reactions;
275 :
276 553 : Active(my_context ctx) : my_base(ctx) {
277 553 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
278 553 : state_machine->keepalive_count_clear();
279 553 : bool flap = (state_machine->get_state() == ESTABLISHED);
280 553 : state_machine->set_state(ACTIVE);
281 553 : if (flap) {
282 114 : state_machine->connection()->increment_flap_count();
283 : }
284 553 : if (state_machine->IsActiveChannel() ) {
285 363 : if (state_machine->get_connect_attempts() >=
286 : XmppStateMachine::kMaxAttempts) {
287 11 : XmppConnection *connection = state_machine->connection();
288 11 : if (connection) {
289 11 : state_machine->SendConnectionInfo(
290 : "Connect failed after retries");
291 : // Notify clients if any action to be taken
292 11 : connection->ChannelMux()->HandleStateEvent(xmsm::ACTIVE);
293 : }
294 : }
295 363 : state_machine->StartConnectTimer(state_machine->GetConnectTime());
296 : }
297 553 : }
298 1106 : ~Active() {
299 553 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
300 553 : state_machine->CancelConnectTimer();
301 553 : state_machine->CancelOpenTimer();
302 1106 : }
303 :
304 : //event on client only
305 270 : sc::result react(const EvConnectTimerExpired &event) {
306 270 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
307 270 : if (state_machine->ConnectTimerCancelled()) {
308 0 : SM_LOG(state_machine, "Discard EvConnectTimerExpired in (Active) State");
309 0 : return discard_event();
310 : } else {
311 270 : state_machine->SendConnectionInfo(event.Name(), "Connect");
312 270 : SM_LOG(state_machine, "EvConnectTimerExpired in (Active) State");
313 270 : return transit<Connect>();
314 : }
315 : }
316 :
317 : // event on server only
318 150 : sc::result react(const EvTcpPassiveOpen &event) {
319 150 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
320 150 : event.session->AsyncReadStart();
321 150 : assert(state_machine->session() == event.session);
322 150 : event.session->set_observer(
323 : boost::bind(&XmppStateMachine::OnSessionEvent,
324 : state_machine, _1, _2));
325 150 : state_machine->StartOpenTimer(XmppStateMachine::kOpenTime);
326 150 : XmppConnectionInfo info;
327 150 : info.set_local_port(event.session->local_port());
328 150 : info.set_remote_port(event.session->remote_port());
329 150 : state_machine->SendConnectionInfo(&info, event.Name());
330 300 : return discard_event();
331 150 : }
332 :
333 : // event on server only
334 1 : sc::result react(const EvTcpClose &event) {
335 1 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
336 1 : if (event.session != state_machine->session()) {
337 0 : return discard_event();
338 : }
339 1 : state_machine->CancelOpenTimer();
340 1 : state_machine->ResetSession();
341 1 : state_machine->SendConnectionInfo(event.Name(), "Idle");
342 1 : return transit<Idle>();
343 : }
344 :
345 : //event on server only
346 1 : sc::result react(const EvOpenTimerExpired &event) {
347 1 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
348 1 : if (state_machine->OpenTimerCancelled()) {
349 0 : SM_LOG(state_machine,
350 : "Discard EvOpenTimerExpired in (Active) State");
351 0 : return discard_event();
352 : }
353 :
354 : // At this point session on connection is not set, hence SendClose
355 : // using session on the state_machine.
356 1 : XmppSession *session = state_machine->session();
357 1 : XmppConnection *connection = state_machine->connection();
358 1 : connection->SendClose(session);
359 1 : state_machine->ResetSession();
360 1 : state_machine->SendConnectionInfo(event.Name(), "Idle");
361 1 : return transit<Idle>();
362 : }
363 :
364 : //event on server only
365 168 : sc::result react(const EvXmppOpen &event) {
366 168 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
367 168 : if (event.session != state_machine->session()) {
368 0 : return discard_event();
369 : }
370 168 : state_machine->AssignSession();
371 :
372 168 : XmppConnection *connection = state_machine->connection();
373 168 : if (connection->IsDeleted()) {
374 0 : state_machine->ResetSession();
375 0 : return discard_event();
376 : }
377 :
378 168 : XmppSession *session = state_machine->session();
379 168 : if (!state_machine->IsAuthEnabled())
380 86 : state_machine->ResurrectOldConnection(connection, session);
381 :
382 : // Get the possibly updated XmppConnection information.
383 168 : connection = state_machine->connection();
384 168 : state_machine->CancelOpenTimer();
385 168 : if (!connection->SendOpenConfirm(session)) {
386 0 : connection->SendClose(session);
387 0 : state_machine->ResetSession();
388 0 : state_machine->SendConnectionInfo("Send Open Confirm Failed",
389 : "Idle");
390 0 : return transit<Idle>();
391 : } else {
392 168 : XmppConnectionInfo info;
393 168 : info.set_identifier(event.msg->from);
394 168 : if (state_machine->IsAuthEnabled()) {
395 82 : state_machine->SendConnectionInfo(&info, event.Name(),
396 : "Open Confirm");
397 82 : return transit<OpenConfirm>();
398 : } else {
399 86 : connection->StartKeepAliveTimer();
400 86 : state_machine->SendConnectionInfo(&info, event.Name(),
401 : "Established");
402 86 : return transit<XmppStreamEstablished>();
403 : }
404 168 : }
405 : }
406 :
407 4 : sc::result react(const EvStop &event) {
408 4 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
409 4 : XmppConnection *connection = state_machine->connection();
410 4 : state_machine->CancelOpenTimer();
411 4 : state_machine->CancelConnectTimer();
412 4 : connection->StopKeepAliveTimer();
413 :
414 4 : if (state_machine->IsActiveChannel() ) {
415 1 : state_machine->set_session(NULL);
416 1 : state_machine->SendConnectionInfo(event.Name(), "Active");
417 1 : return transit<Active>();
418 : } else {
419 3 : state_machine->ResetSession();
420 3 : state_machine->SendConnectionInfo(event.Name(), "Idle");
421 3 : return transit<Idle>();
422 : }
423 : }
424 :
425 0 : sc::result react(const EvAdminDown &event) {
426 0 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
427 0 : XmppConnectionInfo info;
428 0 : info.set_close_reason("Administratively down");
429 0 : state_machine->connection()->set_close_reason("Administratively down");
430 0 : state_machine->SendConnectionInfo(&info, event.Name(), "Idle");
431 0 : return transit<Idle>();
432 0 : }
433 :
434 : };
435 :
436 : //State valid only for client side, connection in Active state
437 : struct Connect : public sc::state<Connect, XmppStateMachine> {
438 : typedef mpl::list<
439 : sc::custom_reaction<EvAdminDown>,
440 : sc::custom_reaction<EvConnectTimerExpired>,
441 : sc::custom_reaction<EvTcpConnected>,
442 : sc::custom_reaction<EvTcpConnectFail>,
443 : sc::custom_reaction<EvTcpClose>,
444 : sc::custom_reaction<EvStop>
445 : > reactions;
446 :
447 : static const int kConnectTimeout = 60; // seconds
448 :
449 270 : Connect(my_context ctx) : my_base(ctx) {
450 270 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
451 270 : StartSession(state_machine);
452 270 : state_machine->connect_attempts_inc();
453 270 : state_machine->set_state(CONNECT);
454 270 : state_machine->StartConnectTimer(state_machine->GetConnectTime());
455 270 : XmppSession *session = state_machine->session();
456 270 : if (session != NULL) {
457 270 : XmppConnectionInfo info;
458 270 : info.set_local_port(session->local_port());
459 270 : info.set_remote_port(session->remote_port());
460 270 : state_machine->SendConnectionInfo(&info, "Connect Event");
461 270 : }
462 270 : }
463 540 : ~Connect() {
464 270 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
465 270 : state_machine->CancelConnectTimer();
466 540 : }
467 :
468 1 : sc::result react(const EvConnectTimerExpired &event) {
469 1 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
470 1 : if (state_machine->ConnectTimerCancelled()) {
471 0 : SM_LOG(state_machine,
472 : "Discard EvConnectTimerExpired in (Connect) State");
473 0 : return discard_event();
474 : }
475 1 : CloseSession(state_machine);
476 1 : XmppConnectionInfo info;
477 1 : info.set_close_reason("Connect timer expired");
478 1 : state_machine->connection()->set_close_reason("Connect timer expired");
479 1 : state_machine->SendConnectionInfo(&info, event.Name(), "Active");
480 1 : return transit<Active>();
481 1 : }
482 :
483 188 : sc::result react(const EvTcpConnected &event) {
484 188 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
485 188 : if (event.session != state_machine->session()) {
486 0 : return discard_event();
487 : }
488 188 : XmppConnection *connection = state_machine->connection();
489 188 : state_machine->CancelConnectTimer();
490 188 : XmppSession *session = state_machine->session();
491 188 : XmppConnectionInfo info;
492 188 : info.set_local_port(session->local_port());
493 188 : info.set_remote_port(session->remote_port());
494 188 : if (connection->SendOpen(session)) {
495 188 : state_machine->StartHoldTimer();
496 188 : state_machine->SendConnectionInfo(&info, event.Name(), "OpenSent");
497 188 : return transit<OpenSent>();
498 : } else {
499 0 : SM_LOG(state_machine, "SendOpen failed in (Connect) State");
500 0 : CloseSession(state_machine);
501 0 : info.set_close_reason("SendOpen failed");
502 0 : state_machine->connection()->set_close_reason("Send Open failed");
503 0 : state_machine->SendConnectionInfo(&info, "Send Open failed",
504 : "Active");
505 0 : return transit<Active>();
506 : }
507 188 : }
508 :
509 78 : sc::result react(const EvTcpConnectFail &event) {
510 : // delete session; restart connect timer.
511 78 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
512 78 : if (event.session != state_machine->session()) {
513 0 : return discard_event();
514 : }
515 78 : state_machine->set_session(NULL);
516 78 : state_machine->CancelConnectTimer();
517 78 : state_machine->SendConnectionInfo(event.Name(), "Active");
518 78 : return transit<Active>();
519 : }
520 :
521 1 : sc::result react(const EvTcpClose &event) {
522 1 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
523 1 : if (event.session != state_machine->session()) {
524 0 : return discard_event();
525 : }
526 : // close the tcp sessions.
527 1 : CloseSession(state_machine);
528 1 : state_machine->CancelConnectTimer();
529 1 : state_machine->SendConnectionInfo(event.Name(), "Active");
530 1 : return transit<Active>();
531 : }
532 :
533 1 : sc::result react(const EvStop &event) {
534 1 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
535 1 : CloseSession(state_machine);
536 1 : state_machine->CancelConnectTimer();
537 1 : XmppConnectionInfo info;
538 1 : info.set_close_reason("EvStop received");
539 1 : state_machine->connection()->set_close_reason("EvStop received");
540 1 : state_machine->SendConnectionInfo(&info, event.Name(), "Active");
541 2 : return transit<Active>();
542 1 : }
543 :
544 0 : sc::result react(const EvAdminDown &event) {
545 0 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
546 0 : CloseSession(state_machine);
547 0 : XmppConnectionInfo info;
548 0 : info.set_close_reason("Administratively down");
549 0 : state_machine->connection()->set_close_reason("Administratively down");
550 0 : state_machine->SendConnectionInfo(&info, event.Name(), "Idle");
551 0 : return transit<Idle>();
552 0 : }
553 :
554 : // Create an active connection request.
555 270 : void StartSession(XmppStateMachine *state_machine) {
556 270 : XmppConnection *connection = state_machine->connection();
557 270 : XmppSession *session = connection->CreateSession();
558 270 : state_machine->set_session(session);
559 270 : session->set_observer(boost::bind(&XmppStateMachine::OnSessionEvent,
560 : state_machine, _1, _2));
561 270 : boost::system::error_code err;
562 270 : session->socket()->bind(connection->local_endpoint(), err);
563 270 : if (err) {
564 0 : LOG(WARN, "Bind failure for local address " <<
565 : connection->local_endpoint() << " : " << err.message());
566 0 : assert(false);
567 : }
568 270 : connection->server()->Connect(session, connection->endpoint());
569 270 : }
570 :
571 3 : void CloseSession(XmppStateMachine *state_machine) {
572 3 : state_machine->set_session(NULL);
573 3 : }
574 : };
575 :
576 : // The client reaches OpenSent after sending an immediate OPEN on a active
577 : // connection. Server should not come in this state.
578 : struct OpenSent : public sc::state<OpenSent, XmppStateMachine> {
579 : typedef mpl::list<
580 : sc::custom_reaction<EvAdminDown>,
581 : sc::custom_reaction<EvTcpClose>,
582 : sc::custom_reaction<EvXmppOpen>,
583 : sc::custom_reaction<EvHoldTimerExpired>,
584 : sc::custom_reaction<EvStop>
585 : > reactions;
586 :
587 188 : OpenSent(my_context ctx) : my_base(ctx) {
588 188 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
589 188 : state_machine->set_state(OPENSENT);
590 188 : }
591 376 : ~OpenSent() {
592 188 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
593 188 : state_machine->CancelHoldTimer();
594 376 : }
595 :
596 2 : sc::result react(const EvTcpClose &event) {
597 2 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
598 2 : if (event.session != state_machine->session()) {
599 0 : return discard_event();
600 : }
601 2 : state_machine->CancelHoldTimer();
602 2 : if (event.session) {
603 2 : state_machine->set_session(NULL);
604 2 : state_machine->SendConnectionInfo(event.Name(), "Active");
605 2 : return transit<Active>();
606 : }
607 0 : return discard_event();
608 : }
609 :
610 178 : sc::result react(const EvXmppOpen &event) {
611 178 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
612 178 : XmppConnection *connection = state_machine->connection();
613 178 : if (event.session == state_machine->session()) {
614 178 : state_machine->AssignSession();
615 178 : XmppConnectionInfo info;
616 178 : info.set_identifier(event.msg->from);
617 178 : if (state_machine->IsAuthEnabled()) {
618 86 : state_machine->SendConnectionInfo(&info, event.Name(),
619 : "Open Confirm");
620 86 : return transit<OpenConfirm>();
621 : } else {
622 92 : connection->SendKeepAlive();
623 92 : connection->StartKeepAliveTimer();
624 92 : state_machine->StartHoldTimer();
625 92 : state_machine->SendConnectionInfo(&info, event.Name(),
626 : "Established");
627 92 : return transit<XmppStreamEstablished>();
628 : }
629 178 : }
630 0 : return discard_event();
631 : }
632 :
633 2 : sc::result react(const EvHoldTimerExpired &event) {
634 2 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
635 2 : if (state_machine->HoldTimerCancelled()) {
636 0 : SM_LOG(state_machine,
637 : "Discard EvHoldTimerExpired in (OpenSent) State");
638 0 : return discard_event();
639 : }
640 2 : CloseSession(state_machine);
641 2 : XmppConnectionInfo info;
642 2 : info.set_close_reason("Hold timer expired");
643 2 : state_machine->connection()->set_close_reason("Hold timer expired");
644 2 : state_machine->SendConnectionInfo(&info, event.Name(), "Active");
645 2 : return transit<Active>();
646 2 : }
647 :
648 2 : sc::result react(const EvStop &event) {
649 2 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
650 2 : state_machine->CancelHoldTimer();
651 2 : CloseSession(state_machine);
652 2 : XmppConnectionInfo info;
653 2 : info.set_close_reason("EvStop received");
654 2 : state_machine->connection()->set_close_reason("EvStop received");
655 2 : state_machine->SendConnectionInfo(&info, event.Name(), "Active");
656 4 : return transit<Active>();
657 2 : }
658 :
659 0 : sc::result react(const EvAdminDown &event) {
660 0 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
661 0 : CloseSession(state_machine);
662 0 : XmppConnectionInfo info;
663 0 : info.set_close_reason("Administratively down");
664 0 : state_machine->connection()->set_close_reason("Administratively down");
665 0 : state_machine->SendConnectionInfo(&info, event.Name(), "Idle");
666 0 : return transit<Idle>();
667 0 : }
668 :
669 4 : void CloseSession(XmppStateMachine *state_machine) {
670 4 : state_machine->set_session(NULL);
671 4 : }
672 : };
673 :
674 : struct OpenConfirm : public sc::state<OpenConfirm, XmppStateMachine> {
675 : typedef mpl::list<
676 : sc::custom_reaction<EvAdminDown>,
677 : sc::custom_reaction<EvTcpClose>,
678 : sc::custom_reaction<EvHoldTimerExpired>,
679 : sc::custom_reaction<EvStreamFeatureRequest>, //received by client
680 : sc::custom_reaction<EvStartTls>, //received by server
681 : sc::custom_reaction<EvTlsProceed>, //received by client
682 : sc::custom_reaction<EvTlsHandShakeSuccess>,
683 : sc::custom_reaction<EvTlsHandShakeFailure>,
684 : sc::custom_reaction<EvXmppOpen>, //received by server
685 : sc::custom_reaction<EvStop>
686 : > reactions;
687 :
688 168 : OpenConfirm(my_context ctx) : my_base(ctx) {
689 168 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
690 168 : state_machine->StartHoldTimer();
691 168 : XmppConnectionInfo info;
692 168 : if (!state_machine->IsActiveChannel()) { //server
693 82 : if (state_machine->IsAuthEnabled()) {
694 82 : XmppConnection *connection = state_machine->connection();
695 82 : XmppSession *session = state_machine->session();
696 82 : if (!connection->SendStreamFeatureRequest(session)) {
697 0 : connection->SendClose(session);
698 0 : SM_LOG(state_machine,
699 : "Xmpp Send Stream Feature Request Failed, IDLE");
700 0 : state_machine->ResetSession();
701 0 : info.set_close_reason("Send Stream Feature Request Failed");
702 0 : state_machine->connection()->set_close_reason(
703 : "Send Stream Feature Request Failed");
704 0 : state_machine->SendConnectionInfo(&info,
705 : "Send Stream Feature Request failed", "Idle");
706 : // cannot transition state as this is the constructor
707 : // of new state
708 : }
709 : }
710 : }
711 168 : state_machine->set_state(OPENCONFIRM);
712 168 : state_machine->set_openconfirm_state(OPENCONFIRM_INIT);
713 168 : }
714 :
715 2 : sc::result react(const EvTcpClose &event) {
716 2 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
717 2 : state_machine->CancelHoldTimer();
718 2 : if (state_machine->IsActiveChannel() ) {
719 1 : CloseSession(state_machine);
720 1 : state_machine->SendConnectionInfo(event.Name(), "Active");
721 1 : return transit<Active>();
722 : } else {
723 1 : state_machine->ResetSession();
724 1 : state_machine->SendConnectionInfo(event.Name(), "Idle");
725 1 : return transit<Idle>();
726 : }
727 : }
728 :
729 6 : sc::result react(const EvHoldTimerExpired &event) {
730 6 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
731 6 : if (state_machine->HoldTimerCancelled()) {
732 0 : SM_LOG(state_machine,
733 : "Discard EvHoldTimerExpired in (OpenConfirm) State");
734 0 : return discard_event();
735 : }
736 6 : XmppConnectionInfo info;
737 6 : info.set_close_reason("Hold timer expired");
738 6 : state_machine->connection()->set_close_reason("Hold timer expired");
739 6 : if (state_machine->IsActiveChannel() ) {
740 4 : CloseSession(state_machine);
741 4 : state_machine->SendConnectionInfo(&info, event.Name(), "Active");
742 4 : return transit<Active>();
743 : } else {
744 2 : state_machine->ResetSession();
745 2 : state_machine->SendConnectionInfo(&info, event.Name(), "Idle");
746 2 : return transit<Idle>();
747 : }
748 6 : }
749 :
750 : // received by the client
751 83 : sc::result react(const EvStreamFeatureRequest &event) {
752 83 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
753 83 : XmppConnection *connection = state_machine->connection();
754 83 : XmppSession *session = state_machine->session();
755 : // TODO, we need to have a supported stream feature list
756 : // and compare against the requested stream feature list
757 : // which will enable us to send start of various features
758 83 : if (!connection->SendStartTls(session)) {
759 0 : connection->SendClose(session);
760 0 : state_machine->ResetSession();
761 0 : XmppConnectionInfo info;
762 0 : info.set_close_reason("Send Start Tls Failed");
763 0 : state_machine->SendConnectionInfo(&info, event.Name(), "Active");
764 0 : return transit<Active>();
765 0 : } else {
766 83 : state_machine->StartHoldTimer();
767 83 : state_machine->SendConnectionInfo(event.Name(),
768 : "Sent Start Tls, OpenConfirm Feature Negotiation");
769 83 : state_machine->set_openconfirm_state(
770 : OPENCONFIRM_FEATURE_NEGOTIATION);
771 83 : return discard_event();
772 : }
773 : }
774 :
775 : // received by client
776 81 : sc::result react(const EvTlsProceed &event) {
777 81 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
778 81 : XmppConnection *connection = state_machine->connection();
779 81 : XmppSession *session = state_machine->session();
780 81 : state_machine->StartHoldTimer();
781 81 : XmppConnectionInfo info;
782 81 : info.set_identifier(connection->GetTo());
783 : // Trigger Ssl Handshake on client side
784 81 : session->TriggerSslHandShake(state_machine->HandShakeCbHandler());
785 81 : state_machine->SendConnectionInfo( &info, event.Name(),
786 : "Trigger Client Ssl Handshake");
787 162 : return discard_event();
788 81 : }
789 :
790 : //received by server
791 78 : sc::result react(const EvStartTls &event) {
792 78 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
793 78 : if (event.session != state_machine->session()) {
794 0 : return discard_event();
795 : }
796 78 : XmppConnection *connection = state_machine->connection();
797 78 : XmppSession *session = state_machine->session();
798 78 : XmppConnectionInfo info;
799 78 : info.set_identifier(connection->GetTo());
800 78 : if (!connection->SendProceedTls(session)) {
801 0 : connection->SendClose(session);
802 0 : state_machine->ResetSession();
803 0 : info.set_close_reason("Send Proceed Tls Failed");
804 0 : state_machine->SendConnectionInfo(&info, event.Name(), "Idle");
805 0 : return transit<Idle>();
806 : } else {
807 78 : state_machine->StartHoldTimer();
808 : // Trigger Ssl Handshake on server side
809 78 : session->TriggerSslHandShake(state_machine->HandShakeCbHandler());
810 78 : state_machine->SendConnectionInfo(&info, event.Name(),
811 : "Trigger Server Ssl Handshake");
812 78 : return discard_event();
813 : }
814 78 : }
815 :
816 1 : sc::result react(const EvStop &event) {
817 1 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
818 1 : state_machine->CancelHoldTimer();
819 1 : if (state_machine->IsActiveChannel() ) {
820 1 : CloseSession(state_machine);
821 1 : state_machine->SendConnectionInfo(event.Name(), "Active");
822 1 : return transit<Active>();
823 : } else {
824 0 : state_machine->ResetSession();
825 0 : state_machine->SendConnectionInfo(event.Name(), "Idle");
826 0 : return transit<Idle>();
827 : }
828 : }
829 :
830 152 : sc::result react(const EvTlsHandShakeSuccess &event) {
831 152 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
832 152 : if (event.session != state_machine->session()) {
833 0 : return discard_event();
834 : }
835 152 : XmppConnection *connection = state_machine->connection();
836 152 : XmppSession *session = state_machine->session();
837 152 : session->AsyncReadStart();
838 152 : state_machine->set_openconfirm_state(OPENCONFIRM_FEATURE_SUCCESS);
839 152 : if (state_machine->IsActiveChannel()) { //client
840 76 : if (!connection->SendOpen(session)) {
841 0 : connection->SendClose(session);
842 0 : state_machine->ResetSession();
843 0 : XmppConnectionInfo info;
844 0 : info.set_close_reason("Open send failed in OpenConfirm State");
845 0 : state_machine->SendConnectionInfo(event.Name(), "Active");
846 0 : return transit<Active>();
847 0 : }
848 : }
849 : // both server and client
850 152 : state_machine->StartHoldTimer();
851 152 : state_machine->SendConnectionInfo(event.Name(),
852 : "OpenConfirm Feature Negotiation Success");
853 152 : return discard_event();
854 : }
855 :
856 2 : sc::result react(const EvTlsHandShakeFailure &event) {
857 2 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
858 2 : if (event.session != state_machine->session()) {
859 0 : return discard_event();
860 : }
861 : // Do not send stream close as error occured at TLS layer
862 2 : state_machine->ResetSession();
863 2 : if (state_machine->IsActiveChannel()) { // client
864 1 : state_machine->SendConnectionInfo(event.Name(), "Active");
865 1 : return transit<Active>();
866 : } else {
867 1 : state_machine->SendConnectionInfo(event.Name(), "Idle");
868 1 : return transit<Idle>();
869 : }
870 : }
871 :
872 : //event on server and client
873 147 : sc::result react(const EvXmppOpen &event) {
874 147 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
875 147 : if (event.session != state_machine->session()) {
876 0 : return discard_event();
877 : }
878 147 : XmppConnectionInfo info;
879 147 : info.set_identifier(event.msg->from);
880 147 : XmppConnection *connection = state_machine->connection();
881 147 : XmppSession *session = state_machine->session();
882 147 : if (connection->IsActiveChannel()) { //client
883 74 : connection->SendKeepAlive();
884 74 : connection->StartKeepAliveTimer();
885 74 : state_machine->StartHoldTimer();
886 74 : state_machine->SendConnectionInfo(&info, event.Name(),
887 : "Established");
888 74 : return transit<XmppStreamEstablished>();
889 : } else { //server
890 73 : if (!connection->SendOpenConfirm(session)) {
891 0 : connection->SendClose(session);
892 0 : SM_LOG(state_machine,
893 : "Xmpp Send Open Confirm Failed, IDLE");
894 0 : state_machine->ResetSession();
895 0 : info.set_close_reason("Send Open Confirm Failed");
896 0 : state_machine->connection()->set_close_reason(
897 : "Send Open Confirm Failed");
898 0 : state_machine->SendConnectionInfo(&info, event.Name(), "Idle");
899 0 : return transit<Idle>();
900 : } else {
901 73 : if (state_machine->IsAuthEnabled())
902 73 : state_machine->ResurrectOldConnection(connection, session);
903 73 : connection = state_machine->connection();
904 73 : connection->StartKeepAliveTimer();
905 73 : state_machine->StartHoldTimer();
906 73 : state_machine->SendConnectionInfo(&info, event.Name(),
907 : "Established");
908 73 : return transit<XmppStreamEstablished>();
909 : }
910 : }
911 147 : }
912 :
913 0 : sc::result react(const EvAdminDown &event) {
914 0 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
915 0 : CloseSession(state_machine);
916 0 : XmppConnectionInfo info;
917 0 : info.set_close_reason("Administratively down");
918 0 : state_machine->connection()->set_close_reason("Administratively down");
919 0 : state_machine->SendConnectionInfo(&info, event.Name(), "Idle");
920 0 : return transit<Idle>();
921 0 : }
922 :
923 6 : void CloseSession(XmppStateMachine *state_machine) {
924 6 : XmppConnection *connection = state_machine->connection();
925 6 : if (connection != NULL) connection->StopKeepAliveTimer();
926 6 : state_machine->set_session(NULL);
927 6 : }
928 : };
929 :
930 : struct XmppStreamEstablished :
931 : public sc::state<XmppStreamEstablished, XmppStateMachine> {
932 : typedef mpl::list<
933 : sc::custom_reaction<EvAdminDown>,
934 : sc::custom_reaction<EvTcpClose>,
935 : sc::custom_reaction<EvXmppKeepalive>,
936 : sc::custom_reaction<EvXmppMessageStanza>,
937 : sc::custom_reaction<EvXmppIqStanza>,
938 : sc::custom_reaction<EvHoldTimerExpired>,
939 : sc::custom_reaction<EvStop>
940 : > reactions;
941 :
942 325 : XmppStreamEstablished(my_context ctx) : my_base(ctx) {
943 325 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
944 325 : XmppConnection *connection = state_machine->connection();
945 325 : state_machine->StartHoldTimer();
946 325 : state_machine->set_state(ESTABLISHED);
947 325 : connection->ChannelMux()->HandleStateEvent(xmsm::ESTABLISHED);
948 325 : }
949 650 : ~XmppStreamEstablished() {
950 325 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
951 325 : state_machine->CancelHoldTimer();
952 650 : }
953 :
954 141 : sc::result react(const EvTcpClose &event) {
955 141 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
956 141 : if (event.session != state_machine->session()) {
957 0 : return discard_event();
958 : }
959 141 : state_machine->ResetSession();
960 141 : if (state_machine->IsActiveChannel()) {
961 110 : state_machine->SendConnectionInfo(event.Name(), "Active");
962 110 : return transit<Active>();
963 : } else {
964 31 : state_machine->SendConnectionInfo(event.Name(), "Idle");
965 31 : return transit<Idle>();
966 : }
967 : }
968 :
969 189665 : sc::result react(const EvXmppKeepalive &event) {
970 189665 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
971 189665 : if (event.session != state_machine->session()) {
972 0 : return discard_event();
973 : }
974 189665 : state_machine->keepalive_count_inc();
975 189665 : if (state_machine->get_keepalive_count() == 3) {
976 84 : state_machine->connect_attempts_clear();
977 : }
978 189665 : state_machine->StartHoldTimer();
979 189665 : return discard_event();
980 : }
981 :
982 120084 : sc::result react(const EvXmppMessageStanza &event) {
983 120084 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
984 120084 : if (event.session != state_machine->session()) {
985 0 : return discard_event();
986 : }
987 120084 : state_machine->StartHoldTimer();
988 240168 : state_machine->connection()->ProcessXmppChatMessage(
989 120084 : static_cast<const XmppStanza::XmppChatMessage *>(event.msg.get()));
990 120084 : return discard_event();
991 : }
992 :
993 1325 : sc::result react(const EvXmppIqStanza &event) {
994 1325 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
995 1325 : if (event.session != state_machine->session()) {
996 0 : return discard_event();
997 : }
998 1325 : state_machine->StartHoldTimer();
999 2650 : state_machine->connection()->ProcessXmppIqMessage(
1000 1325 : static_cast<const XmppStanza::XmppMessage *>(event.msg.get()));
1001 1325 : return discard_event();
1002 : }
1003 :
1004 5 : sc::result react(const EvHoldTimerExpired &event) {
1005 5 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
1006 5 : if (state_machine->HoldTimerCancelled()) {
1007 0 : SM_LOG(state_machine,
1008 : "Discard EvHoldTimerExpired in (Established) State");
1009 0 : return discard_event();
1010 : }
1011 5 : XMPP_NOTICE(XmppStateMachineNotice,
1012 : state_machine->connection()->ToUVEKey(),
1013 : XMPP_PEER_DIR_NA,
1014 : state_machine->ChannelType(),
1015 : "EvHoldTimerExpired in (Established) State. Transit to IDLE");
1016 5 : state_machine->SendConnectionInfo(event.Name());
1017 5 : state_machine->AssertOnHoldTimeout();
1018 5 : state_machine->ResetSession();
1019 5 : if (state_machine->IsActiveChannel()) {
1020 4 : return transit<Active>();
1021 : } else {
1022 1 : return transit<Idle>();
1023 : }
1024 : }
1025 :
1026 122 : sc::result react(const EvStop &event) {
1027 122 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
1028 122 : state_machine->SendConnectionInfo(event.Name());
1029 122 : state_machine->ResetSession();
1030 122 : if (state_machine->IsActiveChannel()) {
1031 0 : return transit<Active>();
1032 : } else {
1033 122 : return transit<Idle>();
1034 : }
1035 : }
1036 :
1037 32 : sc::result react(const EvAdminDown &event) {
1038 32 : XmppStateMachine *state_machine = &context<XmppStateMachine>();
1039 32 : state_machine->ResetSession();
1040 32 : XmppConnectionInfo info;
1041 32 : info.set_close_reason("Administratively down");
1042 32 : state_machine->connection()->set_close_reason("Administratively down");
1043 32 : state_machine->SendConnectionInfo(&info, event.Name(), "Idle");
1044 64 : return transit<Idle>();
1045 32 : }
1046 : };
1047 :
1048 : } // namespace xmsm
1049 :
1050 5 : void XmppStateMachine::AssertOnHoldTimeout() {
1051 : static bool init_ = false;
1052 : static bool assert_ = false;
1053 :
1054 5 : if (!init_) {
1055 3 : char *str = getenv("XMPP_ASSERT_ON_HOLD_TIMEOUT");
1056 3 : if (str && strtoul(str, NULL, 0) != 0) assert_ = true;
1057 3 : init_ = true;
1058 : }
1059 :
1060 5 : if (!assert_) return;
1061 :
1062 0 : if (connection()) {
1063 0 : connection()->LogMsg("HOLD TIMER EXPIRED: ");
1064 : } else {
1065 0 : LOG4CPLUS_DEBUG(log4cplus::Logger::getRoot(), "HOLD TIMER EXPIRED: ");
1066 : }
1067 :
1068 0 : assert(!assert_);
1069 : }
1070 :
1071 310 : void XmppStateMachine::ResetSession() {
1072 310 : XmppConnection *connection = this->connection();
1073 310 : set_session(NULL);
1074 310 : CancelHoldTimer();
1075 :
1076 310 : if (!connection)
1077 0 : return;
1078 :
1079 : // Stop keepalives, transition to IDLE and notify registered entities.
1080 310 : connection->StopKeepAliveTimer();
1081 310 : connection->ChannelMux()->HandleStateEvent(xmsm::IDLE);
1082 310 : if (IsActiveChannel())
1083 147 : return;
1084 :
1085 : // Retain the connection if graceful restart is supported.
1086 163 : XmppServer *server = dynamic_cast<XmppServer *>(connection->server());
1087 163 : if (!server->IsPeerCloseGraceful())
1088 147 : connection->ManagedDelete();
1089 : }
1090 :
1091 311 : XmppStateMachine::XmppStateMachine(XmppConnection *connection, bool active,
1092 311 : bool auth_enabled, int config_hold_time)
1093 311 : : work_queue_(TaskScheduler::GetInstance()->GetTaskId("xmpp::StateMachine"),
1094 : connection->GetTaskInstance(),
1095 : boost::bind(&XmppStateMachine::DequeueEvent, this, _1)),
1096 311 : connection_(connection),
1097 311 : session_(NULL),
1098 311 : server_(connection->server()),
1099 311 : connect_timer_(
1100 311 : TimerManager::CreateTimer(*server_->event_manager()->io_service(),
1101 : "Connect timer",
1102 : TaskScheduler::GetInstance()->GetTaskId("xmpp::StateMachine"),
1103 : connection->GetTaskInstance())),
1104 311 : open_timer_(
1105 311 : TimerManager::CreateTimer(*server_->event_manager()->io_service(),
1106 : "Open timer",
1107 : TaskScheduler::GetInstance()->GetTaskId("xmpp::StateMachine"),
1108 : connection->GetTaskInstance())),
1109 311 : hold_timer_(
1110 311 : TimerManager::CreateTimer(*server_->event_manager()->io_service(),
1111 : "Hold timer",
1112 : TaskScheduler::GetInstance()->GetTaskId("xmpp::StateMachine"),
1113 : connection->GetTaskInstance())),
1114 311 : config_hold_time_(config_hold_time),
1115 311 : hold_time_(GetConfiguredHoldTime()),
1116 311 : attempts_(0),
1117 311 : keepalive_count_(0),
1118 311 : deleted_(false),
1119 311 : in_dequeue_(false),
1120 311 : is_active_(active),
1121 311 : auth_enabled_(auth_enabled),
1122 311 : state_(xmsm::IDLE),
1123 311 : last_state_(xmsm::IDLE),
1124 622 : openconfirm_state_(xmsm::OPENCONFIRM_INIT) {
1125 : handshake_cb_ = boost::bind(
1126 311 : &XmppConnection::ProcessSslHandShakeResponse, connection, _1, _2);
1127 311 : }
1128 :
1129 406 : XmppStateMachine::~XmppStateMachine() {
1130 311 : assert(!deleted_);
1131 311 : deleted_ = true;
1132 311 : work_queue_.Shutdown();
1133 311 : set_session(NULL);
1134 :
1135 : // Explicitly call the state destructor before the state machine itself.
1136 : // This is needed because some of the destructors access the state machine
1137 : // context.
1138 311 : terminate();
1139 :
1140 : // Delete timer after state machine is terminated so that there is no
1141 : // possible reference to the timers being deleted any more
1142 311 : TimerManager::DeleteTimer(connect_timer_);
1143 311 : TimerManager::DeleteTimer(open_timer_);
1144 311 : TimerManager::DeleteTimer(hold_timer_);
1145 406 : }
1146 :
1147 328 : void XmppStateMachine::Initialize() {
1148 328 : initiate();
1149 328 : Enqueue(xmsm::EvStart());
1150 328 : }
1151 :
1152 : // Note this api does not enqueue the deletion of TCP session
1153 311 : void XmppStateMachine::clear_session() {
1154 311 : if (session_ != NULL) {
1155 120 : session_->set_observer(NULL);
1156 120 : session_->ClearConnection();
1157 120 : session_->Close();
1158 120 : connection_->clear_session();
1159 120 : session_ = NULL;
1160 : }
1161 311 : }
1162 :
1163 321 : void XmppStateMachine::DeleteSession(XmppSession *session) {
1164 : // If there is a session assigned to this state machine, reset the observer
1165 : // so that tcp does not have a reference to 'this' which is going away
1166 321 : if (session != NULL) {
1167 321 : session->set_observer(NULL);
1168 321 : session->ClearConnection();
1169 321 : session->Close();
1170 321 : Enqueue(xmsm::EvTcpDeleteSession(session));
1171 : }
1172 321 : }
1173 :
1174 1156 : void XmppStateMachine::set_session(TcpSession *session) {
1175 1156 : if (session_ != NULL) {
1176 321 : connection_->clear_session();
1177 321 : DeleteSession(session_);
1178 : }
1179 1156 : session_ = static_cast<XmppSession *>(session);
1180 1156 : }
1181 :
1182 0 : void XmppStateMachine::TimerErrorHandler(std::string name, std::string error) {
1183 0 : }
1184 :
1185 165 : void XmppStateMachine::StartConnectTimer(int seconds) {
1186 165 : CancelConnectTimer();
1187 :
1188 : // Add up to +/- kJitter percentage to reduce connection collisions.
1189 165 : int ms = ((seconds)? seconds * 1000 : 50);
1190 165 : ms = (ms * (100 - kJitter)) / 100;
1191 165 : ms += (ms * (rand() % (kJitter * 2))) / 100;
1192 165 : connect_timer_->Start(ms,
1193 : boost::bind(&XmppStateMachine::ConnectTimerExpired, this),
1194 : boost::bind(&XmppStateMachine::TimerErrorHandler, this, _1, _2));
1195 165 : }
1196 :
1197 1260 : void XmppStateMachine::CancelConnectTimer() {
1198 1260 : connect_timer_->Cancel();
1199 1260 : }
1200 :
1201 14 : void XmppStateMachine::StartOpenTimer(int seconds) {
1202 14 : CancelOpenTimer();
1203 14 : open_timer_->Start(seconds * 1000,
1204 : boost::bind(&XmppStateMachine::OpenTimerExpired, this),
1205 : boost::bind(&XmppStateMachine::TimerErrorHandler, this, _1, _2));
1206 14 : }
1207 :
1208 740 : void XmppStateMachine::CancelOpenTimer() {
1209 740 : open_timer_->Cancel();
1210 740 : }
1211 :
1212 311 : int XmppStateMachine::GetConfiguredHoldTime() const {
1213 : static std::atomic<bool> env_checked = std::atomic<bool>();
1214 : static std::atomic<int> env_hold_time = std::atomic<int>();
1215 :
1216 : // For testing only - configure through environment variable.
1217 311 : if (!env_checked) {
1218 10 : char *keepalive_time_str = getenv("XMPP_KEEPALIVE_SECONDS");
1219 10 : if (keepalive_time_str) {
1220 0 : env_hold_time = strtoul(keepalive_time_str, NULL, 0) * 3;
1221 0 : env_checked = true;
1222 0 : return env_hold_time;
1223 : } else {
1224 10 : env_checked = true;
1225 : }
1226 301 : } else if (env_hold_time) {
1227 0 : return env_hold_time;
1228 301 : } else if (config_hold_time_) {
1229 301 : return config_hold_time_;
1230 : }
1231 :
1232 : // Use hard coded default.
1233 10 : return kHoldTime;
1234 : }
1235 :
1236 312388 : void XmppStateMachine::StartHoldTimer() {
1237 312388 : CancelHoldTimer();
1238 :
1239 312388 : if (hold_time_ <= 0)
1240 0 : return;
1241 :
1242 312388 : hold_timer_->Start(hold_time_ * 1000,
1243 : boost::bind(&XmppStateMachine::HoldTimerExpired, this),
1244 : boost::bind(&XmppStateMachine::TimerErrorHandler, this, _1, _2));
1245 : }
1246 :
1247 313218 : void XmppStateMachine::CancelHoldTimer() {
1248 313218 : hold_timer_->Cancel();
1249 313218 : }
1250 :
1251 344 : void XmppStateMachine::OnStart(const xmsm::EvStart &event) {
1252 344 : }
1253 :
1254 271 : bool XmppStateMachine::ConnectTimerExpired() {
1255 271 : XMPP_UTDEBUG(XmppStateMachineTimerExpire,
1256 : connection() ? connection()->ToUVEKey() : "",
1257 : XMPP_PEER_DIR_NA, this->ChannelType(), "Connect", StateName());
1258 271 : Enqueue(xmsm::EvConnectTimerExpired());
1259 271 : return false;
1260 : }
1261 :
1262 1 : bool XmppStateMachine::OpenTimerExpired() {
1263 1 : XMPP_NOTICE(XmppEventLog, connection()->ToUVEKey(), XMPP_PEER_DIR_NA,
1264 : this->ChannelType(),
1265 : "Event: OpenTimer Expired ",
1266 : connection()->endpoint().address().to_string(),
1267 : connection()->GetTo());
1268 1 : Enqueue(xmsm::EvOpenTimerExpired());
1269 1 : return false;
1270 : }
1271 :
1272 :
1273 13 : bool XmppStateMachine::HoldTimerExpired() {
1274 13 : boost::system::error_code error;
1275 :
1276 : // Reset hold timer if there is data already present in the socket.
1277 26 : if (session() && session()->socket() &&
1278 13 : session()->socket()->available(error) > 0) {
1279 0 : return true;
1280 : }
1281 13 : XMPP_NOTICE(XmppEventLog, connection()->ToUVEKey(), XMPP_PEER_DIR_NA,
1282 : this->ChannelType(), "Event: HoldTimer Expired ",
1283 : connection()->endpoint().address().to_string(),
1284 : connection()->GetTo());
1285 13 : Enqueue(xmsm::EvHoldTimerExpired());
1286 13 : return false;
1287 : }
1288 :
1289 578 : void XmppStateMachine::OnSessionEvent(
1290 : TcpSession *session, TcpSession::Event event) {
1291 578 : switch (event) {
1292 149 : case TcpSession::ACCEPT:
1293 149 : break;
1294 188 : case TcpSession::CONNECT_COMPLETE:
1295 188 : XMPP_NOTICE(XmppEventLog, session->ToUVEKey(), XMPP_PEER_DIR_IN,
1296 : this->ChannelType(), "Event: Tcp Connected ",
1297 : connection()->endpoint().address().to_string(),
1298 : connection()->GetTo());
1299 188 : Enqueue(xmsm::EvTcpConnected(static_cast<XmppSession *>(session)));
1300 188 : break;
1301 78 : case TcpSession::CONNECT_FAILED:
1302 78 : XMPP_NOTICE(XmppEventLog, session->ToUVEKey(), XMPP_PEER_DIR_IN,
1303 : this->ChannelType(), "Event: Tcp Connect Fail ",
1304 : connection()->endpoint().address().to_string(),
1305 : connection()->GetTo());
1306 78 : Enqueue(xmsm::EvTcpConnectFail(static_cast<XmppSession *>(session)));
1307 78 : connection_->inc_connect_error();
1308 78 : break;
1309 163 : case TcpSession::CLOSE:
1310 163 : XMPP_NOTICE(XmppEventLog, session->ToUVEKey(), XMPP_PEER_DIR_IN,
1311 : this->ChannelType(), "Event: Tcp Connection Closed ",
1312 : connection()->endpoint().address().to_string(),
1313 : connection()->GetTo());
1314 163 : Enqueue(xmsm::EvTcpClose(static_cast<XmppSession *>(session)));
1315 163 : connection_->inc_session_close();
1316 163 : break;
1317 0 : default:
1318 0 : XMPP_WARNING(XmppUnknownEvent, session->ToUVEKey(), XMPP_PEER_DIR_IN,
1319 : this->ChannelType(), event);
1320 0 : break;
1321 : }
1322 578 : }
1323 :
1324 150 : bool XmppStateMachine::PassiveOpen(XmppSession *session) {
1325 150 : string state = "PassiveOpen in state: " + StateName();
1326 150 : XMPP_NOTICE(XmppEventLog, session->ToUVEKey(), XMPP_PEER_DIR_IN,
1327 : this->ChannelType(), state,
1328 : session->Connection()->endpoint().address().to_string(), "");
1329 300 : return Enqueue(xmsm::EvTcpPassiveOpen(session));
1330 150 : }
1331 :
1332 : // Process XmppStream header message received over a session. Close the stream
1333 : // if an old session is still present and undergoing graceful restart.
1334 : //
1335 : // Return true if msg is enqueued for further processing, false otherwise.
1336 494 : void XmppStateMachine::ProcessStreamHeaderMessage(XmppSession *session,
1337 : const XmppStanza::XmppMessage *msg) {
1338 : XmppConnectionManager *connection_manager =
1339 494 : dynamic_cast<XmppConnectionManager *>(connection_->server());
1340 494 : std::scoped_lock lock(connection_manager->mutex());
1341 :
1342 : // Update "To" information which can be used to map an older session
1343 494 : session->Connection()->SetTo(msg->from);
1344 :
1345 494 : XmppServer *xmpp_server = dynamic_cast<XmppServer *>(server_);
1346 494 : XmppConnectionEndpoint *endpoint = NULL;
1347 :
1348 : // Look for an endpoint which may already exist
1349 494 : if (xmpp_server) {
1350 : endpoint =
1351 242 : xmpp_server->FindConnectionEndpoint(connection_->ToString());
1352 :
1353 484 : if (!xmpp_server->subcluster_name().empty() &&
1354 242 : xmpp_server->subcluster_name() != msg->xmlns) {
1355 0 : string reason = "Subcluster mismatch: Agent subcluster " +
1356 0 : msg->xmlns + ", Control subcluster " +
1357 0 : xmpp_server->subcluster_name();
1358 0 : XMPP_WARNING(XmppDeleteConnection, session->ToUVEKey(),
1359 : XMPP_PEER_DIR_IN,
1360 : "Drop new xmpp connection " + session->ToString() +
1361 : " as " + reason);
1362 0 : session->Connection()->set_close_reason(reason);
1363 0 : ProcessEvent(xmsm::EvTcpClose(session));
1364 0 : delete msg;
1365 0 : return;
1366 0 : }
1367 : }
1368 :
1369 : // If there is no connection already associated with the end-point,
1370 : // process the incoming open message and move forward with the session
1371 : // establishment.
1372 519 : if (!endpoint || !endpoint->connection() ||
1373 25 : connection_ == endpoint->connection()) {
1374 470 : ProcessEvent(xmsm::EvXmppOpen(session, msg));
1375 470 : return;
1376 : }
1377 :
1378 : // Check if the IP addresses match.
1379 : boost::asio::ip::address addr =
1380 24 : endpoint->connection()->endpoint().address();
1381 24 : if (connection_->endpoint().address() != addr) {
1382 0 : XMPP_WARNING(XmppDeleteConnection, session->ToUVEKey(),
1383 : XMPP_PEER_DIR_IN,
1384 : "Drop new xmpp connection " + session->ToString() +
1385 : " as another connection with same name " + msg->from +
1386 : " but with different IP address " + addr.to_string() +
1387 : " already exists");
1388 0 : ProcessEvent(xmsm::EvTcpClose(session));
1389 0 : delete msg;
1390 0 : return;
1391 : }
1392 :
1393 24 : XmppChannelMux *channel = endpoint->connection()->ChannelMux();
1394 :
1395 : // If GR Helper mode is not enabled, ignore the new connection request.
1396 : // Existing connection, if down will get cleaned up eventually. If it is
1397 : // up, it shall remain intact.
1398 24 : if (!xmpp_server->IsPeerCloseGraceful()) {
1399 0 : XMPP_NOTICE(XmppDeleteConnection, session->ToUVEKey(),
1400 : XMPP_PEER_DIR_IN,
1401 : "Drop new xmpp connection " + session->ToString() +
1402 : " as another connection is alreready present");
1403 0 : ProcessEvent(xmsm::EvTcpClose(session));
1404 0 : delete msg;
1405 0 : return;
1406 : }
1407 :
1408 24 : XmppStateMachine *old_sm = endpoint->connection()->state_machine();
1409 :
1410 : // Bring down old session if connection is already up and ready. This is
1411 : // the scenario in which old session's TCP did not learn the session down
1412 : // event, possibly due to compute cold reboot. In that case, trigger
1413 : // closure (and possibly GR) process for the old session.
1414 24 : if (channel->GetPeerState() == xmps::READY) {
1415 0 : XMPP_NOTICE(XmppDeleteConnection, old_sm->session()->ToUVEKey(),
1416 : XMPP_PEER_DIR_IN, "Delete old xmpp connection " +
1417 : old_sm->session()->ToString() +
1418 : " as a new connection as been initiated (GR Helper is active)");
1419 0 : old_sm->Enqueue(xmsm::EvTcpClose(old_sm->session()));
1420 :
1421 : // Drop the new session until old one is deleted or marked stale.
1422 0 : ProcessEvent(xmsm::EvTcpClose(session));
1423 0 : delete msg;
1424 0 : return;
1425 : }
1426 :
1427 : // If previous closure is still in progress, drop the new connection.
1428 24 : if (channel->ReceiverCount()) {
1429 0 : XMPP_NOTICE(XmppDeleteConnection, session->ToUVEKey(),
1430 : XMPP_PEER_DIR_IN,
1431 : "Drop new xmpp connection " + session->ToString() +
1432 : " as current connection is still under deletion");
1433 0 : ProcessEvent(xmsm::EvTcpClose(session));
1434 0 : delete msg;
1435 0 : return;
1436 : }
1437 :
1438 : // Now we reach for the classic GR case, in which existing session stale
1439 : // process is complete, (peer is in GR/LLGR timer wait state).
1440 : // In this case, we should process the open message, and later switch to
1441 : // the new connection and state machine, retaining the old XmppPeer,
1442 : // BgpXmppChannel, routes, etc.
1443 24 : ProcessEvent(xmsm::EvXmppOpen(session, msg));
1444 494 : }
1445 :
1446 312020 : void XmppStateMachine::OnMessage(XmppSession *session,
1447 : const XmppStanza::XmppMessage *msg) {
1448 312020 : if (!Enqueue(xmsm::EvXmppMessage(session, msg)))
1449 0 : delete msg;
1450 312020 : }
1451 :
1452 312019 : void XmppStateMachine::ProcessMessage(XmppSession *session,
1453 : const XmppStanza::XmppMessage *msg) {
1454 : // Bail if session is already reset and disassociated from the connection.
1455 312019 : if (!session->Connection()) {
1456 0 : delete msg;
1457 0 : return;
1458 : }
1459 :
1460 312020 : const XmppStanza::XmppStreamMessage *stream_msg =
1461 : static_cast<const XmppStanza::XmppStreamMessage *>(msg);
1462 :
1463 312020 : switch (msg->type) {
1464 736 : case XmppStanza::STREAM_HEADER:
1465 736 : if (stream_msg->strmtype ==
1466 : XmppStanza::XmppStreamMessage::FEATURE_TLS) {
1467 :
1468 242 : switch (stream_msg->strmtlstype) {
1469 83 : case (XmppStanza::XmppStreamMessage::TLS_FEATURE_REQUEST):
1470 83 : ProcessEvent(xmsm::EvStreamFeatureRequest(session,
1471 : msg));
1472 83 : break;
1473 78 : case (XmppStanza::XmppStreamMessage::TLS_START):
1474 78 : ProcessEvent(xmsm::EvStartTls(session, msg));
1475 78 : break;
1476 81 : case (XmppStanza::XmppStreamMessage::TLS_PROCEED):
1477 81 : ProcessEvent(xmsm::EvTlsProceed(session, msg));
1478 81 : break;
1479 0 : default:
1480 0 : break;
1481 : }
1482 :
1483 494 : } else if (stream_msg->strmtype ==
1484 0 : XmppStanza::XmppStreamMessage::INIT_STREAM_HEADER ||
1485 0 : stream_msg->strmtype ==
1486 : XmppStanza::XmppStreamMessage::INIT_STREAM_HEADER_RESP)
1487 494 : ProcessStreamHeaderMessage(session, msg);
1488 736 : break;
1489 189875 : case XmppStanza::WHITESPACE_MESSAGE_STANZA:
1490 189875 : ProcessEvent(xmsm::EvXmppKeepalive(session, msg));
1491 189875 : break;
1492 1325 : case XmppStanza::IQ_STANZA:
1493 1325 : ProcessEvent(xmsm::EvXmppIqStanza(session, msg));
1494 1325 : break;
1495 120084 : case XmppStanza::MESSAGE_STANZA:
1496 120084 : ProcessEvent(xmsm::EvXmppMessageStanza(session, msg));
1497 120084 : break;
1498 0 : default:
1499 0 : if (!msg->IsValidType(msg->type)) {
1500 0 : XMPP_NOTICE(XmppStateMachineUnsupportedMessage,
1501 : XMPP_PEER_DIR_IN, session->ToUVEKey(),
1502 : ChannelType(), (int)msg->type);
1503 : }
1504 0 : delete msg;
1505 0 : break;
1506 : }
1507 :
1508 : }
1509 :
1510 223 : void XmppStateMachine::Clear() {
1511 223 : Enqueue(xmsm::EvStop());
1512 223 : }
1513 :
1514 48 : void XmppStateMachine::SetAdminState(bool down) {
1515 48 : assert(IsActiveChannel());
1516 48 : if (down) {
1517 32 : Enqueue(xmsm::EvAdminDown());
1518 : } else {
1519 16 : Enqueue(xmsm::EvStart());
1520 : }
1521 48 : }
1522 :
1523 154 : void XmppStateMachine::OnEvent(SslSession *session,
1524 : xmsm::SslHandShakeResponse resp) {
1525 154 : XmppSession *sess = static_cast<XmppSession *>(session);
1526 154 : switch(resp) {
1527 2 : case xmsm::EvTLSHANDSHAKE_FAILURE:
1528 2 : Enqueue(xmsm::EvTlsHandShakeFailure(sess));
1529 2 : break;
1530 152 : case xmsm::EvTLSHANDSHAKE_SUCCESS:
1531 152 : Enqueue(xmsm::EvTlsHandShakeSuccess(sess));
1532 152 : break;
1533 0 : default:
1534 0 : break;
1535 : }
1536 154 : }
1537 :
1538 2027 : void XmppStateMachine::set_state(xmsm::XmState state) {
1539 2027 : last_state_ = state_;
1540 2027 : state_ = state;
1541 2027 : state_since_ = UTCTimestampUsec();
1542 :
1543 2027 : if (!logUVE()) return;
1544 :
1545 0 : XmppPeerInfoData peer_info;
1546 0 : peer_info.set_name(connection()->ToUVEKey());
1547 0 : PeerStateInfo state_info;
1548 0 : state_info.set_state(StateName());
1549 0 : state_info.set_last_state(LastStateName());
1550 0 : state_info.set_last_state_at(state_since_);
1551 0 : peer_info.set_state_info(state_info);
1552 0 : assert(!peer_info.get_name().empty());
1553 0 : XMPPPeerInfo::Send(peer_info);
1554 0 : }
1555 :
1556 :
1557 403 : void XmppStateMachine::set_openconfirm_state(xmsm::XmOpenConfirmState state) {
1558 403 : openconfirm_state_ = state;
1559 403 : }
1560 :
1561 : static const char *state_names[] = {
1562 : "Idle",
1563 : "Active",
1564 : "Connect",
1565 : "OpenSent",
1566 : "OpenConfirm",
1567 : "Established" };
1568 :
1569 317604 : string XmppStateMachine::StateName() const {
1570 317604 : return state_names[state_];
1571 : }
1572 :
1573 6 : string XmppStateMachine::LastStateName() const {
1574 6 : return state_names[last_state_];
1575 : }
1576 :
1577 0 : string XmppStateMachine::LastStateChangeAt() const {
1578 0 : return integerToString(UTCUsecToPTime(state_since_));
1579 : }
1580 :
1581 576388 : xmsm::XmState XmppStateMachine::StateType() const {
1582 576388 : return state_;
1583 : }
1584 :
1585 322275 : xmsm::XmOpenConfirmState XmppStateMachine::OpenConfirmStateType() const {
1586 322275 : return openconfirm_state_;
1587 : }
1588 :
1589 346 : void XmppStateMachine::AssignSession() {
1590 346 : connection_->set_session(static_cast<XmppSession *>(session_));
1591 346 : }
1592 :
1593 : const int XmppStateMachine::kConnectInterval;
1594 :
1595 633 : int XmppStateMachine::GetConnectTime() const {
1596 633 : int backoff = attempts_ > 6 ? 6 : attempts_;
1597 633 : return std::min(backoff ? 1 << (backoff - 1) : 0, kConnectInterval);
1598 : }
1599 :
1600 318591 : bool XmppStateMachine::IsActiveChannel() {
1601 318591 : return is_active_;
1602 : }
1603 :
1604 315627 : bool XmppStateMachine::logUVE() {
1605 315627 : return connection()->logUVE();
1606 : }
1607 :
1608 316223 : const char *XmppStateMachine::ChannelType() {
1609 316223 : return (IsActiveChannel() ? " Mode Client: " : " Mode Server: " );
1610 : }
1611 :
1612 313919 : bool XmppStateMachine::DequeueEvent(
1613 : boost::intrusive_ptr<const sc::event_base> event) {
1614 : // Process message event and enqueue additional events as necessary.
1615 : const xmsm::EvXmppMessage *ev_xmpp_message =
1616 313919 : dynamic_cast<const xmsm::EvXmppMessage *>(event.get());
1617 313919 : if (ev_xmpp_message) {
1618 312019 : ProcessMessage(ev_xmpp_message->session, ev_xmpp_message->msg);
1619 : } else {
1620 1900 : ProcessEvent(*event);
1621 1901 : event.reset();
1622 : }
1623 313921 : return true;
1624 : }
1625 :
1626 313919 : void XmppStateMachine::ProcessEvent(const sc::event_base &event) {
1627 313919 : const xmsm::EvTcpDeleteSession *deferred_delete =
1628 313919 : dynamic_cast<const xmsm::EvTcpDeleteSession *>(&event);
1629 313919 : XMPP_UTDEBUG(XmppStateMachineDequeueEvent,
1630 : connection() ? connection()->ToUVEKey() : "",
1631 : XMPP_PEER_DIR_IN, ChannelType(), TYPE_NAME(event), StateName(),
1632 : connection() ?
1633 : connection()->endpoint().address().to_string() : "",
1634 : connection() ? connection()->GetTo() : "");
1635 313921 : if (deferred_delete) {
1636 321 : TcpSession *session = deferred_delete->session;
1637 321 : session->server()->DeleteSession(session);
1638 321 : return;
1639 : }
1640 313600 : if (deleted_) {
1641 0 : return;
1642 : }
1643 :
1644 313600 : update_last_event(TYPE_NAME(event));
1645 313600 : in_dequeue_ = true;
1646 313600 : process_event(event);
1647 313600 : in_dequeue_ = false;
1648 : }
1649 :
1650 283 : void XmppStateMachine::unconsumed_event(const sc::event_base &event) {
1651 283 : XMPP_INFO(XmppUnconsumedEvent, connection() ? connection()->ToUVEKey() : "",
1652 : XMPP_PEER_DIR_IN, ChannelType(), TYPE_NAME(event), StateName());
1653 283 : }
1654 :
1655 313600 : void XmppStateMachine::update_last_event(const std::string &event) {
1656 313600 : set_last_event(event);
1657 :
1658 313600 : if (!logUVE()) return;
1659 :
1660 : // Skip iq and message events.
1661 0 : if (event == "xmsm::EvXmppIqStanza" ||
1662 0 : event == "xmsm::EvXmppMessageStanza") {
1663 0 : return;
1664 : }
1665 :
1666 : // Skip keepalive events after we've reached established state.
1667 0 : if (state_ == xmsm::ESTABLISHED && event == "xmsm::EvXmppKeepalive") {
1668 0 : return;
1669 : }
1670 :
1671 0 : XmppPeerInfoData peer_info;
1672 0 : peer_info.set_name(connection()->ToUVEKey());
1673 0 : PeerEventInfo event_info;
1674 0 : event_info.set_last_event(last_event_);
1675 0 : event_info.set_last_event_at(last_event_at_);
1676 0 : peer_info.set_event_info(event_info);
1677 :
1678 0 : assert(!peer_info.get_name().empty());
1679 0 : XMPPPeerInfo::Send(peer_info);
1680 0 : }
1681 :
1682 : //
1683 : // Enqueue an event to xmpp state machine.
1684 : // Return false if the event is not enqueued.
1685 : //
1686 313957 : bool XmppStateMachine::Enqueue(const sc::event_base &event) {
1687 313957 : if (!deleted_) {
1688 313957 : work_queue_.Enqueue(event.intrusive_from_this());
1689 313958 : return true;
1690 : }
1691 :
1692 0 : return false;
1693 : }
1694 :
1695 : // Object trace routines.
1696 1399 : void XmppStateMachine::SendConnectionInfo(const string &event,
1697 : const string &nextstate) {
1698 1399 : XmppConnectionInfo info;
1699 1399 : SendConnectionInfo(&info, event, nextstate);
1700 2798 : return;
1701 1399 : }
1702 :
1703 2703 : void XmppStateMachine::SendConnectionInfo(XmppConnectionInfo *info,
1704 : const string &event, const string &nextstate) {
1705 :
1706 2703 : info->set_ip_address(this->connection()->endpoint().address().to_string());
1707 2703 : info->set_state(StateName());
1708 2703 : info->set_event(event);
1709 2703 : if (!nextstate.empty()) {
1710 2145 : info->set_next_state(nextstate);
1711 : }
1712 2703 : XMPP_CONNECTION_LOG_MSG(*info);
1713 2703 : return;
1714 : }
1715 :
1716 : // Resurrect an old xmpp connection if present (when under GracefulRestart)
1717 : //
1718 : // During Graceful Restart (or otherwise), new connections are rejected in
1719 : // ProcessStreamHeaderMessage() itself until old one's cleanup process is
1720 : // complete and the system is ready to start a new session.
1721 : //
1722 : // Hence in here, when called upon receipt of OpenMessage, we can try to reuse
1723 : // old XmppConnection if present and there by complete any pending GR process
1724 : //
1725 : // We do so by reusing XmppConnection, XmppChannel, etc. from the old connection
1726 : // and only use the XmppSession and XmppStateMachine from the new session
1727 : //
1728 : // New connection is instead associated with the old state machine and session,
1729 : // and their deletion is triggered
1730 159 : void XmppStateMachine::ResurrectOldConnection(XmppConnection *new_connection,
1731 : XmppSession *new_session) {
1732 :
1733 : // Look for an endpoint (which is a persistent data structure) across
1734 : // xmpp session flips
1735 : bool created;
1736 : XmppConnectionEndpoint *connection_endpoint =
1737 : static_cast<XmppServer *>(
1738 159 : new_connection->server())->LocateConnectionEndpoint(
1739 : static_cast<XmppServerConnection *>(new_connection), created);
1740 :
1741 : // If this is a new endpoint, then there is no older connection to manage.
1742 159 : if (created)
1743 143 : return;
1744 :
1745 : // GR Helper must be enabled when we are trying to resurrect connection.
1746 16 : XmppServer *server = dynamic_cast<XmppServer *>(new_connection->server());
1747 16 : assert(server->IsPeerCloseGraceful());
1748 :
1749 16 : XMPP_NOTICE(XmppCreateConnection, new_session->ToUVEKey(),
1750 : XMPP_PEER_DIR_IN,
1751 : "Resurrect xmpp connection " + new_session->ToString());
1752 :
1753 : // Retrieve old XmppConnection and XmppStateMachine (to reuse)
1754 16 : XmppConnection *old_xmpp_connection = connection_endpoint->connection();
1755 16 : assert(old_xmpp_connection);
1756 :
1757 16 : XmppStateMachine *old_state_machine = old_xmpp_connection->state_machine();
1758 16 : assert(old_state_machine);
1759 :
1760 : // Swap Old and New connections and state machines linkages
1761 16 : new_connection->SwapXmppStateMachine(old_xmpp_connection);
1762 16 : this->SwapXmppConnection(old_state_machine);
1763 :
1764 : // Update XmppConnection in the old session.
1765 16 : XmppSession *old_xmpp_session = old_state_machine->session();
1766 16 : if (old_xmpp_session)
1767 0 : old_xmpp_session->SetConnection(new_connection);
1768 16 : new_session->SetConnection(old_xmpp_connection);
1769 :
1770 : // Set new session with the old connection as it would be the current active
1771 : // connection from now on.
1772 16 : old_xmpp_connection->set_session(new_session);
1773 :
1774 : // Trigger deletion of the new connection which now is associated wth the
1775 : // the old_state_machine
1776 16 : new_connection->Shutdown();
1777 : }
1778 :
1779 16 : void XmppStateMachine::SwapXmppConnection(XmppStateMachine *other) {
1780 16 : swap(connection_, other->connection_);
1781 16 : connection_->SwapContents(other->connection_);
1782 16 : }
|