Commit e66356e9 authored by Adrien Béraud's avatar Adrien Béraud Committed by Tristan Matthews

accounts: split SIPAccount class

Move some parts of SIPAccount that will be shared with DHTAccount
to a new class SIPAccountBase.

Refs #53127

Change-Id: Ib4bcb1e20cac5bbd0e134ae4ba782fba9e5ccdad
parent 7996dda1
......@@ -4,11 +4,13 @@ noinst_LTLIBRARIES = libsiplink.la
libsiplink_la_CXXFLAGS = @CXXFLAGS@
libsiplink_la_SOURCES = \
sipaccountbase.cpp \
sdp.cpp \
sipaccount.cpp \
sipcall.cpp \
sipvoiplink.cpp \
siptransport.cpp \
sipaccountbase.h \
sdp.h \
sipaccount.h \
sipcall.h \
......
......@@ -32,6 +32,7 @@
#include "pjsip/sip_multipart.h"
#include "sipaccount.h"
#include "sipvoiplink.h"
#include "manager.h"
#include "sippresence.h"
......
......@@ -75,9 +75,6 @@ static const char *const VALID_TLS_METHODS[] = {"Default", "TLSv1", "SSLv3", "SS
static const char *const VALID_SRTP_KEY_EXCHANGES[] = {"", "sdes", "zrtp"};
constexpr const char * const SIPAccount::ACCOUNT_TYPE;
// we force RTP ports to be even, so we only need HALF_MAX_PORT booleans
bool SIPAccount::portsInUse_[HALF_MAX_PORT];
static void
registration_cb(pjsip_regc_cbparam *param)
{
......@@ -96,30 +93,16 @@ registration_cb(pjsip_regc_cbparam *param)
}
SIPAccount::SIPAccount(const std::string& accountID, bool presenceEnabled)
: Account(accountID)
: SIPAccountBase(accountID)
, auto_rereg_()
, credentials_()
, transport_(nullptr)
, tlsListener_(nullptr)
, regc_(nullptr)
, bRegister_(false)
, registrationExpire_(MIN_REGISTRATION_TIME)
, interface_("default")
, publishedSameasLocal_(true)
, publishedIp_()
, publishedIpAddress_()
, localPort_(DEFAULT_SIP_PORT)
, publishedPort_(DEFAULT_SIP_PORT)
, serviceRoute_()
, tlsListenerPort_(DEFAULT_SIP_TLS_PORT)
, transportType_(PJSIP_TRANSPORT_UNSPECIFIED)
, cred_()
, tlsSetting_()
, ciphers_(100)
, stunServerName_()
, stunPort_(PJ_STUN_PORT)
, dtmfType_(OVERRTP_STR)
, tlsEnable_(false)
, tlsCaListFile_()
, tlsCertificateFile_()
, tlsPrivateKeyFile_()
......@@ -131,11 +114,6 @@ SIPAccount::SIPAccount(const std::string& accountID, bool presenceEnabled)
, tlsVerifyClient_(true)
, tlsRequireClientCertificate_(true)
, tlsNegotiationTimeoutSec_("2")
, stunServer_("")
, stunEnabled_(false)
, srtpEnabled_(false)
, srtpKeyExchange_("")
, srtpFallback_(false)
, zrtpDisplaySas_(true)
, zrtpDisplaySasOnce_(false)
, zrtpHelloHash_(true)
......@@ -144,7 +122,6 @@ SIPAccount::SIPAccount(const std::string& accountID, bool presenceEnabled)
, keepAliveEnabled_(false)
, keepAliveTimer_()
, keepAliveTimerActive_(false)
, link_(getSIPVoIPLink())
, receivedParameter_("")
, rPort_(-1)
, via_addr_()
......@@ -155,10 +132,6 @@ SIPAccount::SIPAccount(const std::string& accountID, bool presenceEnabled)
, allowContactRewrite_(1)
, contactOverwritten_(false)
, via_tp_(nullptr)
, audioPortRange_({16384, 32766})
#ifdef SFL_VIDEO
, videoPortRange_({49152, (MAX_PORT) - 2})
#endif
#ifdef SFL_PRESENCE
, presence_(presenceEnabled ? new SIPPresence(this) : nullptr)
#endif
......@@ -220,7 +193,6 @@ unserializeRange(const Conf::YamlNode &mapNode, const char *minKey, const char *
updateRange(tmpMin, tmpMax, range);
}
template <>
std::shared_ptr<SIPCall>
SIPAccount::newIncomingCall(const std::string& id)
{
......@@ -235,6 +207,8 @@ SIPAccount::newOutgoingCall(const std::string& id, const std::string& toUrl)
std::string toUri;
int family;
auto call = Manager::instance().callFactory.newCall<SIPCall, SIPAccount>(*this, id, Call::OUTGOING);
if (isIP2IP()) {
bool ipv6 = false;
#if HAVE_IPV6
......@@ -262,8 +236,6 @@ SIPAccount::newOutgoingCall(const std::string& id, const std::string& toUrl)
DEBUG("UserAgent: New registered account call to %s", toUrl.c_str());
}
auto call = Manager::instance().callFactory.newCall<SIPCall, SIPAccount>(*this, id, Call::OUTGOING);
call->setIPToIP(isIP2IP());
call->setPeerNumber(toUri);
call->initRecFilename(to);
......@@ -403,9 +375,6 @@ SIPAccount::SIPStartCall(std::shared_ptr<SIPCall>& call)
void SIPAccount::serialize(Conf::YamlEmitter &emitter)
{
using namespace Conf;
using std::vector;
using std::string;
using std::map;
MappingNode accountmap(nullptr);
MappingNode srtpmap(nullptr);
MappingNode zrtpmap(nullptr);
......@@ -962,16 +931,7 @@ static std::string retrievePassword(const std::map<std::string, std::string>& ma
return "";
}
void
addRangeToDetails(std::map<std::string, std::string> &a, const char *minKey, const char *maxKey, const std::pair<uint16_t, uint16_t> &range)
{
std::ostringstream os;
os << range.first;
a[minKey] = os.str();
os.str("");
os << range.second;
a[maxKey] = os.str();
}
std::map<std::string, std::string> SIPAccount::getAccountDetails() const
{
......@@ -2080,41 +2040,6 @@ SIPAccount::matches(const std::string &userName, const std::string &server,
}
}
// returns even number in range [lower, upper]
uint16_t
SIPAccount::getRandomEvenNumber(const std::pair<uint16_t, uint16_t> &range)
{
const uint16_t halfUpper = range.second * 0.5;
const uint16_t halfLower = range.first * 0.5;
uint16_t result;
do {
result = 2 * (halfLower + rand() % (halfUpper - halfLower + 1));
} while (portsInUse_[result / 2]);
portsInUse_[result / 2] = true;
return result;
}
void
SIPAccount::releasePort(uint16_t port)
{
portsInUse_[port / 2] = false;
}
uint16_t
SIPAccount::generateAudioPort() const
{
return getRandomEvenNumber(audioPortRange_);
}
#ifdef SFL_VIDEO
uint16_t
SIPAccount::generateVideoPort() const
{
return getRandomEvenNumber(videoPortRange_);
}
#endif
void
SIPAccount::destroyRegistrationInfo()
{
......
This diff is collapsed.
/*
* Copyright (C) 2014 Savoir-Faire Linux Inc.
*
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "sipaccountbase.h"
#include "sipvoiplink.h"
bool SIPAccountBase::portsInUse_[HALF_MAX_PORT];
SIPAccountBase::SIPAccountBase(const std::string& accountID)
: Account(accountID), link_(getSIPVoIPLink())
{}
void
SIPAccountBase::setTransport(pjsip_transport* transport, pjsip_tpfactory* lis)
{
// release old transport
if (transport_ && transport_ != transport) {
pjsip_transport_dec_ref(transport_);
}
if (tlsListener_ && tlsListener_ != lis)
tlsListener_->destroy(tlsListener_);
// set new transport
transport_ = transport;
tlsListener_ = lis;
}
// returns even number in range [lower, upper]
uint16_t
SIPAccountBase::getRandomEvenNumber(const std::pair<uint16_t, uint16_t> &range)
{
const uint16_t halfUpper = range.second * 0.5;
const uint16_t halfLower = range.first * 0.5;
uint16_t result;
do {
result = 2 * (halfLower + rand() % (halfUpper - halfLower + 1));
} while (portsInUse_[result / 2]);
portsInUse_[result / 2] = true;
return result;
}
void
SIPAccountBase::releasePort(uint16_t port)
{
portsInUse_[port / 2] = false;
}
uint16_t
SIPAccountBase::generateAudioPort() const
{
return getRandomEvenNumber(audioPortRange_);
}
#ifdef SFL_VIDEO
uint16_t
SIPAccountBase::generateVideoPort() const
{
return getRandomEvenNumber(videoPortRange_);
}
#endif
This diff is collapsed.
......@@ -34,6 +34,8 @@
#include "call_factory.h"
#include "sipcall.h"
#include "sipaccount.h" // for SIPAccount::ACCOUNT_TYPE
#include "sipaccountbase.h"
#include "sipvoiplink.h"
#include "sip_utils.h"
#include "logger.h" // for _debug
......@@ -96,7 +98,7 @@ dtmfSend(SIPCall &call, char code, const std::string &dtmf)
call.sendSIPInfo(dtmf_body, "dtmf-relay");
}
SIPCall::SIPCall(SIPAccount& account, const std::string& id,
SIPCall::SIPCall(SIPAccountBase& account, const std::string& id,
Call::CallType type)
: Call(account, id, type)
, audiortp_(this)
......@@ -138,10 +140,10 @@ SIPCall::stopRtpIfCurrent()
}
}
SIPAccount&
SIPAccountBase&
SIPCall::getSIPAccount() const
{
return static_cast<SIPAccount&>(getAccount());
return static_cast<SIPAccountBase&>(getAccount());
}
void
......
......@@ -56,7 +56,7 @@ struct pj_pool_t;
struct pjsip_inv_session;
class Sdp;
class SIPAccount;
class SIPAccountBase;
/**
* @file sipcall.h
......@@ -74,7 +74,7 @@ class SIPCall : public Call
* @param type The type of the call. Could be Incoming
* Outgoing
*/
SIPCall(SIPAccount& account, const std::string& id, Call::CallType type);
SIPCall(SIPAccountBase& account, const std::string& id, Call::CallType type);
public:
/**
......@@ -147,7 +147,7 @@ class SIPCall : public Call
const std::string& from);
#endif
SIPAccount& getSIPAccount() const;
SIPAccountBase& getSIPAccount() const;
void updateSDPFromSTUN();
......
......@@ -146,7 +146,7 @@ SipTransport::waitForReleased(pjsip_transport* tp, std::function<void(bool)> rel
}
void
SipTransport::createSipTransport(SIPAccount &account)
SipTransport::createSipTransport(SIPAccountBase &account)
{
// Remove any existing transport from the account
account.setTransport();
......@@ -241,7 +241,7 @@ SipTransport::createUdpTransport(const std::string &interface, pj_uint16_t port,
#if HAVE_TLS
pjsip_tpfactory*
SipTransport::createTlsListener(SIPAccount &account, pj_uint16_t family)
SipTransport::createTlsListener(SIPAccountBase &account, pj_uint16_t family)
{
RETURN_IF_FAIL(account.getTlsSetting() != nullptr, nullptr, "TLS settings not specified");
......@@ -271,7 +271,7 @@ SipTransport::createTlsListener(SIPAccount &account, pj_uint16_t family)
}
pjsip_transport *
SipTransport::createTlsTransport(SIPAccount &account)
SipTransport::createTlsTransport(SIPAccountBase &account)
{
std::string remoteSipUri(account.getServerUri());
static const char SIPS_PREFIX[] = "<sips:";
......@@ -362,7 +362,7 @@ SipTransport::_cleanupTransports()
}
std::vector<pj_sockaddr>
SipTransport::getSTUNAddresses(const SIPAccount &account,
SipTransport::getSTUNAddresses(const SIPAccountBase &account,
std::vector<long> &socketDescriptors) const
{
const pj_str_t serverName = account.getStunServerName();
......
......@@ -54,7 +54,7 @@
#include <vector>
#include <memory>
class SIPAccount;
class SIPAccountBase;
class SipTransport {
......@@ -67,7 +67,7 @@ class SipTransport {
* transport type specified in account settings
* @param account The account for which a transport must be created.
*/
void createSipTransport(SIPAccount &account);
void createSipTransport(SIPAccountBase &account);
/**
* Initialize the transport selector
......@@ -82,7 +82,7 @@ class SipTransport {
/**
* This function returns a list of STUN mapped sockets for
* a given set of socket file descriptors */
std::vector<pj_sockaddr> getSTUNAddresses(const SIPAccount &account, std::vector<long> &socks) const;
std::vector<pj_sockaddr> getSTUNAddresses(const SIPAccountBase &account, std::vector<long> &socks) const;
/**
* Get the correct address to use (ie advertised) from
......@@ -122,7 +122,7 @@ class SipTransport {
* @param the account that is creating the TLS transport
*/
pjsip_transport *
createTlsTransport(SIPAccount &account);
createTlsTransport(SIPAccountBase &account);
/**
* Create The default TLS listener which is global to the application. This means that
......@@ -132,7 +132,7 @@ class SipTransport {
* @return a pointer to the new listener
*/
pjsip_tpfactory *
createTlsListener(SIPAccount &account, pj_uint16_t family = pj_AF_UNSPEC());
createTlsListener(SIPAccountBase &account, pj_uint16_t family = pj_AF_UNSPEC());
#endif
/**
......
......@@ -40,6 +40,7 @@
#include "sdp.h"
#include "sipcall.h"
#include "sipaccount.h"
#include "sip_utils.h"
#include "call_factory.h"
......@@ -225,9 +226,12 @@ transaction_request_cb(pjsip_rx_data *rdata)
return PJ_FALSE;
}
std::string toUsername(sip_to_uri->user.ptr, sip_to_uri->user.slen);
std::string toHost(sip_to_uri->host.ptr, sip_to_uri->host.slen);
std::string viaHostname(sip_via.host.ptr, sip_via.host.slen);
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 sipaccount(getSIPVoIPLink()->guessAccountFromNameAndServer(toUsername, viaHostname));
auto sipaccount(getSIPVoIPLink()->guessAccount(toUsername, viaHostname, remote_hostname));
if (!sipaccount) {
ERROR("NULL account");
return PJ_FALSE;
......@@ -283,7 +287,7 @@ transaction_request_cb(pjsip_rx_data *rdata)
Manager::instance().hookPreference.runHook(rdata->msg_info.msg);
auto call = sipaccount->newIncomingCall<SIPCall>(Manager::instance().getNewCallID());
auto call = sipaccount->newIncomingCall(Manager::instance().getNewCallID());
// FIXME : for now, use the same address family as the SIP tranport
auto family = pjsip_transport_type_get_af(sipaccount->getTransportType());
......@@ -300,9 +304,6 @@ transaction_request_cb(pjsip_rx_data *rdata)
std::string peerNumber(tmp, std::min(length, sizeof tmp));
sip_utils::stripSipUriPrefix(peerNumber);
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);
if (not remote_user.empty() and not remote_hostname.empty())
peerNumber = remote_user + "@" + remote_hostname;
......@@ -616,14 +617,15 @@ SIPVoIPLink::~SIPVoIPLink()
pj_shutdown();
}
std::shared_ptr<SIPAccount>
SIPVoIPLink::guessAccountFromNameAndServer(const std::string &userName,
const std::string &server) const
std::shared_ptr<SIPAccountBase>
SIPVoIPLink::guessAccount(const std::string& userName,
const std::string& server,
const std::string& fromUri) const
{
DEBUG("username = %s, server = %s", userName.c_str(), server.c_str());
DEBUG("username = %s, server = %s, from = %s", userName.c_str(), server.c_str(), fromUri.c_str());
// Try to find the account id from username and server name by full match
auto result = std::static_pointer_cast<SIPAccount>(Manager::instance().getIP2IPAccount()); // default result
auto result = std::static_pointer_cast<SIPAccountBase>(Manager::instance().getIP2IPAccount()); // default result
MatchRank best = MatchRank::NONE;
for (const auto& sipaccount : Manager::instance().getAllAccounts<SIPAccount>()) {
......@@ -634,7 +636,7 @@ SIPVoIPLink::guessAccountFromNameAndServer(const std::string &userName,
// return right away if this is a full match
if (match == MatchRank::FULL) {
return sipaccount;
return std::static_pointer_cast<SIPAccountBase>(sipaccount);
} else if (match > best) {
best = match;
result = sipaccount;
......
......@@ -42,7 +42,6 @@
#endif
#include "sfl_types.h"
#include "sipaccount.h"
#include "siptransport.h"
#include <pjsip.h>
......@@ -60,7 +59,8 @@
#include <memory>
class SIPCall;
class SIPAccount;
class SIPAccountBase;
class SIPVoIPLink;
typedef std::map<std::string, std::shared_ptr<SIPCall> > SipCallMap;
......@@ -107,13 +107,6 @@ class SIPVoIPLink {
*/
void cancelKeepAliveTimer(pj_timer_entry& timer);
/**
* Start a new SIP call using the IP2IP profile
* @param The call id
* @param The target sip uri
*/
std::shared_ptr<Call> SIPNewIpToIpCall(const std::string& id, const std::string& to);
/**
* Get the memory pool factory since each calls has its own memory pool
*/
......@@ -139,9 +132,14 @@ class SIPVoIPLink {
static void enqueueKeyframeRequest(const std::string &callID);
#endif
std::shared_ptr<SIPAccount>
guessAccountFromNameAndServer(const std::string &userName,
const std::string &server) const;
/**
* Guess the account related to an incoming SIP call.
*/
std::shared_ptr<SIPAccountBase>
guessAccount(const std::string& userName,
const std::string& server,
const std::string& fromUri) const;
int getModId();
pjsip_endpoint * getEndpoint();
pjsip_module * getMod();
......
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