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 11605 : XmppChannelMux::XmppChannelMux(XmppConnection *connection) 18 11605 : : connection_(connection), rx_message_trace_cb_(NULL), 19 23210 : tx_message_trace_cb_(NULL) { 20 11605 : last_received_ = 0; 21 11605 : last_sent_ = 0; 22 11605 : } 23 : 24 11702 : XmppChannelMux::~XmppChannelMux() { 25 11605 : assert(map_.empty()); 26 11702 : } 27 : 28 540 : void XmppChannelMux::Close() { 29 540 : connection_->Clear(); 30 540 : } 31 : 32 8981 : bool XmppChannelMux::LastReceived(time_t duration) const { 33 8981 : return (UTCTimestamp() - last_received_) <= duration; 34 : } 35 : 36 2821 : bool XmppChannelMux::LastSent(time_t duration) const { 37 2821 : return (UTCTimestamp() - last_sent_) <= duration; 38 : } 39 : 40 3576980 : xmps::PeerState XmppChannelMux::GetPeerState() const { 41 3576980 : xmsm::XmState st = connection_->GetStateMcState(); 42 3576891 : return (st == xmsm::ESTABLISHED) ? xmps::READY : 43 3576891 : xmps::NOT_READY; 44 : } 45 : 46 1 : void XmppChannelMux::WriteReady(const boost::system::error_code &ec) { 47 1 : std::scoped_lock lock(mutex_); 48 : 49 1 : WriteReadyCbMap::iterator iter = map_.begin(); 50 1 : WriteReadyCbMap::iterator next = iter; 51 2 : for (; iter != map_.end(); iter = next) { 52 1 : ++next; 53 1 : SendReadyCb cb = iter->second; 54 1 : cb(ec); 55 1 : map_.erase(iter); 56 1 : } 57 1 : } 58 : 59 1647891 : bool XmppChannelMux::Send(const uint8_t *msg, size_t msgsize, 60 : const string *msg_str, xmps::PeerId id, 61 : SendReadyCb cb) { 62 1647891 : if (!connection_) return false; 63 : 64 1647891 : std::scoped_lock lock(mutex_); 65 1648384 : last_sent_ = UTCTimestamp(); 66 1648509 : bool res = connection_->Send(msg, msgsize, msg_str); 67 1648500 : if (res == false) { 68 92 : RegisterWriteReady(id, cb); 69 : } 70 1648500 : return res; 71 1648500 : } 72 : 73 22355 : int XmppChannelMux::GetTaskInstance() const { 74 22355 : return connection_->GetTaskInstance(); 75 : } 76 : 77 9995 : void XmppChannelMux::RegisterReferer(xmps::PeerId id) { 78 9995 : referers_.insert(id); 79 9995 : } 80 : 81 9995 : void XmppChannelMux::UnRegisterReferer(xmps::PeerId id) { 82 9995 : referers_.erase(id); 83 9995 : } 84 : 85 13943 : void XmppChannelMux::RegisterReceive(xmps::PeerId id, ReceiveCb cb) { 86 13943 : rxmap_.insert(make_pair(id, cb)); 87 13943 : } 88 : 89 14063 : void XmppChannelMux::UnRegisterReceive(xmps::PeerId id) { 90 14063 : ReceiveCbMap::iterator it = rxmap_.find(id); 91 14063 : if (it != rxmap_.end()) { 92 13939 : rxmap_.erase(it); 93 : } 94 : 95 14063 : if (ReceiverCount()) 96 4075 : return; 97 : 98 : XmppServerConnection *server_connection = 99 10240 : 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 10509 : if (!connection_->IsDeleted() && server_connection && 104 269 : server_connection->server()->IsGRHelperModeEnabled()) { 105 252 : server_connection->state_machine()->Initialize(); 106 252 : return; 107 : } 108 : 109 9988 : connection_->RetryDelete(); 110 : } 111 : 112 11667 : size_t XmppChannelMux::RefererCount() const { 113 11667 : return referers_.size(); 114 : } 115 : 116 31893 : size_t XmppChannelMux::ReceiverCount() const { 117 31893 : return rxmap_.size(); 118 : } 119 : 120 72 : vector<string> XmppChannelMux::GetReceiverList() const { 121 72 : vector<string> receivers; 122 96 : for (const auto& value : rxmap_) { 123 24 : receivers.push_back(xmps::PeerIdToName(value.first)); 124 : } 125 72 : return receivers; 126 0 : } 127 : 128 : // 129 : // To be called after acquiring mutex 130 : // 131 93 : void XmppChannelMux::RegisterWriteReady(xmps::PeerId id, SendReadyCb cb) { 132 93 : map_.insert(make_pair(id, cb)); 133 93 : } 134 : 135 : // 136 : // To be called after acquiring mutex 137 : // 138 13839 : void XmppChannelMux::UnRegisterWriteReady(xmps::PeerId id) { 139 13839 : map_.erase(id); 140 13839 : } 141 : 142 1703748 : const std::string &XmppChannelMux::ToString() const { 143 1703748 : return connection_->ToString(); 144 : } 145 : 146 0 : const std::string &XmppChannelMux::FromString() const { 147 0 : return connection_->FromString(); 148 : } 149 : 150 974 : std::string XmppChannelMux::StateName() const { 151 974 : 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 3219298 : inline bool MatchCallback(string to, xmps::PeerId peer) { 163 3219298 : if ((to.find(XmppInit::kBgpPeer) != string::npos) && 164 : (peer == xmps::BGP)) { 165 1638092 : return true; 166 : } 167 1581235 : if ((to.find(XmppInit::kConfigPeer) != string::npos) && 168 : (peer == xmps::CONFIG)) { 169 0 : return true; 170 : } 171 1581235 : if ((to.find(XmppInit::kDnsPeer) != string::npos) && 172 : (peer == xmps::DNS)) { 173 0 : return true; 174 : } 175 1581235 : if ((to.find(XmppInit::kOtherPeer) != string::npos) && 176 : (peer == xmps::OTHER)) { 177 4 : return true; 178 : } 179 1581231 : return false; 180 : } 181 : 182 1638287 : void XmppChannelMux::ProcessXmppMessage(const XmppStanza::XmppMessage *msg) { 183 1638287 : last_received_ = UTCTimestamp(); 184 1638289 : ReceiveCbMap::iterator iter = rxmap_.begin(); 185 4857606 : for (; iter != rxmap_.end(); ++iter) { 186 3219313 : if (MatchCallback(msg->to, iter->first)) { 187 1638094 : ReceiveCb cb = iter->second; 188 1638085 : cb(msg, GetPeerState()); 189 1638093 : } 190 : } 191 1638284 : } 192 : 193 25235 : void XmppChannelMux::HandleStateEvent(xmsm::XmState state) { 194 25235 : CHECK_CONCURRENCY("xmpp::StateMachine"); 195 25229 : xmps::PeerState st = xmps::NOT_READY; 196 25229 : if (state == xmsm::ESTABLISHED) { 197 12603 : st = xmps::READY; 198 12626 : } else if (state == xmsm::ACTIVE) { 199 271 : st = xmps::TIMEDOUT; 200 : } 201 : 202 25229 : if (connection_->IsClient()) { 203 12332 : XmppClient *client = static_cast<XmppClient *>(connection_->server()); 204 12332 : client->NotifyConnectionEvent(this, st); 205 : } else { 206 : // Event to create the peer on server 207 12897 : XmppServer *server = static_cast<XmppServer *>(connection_->server()); 208 12900 : server->NotifyConnectionEvent(this, st); 209 : } 210 25235 : } 211 : 212 490 : std::string XmppChannelMux::LastStateName() const { 213 490 : return connection_->LastStateName(); 214 : } 215 487 : std::string XmppChannelMux::LastStateChangeAt() const { 216 487 : return connection_->LastStateChangeAt(); 217 : } 218 487 : std::string XmppChannelMux::LastEvent() const { 219 487 : return connection_->LastEvent(); 220 : } 221 578 : uint32_t XmppChannelMux::rx_open() const { 222 578 : return connection_->rx_open(); 223 : } 224 578 : uint32_t XmppChannelMux::rx_close() const { 225 578 : return connection_->rx_close(); 226 : } 227 578 : uint32_t XmppChannelMux::rx_update() const { 228 578 : return connection_->rx_update(); 229 : } 230 578 : uint32_t XmppChannelMux::rx_keepalive() const { 231 578 : return connection_->rx_keepalive(); 232 : } 233 578 : uint32_t XmppChannelMux::tx_open() const { 234 578 : return connection_->tx_open(); 235 : } 236 578 : uint32_t XmppChannelMux::tx_close() const { 237 578 : return connection_->tx_close(); 238 : } 239 578 : uint32_t XmppChannelMux::tx_update() const { 240 578 : return connection_->tx_update(); 241 : } 242 578 : uint32_t XmppChannelMux::tx_keepalive() const { 243 578 : return connection_->tx_keepalive(); 244 : } 245 1461 : uint32_t XmppChannelMux::FlapCount() const { 246 1461 : return connection_->flap_count(); 247 : } 248 1461 : std::string XmppChannelMux::LastFlap() const { 249 1461 : return connection_->last_flap_at(); 250 : } 251 : 252 1 : void XmppChannelMux::RegisterRxMessageTraceCallback(RxMessageTraceCb cb) { 253 1 : rx_message_trace_cb_ = cb; 254 1 : } 255 1 : void XmppChannelMux::RegisterTxMessageTraceCallback(TxMessageTraceCb cb) { 256 1 : tx_message_trace_cb_ = cb; 257 1 : } 258 : 259 1658487 : 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 1658487 : if (rx_message_trace_cb_) { 265 0 : return rx_message_trace_cb_(to_address, port, msg_size, msg, xmpp_msg); 266 : } 267 1658485 : return false; 268 : } 269 : 270 1647711 : 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 1647711 : if (tx_message_trace_cb_) { 276 0 : return tx_message_trace_cb_(to_address, port, msg_size, msg, xmpp_msg); 277 : } 278 1647793 : return false; 279 : }