/
xmpp_client.cc
246 lines (207 loc) · 7.11 KB
/
xmpp_client.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "xmpp/xmpp_client.h"
#include <boost/foreach.hpp>
#include <boost/tuple/tuple.hpp>
#include "base/task_annotations.h"
#include "xmpp/xmpp_factory.h"
#include "xmpp/xmpp_log.h"
#include "xmpp/xmpp_session.h"
#include "sandesh/sandesh_types.h"
#include "sandesh/sandesh.h"
#include "sandesh/common/vns_types.h"
#include "sandesh/common/vns_constants.h"
#include "sandesh/xmpp_client_server_sandesh_types.h"
using namespace std;
using namespace boost::asio;
using boost::tie;
class XmppClient::DeleteActor : public LifetimeActor {
public:
DeleteActor(XmppClient *client)
: LifetimeActor(client->lifetime_manager()), client_(client) { }
virtual bool MayDelete() const {
CHECK_CONCURRENCY("bgp::Config");
return (client_->GetSessionQueueSize() == 0);
}
virtual void Shutdown() {
CHECK_CONCURRENCY("bgp::Config");
}
virtual void Destroy() {
CHECK_CONCURRENCY("bgp::Config");
}
private:
XmppClient *client_;
};
XmppClient::XmppClient(EventManager *evm)
: XmppConnectionManager(evm, ssl::context::tlsv1_client, false, false),
config_mgr_(new XmppConfigManager),
lifetime_manager_(new LifetimeManager(
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"))),
deleter_(new DeleteActor(this)),
auth_enabled_(false),
tcp_hold_time_(XmppChannelConfig::kTcpHoldTime) {
}
XmppClient::XmppClient(EventManager *evm, const XmppChannelConfig *config)
: XmppConnectionManager(
evm, ssl::context::tlsv1_client, config->auth_enabled, true),
config_mgr_(new XmppConfigManager),
lifetime_manager_(new LifetimeManager(
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"))),
deleter_(new DeleteActor(this)),
auth_enabled_(config->auth_enabled),
tcp_hold_time_(config->tcp_hold_time) {
if (config->auth_enabled) {
// Get SSL context from base class and update
boost::asio::ssl::context *ctx = context();
boost::system::error_code ec;
//set mode
ctx->set_options(ssl::context::default_workarounds |
ssl::context::no_sslv3 | ssl::context::no_sslv2, ec);
assert(ec.value() == 0);
// Verify server to have a valid certificate
ctx->load_verify_file(config->path_to_server_cert, ec);
assert(ec.value() == 0);
ctx->set_verify_mode(boost::asio::ssl::verify_peer, ec);
assert(ec.value() == 0);
}
}
XmppClient::~XmppClient() {
}
bool XmppClient::Initialize(short port) {
TcpServer::Initialize(port);
return true;
}
LifetimeActor *XmppClient::deleter() {
return deleter_.get();
}
LifetimeManager *XmppClient::lifetime_manager() {
return lifetime_manager_.get();
}
TcpSession *XmppClient::CreateSession() {
typedef boost::asio::detail::socket_option::boolean<
#ifdef __APPLE__
SOL_SOCKET, SO_REUSEPORT> reuse_port_t;
#else
SOL_SOCKET, SO_REUSEADDR> reuse_addr_t;
#endif
TcpSession *session = TcpServer::CreateSession();
Socket *socket = session->socket();
boost::system::error_code err;
socket->open(ip::tcp::v4(), err);
if (err) {
XMPP_WARNING(ClientOpenFail, err.message());
DeleteSession(session);
return NULL;
}
#ifdef __APPLE__
socket->set_option(reuse_port_t(true), err);
#else
socket->set_option(reuse_addr_t(true), err);
#endif
if (err) {
XMPP_WARNING(SetSockOptFail, err.message());
return session;
}
err = session->SetSocketOptions();
if (err) {
DeleteSession(session);
assert(0);
}
XmppSession *xmpps = static_cast<XmppSession *>(session);
err = xmpps->EnableTcpKeepalive(tcp_hold_time_);
if (err) {
DeleteSession(xmpps);
assert(0);
}
return session;
}
void XmppClient::Shutdown() {
XmppConnectionManager::Shutdown();
deleter_->Delete();
}
void
XmppClient::ProcessConfigUpdate(XmppConfigManager::DiffType delta,
const XmppChannelConfig *current, const XmppChannelConfig *future) {
if (delta == XmppConfigManager::DF_ADD) {
XmppClientConnection *connection = CreateConnection(future);
connection->Initialize(); // trigger state-machine
}
if (delta == XmppConfigManager::DF_DELETE) {
ConnectionMap::iterator loc = connection_map_.find(current->endpoint);
if (loc != connection_map_.end()) {
loc->second->ManagedDelete();
}
}
}
void
XmppClient::ConfigUpdate(const XmppConfigData *cfg) {
config_mgr_->SetFuture(cfg);
config_mgr_->PeerConfigDiff(
boost::bind(&XmppClient::ProcessConfigUpdate, this, _1, _2, _3));
config_mgr_->AcceptFuture();
}
void XmppClient::RegisterConnectionEvent(xmps::PeerId id,
ConnectionEventCb cb) {
connection_event_map_.insert(make_pair(id, cb));
}
void XmppClient::UnRegisterConnectionEvent(xmps::PeerId id) {
ConnectionEventCbMap::iterator it = connection_event_map_.find(id);
if (it != connection_event_map_.end())
connection_event_map_.erase(it);
}
void XmppClient::NotifyConnectionEvent(XmppChannelMux *mux,
xmps::PeerState state) {
ConnectionEventCbMap::iterator iter = connection_event_map_.begin();
for (; iter != connection_event_map_.end(); ++iter) {
ConnectionEventCb cb = iter->second;
cb(mux, state);
}
}
size_t XmppClient::ConnectionEventCount() const {
return connection_event_map_.size();
}
size_t XmppClient::ConnectionCount() const {
return connection_map_.size();
}
SslSession *XmppClient::AllocSession(SslSocket *socket) {
SslSession *session = new XmppSession(this, socket);
return session;
}
XmppClientConnection *XmppClient::FindConnection(const string &address) {
BOOST_FOREACH(ConnectionMap::value_type &value, connection_map_) {
if (value.second->ToString() == address)
return value.second;
}
return NULL;
}
XmppClientConnection *XmppClient::CreateConnection(
const XmppChannelConfig *config) {
XmppClientConnection *connection =
XmppObjectFactory::Create<XmppClientConnection>(this, config);
Endpoint endpoint = connection->endpoint();
ConnectionMap::iterator loc;
bool result;
tie(loc, result) = connection_map_.insert(make_pair(endpoint, connection));
assert(result);
return connection;
}
void XmppClient::InsertConnection(XmppClientConnection *connection) {
assert(!connection->IsDeleted());
Endpoint endpoint = connection->endpoint();
ConnectionMap::iterator loc;
bool result;
tie(loc, result) = connection_map_.insert(make_pair(endpoint, connection));
assert(result);
}
void XmppClient::RemoveConnection(XmppClientConnection *connection) {
assert(connection->IsDeleted());
Endpoint endpoint = connection->endpoint();
ConnectionMap::iterator loc = connection_map_.find(endpoint);
assert(loc != connection_map_.end() && loc->second == connection);
connection_map_.erase(loc);
}
XmppChannel *XmppClient::FindChannel(const string &address) {
XmppClientConnection *connection = FindConnection(address);
return (connection ? connection->ChannelMux() : NULL);
}