Ring Daemon
Loading...
Searching...
No Matches
sipaccount.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include "sip/sipaccountbase.h"
24#include "sip/siptransport.h"
25#include "noncopyable.h"
26#include "sipaccount_config.h"
27
28#include <pjsip/sip_transport_tls.h>
29#include <pjsip/sip_types.h>
30#include <pjsip-ua/sip_regc.h>
31
32#include <vector>
33#include <map>
34
35namespace jami {
36
37typedef std::vector<pj_ssl_cipher> CipherArray;
38
39class SIPPresence;
40class SIPCall;
41
47{
48public:
49 constexpr static auto ACCOUNT_TYPE = ACCOUNT_TYPE_SIP;
50
51 std::shared_ptr<SIPAccount> shared() { return std::static_pointer_cast<SIPAccount>(shared_from_this()); }
52 std::shared_ptr<SIPAccount const> shared() const
53 {
54 return std::static_pointer_cast<SIPAccount const>(shared_from_this());
55 }
56 std::weak_ptr<SIPAccount> weak() { return std::static_pointer_cast<SIPAccount>(shared_from_this()); }
57 std::weak_ptr<SIPAccount const> weak() const
58 {
59 return std::static_pointer_cast<SIPAccount const>(shared_from_this());
60 }
61
66 SIPAccount(const std::string& accountID, bool presenceEnabled);
67
69
70 const SipAccountConfig& config() const { return *static_cast<const SipAccountConfig*>(&Account::config()); }
71
72 std::unique_ptr<AccountConfig> buildConfig() const override
73 {
74 return std::make_unique<SipAccountConfig>(getAccountID());
75 }
76 inline void editConfig(std::function<void(SipAccountConfig& conf)>&& edit)
77 {
78 Account::editConfig([&](AccountConfig& conf) { edit(*static_cast<SipAccountConfig*>(&conf)); });
79 }
80
81 std::string_view getAccountType() const override { return ACCOUNT_TYPE; }
82
84
86
88
96
100 bool isIP2IP() const override;
101
106 virtual std::map<std::string, std::string> getVolatileAccountDetails() const override;
107
112 std::map<std::string, std::string> getTlsSettings() const;
113
117 void loadConfig() override;
118
122 void updateProfile(const std::string& displayName,
123 const std::string& avatar,
124 const std::string& fileType,
125 int32_t flag) override;
126
130 void doRegister() override;
131
135 void doUnregister(bool forceShutdownConnections = false) override;
136
140 void sendRegister();
141
146 void sendUnregister();
147
148 const pjsip_cred_info* getCredInfo() const { return cred_.data(); }
149
156 unsigned getCredentialCount() const { return config().credentials.size(); }
157
158 bool hasCredentials() const { return not config().credentials.empty(); }
159
160 std::vector<std::map<std::string, std::string>> getCredentials() const { return config().getCredentials(); }
161
162 virtual void setRegistrationState(RegistrationState state,
163 int code = 0,
164 const std::string& detail_str = {}) override;
165
173 unsigned getRegistrationExpire() const
174 {
175 unsigned re = config().registrationExpire;
177 }
178
182 bool isRegistered() const { return bRegister_; }
183
190 pjsip_regc* getRegistrationInfo() { return regc_; }
191
199 {
200 if (regc_)
202 regc_ = regc;
203 }
204
206
212 uint16_t getLocalPort() const { return config().localPort; }
213
218
224 pjsip_tls_setting* getTlsSetting() { return &tlsSetting_; }
225
231
232 pj_str_t getStunServerName() const { return stunServerName_; }
233
234 static const std::vector<std::string>& getSupportedTlsCiphers();
235 static const std::vector<std::string>& getSupportedTlsProtocols();
236
242 pj_uint16_t getStunPort() const override { return stunPort_; }
243
248 bool isStunEnabled() const override { return config().stunEnabled; }
249
255 std::string getStunServer() const { return config().stunServer; }
256
266 std::string getFromUri() const override;
267
276 std::string getToUri(const std::string& username) const override;
277
286 std::string getServerUri() const;
287
292 dhtnet::IpAddr getContactAddress() const;
297 std::string getContactHeader() const;
298
299 std::string getServiceRoute() const { return config().serviceRoute; }
300
301 bool hasServiceRoute() const { return not config().serviceRoute.empty(); }
302
303 virtual bool isTlsEnabled() const override { return config().tlsEnable; }
304
305 void setReceivedParameter(const std::string& received)
306 {
307 receivedParameter_ = received;
308 via_addr_.host = sip_utils::CONST_PJ_STR(receivedParameter_);
309 }
310
311 const std::string& getReceivedParameter() const { return receivedParameter_; }
312
313 pjsip_host_port* getViaAddr() { return &via_addr_; }
314
315 int getRPort() const
316 {
317 if (rPort_ == -1)
318 return config().localPort;
319 else
320 return rPort_;
321 }
322
323 void setRPort(int rPort)
324 {
325 rPort_ = rPort;
326 via_addr_.port = rPort;
327 }
328
330
331 void setTransport(const std::shared_ptr<SipTransport>& = nullptr);
332
333 virtual inline std::shared_ptr<SipTransport> getTransport() { return transport_; }
334
335 inline pjsip_transport_type_e getTransportType() const { return transportType_; }
336
341
342 /* Returns true if the username and/or hostname match this account */
343 MatchRank matches(std::string_view username, std::string_view hostname) const override;
344
348 SIPPresence* getPresence() const;
349
355 void enablePresence(const bool& enable);
360 void supportPresence(int function, bool enable);
361
368 std::shared_ptr<Call> newOutgoingCall(std::string_view toUrl,
369 const std::vector<libjami::MediaMap>& mediaList) override;
370
378 std::shared_ptr<SIPCall> newIncomingCall(const std::string& from,
379 const std::vector<libjami::MediaMap>& mediaList,
380 const std::shared_ptr<SipTransport>& sipTr = {}) override;
381
383
384 virtual void sendMessage(const std::string& to,
385 const std::string& deviceId,
386 const std::map<std::string, std::string>& payloads,
387 uint64_t id,
388 bool retryOnTimeout = true,
389 bool onlyConnected = false) override;
390
391 void connectivityChanged() override;
392
393 std::string getUserUri() const override;
394
399 dhtnet::IpAddr createBindingAddress();
400
401 void setActiveCodecs(const std::vector<unsigned>& list) override;
402 bool isSrtpEnabled() const override { return config().srtpKeyExchange != KeyExchangeProtocol::NONE; }
403
404 bool setPushNotificationToken(const std::string& pushDeviceToken = "") override;
405 bool setPushNotificationConfig(const std::map<std::string, std::string>& data) override;
406
410 void pushNotificationReceived(const std::string& from, const std::map<std::string, std::string>& data);
411
412private:
413 void doRegister1_();
414 void doRegister2_();
415
416 // Initialize the address to be used in contact header. Might
417 // be updated (as the contact header)after the registration.
418 bool initContactAddress();
419 void updateContactHeader();
420
421 void setCredentials(const std::vector<SipAccountConfig::Credentials>& creds);
422
423 void setUpTransmissionData(pjsip_tx_data* tdata, long transportKeyType);
424
426
427 std::shared_ptr<Call> newRegisteredAccountCall(const std::string& id, const std::string& toUrl);
428
434 bool SIPStartCall(std::shared_ptr<SIPCall>& call);
435
436 void usePublishedAddressPortInVIA();
437 void useUPnPAddressPortInVIA();
438 bool fullMatch(std::string_view username, std::string_view hostname) const;
439 bool userMatch(std::string_view username) const;
440 bool hostnameMatch(std::string_view hostname) const;
441 bool proxyMatch(std::string_view hostname) const;
442
447 virtual void onTransportStateChanged(pjsip_transport_state state, const pjsip_transport_state_info* info);
448
449 struct
450 {
453 unsigned attempt_cnt {0};
454 } auto_rereg_ {};
456 std::uniform_int_distribution<int> delay10ZeroDist_ {-10000, 10000};
457 std::uniform_int_distribution<unsigned int> delay10PosDist_ {0, 10000};
458
459 void scheduleReregistration();
460 void autoReregTimerCb();
461
462 std::shared_ptr<SipTransport> transport_ {};
463
464 std::shared_ptr<TlsListener> tlsListener_ {};
465
473
480 static pj_uint32_t tlsProtocolFromString(const std::string& method);
481
485 void initTlsConfiguration();
486
491 void trimCiphers();
492
496 void initStunConfiguration();
497
504 static std::string getLoginName();
505
509 bool mapPortUPnP();
510
514 static std::string printContactHeader(const std::string& username,
515 const std::string& displayName,
516 const std::string& address,
518 bool secure,
519 const std::string& deviceKey = {});
520
524 dhtnet::IpAddr hostIp_;
525
529 pjsip_regc* regc_ {nullptr};
530
534 bool bRegister_;
535
540 std::vector<pjsip_cred_info> cred_;
541
545 pjsip_tls_setting tlsSetting_;
546
550 CipherArray ciphers_;
551
555 pj_str_t stunServerName_ {nullptr, 0};
556
560 pj_uint16_t stunPort_ {PJ_STUN_PORT};
561
565 static void onComplete(void* token, pjsip_event* event);
566
571 std::pair<int, std::string> registrationStateDetailed_;
572
576 std::string receivedParameter_;
577
581 int rPort_ {-1};
582
586 pjsip_host_port via_addr_;
587
588 // This is used at runtime . Mainly by SIPAccount::usePublishedAddressPortInVIA()
589 std::string publishedIpStr_ {};
590
595 std::string upnpIpAddr_;
596
597 mutable std::mutex contactMutex_;
598 // Contact header
599 std::string contactHeader_;
600 // Contact address (the address part of a SIP URI)
601 dhtnet::IpAddr contactAddress_ {};
602 pjsip_transport* via_tp_ {nullptr};
603
607 SIPPresence* presence_;
608
615 pj_uint16_t publishedPortUsed_ {sip_utils::DEFAULT_SIP_PORT};
616};
617
618} // namespace jami
const std::string & getAccountID() const
Get the account ID.
Definition account.h:149
void editConfig(std::function< void(AccountConfig &config)> &&edit)
Definition account.h:116
const AccountConfig & config() const
Definition account.h:108
~SIPAccount() noexcept
std::string getStunServer() const
Definition sipaccount.h:255
void destroyRegistrationInfo()
std::string getToUri(const std::string &username) const override
This method adds the correct scheme, hostname and append the ;transport= parameter at the end of the ...
void pushNotificationReceived(const std::string &from, const std::map< std::string, std::string > &data)
To be called by clients with relevant data when a push notification is received.
std::unique_ptr< AccountConfig > buildConfig() const override
Definition sipaccount.h:72
void editConfig(std::function< void(SipAccountConfig &conf)> &&edit)
Definition sipaccount.h:76
bool setPushNotificationConfig(const std::map< std::string, std::string > &data) override
bool isStunEnabled() const override
Definition sipaccount.h:248
void resetAutoRegistration()
pj_bool_t active
Flag of reregister status.
Definition sipaccount.h:451
virtual void setRegistrationState(RegistrationState state, int code=0, const std::string &detail_str={}) override
Set the registration state of the specified link.
void loadConfig() override
Actually useless, since config loading is done in init()
const pjsip_cred_info * getCredInfo() const
Definition sipaccount.h:148
const SipAccountConfig & config() const
Definition sipaccount.h:70
void doUnregister(bool forceShutdownConnections=false) override
Send unregistration.
dhtnet::IpAddr getContactAddress() const
Get the contact address.
pj_uint16_t getStunPort() const override
Definition sipaccount.h:242
static constexpr auto ACCOUNT_TYPE
Definition sipaccount.h:49
unsigned attempt_cnt
Attempt counter.
Definition sipaccount.h:453
pjsip_tpselector getTransportSelector()
Shortcut for SipTransport::getTransportSelector(account.getTransport()).
void updateDialogViaSentBy(pjsip_dialog *dlg)
unsigned getCredentialCount() const
Get the number of credentials defined for this account.
Definition sipaccount.h:156
bool setPushNotificationToken(const std::string &pushDeviceToken="") override
int getRPort() const
Definition sipaccount.h:315
void enablePresence(const bool &enable)
Activate the module.
pjsip_host_port getHostPortFromSTUN(pj_pool_t *pool)
bool isIP2IP() const override
Returns true if this is the IP2IP account.
static const std::vector< std::string > & getSupportedTlsCiphers()
bool isRegistrationRefreshEnabled() const
Definition sipaccount.h:329
void setRPort(int rPort)
Definition sipaccount.h:323
void sendRegister()
Build and send SIP registration request.
std::shared_ptr< SIPCall > newIncomingCall(const std::string &from, const std::vector< libjami::MediaMap > &mediaList, const std::shared_ptr< SipTransport > &sipTr={}) override
Create incoming SIPCall.
MatchRank matches(std::string_view username, std::string_view hostname) const override
std::shared_ptr< SIPAccount > shared()
Definition sipaccount.h:51
pjsip_transport_type_e getTransportType() const
Definition sipaccount.h:335
pjsip_host_port * getViaAddr()
Definition sipaccount.h:313
std::string_view getAccountType() const override
Definition sipaccount.h:81
uint16_t getLocalPort() const
Get the port on which the transport/listener should use, or is actually using.
Definition sipaccount.h:212
dhtnet::IpAddr createBindingAddress()
Create the Ip address that the transport uses.
virtual std::map< std::string, std::string > getVolatileAccountDetails() const override
Retrieve volatile details such as recent registration errors.
bool hasServiceRoute() const
Definition sipaccount.h:301
void setActiveCodecs(const std::vector< unsigned > &list) override
Update both the codec order structure and the codec string used for SDP offer and configuration respe...
std::string getContactHeader() const
Get the contact header.
std::weak_ptr< SIPAccount > weak()
Definition sipaccount.h:56
pj_str_t getStunServerName() const
Definition sipaccount.h:232
bool hasCredentials() const
Definition sipaccount.h:158
SIPPresence * getPresence() const
Presence management.
std::weak_ptr< SIPAccount const > weak() const
Definition sipaccount.h:57
void setLocalPort(uint16_t port)
Definition sipaccount.h:214
bool checkNATAddress(pjsip_regc_cbparam *param, pj_pool_t *pool)
Update NAT address, Via and Contact header from the REGISTER response.
void connectivityChanged() override
Inform the account that the network status has changed.
std::string getFromUri() const override
void setTransport(const std::shared_ptr< SipTransport > &=nullptr)
void setRegistrationInfo(pjsip_regc *regc)
Set the registration structure that is used for PJSIP in the registration process; @pram A pointer to...
Definition sipaccount.h:198
std::shared_ptr< Call > newOutgoingCall(std::string_view toUrl, const std::vector< libjami::MediaMap > &mediaList) override
Create outgoing SIPCall.
void supportPresence(int function, bool enable)
Activate the publish/subscribe.
void sendUnregister()
Build and send SIP unregistration request.
pj_uint16_t getTlsListenerPort() const
Get the local port for TLS listener.
Definition sipaccount.h:230
pj_timer_entry timer
Timer for reregistration.
Definition sipaccount.h:452
static const std::vector< std::string > & getSupportedTlsProtocols()
std::string getServiceRoute() const
Definition sipaccount.h:299
virtual bool isTlsEnabled() const override
Determine if TLS is enabled for this account.
Definition sipaccount.h:303
unsigned getRegistrationExpire() const
A client sendings a REGISTER request MAY suggest an expiration interval that indicates how long the c...
Definition sipaccount.h:173
void setReceivedParameter(const std::string &received)
Definition sipaccount.h:305
virtual void sendMessage(const std::string &to, const std::string &deviceId, const std::map< std::string, std::string > &payloads, uint64_t id, bool retryOnTimeout=true, bool onlyConnected=false) override
void updateProfile(const std::string &displayName, const std::string &avatar, const std::string &fileType, int32_t flag) override
updates SIP account profile
const std::string & getReceivedParameter() const
Definition sipaccount.h:311
void onRegister(pjsip_regc_cbparam *param)
pjsip_tls_setting * getTlsSetting()
Definition sipaccount.h:224
std::map< std::string, std::string > getTlsSettings() const
Return the TLS settings, mainly used to return security information to a client application.
bool isSrtpEnabled() const override
Definition sipaccount.h:402
std::string getUserUri() const override
bool isRegistered() const
Registration flag.
Definition sipaccount.h:182
virtual std::shared_ptr< SipTransport > getTransport()
Definition sipaccount.h:333
void doRegister() override
Initialize the SIP voip link with the account parameters and send registration.
std::vector< std::map< std::string, std::string > > getCredentials() const
Definition sipaccount.h:160
pjsip_regc * getRegistrationInfo()
Get the registration structure that is used for PJSIP in the registration process.
Definition sipaccount.h:190
std::shared_ptr< SIPAccount const > shared() const
Definition sipaccount.h:52
std::string getServerUri() const
In the current version, "srv" uri is obtained in the preformatted way: hostname:port.
static constexpr int DEFAULT_SIP_PORT
Definition sip_utils.h:51
constexpr const pj_str_t CONST_PJ_STR(T(&a)[N]) noexcept
Definition sip_utils.h:87
void emitSignal(Args... args)
Definition jami_signal.h:64
std::vector< pj_ssl_cipher > CipherArray
Definition sipaccount.h:37
RegistrationState
Contains all the Registration states for an account can be in.
static constexpr std::string_view ACCOUNT_TYPE_SIP
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30
std::string stunServer
The STUN server hostname (optional), used to provide the public IP address in case the softphone stay...
std::vector< std::map< std::string, std::string > > getCredentials() const
bool stunEnabled
Determine if STUN public address resolution is required to register this account.
uint16_t localPort
Local port to whih this account is bound.
unsigned registrationExpire
Network settings.
uint16_t tlsListenerPort
The TLS listener port.
std::string serviceRoute
Input Outbound Proxy Server Address.
std::vector< Credentials > credentials
KeyExchangeProtocol srtpKeyExchange
Specifies the type of key exchange used for SRTP, if any.