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 125 : XmppChannelMux::XmppChannelMux(XmppConnection *connection) 18 125 : : connection_(connection), rx_message_trace_cb_(NULL), 19 250 : tx_message_trace_cb_(NULL) { 20 125 : last_received_ = 0; 21 125 : last_sent_ = 0; 22 125 : } 23 : 24 216 : XmppChannelMux::~XmppChannelMux() { 25 125 : assert(map_.empty()); 26 216 : } 27 : 28 0 : void XmppChannelMux::Close() { 29 0 : connection_->Clear(); 30 0 : } 31 : 32 49 : bool XmppChannelMux::LastReceived(time_t duration) const { 33 49 : return (UTCTimestamp() - last_received_) <= duration; 34 : } 35 : 36 0 : bool XmppChannelMux::LastSent(time_t duration) const { 37 0 : return (UTCTimestamp() - last_sent_) <= duration; 38 : } 39 : 40 9706 : xmps::PeerState XmppChannelMux::GetPeerState() const { 41 9706 : xmsm::XmState st = connection_->GetStateMcState(); 42 9706 : return (st == xmsm::ESTABLISHED) ? xmps::READY : 43 9706 : 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 3792 : bool XmppChannelMux::Send(const uint8_t *msg, size_t msgsize, 60 : const string *msg_str, xmps::PeerId id, 61 : SendReadyCb cb) { 62 3792 : if (!connection_) return false; 63 : 64 3792 : std::scoped_lock lock(mutex_); 65 3792 : last_sent_ = UTCTimestamp(); 66 3792 : bool res = connection_->Send(msg, msgsize, msg_str); 67 3791 : if (res == false) { 68 0 : RegisterWriteReady(id, cb); 69 : } 70 3791 : return res; 71 3791 : } 72 : 73 68 : int XmppChannelMux::GetTaskInstance() const { 74 68 : return connection_->GetTaskInstance(); 75 : } 76 : 77 34 : void XmppChannelMux::RegisterReferer(xmps::PeerId id) { 78 34 : referers_.insert(id); 79 34 : } 80 : 81 34 : void XmppChannelMux::UnRegisterReferer(xmps::PeerId id) { 82 34 : referers_.erase(id); 83 34 : } 84 : 85 70 : void XmppChannelMux::RegisterReceive(xmps::PeerId id, ReceiveCb cb) { 86 70 : rxmap_.insert(make_pair(id, cb)); 87 70 : } 88 : 89 87 : void XmppChannelMux::UnRegisterReceive(xmps::PeerId id) { 90 87 : ReceiveCbMap::iterator it = rxmap_.find(id); 91 87 : if (it != rxmap_.end()) { 92 66 : rxmap_.erase(it); 93 : } 94 : 95 87 : if (ReceiverCount()) 96 34 : return; 97 : 98 : XmppServerConnection *server_connection = 99 70 : 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 104 : if (!connection_->IsDeleted() && server_connection && 104 34 : server_connection->server()->IsGRHelperModeEnabled()) { 105 17 : server_connection->state_machine()->Initialize(); 106 17 : return; 107 : } 108 : 109 53 : connection_->RetryDelete(); 110 : } 111 : 112 142 : size_t XmppChannelMux::RefererCount() const { 113 142 : return referers_.size(); 114 : } 115 : 116 229 : size_t XmppChannelMux::ReceiverCount() const { 117 229 : 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 70 : void XmppChannelMux::UnRegisterWriteReady(xmps::PeerId id) { 139 70 : map_.erase(id); 140 70 : } 141 : 142 3770 : const std::string &XmppChannelMux::ToString() const { 143 3770 : 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 6870 : inline bool MatchCallback(string to, xmps::PeerId peer) { 163 6870 : if ((to.find(XmppInit::kBgpPeer) != string::npos) && 164 : (peer == xmps::BGP)) { 165 3770 : return true; 166 : } 167 3100 : if ((to.find(XmppInit::kConfigPeer) != string::npos) && 168 : (peer == xmps::CONFIG)) { 169 0 : return true; 170 : } 171 3100 : if ((to.find(XmppInit::kDnsPeer) != string::npos) && 172 : (peer == xmps::DNS)) { 173 0 : return true; 174 : } 175 3100 : if ((to.find(XmppInit::kOtherPeer) != string::npos) && 176 : (peer == xmps::OTHER)) { 177 4 : return true; 178 : } 179 3096 : return false; 180 : } 181 : 182 3794 : void XmppChannelMux::ProcessXmppMessage(const XmppStanza::XmppMessage *msg) { 183 3794 : last_received_ = UTCTimestamp(); 184 3795 : ReceiveCbMap::iterator iter = rxmap_.begin(); 185 10665 : for (; iter != rxmap_.end(); ++iter) { 186 6870 : if (MatchCallback(msg->to, iter->first)) { 187 3774 : ReceiveCb cb = iter->second; 188 3773 : cb(msg, GetPeerState()); 189 3774 : } 190 : } 191 3795 : } 192 : 193 186 : void XmppChannelMux::HandleStateEvent(xmsm::XmState state) { 194 186 : CHECK_CONCURRENCY("xmpp::StateMachine"); 195 186 : xmps::PeerState st = xmps::NOT_READY; 196 186 : if (state == xmsm::ESTABLISHED) { 197 87 : st = xmps::READY; 198 99 : } else if (state == xmsm::ACTIVE) { 199 11 : st = xmps::TIMEDOUT; 200 : } 201 : 202 186 : if (connection_->IsClient()) { 203 86 : XmppClient *client = static_cast<XmppClient *>(connection_->server()); 204 86 : client->NotifyConnectionEvent(this, st); 205 : } else { 206 : // Event to create the peer on server 207 100 : XmppServer *server = static_cast<XmppServer *>(connection_->server()); 208 100 : server->NotifyConnectionEvent(this, st); 209 : } 210 186 : } 211 : 212 0 : std::string XmppChannelMux::LastStateName() const { 213 0 : 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 0 : void XmppChannelMux::RegisterRxMessageTraceCallback(RxMessageTraceCb cb) { 253 0 : rx_message_trace_cb_ = cb; 254 0 : } 255 0 : void XmppChannelMux::RegisterTxMessageTraceCallback(TxMessageTraceCb cb) { 256 0 : tx_message_trace_cb_ = cb; 257 0 : } 258 : 259 3856 : 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 3856 : if (rx_message_trace_cb_) { 265 0 : return rx_message_trace_cb_(to_address, port, msg_size, msg, xmpp_msg); 266 : } 267 3856 : return false; 268 : } 269 : 270 3792 : 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 3792 : if (tx_message_trace_cb_) { 276 0 : return tx_message_trace_cb_(to_address, port, msg_size, msg, xmpp_msg); 277 : } 278 3792 : return false; 279 : }