Skip to content
Snippets Groups Projects
Commit 721347e5 authored by Ming Rui Zhang's avatar Ming Rui Zhang
Browse files

sipaccount: use proxy server address when it is specified

Note: pjsip auto registration does not use the outbound proxy that
      we set, so, need to use our keep alive mechanism instead

Gitlab: #346
Change-Id: Ibf97c6fa5d94dc5b4d1345afeeb0db7084094c78
parent 26694c15
No related branches found
No related tags found
No related merge requests found
......@@ -87,6 +87,7 @@ using yaml_utils::parseValue;
using yaml_utils::parseVectorMap;
using sip_utils::CONST_PJ_STR;
static constexpr int KEEP_ALIVE_DELAY = 10; // seconds
static constexpr int MIN_REGISTRATION_TIME = 60; // seconds
static constexpr unsigned DEFAULT_REGISTRATION_TIME = 3600; // seconds
static constexpr unsigned REGISTRATION_FIRST_RETRY_INTERVAL = 60; // seconds
......@@ -222,7 +223,7 @@ SIPAccount::newOutgoingCall(std::string_view toUrl,
// FIXME : for now, use the same address family as the SIP transport
family = pjsip_transport_type_get_af(getTransportType());
JAMI_DBG("UserAgent: New registered account call to %.*s", (int)toUrl.size(), toUrl.data());
JAMI_DBG("UserAgent: New registered account call to %.*s", (int) toUrl.size(), toUrl.data());
}
auto toUri = getToUri(to);
......@@ -408,6 +409,8 @@ SIPAccount::SIPStartCall(std::shared_ptr<SIPCall>& call)
return false;
}
setUpTransmissionData(tdata, tp_sel.u.transport->key.type);
// Add user-agent header
sip_utils::addUserAgenttHeader(getUserAgentName(), tdata);
......@@ -908,8 +911,6 @@ SIPAccount::doUnregister(std::function<void(bool)> released_cb)
std::unique_lock<std::mutex> lock(configurationMutex_);
tlsListener_.reset();
if (transport_)
setTransport();
if (!isIP2IP()) {
try {
......@@ -918,6 +919,9 @@ SIPAccount::doUnregister(std::function<void(bool)> released_cb)
JAMI_ERR("doUnregister %s", e.what());
}
}
if (transport_)
setTransport();
resetAutoRegistration();
lock.unlock();
......@@ -968,10 +972,10 @@ SIPAccount::startKeepAliveTimer()
pj_time_val keepAliveDelay_;
if (registrationExpire_ == 0) {
JAMI_DBG("Registration Expire: 0, taking 60 instead");
keepAliveDelay_.sec = 3600;
keepAliveDelay_.sec = MIN_REGISTRATION_TIME;
} else {
JAMI_DBG("Registration Expire: %d", registrationExpire_);
keepAliveDelay_.sec = registrationExpire_ + MIN_REGISTRATION_TIME;
keepAliveDelay_.sec = registrationExpire_ + KEEP_ALIVE_DELAY;
}
keepAliveDelay_.msec = 0;
keepAliveTimerActive_ = true;
......@@ -988,7 +992,7 @@ SIPAccount::stopKeepAliveTimer()
keepAliveTimerActive_ = false;
link_.cancelKeepAliveTimer(keepAliveTimer_);
if (keepAliveTimer_.user_data) {
delete ((std::weak_ptr<SIPAccount>*)keepAliveTimer_.user_data);
delete ((std::weak_ptr<SIPAccount>*) keepAliveTimer_.user_data);
keepAliveTimer_.user_data = nullptr;
}
}
......@@ -1070,26 +1074,18 @@ SIPAccount::sendRegister()
pjsip_regc_add_headers(regc, &hdr_list);
pjsip_tx_data* tdata;
if (pjsip_regc_register(regc, PJ_TRUE, &tdata) != PJ_SUCCESS)
if (pjsip_regc_register(regc, PJ_FALSE, &tdata) != PJ_SUCCESS)
throw VoipLinkException("Unable to initialize transaction data for account registration");
const pjsip_tpselector tp_sel = getTransportSelector();
if (pjsip_regc_set_transport(regc, &tp_sel) != PJ_SUCCESS)
throw VoipLinkException("Unable to set transport");
if (hostIp_) {
auto ai = &tdata->dest_info;
ai->name = pj_strdup3(tdata->pool, hostname_.c_str());
ai->addr.count = 1;
ai->addr.entry[0].type = (pjsip_transport_type_e) tp_sel.u.transport->key.type;
pj_memcpy(&ai->addr.entry[0].addr, hostIp_.pjPtr(), sizeof(pj_sockaddr));
ai->addr.entry[0].addr_len = hostIp_.getLength();
ai->cur_addr = 0;
}
setUpTransmissionData(tdata, tp_sel.u.transport->key.type);
// pjsip_regc_send increment the transport ref count by one,
if ((status = pjsip_regc_send(regc, tdata)) != PJ_SUCCESS) {
JAMI_ERR("pjsip_regc_init failed with error %d: %s",
JAMI_ERR("pjsip_regc_send failed with error %d: %s",
status,
sip_utils::sip_strerror(status).c_str());
throw VoipLinkException("Unable to send account registration request");
......@@ -1098,6 +1094,31 @@ SIPAccount::sendRegister()
setRegistrationInfo(regc);
}
void
SIPAccount::setUpTransmissionData(pjsip_tx_data* tdata, long transportKeyType)
{
if (hasServiceRoute()) {
IpAddr proxyServer {getServiceRoute()};
if (proxyServer.getPort() == 0)
proxyServer.setPort(sip_utils::DEFAULT_SIP_PORT);
auto ai = &tdata->dest_info;
ai->name = pj_strdup3(tdata->pool, hostname_.c_str());
ai->addr.count = 1;
ai->addr.entry[0].type = (pjsip_transport_type_e) transportKeyType;
pj_memcpy(&ai->addr.entry[0].addr, proxyServer.pjPtr(), sizeof(pj_sockaddr));
ai->addr.entry[0].addr_len = proxyServer.getLength();
ai->cur_addr = 0;
} else if (hostIp_) {
auto ai = &tdata->dest_info;
ai->name = pj_strdup3(tdata->pool, hostname_.c_str());
ai->addr.count = 1;
ai->addr.entry[0].type = (pjsip_transport_type_e) transportKeyType;
pj_memcpy(&ai->addr.entry[0].addr, hostIp_.pjPtr(), sizeof(pj_sockaddr));
ai->addr.entry[0].addr_len = hostIp_.getLength();
ai->cur_addr = 0;
}
}
void
SIPAccount::onRegister(pjsip_regc_cbparam* param)
{
......@@ -1208,6 +1229,12 @@ SIPAccount::sendUnregister()
if (pjsip_regc_unregister(regc, &tdata) != PJ_SUCCESS)
throw VoipLinkException("Unable to unregister sip account");
const pjsip_tpselector tp_sel = getTransportSelector();
if (pjsip_regc_set_transport(regc, &tp_sel) != PJ_SUCCESS)
throw VoipLinkException("Unable to set transport");
setUpTransmissionData(tdata, tp_sel.u.transport->key.type);
pj_status_t status;
if ((status = pjsip_regc_send(regc, tdata)) != PJ_SUCCESS) {
JAMI_ERR("pjsip_regc_send failed with error %d: %s",
......@@ -1807,17 +1834,25 @@ SIPAccount::matches(std::string_view userName, std::string_view server) const
{
if (fullMatch(userName, server)) {
JAMI_DBG("Matching account id in request is a fullmatch %.*s@%.*s",
(int)userName.size(), userName.data(),
(int)server.size(), server.data());
(int) userName.size(),
userName.data(),
(int) server.size(),
server.data());
return MatchRank::FULL;
} else if (hostnameMatch(server)) {
JAMI_DBG("Matching account id in request with hostname %.*s", (int)server.size(), server.data());
JAMI_DBG("Matching account id in request with hostname %.*s",
(int) server.size(),
server.data());
return MatchRank::PARTIAL;
} else if (userMatch(userName)) {
JAMI_DBG("Matching account id in request with username %.*s", (int)userName.size(), userName.data());
JAMI_DBG("Matching account id in request with username %.*s",
(int) userName.size(),
userName.data());
return MatchRank::PARTIAL;
} else if (proxyMatch(server)) {
JAMI_DBG("Matching account id in request with proxy %.*s", (int)server.size(), server.data());
JAMI_DBG("Matching account id in request with proxy %.*s",
(int) server.size(),
server.data());
return MatchRank::PARTIAL;
} else {
return MatchRank::NONE;
......@@ -1839,7 +1874,7 @@ SIPAccount::resetAutoRegistration()
auto_rereg_.active = PJ_FALSE;
auto_rereg_.attempt_cnt = 0;
if (auto_rereg_.timer.user_data) {
delete ((std::weak_ptr<SIPAccount>*)auto_rereg_.timer.user_data);
delete ((std::weak_ptr<SIPAccount>*) auto_rereg_.timer.user_data);
auto_rereg_.timer.user_data = nullptr;
}
}
......@@ -2203,6 +2238,8 @@ SIPAccount::sendTextMessage(const std::string& to,
return;
}
setUpTransmissionData(tdata, tp_sel.u.transport->key.type);
im::fillPJSIPMessageBody(*tdata, payloads);
// Send message request with callback SendMessageOnComplete
......
......@@ -463,13 +463,11 @@ public:
#ifndef _MSC_VER
template<class T = SIPCall>
std::shared_ptr<enable_if_base_of<T, SIPCall>> newOutgoingCall(
std::string_view toUrl,
const std::map<std::string, std::string>& volatileCallDetails = {});
std::string_view toUrl, const std::map<std::string, std::string>& volatileCallDetails = {});
#else
template<class T>
std::shared_ptr<T> newOutgoingCall(
std::string_view toUrl,
const std::map<std::string, std::string>& volatileCallDetails = {});
std::string_view toUrl, const std::map<std::string, std::string>& volatileCallDetails = {});
#endif
/**
......@@ -507,6 +505,8 @@ private:
void doRegister1_();
void doRegister2_();
void setUpTransmissionData(pjsip_tx_data* tdata, long transportKeyType);
/**
* Set the internal state for this account, mainly used to manage account details from the
* client application.
......@@ -642,7 +642,7 @@ private:
int registrationExpire_;
/**
* Optional list of SIP service this
* Input Outbound Proxy Server Address
*/
std::string serviceRoute_;
......
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