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_channel_mux.h" 7 : 8 : #include <boost/foreach.hpp> 9 : 10 : #include "base/task_annotations.h" 11 : #include "xmpp/xmpp_init.h" 12 : #include "xmpp/xmpp_connection.h" 13 : 14 : using namespace std; 15 : using namespace xmsm; 16 : 17 311 : XmppChannelMux::XmppChannelMux(XmppConnection *connection) 18 311 : : connection_(connection), rx_message_trace_cb_(NULL), 19 622 : tx_message_trace_cb_(NULL) { 20 311 : last_received_ = 0; 21 311 : last_sent_ = 0; 22 311 : } 23 : 24 406 : XmppChannelMux::~XmppChannelMux() { 25 311 : assert(map_.empty()); 26 406 : } 27 : 28 0 : void XmppChannelMux::Close() { 29 0 : connection_->Clear(); 30 0 : } 31 : 32 527 : bool XmppChannelMux::LastReceived(time_t duration) const { 33 527 : return (UTCTimestamp() - last_received_) <= duration; 34 : } 35 : 36 61 : bool XmppChannelMux::LastSent(time_t duration) const { 37 61 : return (UTCTimestamp() - last_sent_) <= duration; 38 : } 39 : 40 253776 : xmps::PeerState XmppChannelMux::GetPeerState() const { 41 253776 : xmsm::XmState st = connection_->GetStateMcState(); 42 253737 : return (st == xmsm::ESTABLISHED) ? xmps::READY : 43 253737 : xmps::NOT_READY; 44 : } 45 : 46 0 : void XmppChannelMux::WriteReady(const boost::system::error_code &ec) { 47 0 : std::scoped_lock lock(mutex_); 48 : 49 0 : WriteReadyCbMap::iterator iter = map_.begin(); 50 0 : WriteReadyCbMap::iterator next = iter; 51 0 : for (; iter != map_.end(); iter = next) { 52 0 : ++next; 53 0 : SendReadyCb cb = iter->second; 54 0 : cb(ec); 55 0 : map_.erase(iter); 56 0 : } 57 0 : } 58 : 59 121272 : bool XmppChannelMux::Send(const uint8_t *msg, size_t msgsize, 60 : const string *msg_str, xmps::PeerId id, 61 : SendReadyCb cb) { 62 121272 : if (!connection_) return false; 63 : 64 121272 : std::scoped_lock lock(mutex_); 65 121366 : last_sent_ = UTCTimestamp(); 66 121389 : bool res = connection_->Send(msg, msgsize, msg_str); 67 121384 : if (res == false) { 68 0 : RegisterWriteReady(id, cb); 69 : } 70 121384 : return res; 71 121384 : } 72 : 73 440 : int XmppChannelMux::GetTaskInstance() const { 74 440 : return connection_->GetTaskInstance(); 75 : } 76 : 77 200 : void XmppChannelMux::RegisterReferer(xmps::PeerId id) { 78 200 : referers_.insert(id); 79 200 : } 80 : 81 200 : void XmppChannelMux::UnRegisterReferer(xmps::PeerId id) { 82 200 : referers_.erase(id); 83 200 : } 84 : 85 321 : void XmppChannelMux::RegisterReceive(xmps::PeerId id, ReceiveCb cb) { 86 321 : rxmap_.insert(make_pair(id, cb)); 87 321 : } 88 : 89 321 : void XmppChannelMux::UnRegisterReceive(xmps::PeerId id) { 90 321 : ReceiveCbMap::iterator it = rxmap_.find(id); 91 321 : if (it != rxmap_.end()) { 92 317 : rxmap_.erase(it); 93 : } 94 : 95 321 : if (ReceiverCount()) 96 98 : return; 97 : 98 : XmppServerConnection *server_connection = 99 239 : dynamic_cast<XmppServerConnection *>(connection_); 100 : 101 : // If GracefulRestart helper mode close process is complete, restart the 102 : // state machine to form new session with the client. 103 272 : if (!connection_->IsDeleted() && server_connection && 104 33 : server_connection->server()->IsGRHelperModeEnabled()) { 105 16 : server_connection->state_machine()->Initialize(); 106 16 : return; 107 : } 108 : 109 223 : connection_->RetryDelete(); 110 : } 111 : 112 311 : size_t XmppChannelMux::RefererCount() const { 113 311 : return referers_.size(); 114 : } 115 : 116 776 : size_t XmppChannelMux::ReceiverCount() const { 117 776 : return rxmap_.size(); 118 : } 119 : 120 0 : vector<string> XmppChannelMux::GetReceiverList() const { 121 0 : vector<string> receivers; 122 0 : for (const auto& value : rxmap_) { 123 0 : receivers.push_back(xmps::PeerIdToName(value.first)); 124 : } 125 0 : return receivers; 126 0 : } 127 : 128 : // 129 : // To be called after acquiring mutex 130 : // 131 0 : void XmppChannelMux::RegisterWriteReady(xmps::PeerId id, SendReadyCb cb) { 132 0 : map_.insert(make_pair(id, cb)); 133 0 : } 134 : 135 : // 136 : // To be called after acquiring mutex 137 : // 138 305 : void XmppChannelMux::UnRegisterWriteReady(xmps::PeerId id) { 139 305 : map_.erase(id); 140 305 : } 141 : 142 126152 : const std::string &XmppChannelMux::ToString() const { 143 126152 : return connection_->ToString(); 144 : } 145 : 146 0 : const std::string &XmppChannelMux::FromString() const { 147 0 : return connection_->FromString(); 148 : } 149 : 150 0 : std::string XmppChannelMux::StateName() const { 151 0 : return connection_->StateName(); 152 : } 153 : 154 0 : std::string XmppChannelMux::AuthType() const { 155 0 : return connection_->GetXmppAuthenticationType(); 156 : } 157 : 158 0 : std::string XmppChannelMux::PeerAddress() const { 159 0 : return connection_->endpoint_string(); 160 : } 161 : 162 241468 : inline bool MatchCallback(string to, xmps::PeerId peer) { 163 241468 : if ((to.find(XmppInit::kBgpPeer) != string::npos) && 164 : (peer == xmps::BGP)) { 165 121384 : return true; 166 : } 167 120084 : if ((to.find(XmppInit::kConfigPeer) != string::npos) && 168 : (peer == xmps::CONFIG)) { 169 0 : return true; 170 : } 171 120084 : if ((to.find(XmppInit::kDnsPeer) != string::npos) && 172 : (peer == xmps::DNS)) { 173 0 : return true; 174 : } 175 120084 : if ((to.find(XmppInit::kOtherPeer) != string::npos) && 176 : (peer == xmps::OTHER)) { 177 4 : return true; 178 : } 179 120080 : return false; 180 : } 181 : 182 121409 : void XmppChannelMux::ProcessXmppMessage(const XmppStanza::XmppMessage *msg) { 183 121409 : last_received_ = UTCTimestamp(); 184 121409 : ReceiveCbMap::iterator iter = rxmap_.begin(); 185 362877 : for (; iter != rxmap_.end(); ++iter) { 186 241468 : if (MatchCallback(msg->to, iter->first)) { 187 121388 : ReceiveCb cb = iter->second; 188 121388 : cb(msg, GetPeerState()); 189 121388 : } 190 : } 191 121409 : } 192 : 193 646 : void XmppChannelMux::HandleStateEvent(xmsm::XmState state) { 194 646 : CHECK_CONCURRENCY("xmpp::StateMachine"); 195 646 : xmps::PeerState st = xmps::NOT_READY; 196 646 : if (state == xmsm::ESTABLISHED) { 197 325 : st = xmps::READY; 198 321 : } else if (state == xmsm::ACTIVE) { 199 11 : st = xmps::TIMEDOUT; 200 : } 201 : 202 646 : if (connection_->IsClient()) { 203 324 : XmppClient *client = static_cast<XmppClient *>(connection_->server()); 204 324 : client->NotifyConnectionEvent(this, st); 205 : } else { 206 : // Event to create the peer on server 207 322 : XmppServer *server = static_cast<XmppServer *>(connection_->server()); 208 322 : server->NotifyConnectionEvent(this, st); 209 : } 210 646 : } 211 : 212 6 : std::string XmppChannelMux::LastStateName() const { 213 6 : return connection_->LastStateName(); 214 : } 215 0 : std::string XmppChannelMux::LastStateChangeAt() const { 216 0 : return connection_->LastStateChangeAt(); 217 : } 218 0 : std::string XmppChannelMux::LastEvent() const { 219 0 : return connection_->LastEvent(); 220 : } 221 0 : uint32_t XmppChannelMux::rx_open() const { 222 0 : return connection_->rx_open(); 223 : } 224 0 : uint32_t XmppChannelMux::rx_close() const { 225 0 : return connection_->rx_close(); 226 : } 227 0 : uint32_t XmppChannelMux::rx_update() const { 228 0 : return connection_->rx_update(); 229 : } 230 0 : uint32_t XmppChannelMux::rx_keepalive() const { 231 0 : return connection_->rx_keepalive(); 232 : } 233 0 : uint32_t XmppChannelMux::tx_open() const { 234 0 : return connection_->tx_open(); 235 : } 236 0 : uint32_t XmppChannelMux::tx_close() const { 237 0 : return connection_->tx_close(); 238 : } 239 0 : uint32_t XmppChannelMux::tx_update() const { 240 0 : return connection_->tx_update(); 241 : } 242 0 : uint32_t XmppChannelMux::tx_keepalive() const { 243 0 : return connection_->tx_keepalive(); 244 : } 245 0 : uint32_t XmppChannelMux::FlapCount() const { 246 0 : return connection_->flap_count(); 247 : } 248 0 : std::string XmppChannelMux::LastFlap() const { 249 0 : return connection_->last_flap_at(); 250 : } 251 : 252 2 : void XmppChannelMux::RegisterRxMessageTraceCallback(RxMessageTraceCb cb) { 253 2 : rx_message_trace_cb_ = cb; 254 2 : } 255 2 : void XmppChannelMux::RegisterTxMessageTraceCallback(TxMessageTraceCb cb) { 256 2 : tx_message_trace_cb_ = cb; 257 2 : } 258 : 259 122047 : bool XmppChannelMux::RxMessageTrace(const std::string &to_address, 260 : int port, 261 : int msg_size, 262 : const std::string &msg, 263 : const XmppStanza::XmppMessage *xmpp_msg) { 264 122047 : if (rx_message_trace_cb_) { 265 0 : return rx_message_trace_cb_(to_address, port, msg_size, msg, xmpp_msg); 266 : } 267 122048 : return false; 268 : } 269 : 270 121214 : bool XmppChannelMux::TxMessageTrace(const std::string &to_address, 271 : int port, 272 : int msg_size, 273 : const std::string &msg, 274 : const XmppStanza::XmppMessage *xmpp_msg) { 275 121214 : if (tx_message_trace_cb_) { 276 0 : return tx_message_trace_cb_(to_address, port, msg_size, msg, xmpp_msg); 277 : } 278 121239 : return false; 279 : }