Commit 47312812 authored by Tristan Matthews's avatar Tristan Matthews

* #34663: sip: store contact header as pj_str

parent 9ba2b1bd
......@@ -112,8 +112,7 @@ PresSubServer::pres_on_rx_subscribe_request(pjsip_rx_data *rdata)
pres->lock();
/* Create UAS dialog: */
std::string c(acc->getContactHeader());
const pj_str_t contact = pj_str((char*) c.c_str());
const pj_str_t contact(acc->getContactHeader());
status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, &contact, &dlg);
if (status != PJ_SUCCESS) {
......
......@@ -208,13 +208,11 @@ sip_utils::getIPList(const std::string &name)
}
void
sip_utils::addContactHeader(const std::string &contactStr, pjsip_tx_data *tdata)
sip_utils::addContactHeader(const pj_str_t *contact_str, pjsip_tx_data *tdata)
{
pj_str_t pjContact = pj_str((char*) contactStr.c_str());
pjsip_contact_hdr *contact = pjsip_contact_hdr_create(tdata->pool);
contact->uri = pjsip_parse_uri(tdata->pool, pjContact.ptr,
pjContact.slen, PJSIP_PARSE_URI_AS_NAMEADDR);
contact->uri = pjsip_parse_uri(tdata->pool, contact_str->ptr,
contact_str->slen, PJSIP_PARSE_URI_AS_NAMEADDR);
// remove old contact header (if present)
pjsip_msg_find_remove_hdr(tdata->msg, PJSIP_H_CONTACT, NULL);
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) contact);
......
......@@ -57,7 +57,7 @@ namespace sip_utils {
std::vector<std::string> getIPList(const std::string &name);
void addContactHeader(const std::string &contactStr, pjsip_tx_data *tdata);
void addContactHeader(const pj_str_t *contactStr, pjsip_tx_data *tdata);
}
#endif // SIP_UTILS_H_
......@@ -128,7 +128,8 @@ SIPAccount::SIPAccount(const std::string& accountID, bool presenceEnabled)
, receivedParameter_("")
, rPort_(-1)
, via_addr_()
, contact_()
, contactBuffer_()
, contact_{contactBuffer_, 0}
, contactRewriteMethod_(2)
, allowViaRewrite_(true)
, allowContactRewrite_(true)
......@@ -1181,7 +1182,8 @@ std::string SIPAccount::getServerUri() const
}
std::string SIPAccount::getContactHeader() const
pj_str_t
SIPAccount::getContactHeader()
{
if (transport_ == NULL)
ERROR("Transport not created yet");
......@@ -1226,9 +1228,17 @@ std::string SIPAccount::getContactHeader() const
} else
scheme = "sip:";
return displayName_ + (displayName_.empty() ? "" : " ") + "<" +
scheme + username_ + (username_.empty() ? "" : "@") +
address + ":" + port + transport + ">";
contact_.slen = pj_ansi_snprintf(contact_.ptr, PJSIP_MAX_URL_SIZE,
"%s%s<%s%s%s%s:%s%s>",
displayName_.c_str(),
(displayName_.empty() ? "" : " "),
scheme.c_str(),
username_.c_str(),
(username_.empty() ? "" : "@"),
address.c_str(),
port.c_str(),
transport.c_str());
return contact_;
}
......@@ -1641,7 +1651,7 @@ SIPAccount::checkNATAddress(pjsip_regc_cbparam *param, pj_pool_t *pool)
return false;
/* Compare received and rport with the URI in our registration */
const pj_str_t STR_CONTACT = { "Contact", 7 };
const pj_str_t STR_CONTACT = { (char*) "Contact", 7 };
pjsip_contact_hdr *contact_hdr = (pjsip_contact_hdr*)
pjsip_parse_hdr(pool, &STR_CONTACT, contact_.ptr, contact_.slen, NULL);
pj_assert(contact_hdr != NULL);
......@@ -1773,14 +1783,23 @@ SIPAccount::checkNATAddress(pjsip_regc_cbparam *param, pj_pool_t *pool)
return false;
}
pj_strdup2_with_null(pool, &contact_, tmp);
pj_str_t tmp_str = {tmp, len};
pj_strncpy_with_null(&contact_, &tmp_str, PJSIP_MAX_URL_SIZE);
}
if (contactRewriteMethod_ == 2 && regc_ != NULL)
pjsip_regc_update_contact(regc_, 1, &contact_);
#if 0
/* TODO: Perform new registration */
//pjsua_acc_set_registration(acc->index, PJ_TRUE);
/* Perform new registration */
try {
link_->sendRegister(this);
} catch (const VoipLinkException &e) {
ERROR("%s", e.what());
}
#endif
return true;
......
......@@ -399,7 +399,7 @@ class SIPAccount : public Account {
* Get the contact header for
* @return pj_str_t The contact header based on account information
*/
std::string getContactHeader() const;
pj_str_t getContactHeader();
/**
* Get the local interface name on which this account is bound.
......@@ -828,10 +828,12 @@ class SIPAccount : public Account {
*/
pjsip_host_port via_addr_;
char contactBuffer_[PJSIP_MAX_URL_SIZE];
pj_str_t contact_;
int contactRewriteMethod_;
bool allowViaRewrite_;
bool allowContactRewrite_;
/* Undocumented feature in pjsip, this can == 2 */
int allowContactRewrite_;
pjsip_transport *via_tp_;
/*
......@@ -855,7 +857,6 @@ class SIPAccount : public Account {
static bool portsInUse_[HALF_MAX_PORT];
static uint16_t getRandomEvenNumber(const std::pair<uint16_t, uint16_t> &range);
};
#endif
......@@ -56,7 +56,8 @@ SIPCall::SIPCall(const std::string& id, Call::CallType type,
#endif
, pool_(pj_pool_create(&caching_pool->factory, id.c_str(), INITIAL_SIZE, INCREMENT_SIZE, NULL))
, local_sdp_(new Sdp(pool_))
, contactHeader_()
, contactBuffer_()
, contactHeader_{contactBuffer_, 0}
{}
SIPCall::~SIPCall()
......@@ -65,9 +66,9 @@ SIPCall::~SIPCall()
pj_pool_release(pool_);
}
void SIPCall::setContactHeader(const std::string &contact)
void SIPCall::setContactHeader(pj_str_t *contact)
{
contactHeader_ = contact;
pj_strcpy(&contactHeader_, contact);
}
void SIPCall::answer()
......@@ -81,9 +82,9 @@ void SIPCall::answer()
throw std::runtime_error("Could not init invite request answer (200 OK)");
// contactStr must stay in scope as long as tdata
if (not contactHeader_.empty()) {
DEBUG("Answering with contact header: %s", contactHeader_.c_str());
sip_utils::addContactHeader(contactHeader_, tdata);
if (contactHeader_.slen == 0) {
DEBUG("Answering with contact header: %.*s", contactHeader_.slen, contactHeader_.ptr);
sip_utils::addContactHeader(&contactHeader_, tdata);
}
if (pjsip_inv_send_msg(inv, tdata) != PJ_SUCCESS)
......
......@@ -44,6 +44,8 @@
#include "noncopyable.h"
#include "pjsip/sip_config.h"
struct pjsip_evsub;
struct pj_caching_pool;
struct pj_pool_t;
......@@ -108,7 +110,7 @@ class SIPCall : public Call {
*/
pjsip_inv_session *inv;
void setContactHeader(const std::string &contact);
void setContactHeader(pj_str_t *contact);
private:
// override of Call::getDetails
......@@ -143,7 +145,8 @@ class SIPCall : public Call {
*/
Sdp *local_sdp_;
std::string contactHeader_;
char contactBuffer_[PJSIP_MAX_URL_SIZE];
pj_str_t contactHeader_;
};
#endif // __SIPCALL_H__
......@@ -476,8 +476,8 @@ pj_bool_t transaction_request_cb(pjsip_rx_data *rdata)
}
// contactStr must stay in scope as long as tdata
const std::string contactStr(account->getContactHeader());
sip_utils::addContactHeader(contactStr, tdata);
const pj_str_t contactStr(account->getContactHeader());
sip_utils::addContactHeader(&contactStr, tdata);
if (pjsip_inv_send_msg(call->inv, tdata) != PJ_SUCCESS) {
ERROR("Could not send msg for invite");
......@@ -776,9 +776,7 @@ void SIPVoIPLink::sendRegister(Account *a)
std::string received(account->getReceivedParameter());
// Get the contact header
std::string contact = account->getContactHeader();
pj_str_t pjContact = pj_str((char*) contact.c_str());
const pj_str_t pjContact(account->getContactHeader());
if (account->transport_) {
if (account->isStunEnabled()) {
......@@ -1090,7 +1088,8 @@ SIPVoIPLink::answer(Call *call)
updateSDPFromSTUN(*sipCall, *account, SIPVoIPLink::instance()->sipTransport);
}
sipCall->setContactHeader(account->getContactHeader());
pj_str_t contact(account->getContactHeader());
sipCall->setContactHeader(&contact);
sipCall->answer();
}
......@@ -1152,8 +1151,8 @@ SIPVoIPLink::hangup(const std::string& id, int reason)
return;
// contactStr must stay in scope as long as tdata
const std::string contactStr(account->getContactHeader());
sip_utils::addContactHeader(contactStr, tdata);
const pj_str_t contactStr(account->getContactHeader());
sip_utils::addContactHeader(&contactStr, tdata);
if (pjsip_inv_send_msg(inv, tdata) != PJ_SUCCESS)
return;
......@@ -1683,9 +1682,7 @@ SIPVoIPLink::SIPStartCall(SIPCall *call)
std::string from(account->getFromUri());
pj_str_t pjFrom = pj_str((char*) from.c_str());
// Get the contact header
std::string contact(account->getContactHeader());
pj_str_t pjContact = pj_str((char*) contact.c_str());
pj_str_t pjContact(account->getContactHeader());
pjsip_dialog *dialog = NULL;
......@@ -2333,7 +2330,7 @@ void registration_cb(pjsip_regc_cbparam *param)
*/
// update_rfc5626_status(acc, param->rdata);
checkNatAddress(*param, *account);
account->checkNATAddress(param, pool_);
/* TODO Check and update Service-Route header */
//update_service_route(acc, param->rdata);
......
Markdown is supported
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