Commit 0bc6f0b8 authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#3963] Throw exceptions for ill-formated sip Messages

parent e2b38af8
......@@ -275,13 +275,13 @@ InstantMessaging::UriList InstantMessaging::parseXmlUriList (std::string& urilis
InstantMessaging::UriList list;
XML_Parser parser = XML_ParserCreate (NULL);
int depth = 0;
XML_SetUserData (parser, &list);
XML_SetElementHandler (parser, startElementCallback, endElementCallback);
if (XML_Parse (parser, urilist.c_str(), urilist.size(), 1) == XML_STATUS_ERROR) {
std::cout << "Error: " << XML_ErrorString (XML_GetErrorCode (parser))
<< " at line " << XML_GetCurrentLineNumber (parser) << std::endl;
throw InstantMessageException ("Error while parsing uri-list xml content");
}
return list;
......@@ -311,15 +311,27 @@ std::string InstantMessaging::findTextUriList (std::string& text)
std::string cdispo = "Content-Disposition: recipient-list";
std::string boundary = ("--boundary--");
// init position pointer
size_t pos = 0;
pos = text.find (ctype);
pos = text.find (cdispo, pos);
size_t begin = 0;
size_t end = 0;
size_t begin = pos+cdispo.size();
// find the content type
if ( (pos = text.find (ctype)) == std::string::npos)
throw InstantMessageException ("Could not find Content-Type tag while parsing sip message for recipient-list");
size_t end = text.find (boundary, begin);
// find the content disposition
if ( (pos = text.find (cdispo, pos)) == std::string::npos)
throw InstantMessageException ("Could not find Content-Disposition tag while parsing sip message for recipient-list");
return text.substr (begin+2, end-begin-2);
// xml content start after content disposition tag (plus \n\n)
begin = pos+cdispo.size() +2;
// find final boundary
if ( (end = text.find (boundary, begin)) == std::string::npos)
throw InstantMessageException ("Could not find final \"boundary\" while parsing sip message for recipient-list");
return text.substr (begin, end-begin);
}
std::string InstantMessaging::findTextMessage (std::string& text)
......@@ -328,12 +340,21 @@ std::string InstantMessaging::findTextMessage (std::string& text)
std::string boundary = "--boundary";
size_t pos = 0;
pos = text.find (ctype);
size_t begin = pos+ctype.size();
size_t begin = 0;
size_t end = 0;
// find the content type
if ( (pos = text.find (ctype)) == std::string::npos)
throw InstantMessageException ("Could not find Content-Type tag while parsing sip message for text");
// plain text content start after content type tag (plus \n\n)
begin = pos+ctype.size() +2;
size_t end = text.find (boundary, begin);
// retrive end of the text content
if ( (end = text.find (boundary, begin)) == std::string::npos)
throw InstantMessageException ("Could not find end of text \"boundary\" while parsing sip message for text");
return text.substr (begin+2, end-begin-3);
return text.substr (begin, end-begin-1);
}
......
......@@ -14,6 +14,7 @@
#include <map>
#include <list>
#include <exception>
#define EMPTY_MESSAGE pj_str((char*)"")
#define STR_TEXT pj_str((char*)"text")
......@@ -31,6 +32,23 @@ namespace sfl
const std::string IM_XML_URI ("uri");
const std::string BOUNDARY ("--boundary");
class InstantMessageException : public std::exception
{
public:
InstantMessageException (const std::string& str="") throw() : errstr (str) {}
virtual ~InstantMessageException() throw() {}
virtual const char *what() const throw() {
std::string expt ("YamlParserException occured: ");
expt.append (errstr);
return expt.c_str();
}
private:
std::string errstr;
};
class InstantMessaging
{
......
......@@ -3475,31 +3475,38 @@ void call_on_tsx_changed (pjsip_inv_session *inv UNUSED, pjsip_transaction *tsx,
pjsip_dlg_create_response (inv->dlg, r_data, PJSIP_SC_OK, NULL, &t_data);
pjsip_dlg_send_response (inv->dlg, tsx, t_data);
// retrive message from formated text
std::string message = imModule->findTextMessage (formatedMessage);
std::string message;
std::string urilist;
InstantMessaging::UriList list;
// retreive the recipient-list of this message
std::string urilist = imModule->findTextUriList (formatedMessage);
try {
// retrive message from formated text
message = imModule->findTextMessage (formatedMessage);
// parse the recipient list xml
InstantMessaging::UriList list = imModule->parseXmlUriList (urilist);
// retreive the recipient-list of this message
urilist = imModule->findTextUriList (formatedMessage);
// If no item present in the list, peer is considered as the sender
if (list.empty()) {
from = call->getPeerNumber ();
} else {
// InstaintMessaging::UriEntry *entry = static_cast<InstantMessaging::UriEntry *>(*iterItem);
// InstantMessaging::UriEntry::iterator iterAttr = entry->find(IM_XML_URI);
InstantMessaging::UriEntry *entry = list.front();
InstantMessaging::UriEntry::iterator iterAttr = entry->find (IM_XML_URI);
if (iterAttr->second != "Me")
from = iterAttr->second;
else
from = call->getPeerNumber ();
}
// parse the recipient list xml
list = imModule->parseXmlUriList (urilist);
// If no item present in the list, peer is considered as the sender
if (list.empty()) {
from = call->getPeerNumber ();
} else {
InstantMessaging::UriEntry *entry = list.front();
InstantMessaging::UriEntry::iterator iterAttr = entry->find (IM_XML_URI);
if (iterAttr->second != "Me")
from = iterAttr->second;
else
from = call->getPeerNumber ();
}
} catch (sfl::InstantMessageException &e) {
_error ("SipVoipLink: %s", e.what());
message = "";
from = call->getPeerNumber ();
}
// Pass through the instant messaging module if needed
// Right now, it does do anything.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment