Ring Daemon 16.0.0
Loading...
Searching...
No Matches
sipaccount.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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 "ring_types.h" // enable_if_base_of
27#include "sipaccount_config.h"
28
29#include <pjsip/sip_transport_tls.h>
30#include <pjsip/sip_types.h>
31#include <pjsip-ua/sip_regc.h>
32
33#include <vector>
34#include <map>
35
36namespace jami {
37
38typedef std::vector<pj_ssl_cipher> CipherArray;
39
40class SIPPresence;
41class SIPCall;
42
48{
49public:
50 constexpr static auto ACCOUNT_TYPE = ACCOUNT_TYPE_SIP;
51
52 std::shared_ptr<SIPAccount> shared()
53 {
54 return std::static_pointer_cast<SIPAccount>(shared_from_this());
55 }
56 std::shared_ptr<SIPAccount const> shared() const
57 {
58 return std::static_pointer_cast<SIPAccount const>(shared_from_this());
59 }
60 std::weak_ptr<SIPAccount> weak()
61 {
62 return std::static_pointer_cast<SIPAccount>(shared_from_this());
63 }
64 std::weak_ptr<SIPAccount const> weak() const
65 {
66 return std::static_pointer_cast<SIPAccount const>(shared_from_this());
67 }
68
73 SIPAccount(const std::string& accountID, bool presenceEnabled);
74
76
78 {
79 return *static_cast<const SipAccountConfig*>(&Account::config());
80 }
81
82 std::unique_ptr<AccountConfig> buildConfig() const override
83 {
84 return std::make_unique<SipAccountConfig>(getAccountID());
85 }
86 inline void editConfig(std::function<void(SipAccountConfig& conf)>&& edit)
87 {
89 [&](AccountConfig& conf) { edit(*static_cast<SipAccountConfig*>(&conf)); });
90 }
91
92 std::string_view getAccountType() const override { return ACCOUNT_TYPE; }
93
95
97
99
107
111 bool isIP2IP() const override;
112
117 virtual std::map<std::string, std::string> getVolatileAccountDetails() const override;
118
123 std::map<std::string, std::string> getTlsSettings() const;
124
128 void loadConfig() override;
129
133 void updateProfile(const std::string& displayName, const std::string& avatar, const std::string& fileType, int32_t flag) override;
134
138 void doRegister() override;
139
143 void doUnregister(bool forceShutdownConnections = false) override;
144
148 void sendRegister();
149
154 void sendUnregister();
155
156 const pjsip_cred_info* getCredInfo() const { return cred_.data(); }
157
164 unsigned getCredentialCount() const { return config().credentials.size(); }
165
166 bool hasCredentials() const { return not config().credentials.empty(); }
167
168 std::vector<std::map<std::string, std::string>> getCredentials() const
169 {
170 return config().getCredentials();
171 }
172
173 virtual void setRegistrationState(RegistrationState state,
174 int code = 0,
175 const std::string& detail_str = {}) override;
176
184 unsigned getRegistrationExpire() const
185 {
186 unsigned re = config().registrationExpire;
188 }
189
193 bool isRegistered() const { return bRegister_; }
194
201 pjsip_regc* getRegistrationInfo() { return regc_; }
202
210 {
211 if (regc_)
213 regc_ = regc;
214 }
215
217
223 uint16_t getLocalPort() const { return config().localPort; }
224
229
235 pjsip_tls_setting* getTlsSetting() { return &tlsSetting_; }
236
242
243 pj_str_t getStunServerName() const { return stunServerName_; }
244
245 static const std::vector<std::string>& getSupportedTlsCiphers();
246 static const std::vector<std::string>& getSupportedTlsProtocols();
247
253 pj_uint16_t getStunPort() const override { return stunPort_; }
254
259 bool isStunEnabled() const override { return config().stunEnabled; }
260
266 std::string getStunServer() const { return config().stunServer; }
267
277 std::string getFromUri() const override;
278
287 std::string getToUri(const std::string& username) const override;
288
297 std::string getServerUri() const;
298
303 dhtnet::IpAddr getContactAddress() const;
308 std::string getContactHeader() const;
309
310 std::string getServiceRoute() const { return config().serviceRoute; }
311
312 bool hasServiceRoute() const { return not config().serviceRoute.empty(); }
313
314 virtual bool isTlsEnabled() const override { return config().tlsEnable; }
315
316 virtual bool getSrtpFallback() const override { return config().srtpFallback; }
317
318 void setReceivedParameter(const std::string& received)
319 {
320 receivedParameter_ = received;
321 via_addr_.host = sip_utils::CONST_PJ_STR(receivedParameter_);
322 }
323
324 const std::string& getReceivedParameter() const { return receivedParameter_; }
325
326 pjsip_host_port* getViaAddr() { return &via_addr_; }
327
328 int getRPort() const
329 {
330 if (rPort_ == -1)
331 return config().localPort;
332 else
333 return rPort_;
334 }
335
336 void setRPort(int rPort)
337 {
338 rPort_ = rPort;
339 via_addr_.port = rPort;
340 }
341
343
344 void setTransport(const std::shared_ptr<SipTransport>& = nullptr);
345
346 virtual inline std::shared_ptr<SipTransport> getTransport() { return transport_; }
347
348 inline pjsip_transport_type_e getTransportType() const { return transportType_; }
349
354
355 /* Returns true if the username and/or hostname match this account */
356 MatchRank matches(std::string_view username, std::string_view hostname) const override;
357
361 SIPPresence* getPresence() const;
362
368 void enablePresence(const bool& enable);
373 void supportPresence(int function, bool enable);
374
381 std::shared_ptr<Call> newOutgoingCall(std::string_view toUrl,
382 const std::vector<libjami::MediaMap>& mediaList) override;
383
391 std::shared_ptr<SIPCall> newIncomingCall(
392 const std::string& from,
393 const std::vector<libjami::MediaMap>& mediaList,
394 const std::shared_ptr<SipTransport>& sipTr = {}) override;
395
397
398 virtual void sendMessage(const std::string& to,
399 const std::string& deviceId,
400 const std::map<std::string, std::string>& payloads,
401 uint64_t id,
402 bool retryOnTimeout = true,
403 bool onlyConnected = false) override;
404
405 void connectivityChanged() override;
406
407 std::string getUserUri() const override;
408
413 dhtnet::IpAddr createBindingAddress();
414
415 void setActiveCodecs(const std::vector<unsigned>& list) override;
416 bool isSrtpEnabled() const override
417 {
419 }
420
421 bool setPushNotificationToken(const std::string& pushDeviceToken = "") override;
422 bool setPushNotificationConfig(const std::map<std::string, std::string>& data) override;
423
427 void pushNotificationReceived(const std::string& from,
428 const std::map<std::string, std::string>& data);
429
430private:
431 void doRegister1_();
432 void doRegister2_();
433
434 // Initialize the address to be used in contact header. Might
435 // be updated (as the contact header)after the registration.
436 bool initContactAddress();
437 void updateContactHeader();
438
439 void setCredentials(const std::vector<SipAccountConfig::Credentials>& creds);
440
441 void setUpTransmissionData(pjsip_tx_data* tdata, long transportKeyType);
442
444
445 std::shared_ptr<Call> newRegisteredAccountCall(const std::string& id, const std::string& toUrl);
446
452 bool SIPStartCall(std::shared_ptr<SIPCall>& call);
453
454 void usePublishedAddressPortInVIA();
455 void useUPnPAddressPortInVIA();
456 bool fullMatch(std::string_view username, std::string_view hostname) const;
457 bool userMatch(std::string_view username) const;
458 bool hostnameMatch(std::string_view hostname) const;
459 bool proxyMatch(std::string_view hostname) const;
460
465 virtual void onTransportStateChanged(pjsip_transport_state state,
466 const pjsip_transport_state_info* info);
467
468 struct
469 {
472 unsigned attempt_cnt {0};
473 } auto_rereg_ {};
475 std::uniform_int_distribution<int> delay10ZeroDist_ {-10000, 10000};
476 std::uniform_int_distribution<unsigned int> delay10PosDist_ {0, 10000};
477
478 void scheduleReregistration();
479 void autoReregTimerCb();
480
481 std::shared_ptr<SipTransport> transport_ {};
482
483 std::shared_ptr<TlsListener> tlsListener_ {};
484
492
499 static pj_uint32_t tlsProtocolFromString(const std::string& method);
500
504 void initTlsConfiguration();
505
510 void trimCiphers();
511
515 void initStunConfiguration();
516
523 static std::string getLoginName();
524
528 bool mapPortUPnP();
529
533 static std::string printContactHeader(const std::string& username,
534 const std::string& displayName,
535 const std::string& address,
537 bool secure,
538 const std::string& deviceKey = {});
539
543 dhtnet::IpAddr hostIp_;
544
548 pjsip_regc* regc_ {nullptr};
549
553 bool bRegister_;
554
559 std::vector<pjsip_cred_info> cred_;
560
564 pjsip_tls_setting tlsSetting_;
565
569 CipherArray ciphers_;
570
574 pj_str_t stunServerName_ {nullptr, 0};
575
579 pj_uint16_t stunPort_ {PJ_STUN_PORT};
580
584 static void onComplete(void* token, pjsip_event* event);
585
590 std::pair<int, std::string> registrationStateDetailed_;
591
595 std::string receivedParameter_;
596
600 int rPort_ {-1};
601
605 pjsip_host_port via_addr_;
606
607 // This is used at runtime . Mainly by SIPAccount::usePublishedAddressPortInVIA()
608 std::string publishedIpStr_ {};
609
614 std::string upnpIpAddr_;
615
616 mutable std::mutex contactMutex_;
617 // Contact header
618 std::string contactHeader_;
619 // Contact address (the address part of a SIP URI)
620 dhtnet::IpAddr contactAddress_ {};
621 pjsip_transport* via_tp_ {nullptr};
622
626 SIPPresence* presence_;
627
634 pj_uint16_t publishedPortUsed_ {sip_utils::DEFAULT_SIP_PORT};
635};
636
637} // namespace jami
const std::string & getAccountID() const
Get the account ID.
Definition account.h:154
void editConfig(std::function< void(AccountConfig &config)> &&edit)
Definition account.h:121
const AccountConfig & config() const
Definition account.h:113
~SIPAccount() noexcept
std::string getStunServer() const
Definition sipaccount.h:266
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:82
void editConfig(std::function< void(SipAccountConfig &conf)> &&edit)
Definition sipaccount.h:86
bool setPushNotificationConfig(const std::map< std::string, std::string > &data) override
bool isStunEnabled() const override
Definition sipaccount.h:259
void resetAutoRegistration()
pj_bool_t active
Flag of reregister status.
Definition sipaccount.h:470
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:156
const SipAccountConfig & config() const
Definition sipaccount.h:77
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:253
static constexpr auto ACCOUNT_TYPE
Definition sipaccount.h:50
unsigned attempt_cnt
Attempt counter.
Definition sipaccount.h:472
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:164
bool setPushNotificationToken(const std::string &pushDeviceToken="") override
int getRPort() const
Definition sipaccount.h:328
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:342
void setRPort(int rPort)
Definition sipaccount.h:336
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:52
pjsip_transport_type_e getTransportType() const
Definition sipaccount.h:348
pjsip_host_port * getViaAddr()
Definition sipaccount.h:326
std::string_view getAccountType() const override
Definition sipaccount.h:92
uint16_t getLocalPort() const
Get the port on which the transport/listener should use, or is actually using.
Definition sipaccount.h:223
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:312
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:60
pj_str_t getStunServerName() const
Definition sipaccount.h:243
bool hasCredentials() const
Definition sipaccount.h:166
SIPPresence * getPresence() const
Presence management.
std::weak_ptr< SIPAccount const > weak() const
Definition sipaccount.h:64
void setLocalPort(uint16_t port)
Definition sipaccount.h:225
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:209
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:241
pj_timer_entry timer
Timer for reregistration.
Definition sipaccount.h:471
static const std::vector< std::string > & getSupportedTlsProtocols()
std::string getServiceRoute() const
Definition sipaccount.h:310
virtual bool isTlsEnabled() const override
Determine if TLS is enabled for this account.
Definition sipaccount.h:314
unsigned getRegistrationExpire() const
A client sendings a REGISTER request MAY suggest an expiration interval that indicates how long the c...
Definition sipaccount.h:184
void setReceivedParameter(const std::string &received)
Definition sipaccount.h:318
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:324
void onRegister(pjsip_regc_cbparam *param)
pjsip_tls_setting * getTlsSetting()
Definition sipaccount.h:235
std::map< std::string, std::string > getTlsSettings() const
Return the TLS settings, mainly used to return security information to a client application.
virtual bool getSrtpFallback() const override
Definition sipaccount.h:316
bool isSrtpEnabled() const override
Definition sipaccount.h:416
std::string getUserUri() const override
bool isRegistered() const
Registration flag.
Definition sipaccount.h:193
virtual std::shared_ptr< SipTransport > getTransport()
Definition sipaccount.h:346
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:168
pjsip_regc * getRegistrationInfo()
Get the registration structure that is used for PJSIP in the registration process.
Definition sipaccount.h:201
std::shared_ptr< SIPAccount const > shared() const
Definition sipaccount.h:56
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:53
constexpr const pj_str_t CONST_PJ_STR(T(&a)[N]) noexcept
Definition sip_utils.h:89
RegistrationState
Contains all the Registration states for an account can be in.
void emitSignal(Args... args)
Definition ring_signal.h:64
std::vector< pj_ssl_cipher > CipherArray
Definition sipaccount.h:38
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...
bool srtpFallback
Determine if the softphone should fallback on non secured media channel if SRTP negotiation fails.
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.