Skip to content
Snippets Groups Projects
Commit 6a06c6fa authored by Alexandre Savard's avatar Alexandre Savard
Browse files

#9830: move sip header parsing function in sip_utils

parent 7fbf9713
No related branches found
No related tags found
No related merge requests found
......@@ -29,9 +29,20 @@
* as that of the covered work.
*/
#include <pjsip.h>
#include <pjlib.h>
#include <pjsip_ua.h>
#include <pjlib-util.h>
#include <pjnath.h>
#include <pjnath/stun_config.h>
#include <pj/string.h>
#include <pjsip/sip_msg.h>
#include <pjsip/sip_types.h>
#include <pjsip/sip_uri.h>
#include <pj/list.h>
#include "sip_utils.h"
#include "pj/string.h"
#include "pjsip/sip_msg.h"
std::string
sip_utils::fetchHeaderValue(pjsip_msg *msg, const std::string &field)
......@@ -51,3 +62,66 @@ sip_utils::fetchHeaderValue(pjsip_msg *msg, const std::string &field)
else
return "";
}
pjsip_route_hdr *
sip_utils::createRouteSet(const std::string &route, pj_pool_t *hdr_pool)
{
int port = 0;
std::string host;
size_t found = route.find(":");
if (found != std::string::npos) {
host = route.substr(0, found);
port = atoi(route.substr(found + 1, route.length()).c_str());
} else
host = route;
pjsip_route_hdr *route_set = pjsip_route_hdr_create(hdr_pool);
pjsip_route_hdr *routing = pjsip_route_hdr_create(hdr_pool);
pjsip_sip_uri *url = pjsip_sip_uri_create(hdr_pool, 0);
routing->name_addr.uri = (pjsip_uri*) url;
pj_strdup2(hdr_pool, &url->host, host.c_str());
url->port = port;
pj_list_push_back(route_set, pjsip_hdr_clone(hdr_pool, routing));
return route_set;
}
std::string
sip_utils::parseDisplayName(const char * buffer)
{
const char* from_header = strstr(buffer, "From: ");
if (!from_header)
return "";
std::string temp(from_header);
size_t begin_displayName = temp.find("\"") + 1;
size_t end_displayName = temp.rfind("\"");
std::string displayName(temp.substr(begin_displayName, end_displayName - begin_displayName));
static const size_t MAX_DISPLAY_NAME_SIZE = 25;
if (displayName.size() > MAX_DISPLAY_NAME_SIZE)
return "";
return displayName;
}
void
sip_utils::stripSipUriPrefix(std::string& sipUri)
{
// Remove sip: prefix
static const char SIP_PREFIX[] = "sip:";
size_t found = sipUri.find(SIP_PREFIX);
if (found != std::string::npos)
sipUri.erase(found, found + (sizeof SIP_PREFIX) - 1);
found = sipUri.find("@");
if (found != std::string::npos)
sipUri.erase(found);
}
......@@ -34,6 +34,8 @@
#include <string>
#include <pjsip/sip_msg.h>
class pjsip_msg;
namespace sip_utils {
......@@ -42,6 +44,12 @@ namespace sip_utils {
* @return Header from SIP message
*/
std::string fetchHeaderValue(pjsip_msg *msg, const std::string &field);
pjsip_route_hdr *createRouteSet(const std::string &route, pj_pool_t *hdr_pool);
void stripSipUriPrefix(std::string& sipUri);
std::string parseDisplayName(const char * buffer);
}
#endif // SIP_UTILS_H_
......@@ -35,6 +35,8 @@
#include "config.h"
#endif
#include "sip_utils.h"
#include "sipvoiplink.h"
#include "manager.h"
#include "logger.h"
......@@ -115,7 +117,7 @@ int SIPSessionReinvite(SIPCall *);
* Helper function to process refer function on call transfer
*/
void onCallTransfered(pjsip_inv_session *inv, pjsip_rx_data *rdata);
/*
pjsip_route_hdr *createRouteSet(const std::string &route, pj_pool_t *hdr_pool)
{
int port = 0;
......@@ -140,6 +142,7 @@ pjsip_route_hdr *createRouteSet(const std::string &route, pj_pool_t *hdr_pool)
return route_set;
}
*/
void handleIncomingOptions(pjsip_rx_data *rdata)
{
......@@ -192,6 +195,7 @@ pj_bool_t transaction_response_cb(pjsip_rx_data *rdata)
return PJ_SUCCESS;
}
/*
std::string parseDisplayName(const char * buffer)
{
const char* from_header = strstr(buffer, "From: ");
......@@ -225,6 +229,7 @@ void stripSipUriPrefix(std::string& sipUri)
if (found != std::string::npos)
sipUri.erase(found);
}
*/
pj_bool_t transaction_request_cb(pjsip_rx_data *rdata)
{
......@@ -239,7 +244,7 @@ pj_bool_t transaction_request_cb(pjsip_rx_data *rdata)
std::string server(sip_from_uri->host.ptr, sip_from_uri->host.slen);
std::string account_id(Manager::instance().getAccountIdFromNameAndServer(userName, server));
std::string displayName(parseDisplayName(rdata->msg_info.msg_buf));
std::string displayName(sip_utils::parseDisplayName(rdata->msg_info.msg_buf));
if (method->id == PJSIP_OTHER_METHOD) {
pj_str_t *str = &method->name;
......@@ -308,7 +313,7 @@ pj_bool_t transaction_request_cb(pjsip_rx_data *rdata)
char tmp[PJSIP_MAX_URL_SIZE];
int length = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, sip_from_uri, tmp, PJSIP_MAX_URL_SIZE);
std::string peerNumber(tmp, length);
stripSipUriPrefix(peerNumber);
sip_utils::stripSipUriPrefix(peerNumber);
call->setConnectionState(Call::PROGRESSING);
call->setPeerNumber(peerNumber);
......@@ -568,7 +573,7 @@ void SIPVoIPLink::sendRegister(Account *a)
throw VoipLinkException("Unable to initialize account registration structure");
if (!account->getServiceRoute().empty())
pjsip_regc_set_route_set(regc, createRouteSet(account->getServiceRoute(), pool_));
pjsip_regc_set_route_set(regc, sip_utils::createRouteSet(account->getServiceRoute(), pool_));
pjsip_regc_set_credentials(regc, account->getCredentialCount(), account->getCredInfo());
......@@ -757,7 +762,7 @@ SIPVoIPLink::hangup(const std::string& id)
// Looks for sip routes
if (not account->getServiceRoute().empty()) {
pjsip_route_hdr *route_set = createRouteSet(account->getServiceRoute(), inv->pool);
pjsip_route_hdr *route_set = sip_utils::createRouteSet(account->getServiceRoute(), inv->pool);
pjsip_dlg_set_route_set(inv->dlg, route_set);
}
......@@ -1083,7 +1088,7 @@ SIPVoIPLink::SIPStartCall(SIPCall *call)
return false;
if (not account->getServiceRoute().empty())
pjsip_dlg_set_route_set(dialog, createRouteSet(account->getServiceRoute(), call->inv->pool));
pjsip_dlg_set_route_set(dialog, sip_utils::createRouteSet(account->getServiceRoute(), call->inv->pool));
pjsip_auth_clt_set_credentials(&dialog->auth_sess, account->getCredentialCount(), account->getCredInfo());
......
......@@ -13,6 +13,7 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment