Commit e6c421dd authored by Adrien Béraud's avatar Adrien Béraud Committed by Sébastien Blin

sip: avoid unneeded calls to strlen

std::string includes the string size.
Use that size to initialize pj_str_t instead of calling strlen.

Also make constexpr some static strings

Change-Id: Icc1cdd16ad17aa03c16f44aef10cbe949e16ff0b
Reviewed-by: Sébastien Blin's avatarSébastien Blin <sebastien.blin@savoirfairelinux.com>
parent 57adfd4f
......@@ -17,6 +17,7 @@
*/
#include "base64.h"
#include "sip/sip_utils.h"
#include <stdint.h>
#include <stdlib.h>
......@@ -54,8 +55,7 @@ std::vector<uint8_t>
decode(const std::string& str)
{
int output_length = PJ_BASE64_TO_BASE256_LEN(str.length());
pj_str_t input;
pj_strset(&input, (char*) &(*str.begin()), str.length());
const pj_str_t input(sip_utils::CONST_PJ_STR(str));
std::vector<uint8_t> out;
out.resize(output_length);
......
......@@ -238,9 +238,9 @@ add_turn_server(pj_pool_t& pool, pj_ice_strans_cfg& cfg, const TurnServerInfo& i
if (not info.password.empty()) {
turn.auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
turn.auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
pj_cstr(&turn.auth_cred.data.static_cred.realm, info.realm.c_str());
pj_cstr(&turn.auth_cred.data.static_cred.username, info.username.c_str());
pj_cstr(&turn.auth_cred.data.static_cred.data, info.password.c_str());
pj_strset(&turn.auth_cred.data.static_cred.realm, (char*)info.realm.c_str(), info.realm.size());
pj_strset(&turn.auth_cred.data.static_cred.username, (char*)info.username.c_str(), info.username.size());
pj_strset(&turn.auth_cred.data.static_cred.data, (char*)info.password.c_str(), info.password.size());
}
JAMI_DBG("[ice] added turn server '%s', port %d", pj_strbuf(&turn.server), turn.port);
......@@ -805,8 +805,8 @@ IceTransport::start(const Attribute& rem_attrs, const std::vector<IceCandidate>&
pj_str_t ufrag, pwd;
JAMI_DBG("[ice:%p] negotiation starting (%zu remote candidates)", this, rem_candidates.size());
auto status = pj_ice_strans_start_ice(pimpl_->icest_.get(),
pj_cstr(&ufrag, rem_attrs.ufrag.c_str()),
pj_cstr(&pwd, rem_attrs.pwd.c_str()),
pj_strset(&ufrag, (char*)rem_attrs.ufrag.c_str(), rem_attrs.ufrag.size()),
pj_strset(&pwd, (char*)rem_attrs.pwd.c_str(), rem_attrs.pwd.size()),
rem_candidates.size(),
rem_candidates.data());
if (status != PJ_SUCCESS) {
......
......@@ -162,7 +162,7 @@ im::sendSipMessage(pjsip_inv_session* session,
return;
}
const pjsip_method msg_method = {PJSIP_OTHER_METHOD, CONST_PJ_STR("MESSAGE")};
constexpr pjsip_method msg_method = {PJSIP_OTHER_METHOD, CONST_PJ_STR("MESSAGE")};
{
auto dialog = session->dlg;
......
......@@ -83,14 +83,13 @@ ip_utils::getAddrList(const std::string &name, pj_uint16_t family)
return ipList;
}
static const unsigned MAX_ADDR_NUM = 128;
static constexpr unsigned MAX_ADDR_NUM = 128;
pj_addrinfo res[MAX_ADDR_NUM];
unsigned addr_num = MAX_ADDR_NUM;
pj_str_t pjname;
pj_cstr(&pjname, name.c_str());
const pj_str_t pjname(sip_utils::CONST_PJ_STR(name));
auto status = pj_getaddrinfo(family, &pjname, &addr_num, res);
if (status != PJ_SUCCESS) {
JAMI_ERR("Error resolving %s : %s", name.c_str(),
JAMI_ERR("Error resolving %.*s : %s", (int)name.size(), name.c_str(),
sip_utils::sip_strerror(status).c_str());
return ipList;
}
......@@ -299,8 +298,7 @@ ip_utils::getLocalNameservers()
bool
IpAddr::isValid(const std::string &address, pj_uint16_t family)
{
pj_str_t pjstring;
pj_cstr(&pjstring, address.c_str());
const pj_str_t pjstring(sip_utils::CONST_PJ_STR(address));
pj_str_t ret_str;
pj_uint16_t ret_port;
int ret_family;
......
......@@ -122,8 +122,7 @@ public:
addr.addr.sa_family = AF_UNSPEC;
return;
}
pj_str_t pjstring;
pj_cstr(&pjstring, str.c_str());
const pj_str_t pjstring {(char*)str.c_str(), (pj_ssize_t)str.size()};
auto status = pj_sockaddr_parse(family, 0, &pjstring, &addr);
if (status != PJ_SUCCESS)
addr.addr.sa_family = AF_UNSPEC;
......
......@@ -194,7 +194,7 @@ PresSubClient::pres_client_evsub_on_state(pjsip_evsub *sub, pjsip_event *event)
* into pending state.
*/
const pjsip_sub_state_hdr *sub_hdr;
pj_str_t sub_state = CONST_PJ_STR("Subscription-State");
constexpr pj_str_t sub_state = CONST_PJ_STR("Subscription-State");
const pjsip_msg *msg;
msg = event->body.tsx_state.src.rdata->msg_info.msg;
......
......@@ -113,6 +113,10 @@ constexpr const pj_str_t CONST_PJ_STR(T (&a)[N]) noexcept {
return {const_cast<char*>(a), N-1};
}
inline const pj_str_t CONST_PJ_STR(const std::string& str) noexcept {
return {const_cast<char*>(str.c_str()), (pj_ssize_t)str.size()};
}
// PJSIP dialog locking in RAII way
// Usage: declare local variable like this: sip_utils::PJDialogLock lock {dialog};
// The lock is kept until the local variable is deleted
......
......@@ -995,7 +995,7 @@ SIPAccount::sendRegister()
pj_list_init(&hdr_list);
std::string useragent(getUserAgentName());
pj_str_t pJuseragent {(char*) useragent.data(), (pj_ssize_t) useragent.size()};
const pj_str_t STR_USER_AGENT = CONST_PJ_STR("User-Agent");
constexpr pj_str_t STR_USER_AGENT = CONST_PJ_STR("User-Agent");
pjsip_generic_string_hdr *h = pjsip_generic_string_hdr_create(link_->getPool(), &STR_USER_AGENT, &pJuseragent);
pj_list_push_back(&hdr_list, (pjsip_hdr*) h);
......@@ -1210,10 +1210,10 @@ void SIPAccount::initTlsConfiguration()
trimCiphers();
pj_cstr(&tlsSetting_.ca_list_file, tlsCaListFile_.c_str());
pj_cstr(&tlsSetting_.cert_file, tlsCertificateFile_.c_str());
pj_cstr(&tlsSetting_.privkey_file, tlsPrivateKeyFile_.c_str());
pj_cstr(&tlsSetting_.password, tlsPassword_.c_str());
pj_strset(&tlsSetting_.ca_list_file, (char*)tlsCaListFile_.c_str(), tlsCaListFile_.size());
pj_strset(&tlsSetting_.cert_file, (char*)tlsCertificateFile_.c_str(), tlsCertificateFile_.size());
pj_strset(&tlsSetting_.privkey_file, (char*)tlsPrivateKeyFile_.c_str(), tlsPrivateKeyFile_.size());
pj_strset(&tlsSetting_.password, (char*)tlsPassword_.c_str(), tlsPassword_.size());
JAMI_DBG("Using %zu ciphers", ciphers_.size());
tlsSetting_.ciphers_num = ciphers_.size();
......@@ -2071,7 +2071,7 @@ SIPAccount::sendTextMessage(const std::string& to, const std::map<std::string, s
auto toUri = getToUri(to);
const pjsip_method msg_method = {PJSIP_OTHER_METHOD, CONST_PJ_STR("MESSAGE")};
constexpr pjsip_method msg_method = {PJSIP_OTHER_METHOD, CONST_PJ_STR("MESSAGE")};
std::string from(getFromUri());
pj_str_t pjFrom = pj_str((char*) from.c_str());
pj_str_t pjTo = pj_str((char*) toUri.c_str());
......
......@@ -76,7 +76,7 @@ SIPAccountBase::CreateClientDialogAndInvite(const pj_str_t* from,
sip_utils::PJDialogLock dlg_lock {dialog};
// Append "Subject: Phone Call" header
auto subj_hdr_name = sip_utils::CONST_PJ_STR("Subject");
constexpr auto subj_hdr_name = sip_utils::CONST_PJ_STR("Subject");
auto subj_hdr = reinterpret_cast<pjsip_hdr*>(pjsip_parse_hdr(dialog->pool,
&subj_hdr_name,
const_cast<char *>("Phone call"),
......
......@@ -243,7 +243,7 @@ SIPCall::sendSIPInfo(const char *const body, const char *const subtype)
throw VoipLinkException("Couldn't get invite dialog");
pj_str_t methodName = CONST_PJ_STR("INFO");
const pj_str_t type = CONST_PJ_STR("application");
constexpr pj_str_t type = CONST_PJ_STR("application");
pjsip_method method;
pjsip_method_init_np(&method, &methodName);
......@@ -271,7 +271,7 @@ SIPCall::sendSIPInfo(const char *const body, const char *const subtype)
void
SIPCall::requestKeyframe()
{
const char * const BODY =
constexpr const char * const BODY =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
"<media_control><vc_primitive><to_encoder>"
"<picture_fast_update/>"
......@@ -485,7 +485,7 @@ transfer_client_cb(pjsip_evsub *sub, pjsip_event *event)
}
bool
SIPCall::transferCommon(pj_str_t *dst)
SIPCall::transferCommon(const pj_str_t *dst)
{
if (not inv or not inv->dlg)
return false;
......@@ -529,11 +529,8 @@ SIPCall::transfer(const std::string& to)
if (Recordable::isRecording())
stopRecording();
std::string toUri;
pj_str_t dst = { 0, 0 };
toUri = account.getToUri(to);
pj_cstr(&dst, toUri.c_str());
std::string toUri = account.getToUri(to);
const pj_str_t dst(CONST_PJ_STR(toUri));
JAMI_DBG("[call:%s] Transferring to %.*s", getCallId().c_str(), (int)dst.slen, dst.ptr);
if (!transferCommon(&dst))
......
......@@ -249,7 +249,7 @@ private:
/**
* Transfer method used for both type of transfer
*/
bool transferCommon(pj_str_t *dst);
bool transferCommon(const pj_str_t *dst);
bool internalOffHold(const std::function<void()> &SDPUpdateFunc);
......
......@@ -132,7 +132,7 @@ void SIPPresence::updateStatus(bool status, const std::string &note)
PJRPID_ELEMENT_TYPE_PERSON,
CONST_PJ_STR("0"),
PJRPID_ACTIVITY_UNKNOWN,
pj_str((char *) note.c_str())
CONST_PJ_STR(note)
};
/* fill activity if user not available. */
......@@ -299,7 +299,7 @@ void SIPPresence::fillDoc(pjsip_tx_data *tdata, const pres_msg_data *msg_data)
{
if (tdata->msg->type == PJSIP_REQUEST_MSG) {
const pj_str_t STR_USER_AGENT = CONST_PJ_STR("User-Agent");
constexpr pj_str_t STR_USER_AGENT = CONST_PJ_STR("User-Agent");
std::string useragent(acc_->getUserAgentName());
pj_str_t pJuseragent = pj_str((char*) useragent.c_str());
pjsip_hdr *h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool, &STR_USER_AGENT, &pJuseragent);
......@@ -322,8 +322,8 @@ void SIPPresence::fillDoc(pjsip_tx_data *tdata, const pres_msg_data *msg_data)
if (msg_data->content_type.slen && msg_data->msg_body.slen) {
pjsip_msg_body *body;
const pj_str_t type = CONST_PJ_STR("application");
const pj_str_t subtype = CONST_PJ_STR("pidf+xml");
constexpr pj_str_t type = CONST_PJ_STR("application");
constexpr pj_str_t subtype = CONST_PJ_STR("pidf+xml");
body = pjsip_msg_body_create(tdata->pool, &type, &subtype, &msg_data->msg_body);
tdata->msg->body = body;
}
......@@ -479,7 +479,7 @@ pj_status_t
SIPPresence::publish(SIPPresence *pres)
{
pj_status_t status;
const pj_str_t STR_PRESENCE = CONST_PJ_STR("presence");
constexpr pj_str_t STR_PRESENCE = CONST_PJ_STR("presence");
SIPAccount * acc = pres->getAccount();
pjsip_endpoint *endpt = getSIPVoIPLink()->getEndpoint();
......
......@@ -575,7 +575,7 @@ SIPVoIPLink::SIPVoIPLink() : pool_(nullptr, pj_pool_release)
};
TRY(pjsip_inv_usage_init(endpt_, &inv_cb));
static const pj_str_t allowed[] = {
static constexpr pj_str_t allowed[] = {
CONST_PJ_STR("INFO"),
CONST_PJ_STR("OPTIONS"),
CONST_PJ_STR("MESSAGE"),
......@@ -584,13 +584,13 @@ SIPVoIPLink::SIPVoIPLink() : pool_(nullptr, pj_pool_release)
pjsip_endpt_add_capability(endpt_, &mod_ua_, PJSIP_H_ALLOW, nullptr, PJ_ARRAY_SIZE(allowed), allowed);
static const pj_str_t text_plain = CONST_PJ_STR("text/plain");
static constexpr pj_str_t text_plain = CONST_PJ_STR("text/plain");
pjsip_endpt_add_capability(endpt_, &mod_ua_, PJSIP_H_ACCEPT, nullptr, 1, &text_plain);
static const pj_str_t accepted = CONST_PJ_STR("application/sdp");
static constexpr pj_str_t accepted = CONST_PJ_STR("application/sdp");
pjsip_endpt_add_capability(endpt_, &mod_ua_, PJSIP_H_ACCEPT, nullptr, 1, &accepted);
static const pj_str_t iscomposing = CONST_PJ_STR("application/im-iscomposing+xml");
static constexpr pj_str_t iscomposing = CONST_PJ_STR("application/im-iscomposing+xml");
pjsip_endpt_add_capability(endpt_, &mod_ua_, PJSIP_H_ACCEPT, nullptr, 1, &iscomposing);
TRY(pjsip_replaces_init_module(endpt_));
......@@ -946,8 +946,8 @@ handleMediaControl(SIPCall& call, pjsip_msg_body* body)
/*
* Incoming INFO request for media control.
*/
const pj_str_t STR_APPLICATION = CONST_PJ_STR("application");
const pj_str_t STR_MEDIA_CONTROL_XML = CONST_PJ_STR("media_control+xml");
constexpr pj_str_t STR_APPLICATION = CONST_PJ_STR("application");
constexpr pj_str_t STR_MEDIA_CONTROL_XML = CONST_PJ_STR("media_control+xml");
if (body->len and pj_stricmp(&body->content_type.type, &STR_APPLICATION) == 0 and
pj_stricmp(&body->content_type.subtype, &STR_MEDIA_CONTROL_XML) == 0) {
......@@ -955,8 +955,8 @@ handleMediaControl(SIPCall& call, pjsip_msg_body* body)
/* Apply and answer the INFO request */
pj_strset(&control_st, (char *) body->data, body->len);
const pj_str_t PICT_FAST_UPDATE = CONST_PJ_STR("picture_fast_update");
const pj_str_t DEVICE_ORIENTATION = CONST_PJ_STR("device_orientation");
constexpr pj_str_t PICT_FAST_UPDATE = CONST_PJ_STR("picture_fast_update");
constexpr pj_str_t DEVICE_ORIENTATION = CONST_PJ_STR("device_orientation");
if (pj_strstr(&control_st, &PICT_FAST_UPDATE)) {
call.sendKeyframe();
......@@ -1012,7 +1012,7 @@ replyToRequest(pjsip_inv_session* inv, pjsip_rx_data* rdata, int status_code)
static void
onRequestRefer(pjsip_inv_session* inv, pjsip_rx_data* rdata, pjsip_msg* msg, SIPCall& call)
{
static const pj_str_t str_refer_to = CONST_PJ_STR("Refer-To");
static constexpr pj_str_t str_refer_to = CONST_PJ_STR("Refer-To");
if (auto refer_to = static_cast<pjsip_generic_string_hdr*>(pjsip_msg_find_hdr_by_name(msg, &str_refer_to, nullptr))) {
// RFC 3515, 2.4.2: reply bad request if no or too many refer-to header.
......@@ -1244,8 +1244,7 @@ SIPVoIPLink::findLocalAddressFromTransport(pjsip_transport* transport,
"Transport manager is NULL in findLocalAddress, using local address %s :%d",
addr.c_str(), port);
pj_str_t pjstring;
pj_cstr(&pjstring, host.c_str());
const pj_str_t pjstring(CONST_PJ_STR(host));
auto tp_sel = getTransportSelector(transport);
pjsip_tpmgr_fla2_param param = { transportType, &tp_sel, pjstring, PJ_FALSE,
......
......@@ -346,10 +346,10 @@ TurnTransport::TurnTransport(const TurnTransportParams& params)
pj_stun_auth_cred cred;
pj_bzero(&cred, sizeof(cred));
cred.type = PJ_STUN_AUTH_CRED_STATIC;
pj_cstr(&cred.data.static_cred.realm, pimpl_->settings.realm.c_str());
pj_cstr(&cred.data.static_cred.username, pimpl_->settings.username.c_str());
pj_strset(&cred.data.static_cred.realm, (char*)pimpl_->settings.realm.c_str(), pimpl_->settings.realm.size());
pj_strset(&cred.data.static_cred.username, (char*)pimpl_->settings.username.c_str(), pimpl_->settings.username.size());
cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
pj_cstr(&cred.data.static_cred.data, pimpl_->settings.password.c_str());
pj_strset(&cred.data.static_cred.data, (char*)pimpl_->settings.password.c_str(), pimpl_->settings.password.size());
pimpl_->relayAddr = pj_strdup3(pimpl_->pool, server.toString().c_str());
......
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