LCOV - code coverage report
Current view: top level - xmpp - xmpp_proto.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 235 299 78.6 %
Date: 2026-11-23 02:04:52 Functions: 22 28 78.6 %
Legend: Lines: hit not hit

          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_proto.h"
       7             : #include <iostream>
       8             : #include <string>
       9             : #include <boost/algorithm/string/replace.hpp>
      10             : #include <boost/algorithm/string/predicate.hpp>
      11             : 
      12             : #include "xmpp/xmpp_connection.h"
      13             : #include "xmpp/xmpp_log.h"
      14             : #include "xmpp/xmpp_session.h"
      15             : #include "xmpp/xmpp_str.h"
      16             : 
      17             : #include "sandesh/sandesh_trace.h"
      18             : #include "sandesh/common/vns_types.h"
      19             : #include "sandesh/common/vns_constants.h"
      20             : #include "sandesh/xmpp_message_sandesh_types.h"
      21             : #include "sandesh/xmpp_trace_sandesh_types.h"
      22             : 
      23             : using namespace std;
      24             : 
      25             : unique_ptr<XmlBase> XmppProto::open_doc_(AllocXmppXmlImpl(sXMPP_STREAM_OPEN));
      26             : 
      27           0 : XmppStanza::XmppStanza() {
      28           0 : }
      29             : 
      30           0 : XmppProto::XmppProto() {
      31           0 : }
      32             : 
      33           0 : XmppProto::~XmppProto() {
      34           0 : }
      35             : 
      36         170 : int XmppProto::EncodeStream(const XmppStreamMessage &str, string &to,
      37             :                             string &from, const string &xmlns, uint8_t *buf,
      38             :                             size_t size) {
      39         170 :     int len = 0;
      40             : 
      41         170 :     switch (str.strmtype) {
      42          77 :         case (XmppStanza::XmppStreamMessage::INIT_STREAM_HEADER):
      43          77 :             len = EncodeOpen(buf, to, from, xmlns, size);
      44          77 :             break;
      45          54 :         case (XmppStanza::XmppStreamMessage::INIT_STREAM_HEADER_RESP):
      46          54 :             len = EncodeOpenResp(buf, to, from, size);
      47          54 :             break;
      48          39 :         case (XmppStanza::XmppStreamMessage::FEATURE_TLS):
      49          39 :             switch (str.strmtlstype) {
      50          14 :                 case (XmppStanza::XmppStreamMessage::TLS_FEATURE_REQUEST):
      51          14 :                     len = EncodeFeatureTlsRequest(buf);
      52          14 :                     break;
      53          15 :                 case (XmppStanza::XmppStreamMessage::TLS_START):
      54          15 :                     len = EncodeFeatureTlsStart(buf);
      55          15 :                     break;
      56          10 :                 case (XmppStanza::XmppStreamMessage::TLS_PROCEED):
      57          10 :                     len = EncodeFeatureTlsProceed(buf);
      58          10 :                     break;
      59             :             }
      60          39 :             break;
      61           0 :         default:
      62           0 :             break;
      63             :     }
      64             : 
      65         170 :     return len;
      66             : }
      67             : 
      68          47 : int XmppProto::EncodeStream(const XmppStanza::XmppMessage &str, uint8_t *buf,
      69             :                             size_t size) {
      70          47 :     int ret = 0;
      71             : 
      72          47 :     if (str.type == XmppStanza::WHITESPACE_MESSAGE_STANZA) {
      73          47 :         return EncodeWhitespace(buf);
      74             :     }
      75             : 
      76           0 :     return ret;
      77             : }
      78             : 
      79           0 : int XmppProto::EncodeMessage(XmlBase *dom, uint8_t *buf, size_t size) {
      80           0 :     int len = dom->WriteDoc(buf);
      81             : 
      82           0 :     return len;
      83             : }
      84             : 
      85           0 : int XmppProto::EncodePresence(uint8_t *buf, size_t size) {
      86           0 :     return 0;
      87             : }
      88             : 
      89           0 : int XmppProto::EncodeIq(const XmppStanza::XmppMessageIq *iq,
      90             :                         XmlBase *doc, uint8_t *buf, size_t size) {
      91           0 :     unique_ptr<XmlBase> send_doc_(AllocXmppXmlImpl());
      92             : 
      93             :     // create
      94           0 :     send_doc_->LoadDoc("");
      95           0 :     send_doc_->AddNode("iq", "");
      96             : 
      97           0 :     switch(iq->stype) {
      98           0 :         case XmppStanza::XmppMessageIq::GET:
      99           0 :             send_doc_->AddAttribute("type", "get");
     100           0 :             break;
     101           0 :         case XmppStanza::XmppMessageIq::SET:
     102           0 :             send_doc_->AddAttribute("type", "set");
     103           0 :             break;
     104           0 :         case XmppStanza::XmppMessageIq::RESULT:
     105           0 :             send_doc_->AddAttribute("type", "result");
     106           0 :             break;
     107           0 :         case XmppStanza::XmppMessageIq::ERROR:
     108           0 :             send_doc_->AddAttribute("type", "error");
     109           0 :             break;
     110           0 :         default:
     111           0 :             break;
     112             :     }
     113           0 :     send_doc_->AddAttribute("from", iq->from);
     114           0 :     send_doc_->AddAttribute("to", iq->to);
     115           0 :     send_doc_->AddAttribute("id", "id1");
     116             : 
     117           0 :     send_doc_->AddChildNode("pubsub", "");
     118           0 :     send_doc_->AddAttribute("xmlns", "http://jabber.org/protocol/pubsub");
     119             : 
     120           0 :     send_doc_->AppendDoc("pubsub", doc);
     121             : 
     122             :     //Returns byte encoded in the doc
     123           0 :     int len = send_doc_->WriteDoc(buf);
     124             : 
     125           0 :     return len;
     126           0 : }
     127             : 
     128          47 : int XmppProto::EncodeWhitespace(uint8_t *buf) {
     129          47 :     string str(sXMPP_WHITESPACE);
     130             : 
     131          47 :     int len = str.size();
     132          47 :     if (len > 0) {
     133          47 :         memcpy(buf, str.data(), len);
     134             :     }
     135             : 
     136          47 :     return len;
     137          47 : }
     138             : 
     139          54 : int XmppProto::EncodeOpenResp(uint8_t *buf, string &to, string &from,
     140             :                               size_t max_size) {
     141             : 
     142          54 :     unique_ptr<XmlBase> resp_doc(XmppStanza::AllocXmppXmlImpl(sXMPP_STREAM_RESP));
     143             : 
     144          54 :     if (resp_doc.get() == NULL) {
     145           0 :         return 0;
     146             :     }
     147             : 
     148          54 :     SetTo(to, resp_doc.get());
     149          54 :     SetFrom(from, resp_doc.get());
     150             : 
     151          54 :     std::stringstream ss;
     152          54 :     resp_doc->PrintDoc(ss);
     153          54 :     std::string msg;
     154          54 :     msg = ss.str();
     155          54 :     size_t len = msg.size();
     156          54 :     if (len > max_size) {
     157           0 :         LOG(ERROR, "\n (Open Confirm) size greater than max buffer size \n");
     158           0 :         return 0;
     159             :     } else {
     160          54 :         boost::algorithm::ireplace_last(msg, "/", " ");
     161          54 :         memcpy(buf, msg.c_str(), len);
     162          54 :         return len;
     163             :     }
     164          54 : }
     165             : 
     166          77 : int XmppProto::EncodeOpen(uint8_t *buf, string &to, string &from,
     167             :                           const string &xmlns, size_t max_size) {
     168             : 
     169          77 :     if (open_doc_.get() ==  NULL) {
     170           0 :         return 0;
     171             :     }
     172             : 
     173          77 :     SetTo(to, open_doc_.get());
     174          77 :     SetFrom(from, open_doc_.get());
     175          77 :     SetXmlns(xmlns, open_doc_.get());
     176             : 
     177             :     //Returns byte encoded in the doc
     178          77 :     std::stringstream ss;
     179          77 :     open_doc_->PrintDoc(ss);
     180          77 :     std::string msg;
     181          77 :     msg = ss.str();
     182          77 :     size_t len = msg.size();
     183          77 :     if (len > max_size) {
     184           0 :         LOG(ERROR, "\n (Open Message) size greater than max buffer size \n");
     185           0 :         return 0;
     186             :     } else {
     187          77 :         boost::algorithm::ireplace_last(msg, "/", " ");
     188          77 :         memcpy(buf, msg.c_str(), len);
     189          77 :         return len;
     190             :     }
     191          77 : }
     192             : 
     193          14 : int XmppProto::EncodeFeatureTlsRequest(uint8_t *buf) {
     194          14 :     unique_ptr<XmlBase> resp_doc(XmppStanza::AllocXmppXmlImpl(sXMPP_STREAM_FEATURE_TLS));
     195             :     //Returns byte encoded in the doc
     196          14 :     int len = resp_doc->WriteDoc(buf);
     197          14 :     return len;
     198          14 : }
     199             : 
     200          15 : int XmppProto::EncodeFeatureTlsStart(uint8_t *buf) {
     201          15 :     unique_ptr<XmlBase> resp_doc(XmppStanza::AllocXmppXmlImpl(sXMPP_STREAM_START_TLS));
     202             :     //Returns byte encoded in the doc
     203          15 :     int len = resp_doc->WriteDoc(buf);
     204          15 :     return len;
     205          15 : }
     206             : 
     207          10 : int XmppProto::EncodeFeatureTlsProceed(uint8_t *buf) {
     208          10 :     unique_ptr<XmlBase> resp_doc(XmppStanza::AllocXmppXmlImpl(sXMPP_STREAM_PROCEED_TLS));
     209             :     //Returns byte encoded in the doc
     210          10 :     int len = resp_doc->WriteDoc(buf);
     211          10 :     return len;
     212          10 : }
     213             : 
     214        9552 : XmppStanza::XmppMessage *XmppProto::Decode(const XmppConnection *connection,
     215             :                                            const string &ts) {
     216        9552 :     XmlBase *impl = XmppStanza::AllocXmppXmlImpl();
     217        9547 :     if (impl == nullptr) {
     218           0 :         return nullptr;
     219             :     }
     220             : 
     221        9547 :     XmppStanza::XmppMessage *msg = DecodeInternal(connection, ts, impl);
     222        9551 :     if (!msg) {
     223           0 :         return nullptr;
     224             :     }
     225             : 
     226             :     // transfer ownership of the dom implementation
     227        9551 :     msg->dom.reset(impl);
     228             : 
     229        9552 :     return msg;
     230             : }
     231             : 
     232        9547 : XmppStanza::XmppMessage *XmppProto::DecodeInternal(
     233             :         const XmppConnection *connection, const string &ts, XmlBase *impl) {
     234        9547 :     XmppStanza::XmppMessage *ret = nullptr;
     235             : 
     236        9547 :     string ns(sXMPP_STREAM_O);
     237        9546 :     string ws(sXMPP_WHITESPACE);
     238        9546 :     string iq(sXMPP_IQ_KEY);
     239             : 
     240        9546 :     if (ts.find(sXMPP_IQ) != string::npos) {
     241        1337 :         string ts_tmp = ts;
     242             : 
     243        1337 :         if (impl->LoadDoc(ts) == -1) {
     244           0 :             XMPP_WARNING(XmppIqMessageParseFail, connection->ToUVEKey(),
     245             :                          XMPP_PEER_DIR_IN);
     246           0 :             assert(false);
     247             :             goto done;
     248             :         }
     249             : 
     250        1337 :         XmppStanza::XmppMessageIq *msg = new XmppStanza::XmppMessageIq;
     251        1337 :         impl->ReadNode(iq);
     252        1337 :         msg->to = XmppProto::GetTo(impl);
     253        1337 :         msg->from = XmppProto::GetFrom(impl);
     254        1337 :         msg->id = XmppProto::GetId(impl);
     255        1337 :         msg->iq_type = XmppProto::GetType(impl);
     256             :         // action is subscribe,publish,collection
     257        1337 :         const char *action = XmppProto::GetAction(impl, msg->iq_type);
     258        1337 :         if (action) {
     259        1322 :             msg->action = action;
     260             :         }
     261        1337 :         if (XmppProto::GetNode(impl, msg->action)) {
     262        1322 :             msg->node = XmppProto::GetNode(impl, msg->action);
     263             :         }
     264             :         //associate or dissociate collection node
     265        1337 :         if (msg->action.compare("collection") == 0) {
     266         642 :             if (XmppProto::GetAsNode(impl)) {
     267         322 :                 msg->as_node = XmppProto::GetAsNode(impl);
     268         322 :                 msg->is_as_node = true;
     269         320 :             } else if (XmppProto::GetDsNode(impl)) {
     270         320 :                 msg->as_node = XmppProto::GetDsNode(impl);
     271         320 :                 msg->is_as_node = false;
     272             :             }
     273             :         }
     274             : 
     275             :         //msg->dom.reset(impl);
     276             : 
     277        1337 :         ret = msg;
     278             : 
     279        1337 :         XMPP_UTDEBUG(XmppIqMessageProcess, connection->ToUVEKey(),
     280             :                      XMPP_PEER_DIR_IN, msg->node, msg->action, msg->from,
     281             :                      msg->to, msg->id, msg->iq_type);
     282        1337 :         goto done;
     283             : 
     284        9552 :     } else if (ts.find(sXMPP_MESSAGE) != string::npos) {
     285             : 
     286        3096 :         if (impl->LoadDoc(ts) == -1) {
     287           0 :             XMPP_WARNING(XmppChatMessageParseFail, connection->ToUVEKey(),
     288             :                          XMPP_PEER_DIR_IN);
     289           0 :             goto done;
     290             :         }
     291             :         XmppStanza::XmppMessage *msg = new XmppStanza::XmppChatMessage(
     292        3096 :                                            STATE_NONE);
     293        3096 :         impl->ReadNode(sXMPP_MESSAGE_KEY);
     294             : 
     295        3096 :         msg->to = XmppProto::GetTo(impl);
     296        3096 :         msg->from = XmppProto::GetFrom(impl);
     297        3096 :         ret = msg;
     298             : 
     299        3096 :         XMPP_UTDEBUG(XmppChatMessageProcess, connection->ToUVEKey(),
     300             :                      XMPP_PEER_DIR_IN, msg->type, msg->from, msg->to);
     301        3096 :         goto done;
     302             : 
     303        5119 :     } else if (ts.find(sXMPP_STREAM_O) != string::npos) {
     304             : 
     305             :         // ensusre stream open is at the beginning of the message
     306          62 :         string ts_tmp = ts;
     307          62 :         ts_tmp.erase(std::remove(ts_tmp.begin(), ts_tmp.end(), '\n'), ts_tmp.end());
     308             : 
     309          62 :         if ((ts_tmp.compare(0, strlen(sXMPP_STREAM_START),
     310         124 :              sXMPP_STREAM_START) != 0) &&
     311          62 :             (ts_tmp.compare(0, strlen(sXMPP_STREAM_START_S),
     312             :              sXMPP_STREAM_START_S) != 0)) {
     313           0 :             XMPP_WARNING(XmppBadMessage, connection->ToUVEKey(),
     314             :                          XMPP_PEER_DIR_IN,
     315             :                          "Open message not at the beginning.", ts);
     316           0 :             goto done;
     317             :         }
     318             : 
     319             :         // check if the buf is xmpp open or response message
     320             :         // As end tag will be missing we need to modify the
     321             :         // string for stream open, else dom decoder will fail
     322          62 :         boost::algorithm::replace_last(ts_tmp, ">", "/>");
     323          62 :         if (impl->LoadDoc(ts_tmp) == -1) {
     324           0 :             XMPP_WARNING(XmppBadMessage, connection->ToUVEKey(),
     325             :                          XMPP_PEER_DIR_IN, "Open message parse failed.", ts);
     326           0 :             goto done;
     327             :         }
     328             : 
     329             :         XmppStanza::XmppStreamMessage *strm =
     330          62 :             new XmppStanza::XmppStreamMessage();
     331          62 :         strm->strmtype = XmppStanza::XmppStreamMessage::INIT_STREAM_HEADER;
     332          62 :         impl->ReadNode(ns);
     333          62 :         strm->to = XmppProto::GetTo(impl);
     334          62 :         strm->from = XmppProto::GetFrom(impl);
     335          62 :         strm->xmlns = XmppProto::GetXmlns(impl);
     336             : 
     337          62 :         ret = strm;
     338             : 
     339          62 :         XMPP_UTDEBUG(XmppRxOpenMessage, connection->ToUVEKey(),
     340             :                      XMPP_PEER_DIR_IN, strm->from, strm->to);
     341             : 
     342        5119 :     } else if (ts.find(sXMPP_STREAM_NS_TLS) != string::npos) {
     343             : 
     344           3 :         if (impl->LoadDoc(ts) == -1) {
     345           0 :             XMPP_WARNING(XmppBadMessage, connection->ToUVEKey(),
     346             :                          XMPP_PEER_DIR_IN, "Stream TLS parse failed.", ts);
     347           0 :             goto done;
     348             :         }
     349             : 
     350             :         // find stream:features tls required
     351           4 :         if ((ts.find(sXMPP_STREAM_FEATURES_O) != string::npos) &&
     352           4 :             (ts.find(sXMPP_STREAM_STARTTLS_O) != string::npos) &&
     353           1 :             (ts.find(sXMPP_REQUIRED_O) != string::npos)) {
     354             : 
     355             :             XmppStanza::XmppStreamMessage *strm =
     356           1 :                 new XmppStanza::XmppStreamMessage();
     357           1 :             strm->strmtype = XmppStanza::XmppStreamMessage::FEATURE_TLS;
     358           1 :             strm->strmtlstype = XmppStanza::XmppStreamMessage::TLS_FEATURE_REQUEST;
     359             : 
     360           1 :             ret = strm;
     361             : 
     362           1 :             XMPP_UTDEBUG(XmppRxStreamTlsRequired, connection->ToUVEKey(),
     363             :                          XMPP_PEER_DIR_IN);
     364             : 
     365           2 :         } else if (ts.find(sXMPP_STREAM_STARTTLS_O) != string::npos) {
     366             :             XmppStanza::XmppStreamMessage *strm =
     367           1 :                 new XmppStanza::XmppStreamMessage();
     368           1 :             strm->strmtype = XmppStanza::XmppStreamMessage::FEATURE_TLS;
     369           1 :             strm->strmtlstype = XmppStanza::XmppStreamMessage::TLS_START;
     370           1 :             ret = strm;
     371             : 
     372           1 :             XMPP_UTDEBUG(XmppRxStreamStartTls, connection->ToUVEKey(),
     373             :                          XMPP_PEER_DIR_IN);
     374             : 
     375           1 :         } else if (ts.find(sXMPP_STREAM_PROCEED_O) != string::npos) {
     376             :             XmppStanza::XmppStreamMessage *strm =
     377           1 :                 new XmppStanza::XmppStreamMessage();
     378           1 :             strm->strmtype = XmppStanza::XmppStreamMessage::FEATURE_TLS;
     379           1 :             strm->strmtlstype = XmppStanza::XmppStreamMessage::TLS_PROCEED;
     380             : 
     381           1 :             ret = strm;
     382             : 
     383           1 :             XMPP_UTDEBUG(XmppRxStreamProceed, connection->ToUVEKey(),
     384             :                          XMPP_PEER_DIR_IN);
     385             :         }
     386           3 :         goto done;
     387             : 
     388        5054 :     } else if (ts.find_first_of(sXMPP_VALIDWS) != string::npos) {
     389             : 
     390             :         XmppStanza::XmppMessage *msg =
     391        5054 :             new XmppStanza::XmppMessage(WHITESPACE_MESSAGE_STANZA);
     392        5054 :         return msg;
     393             :     } else {
     394           0 :         XMPP_WARNING(XmppBadMessage, connection->ToUVEKey(),
     395             :                      XMPP_PEER_DIR_IN, "Message not supported", ts);
     396             :     }
     397             : 
     398        4498 : done:
     399             : 
     400        4498 :     return ret;
     401        9552 : }
     402             : 
     403         131 : int XmppProto::SetTo(string &to, XmlBase *doc) {
     404         131 :     if (!doc) return -1;
     405             : 
     406         131 :     string ns(sXMPP_STREAM_O);
     407         131 :     doc->ReadNode(ns);
     408         131 :     doc->ModifyAttribute("to", to);
     409             : 
     410         131 :     return 0;
     411         131 : }
     412             : 
     413         131 : int XmppProto::SetFrom(string &from, XmlBase *doc) {
     414         131 :     if (!doc) return -1;
     415             : 
     416         131 :     string ns(sXMPP_STREAM_O);
     417         131 :     doc->ReadNode(ns);
     418         131 :     return doc->ModifyAttribute("from", from);
     419         131 : }
     420             : 
     421          77 : int XmppProto::SetXmlns(const string &xmlns, XmlBase *doc) {
     422          77 :     if (!doc)
     423           0 :         return -1;
     424             : 
     425          77 :     string ns(sXMPP_STREAM_O);
     426          77 :     doc->ReadNode(ns);
     427          77 :     return doc->ModifyAttribute("xmlns", xmlns);
     428          77 : }
     429             : 
     430        4495 : const char *XmppProto::GetTo(XmlBase *doc) {
     431        4495 :     if (!doc) return NULL;
     432             : 
     433        4495 :     string tmp("to");
     434        4495 :     return doc->ReadAttrib(tmp);
     435        4493 : }
     436             : 
     437        4494 : const char *XmppProto::GetFrom(XmlBase *doc) {
     438        4494 :     if (!doc) return NULL;
     439             : 
     440        4494 :     string tmp("from");
     441        4495 :     return doc->ReadAttrib(tmp);
     442        4494 : }
     443             : 
     444          62 : const char *XmppProto::GetXmlns(XmlBase *doc) {
     445          62 :     if (!doc)
     446           0 :         return NULL;
     447             : 
     448          62 :     string tmp("xmlns");
     449          62 :     return doc->ReadAttrib(tmp);
     450          62 : }
     451             : 
     452        1337 : const char *XmppProto::GetId(XmlBase *doc) {
     453        1337 :     if (!doc) return NULL;
     454             : 
     455        1337 :     string tmp("id");
     456        1337 :     return doc->ReadAttrib(tmp);
     457        1337 : }
     458             : 
     459        1337 : const char *XmppProto::GetType(XmlBase *doc) {
     460        1337 :     if (!doc) return NULL;
     461             : 
     462        1337 :     string tmp("type");
     463        1337 :     return doc->ReadAttrib(tmp);
     464        1337 : }
     465             : 
     466        1337 : const char *XmppProto::GetAction(XmlBase *doc, const string &str) {
     467        1337 :     if (!doc) return NULL;
     468             : 
     469        1337 :     if (str.compare("set") == 0) {
     470        1322 :         doc->ReadNode("pubsub");
     471        1322 :         return(doc->ReadChildNodeName());
     472          15 :     } else if (str.compare("get") == 0) {
     473             :     }
     474             : 
     475          15 :     return(NULL);
     476             : }
     477             : 
     478        2659 : const char *XmppProto::GetNode(XmlBase *doc, const string &str) {
     479        2659 :     if (!doc) return NULL;
     480             : 
     481        2659 :     if (!str.empty()) {
     482        2644 :         return(doc->ReadAttrib("node"));
     483             :     }
     484             : 
     485          15 :     return(NULL);
     486             : }
     487             : 
     488         964 : const char *XmppProto::GetAsNode(XmlBase *doc) {
     489         964 :     if (!doc) return NULL;
     490             : 
     491         964 :     const char *node = doc->ReadNode("associate");
     492         964 :     if (node != NULL) {
     493         644 :         return(doc->ReadAttrib("node"));
     494             :     }
     495             : 
     496         320 :     return(NULL);
     497             : }
     498             : 
     499         640 : const char *XmppProto::GetDsNode(XmlBase *doc) {
     500         640 :     if (!doc) return NULL;
     501             : 
     502         640 :     const char *node = doc->ReadNode("dissociate");
     503         640 :     if (node != NULL) {
     504         640 :         return(doc->ReadAttrib("node"));
     505             :     }
     506             : 
     507           0 :     return(NULL);
     508             : }

Generated by: LCOV version 1.14