Commit c488ccf0 authored by Guillaume Roguez's avatar Guillaume Roguez
Browse files

sip: move some class methods

Rationale:
These methods depend on internal states owned by an other class
(and obviously parameters). So why to not put them where
theses states are really located?
This is also not the role of the former class to gives these methods.

Refs #64903

Change-Id: I539715cb63ed99acf5b579666588c88e280cde05
parent a3b20937
......@@ -345,7 +345,7 @@ SIPAccount::SIPStartCall(std::shared_ptr<SIPCall>& call)
return false;
}
const pjsip_tpselector tp_sel = SipTransportBroker::getTransportSelector(transport->get());
const pjsip_tpselector tp_sel = link_->getTransportSelector(transport->get());
if (pjsip_dlg_set_transport(dialog, &tp_sel) != PJ_SUCCESS) {
RING_ERR("Unable to associate transport for invite session dialog");
return false;
......@@ -1419,7 +1419,7 @@ SIPAccount::getContactHeader(pjsip_transport* t)
std::string address;
pj_uint16_t port;
link_->sipTransportBroker->findLocalAddressFromTransport(
link_->findLocalAddressFromTransport(
t,
transportType,
hostname_,
......@@ -1435,7 +1435,7 @@ SIPAccount::getContactHeader(pjsip_transport* t)
port = publishedPort_;
RING_DBG("Using published address %s and port %d", address.c_str(), port);
} else if (stunEnabled_) {
link_->sipTransportBroker->findLocalAddressFromSTUN(
link_->findLocalAddressFromSTUN(
t,
&stunServerName_,
stunPort_,
......@@ -1490,7 +1490,7 @@ SIPAccount::getHostPortFromSTUN(pj_pool_t *pool)
{
std::string addr;
pj_uint16_t port;
link_->sipTransportBroker->findLocalAddressFromSTUN(
link_->findLocalAddressFromSTUN(
transport_ ? transport_->get() : nullptr,
&stunServerName_,
stunPort_,
......
......@@ -331,4 +331,11 @@ SIPAccountBase::generateVideoPort() const
}
#endif
pjsip_tpselector
SIPAccountBase::getTransportSelector() {
if (!transport_)
return SIPVoIPLink::getTransportSelector(nullptr);
return SIPVoIPLink::getTransportSelector(transport_->get());
}
} // namespace ring
......@@ -265,11 +265,7 @@ public:
/**
* Shortcut for SipTransport::getTransportSelector(account.getTransport()).
*/
inline pjsip_tpselector getTransportSelector() {
if (!transport_)
return SipTransportBroker::getTransportSelector(nullptr);
return SipTransportBroker::getTransportSelector(transport_->get());
}
pjsip_tpselector getTransportSelector();
protected:
virtual void serialize(YAML::Emitter &out);
......
......@@ -439,80 +439,4 @@ SipTransportBroker::getIceTransport(const std::shared_ptr<IceTransport> ice, uns
}
#endif
#define RETURN_IF_NULL(A, M, ...) if ((A) == NULL) { RING_ERR(M, ##__VA_ARGS__); return; }
void
SipTransportBroker::findLocalAddressFromTransport(pjsip_transport *transport, pjsip_transport_type_e transportType, const std::string &host, std::string &addr, pj_uint16_t &port) const
{
// Initialize the sip port with the default SIP port
port = pjsip_transport_get_default_port_for_type(transportType);
// Initialize the sip address with the hostname
const pj_str_t *pjMachineName = pj_gethostname();
addr = std::string(pjMachineName->ptr, pjMachineName->slen);
// Update address and port with active transport
RETURN_IF_NULL(transport, "Transport is NULL in findLocalAddress, using local address %s :%d", addr.c_str(), port);
// get the transport manager associated with the SIP enpoint
pjsip_tpmgr *tpmgr = pjsip_endpt_get_tpmgr(endpt_);
RETURN_IF_NULL(tpmgr, "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());
pjsip_tpselector tp_sel = getTransportSelector(transport);
pjsip_tpmgr_fla2_param param = {transportType, &tp_sel, pjstring, PJ_FALSE, {nullptr, 0}, 0, nullptr};
if (pjsip_tpmgr_find_local_addr2(tpmgr, &pool_, &param) != PJ_SUCCESS) {
RING_WARN("Could not retrieve local address and port from transport, using %s :%d", addr.c_str(), port);
return;
}
// Update local address based on the transport type
addr = std::string(param.ret_addr.ptr, param.ret_addr.slen);
// Determine the local port based on transport information
port = param.ret_port;
}
void
SipTransportBroker::findLocalAddressFromSTUN(pjsip_transport *transport,
pj_str_t *stunServerName,
int stunPort,
std::string &addr, pj_uint16_t &port) const
{
// Initialize the sip port with the default SIP port
port = DEFAULT_SIP_PORT;
// Initialize the sip address with the hostname
const pj_str_t *pjMachineName = pj_gethostname();
addr = std::string(pjMachineName->ptr, pjMachineName->slen);
// Update address and port with active transport
RETURN_IF_NULL(transport, "Transport is NULL in findLocalAddress, using local address %s:%d", addr.c_str(), port);
IpAddr mapped_addr;
pj_sock_t sipSocket = pjsip_udp_transport_get_socket(transport);
const pjstun_setting stunOpt = {PJ_TRUE, *stunServerName, stunPort, *stunServerName, stunPort};
const pj_status_t stunStatus = pjstun_get_mapped_addr2(&cp_.factory, &stunOpt, 1, &sipSocket, &static_cast<pj_sockaddr_in&>(mapped_addr));
switch (stunStatus) {
case PJLIB_UTIL_ESTUNNOTRESPOND:
RING_ERR("No response from STUN server %.*s", stunServerName->slen, stunServerName->ptr);
return;
case PJLIB_UTIL_ESTUNSYMMETRIC:
RING_ERR("Different mapped addresses are returned by servers.");
return;
case PJ_SUCCESS:
port = mapped_addr.getPort();
addr = mapped_addr.toString();
default:
break;
}
RING_WARN("Using address %s provided by STUN server %.*s",
IpAddr(mapped_addr).toString(true).c_str(), stunServerName->slen, stunServerName->ptr);
}
#undef RETURN_IF_NULL
} // namespace ring
......@@ -163,29 +163,6 @@ public:
std::shared_ptr<SipTransport> findTransport(pjsip_transport*);
/**
* Initialize the transport selector
* @param transport A transport associated with an account
* @return A transport selector structure
*/
static inline pjsip_tpselector getTransportSelector(pjsip_transport *transport) {
pjsip_tpselector tp = {PJSIP_TPSELECTOR_TRANSPORT, {transport}};
return tp;
}
/**
* Get the correct address to use (ie advertised) from
* a uri. The corresponding transport that should be used
* with that uri will be discovered.
*
* @param uri The uri from which we want to discover the address to use
* @param transport The transport to use to discover the address
*/
void findLocalAddressFromTransport(pjsip_transport *transport, pjsip_transport_type_e transportType, const std::string &host, std::string &address, pj_uint16_t &port) const;
void findLocalAddressFromSTUN(pjsip_transport *transport, pj_str_t *stunServerName,
int stunPort, std::string &address, pj_uint16_t &port) const;
/**
* Start gracefull shutdown procedure for all transports
*/
......
......@@ -228,7 +228,9 @@ transaction_request_cb(pjsip_rx_data *rdata)
const std::string remote_user(sip_from_uri->user.ptr, sip_from_uri->user.slen);
const std::string remote_hostname(sip_from_uri->host.ptr, sip_from_uri->host.slen);
auto account(getSIPVoIPLink()->guessAccount(toUsername, viaHostname, remote_hostname));
auto link = getSIPVoIPLink();
auto account(link->guessAccount(toUsername, viaHostname, remote_hostname));
if (!account) {
RING_ERR("NULL account");
return PJ_FALSE;
......@@ -300,7 +302,7 @@ transaction_request_cb(pjsip_rx_data *rdata)
// RING_DBG("transaction_request_cb viaHostname %s toUsername %s addrToUse %s addrSdp %s peerNumber: %s" ,
// viaHostname.c_str(), toUsername.c_str(), addrToUse.toString().c_str(), addrSdp.toString().c_str(), peerNumber.c_str());
auto transport = getSIPVoIPLink()->sipTransportBroker->findTransport(rdata->tp_info.transport);
auto transport = link->sipTransportBroker->findTransport(rdata->tp_info.transport);
if (!transport) {
transport = account->getTransport();
if (!transport) {
......@@ -366,7 +368,7 @@ transaction_request_cb(pjsip_rx_data *rdata)
return PJ_FALSE;
}
pjsip_tpselector tp_sel = SipTransportBroker::getTransportSelector(transport->get());
pjsip_tpselector tp_sel = link->getTransportSelector(transport->get());
if (!dialog or pjsip_dlg_set_transport(dialog, &tp_sel) != PJ_SUCCESS) {
RING_ERR("Could not set transport for dialog");
return PJ_FALSE;
......@@ -1281,4 +1283,101 @@ SIPVoIPLink::resolver_callback(pj_status_t status, void *token, const struct pjs
}
}
#define RETURN_IF_NULL(A, M, ...) \
if ((A) == NULL) { RING_WARN(M, ##__VA_ARGS__); return; }
void
SIPVoIPLink::findLocalAddressFromTransport(pjsip_transport* transport,
pjsip_transport_type_e transportType,
const std::string& host,
std::string& addr,
pj_uint16_t& port) const
{
// Initialize the sip port with the default SIP port
port = pjsip_transport_get_default_port_for_type(transportType);
// Initialize the sip address with the hostname
const auto pjMachineName = pj_gethostname();
addr = std::string(pjMachineName->ptr, pjMachineName->slen);
// Update address and port with active transport
RETURN_IF_NULL(transport,
"Transport is NULL in findLocalAddress, using local address %s :%d",
addr.c_str(), port);
// get the transport manager associated with the SIP enpoint
auto tpmgr = pjsip_endpt_get_tpmgr(endpt_);
RETURN_IF_NULL(tpmgr,
"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());
auto tp_sel = getTransportSelector(transport);
pjsip_tpmgr_fla2_param param = { transportType, &tp_sel, pjstring, PJ_FALSE,
{nullptr, 0}, 0, nullptr };
if (pjsip_tpmgr_find_local_addr2(tpmgr, pool_, &param) != PJ_SUCCESS) {
RING_WARN("Could not retrieve local address and port from transport, using %s :%d",
addr.c_str(), port);
return;
}
// Update local address based on the transport type
addr = std::string(param.ret_addr.ptr, param.ret_addr.slen);
// Determine the local port based on transport information
port = param.ret_port;
}
void
SIPVoIPLink::findLocalAddressFromSTUN(pjsip_transport* transport,
pj_str_t* stunServerName,
int stunPort,
std::string& addr,
pj_uint16_t& port) const
{
// Initialize the sip port with the default SIP port
port = DEFAULT_SIP_PORT;
// Initialize the sip address with the hostname
const pj_str_t* pjMachineName = pj_gethostname();
addr = std::string(pjMachineName->ptr, pjMachineName->slen);
// Update address and port with active transport
RETURN_IF_NULL(transport,
"Transport is NULL in findLocalAddress, using local address %s:%d",
addr.c_str(), port);
IpAddr mapped_addr;
pj_sock_t sipSocket = pjsip_udp_transport_get_socket(transport);
const pjstun_setting stunOpt = {PJ_TRUE, *stunServerName, stunPort,
*stunServerName, stunPort};
const pj_status_t stunStatus = pjstun_get_mapped_addr2(&cp_->factory,
&stunOpt, 1,
&sipSocket,
&static_cast<pj_sockaddr_in&>(mapped_addr));
switch (stunStatus) {
case PJLIB_UTIL_ESTUNNOTRESPOND:
RING_ERR("No response from STUN server %.*s",
stunServerName->slen, stunServerName->ptr);
return;
case PJLIB_UTIL_ESTUNSYMMETRIC:
RING_ERR("Different mapped addresses are returned by servers.");
return;
case PJ_SUCCESS:
port = mapped_addr.getPort();
addr = mapped_addr.toString();
default:
break;
}
RING_WARN("Using address %s provided by STUN server %.*s",
IpAddr(mapped_addr).toString(true).c_str(), stunServerName->slen,
stunServerName->ptr);
}
#undef RETURN_IF_NULL
} // namespace ring
......@@ -156,6 +156,35 @@ class SIPVoIPLink {
pj_pool_t* getPool() const;
/**
* Get the correct address to use (ie advertised) from
* a uri. The corresponding transport that should be used
* with that uri will be discovered.
*
* @param uri The uri from which we want to discover the address to use
* @param transport The transport to use to discover the address
*/
void findLocalAddressFromTransport(pjsip_transport* transport,
pjsip_transport_type_e transportType,
const std::string& host,
std::string& address,
pj_uint16_t& port) const;
void findLocalAddressFromSTUN(pjsip_transport* transport,
pj_str_t* stunServerName,
int stunPort, std::string& address,
pj_uint16_t& port) const;
/**
* Initialize the transport selector
* @param transport A transport associated with an account
* @return A transport selector structure
*/
static inline pjsip_tpselector getTransportSelector(pjsip_transport *transport) {
pjsip_tpselector tp = {PJSIP_TPSELECTOR_TRANSPORT, {transport}};
return tp;
}
private:
NON_COPYABLE(SIPVoIPLink);
......
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