LCOV - code coverage report
Current view: top level - src/sip - sipaccount.h (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 84.2 % 38 32
Test Date: 2026-06-13 09:18:46 Functions: 79.3 % 29 23

            Line data    Source code
       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              : 
      35              : namespace jami {
      36              : 
      37              : typedef std::vector<pj_ssl_cipher> CipherArray;
      38              : 
      39              : class SIPPresence;
      40              : class SIPCall;
      41              : 
      42              : /**
      43              :  * @file sipaccount.h
      44              :  * @brief A SIP Account specify SIP specific functions and object = SIPCall/SIPVoIPLink)
      45              :  */
      46              : class SIPAccount : public SIPAccountBase
      47              : {
      48              : public:
      49              :     constexpr static auto ACCOUNT_TYPE = ACCOUNT_TYPE_SIP;
      50              : 
      51           20 :     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            9 :     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              : 
      62              :     /**
      63              :      * Constructor
      64              :      * @param accountID The account identifier
      65              :      */
      66              :     SIPAccount(const std::string& accountID, bool presenceEnabled);
      67              : 
      68              :     ~SIPAccount() noexcept;
      69              : 
      70          909 :     const SipAccountConfig& config() const { return *static_cast<const SipAccountConfig*>(&Account::config()); }
      71              : 
      72           24 :     std::unique_ptr<AccountConfig> buildConfig() const override
      73              :     {
      74           24 :         return std::make_unique<SipAccountConfig>(getAccountID());
      75              :     }
      76           21 :     inline void editConfig(std::function<void(SipAccountConfig& conf)>&& edit)
      77              :     {
      78           42 :         Account::editConfig([&](AccountConfig& conf) { edit(*static_cast<SipAccountConfig*>(&conf)); });
      79           21 :     }
      80              : 
      81           36 :     std::string_view getAccountType() const override { return ACCOUNT_TYPE; }
      82              : 
      83              :     pjsip_host_port getHostPortFromSTUN(pj_pool_t* pool);
      84              : 
      85              :     void updateDialogViaSentBy(pjsip_dialog* dlg);
      86              : 
      87              :     void resetAutoRegistration();
      88              : 
      89              :     /**
      90              :      * Update NAT address, Via and Contact header from the REGISTER response
      91              :      * @param param pjsip reg cbparam
      92              :      * @param pool
      93              :      * @return update status
      94              :      */
      95              :     bool checkNATAddress(pjsip_regc_cbparam* param, pj_pool_t* pool);
      96              : 
      97              :     /**
      98              :      * Returns true if this is the IP2IP account
      99              :      */
     100              :     bool isIP2IP() const override;
     101              : 
     102              :     /**
     103              :      * Retrieve volatile details such as recent registration errors
     104              :      * @return std::map< std::string, std::string > The account volatile details
     105              :      */
     106              :     virtual std::map<std::string, std::string> getVolatileAccountDetails() const override;
     107              : 
     108              :     /**
     109              :      * Return the TLS settings, mainly used to return security information to
     110              :      * a client application
     111              :      */
     112              :     std::map<std::string, std::string> getTlsSettings() const;
     113              : 
     114              :     /**
     115              :      * Actually useless, since config loading is done in init()
     116              :      */
     117              :     void loadConfig() override;
     118              : 
     119              :     /**
     120              :      * updates SIP account profile
     121              :      */
     122              :     void updateProfile(const std::string& displayName,
     123              :                        const std::string& avatar,
     124              :                        const std::string& fileType,
     125              :                        int32_t flag) override;
     126              : 
     127              :     /**
     128              :      * Initialize the SIP voip link with the account parameters and send registration
     129              :      */
     130              :     void doRegister() override;
     131              : 
     132              :     /**
     133              :      * Send unregistration.
     134              :      */
     135              :     void doUnregister(bool forceShutdownConnections = false) override;
     136              : 
     137              :     /**
     138              :      * Build and send SIP registration request
     139              :      */
     140              :     void sendRegister();
     141              : 
     142              :     /**
     143              :      * Build and send SIP unregistration request
     144              :      * @param destroy_transport If true, attempt to destroy the transport.
     145              :      */
     146              :     void sendUnregister();
     147              : 
     148           15 :     const pjsip_cred_info* getCredInfo() const { return cred_.data(); }
     149              : 
     150              :     /**
     151              :      * Get the number of credentials defined for
     152              :      * this account.
     153              :      * @param none
     154              :      * @return int The number of credentials set for this account.
     155              :      */
     156           15 :     unsigned getCredentialCount() const { return config().credentials.size(); }
     157              : 
     158           10 :     bool hasCredentials() const { return not config().credentials.empty(); }
     159              : 
     160            0 :     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              : 
     166              :     /**
     167              :      * A client sendings a REGISTER request MAY suggest an expiration
     168              :      * interval that indicates how long the client would like the
     169              :      * registration to be valid.
     170              :      *
     171              :      * @return the expiration value.
     172              :      */
     173            5 :     unsigned getRegistrationExpire() const
     174              :     {
     175            5 :         unsigned re = config().registrationExpire;
     176            5 :         return re ? re : PJSIP_REGC_EXPIRATION_NOT_SPECIFIED;
     177              :     }
     178              : 
     179              :     /**
     180              :      * Registration flag
     181              :      */
     182            6 :     bool isRegistered() const { return bRegister_; }
     183              : 
     184              :     /**
     185              :      * Get the registration structure that is used
     186              :      * for PJSIP in the registration process.
     187              :      * Settings are loaded from configuration file.
     188              :      * @return pjsip_regc* A pointer to the registration structure
     189              :      */
     190            6 :     pjsip_regc* getRegistrationInfo() { return regc_; }
     191              : 
     192              :     /**
     193              :      * Set the registration structure that is used
     194              :      * for PJSIP in the registration process;
     195              :      * @pram A pointer to the new registration structure
     196              :      * @return void
     197              :      */
     198            5 :     void setRegistrationInfo(pjsip_regc* regc)
     199              :     {
     200            5 :         if (regc_)
     201            3 :             destroyRegistrationInfo();
     202            5 :         regc_ = regc;
     203            5 :     }
     204              : 
     205              :     void destroyRegistrationInfo();
     206              : 
     207              :     /**
     208              :      * Get the port on which the transport/listener should use, or is
     209              :      * actually using.
     210              :      * @return pj_uint16 The port used for that account
     211              :      */
     212              :     uint16_t getLocalPort() const { return config().localPort; }
     213              : 
     214           21 :     void setLocalPort(uint16_t port)
     215              :     {
     216           21 :         editConfig([&](SipAccountConfig& config) { config.localPort = port; });
     217           21 :     }
     218              : 
     219              :     /**
     220              :      * @return pjsip_tls_setting structure, filled from the configuration
     221              :      * file, that can be used directly by PJSIP to initialize
     222              :      * TLS transport.
     223              :      */
     224            0 :     pjsip_tls_setting* getTlsSetting() { return &tlsSetting_; }
     225              : 
     226              :     /**
     227              :      * Get the local port for TLS listener.
     228              :      * @return pj_uint16 The port used for that account
     229              :      */
     230              :     pj_uint16_t getTlsListenerPort() const { return config().tlsListenerPort; }
     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              : 
     237              :     /**
     238              :      * @return pj_uint8_t structure, filled from the configuration
     239              :      * file, that can be used directly by PJSIP to initialize
     240              :      * an alternate UDP transport.
     241              :      */
     242            0 :     pj_uint16_t getStunPort() const override { return stunPort_; }
     243              : 
     244              :     /**
     245              :      * @return bool Tells if current transport for that
     246              :      * account is set to OTHER.
     247              :      */
     248           25 :     bool isStunEnabled() const override { return config().stunEnabled; }
     249              : 
     250              :     /**
     251              :      * @return pj_str_t , filled from the configuration
     252              :      * file, that can be used directly by PJSIP to initialize
     253              :      * an alternate UDP transport.
     254              :      */
     255              :     std::string getStunServer() const { return config().stunServer; }
     256              : 
     257              :     /**
     258              :      * @return pj_str_t "From" uri based on account information.
     259              :      * From RFC3261: "The To header field first and foremost specifies the desired
     260              :      * logical" recipient of the request, or the address-of-record of the
     261              :      * user or resource that is the target of this request. [...]  As such, it is
     262              :      * very important that the From URI not contain IP addresses or the FQDN
     263              :      * of the host on which the UA is running, since these are not logical
     264              :      * names."
     265              :      */
     266              :     std::string getFromUri() const override;
     267              : 
     268              :     /**
     269              :      * This method adds the correct scheme, hostname and append
     270              :      * the ;transport= parameter at the end of the uri, in accordance with RFC3261.
     271              :      * It is expected that "port" is present in the internal hostname_.
     272              :      *
     273              :      * @return pj_str_t "To" uri based on @param username
     274              :      * @param username A string formatted as : "username"
     275              :      */
     276              :     std::string getToUri(const std::string& username) const override;
     277              : 
     278              :     /**
     279              :      * In the current version, "srv" uri is obtained in the preformatted
     280              :      * way: hostname:port. This method adds the correct scheme and append
     281              :      * the ;transport= parameter at the end of the uri, in accordance with RFC3261.
     282              :      *
     283              :      * @return pj_str_t "server" uri based on @param hostPort
     284              :      * @param hostPort A string formatted as : "hostname:port"
     285              :      */
     286              :     std::string getServerUri() const;
     287              : 
     288              :     /**
     289              :      * Get the contact address
     290              :      * @return The current contact address
     291              :      */
     292              :     dhtnet::IpAddr getContactAddress() const;
     293              :     /**
     294              :      * Get the contact header
     295              :      * @return The current contact header
     296              :      */
     297              :     std::string getContactHeader() const;
     298              : 
     299            0 :     std::string getServiceRoute() const { return config().serviceRoute; }
     300              : 
     301           23 :     bool hasServiceRoute() const { return not config().serviceRoute.empty(); }
     302              : 
     303           14 :     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            5 :     const std::string& getReceivedParameter() const { return receivedParameter_; }
     312              : 
     313            0 :     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              : 
     329            5 :     bool isRegistrationRefreshEnabled() const { return config().registrationRefreshEnabled; }
     330              : 
     331              :     void setTransport(const std::shared_ptr<SipTransport>& = nullptr);
     332              : 
     333            0 :     virtual inline std::shared_ptr<SipTransport> getTransport() { return transport_; }
     334              : 
     335            1 :     inline pjsip_transport_type_e getTransportType() const { return transportType_; }
     336              : 
     337              :     /**
     338              :      * Shortcut for SipTransport::getTransportSelector(account.getTransport()).
     339              :      */
     340              :     pjsip_tpselector getTransportSelector();
     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              : 
     345              :     /**
     346              :      * Presence management
     347              :      */
     348              :     SIPPresence* getPresence() const;
     349              : 
     350              :     /**
     351              :      * Activate the module.
     352              :      * @param function Publish or subscribe to enable
     353              :      * @param enable Flag
     354              :      */
     355              :     void enablePresence(const bool& enable);
     356              :     /**
     357              :      * Activate the publish/subscribe.
     358              :      * @param enable Flag
     359              :      */
     360              :     void supportPresence(int function, bool enable);
     361              : 
     362              :     /**
     363              :      * Create outgoing SIPCall.
     364              :      * @param[in] toUrl the address to call
     365              :      * @param[in] mediaList list of medias
     366              :      * @return a shared pointer on the created call.
     367              :      */
     368              :     std::shared_ptr<Call> newOutgoingCall(std::string_view toUrl,
     369              :                                           const std::vector<libjami::MediaMap>& mediaList) override;
     370              : 
     371              :     /**
     372              :      * Create incoming SIPCall.
     373              :      * @param[in] from The origin of the call
     374              :      * @param mediaList A list of media
     375              :      * @param sipTr: SIP Transport
     376              :      * @return A shared pointer on the created call.
     377              :      */
     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              : 
     382              :     void onRegister(pjsip_regc_cbparam* param);
     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              : 
     395              :     /**
     396              :      * Create the Ip address that the transport uses
     397              :      * @return IpAddr created
     398              :      */
     399              :     dhtnet::IpAddr createBindingAddress();
     400              : 
     401              :     void setActiveCodecs(const std::vector<unsigned>& list) override;
     402           32 :     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              : 
     407              :     /**
     408              :      * To be called by clients with relevant data when a push notification is received.
     409              :      */
     410              :     void pushNotificationReceived(const std::string& from, const std::map<std::string, std::string>& data);
     411              : 
     412              : private:
     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              : 
     425              :     NON_COPYABLE(SIPAccount);
     426              : 
     427              :     std::shared_ptr<Call> newRegisteredAccountCall(const std::string& id, const std::string& toUrl);
     428              : 
     429              :     /**
     430              :      * Start a SIP Call
     431              :      * @param call  The current call
     432              :      * @return true if all is correct
     433              :      */
     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              : 
     443              :     /**
     444              :      * Callback called by the transport layer when the registration
     445              :      * transport state changes.
     446              :      */
     447              :     virtual void onTransportStateChanged(pjsip_transport_state state, const pjsip_transport_state_info* info);
     448              : 
     449              :     struct
     450              :     {
     451              :         pj_bool_t active {false}; /**< Flag of reregister status. */
     452              :         pj_timer_entry timer {};  /**< Timer for reregistration.  */
     453              :         unsigned attempt_cnt {0}; /**< Attempt counter.     */
     454              :     } auto_rereg_ {};             /**< Reregister/reconnect data. */
     455              : 
     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              : 
     466              :     /**
     467              :      * Transport type used for this sip account. Currently supported types:
     468              :      *    PJSIP_TRANSPORT_UNSPECIFIED
     469              :      *    PJSIP_TRANSPORT_UDP
     470              :      *    PJSIP_TRANSPORT_TLS
     471              :      */
     472              :     pjsip_transport_type_e transportType_ {PJSIP_TRANSPORT_UNSPECIFIED};
     473              : 
     474              :     /**
     475              :      * Maps a string description of the SSL method
     476              :      * to the corresponding enum value in pjsip_ssl_method.
     477              :      * @param method The string representation
     478              :      * @return pjsip_ssl_method The corresponding value in the enum
     479              :      */
     480              :     static pj_uint32_t tlsProtocolFromString(const std::string& method);
     481              : 
     482              :     /**
     483              :      * Initializes tls settings from configuration file.
     484              :      */
     485              :     void initTlsConfiguration();
     486              : 
     487              :     /**
     488              :      * PJSIP aborts if the string length of our cipher list is too
     489              :      * great, so this function forces our cipher list to fit this constraint.
     490              :      */
     491              :     void trimCiphers();
     492              : 
     493              :     /**
     494              :      * Initializes STUN config from the config file
     495              :      */
     496              :     void initStunConfiguration();
     497              : 
     498              :     /**
     499              :      * If username is not provided, as it happens for Direct ip calls,
     500              :      * fetch the Real Name field of the user that is currently
     501              :      * running this program.
     502              :      * @return std::string The login name under which the software is running.
     503              :      */
     504              :     static std::string getLoginName();
     505              : 
     506              :     /**
     507              :      * Maps require port via UPnP
     508              :      */
     509              :     bool mapPortUPnP();
     510              : 
     511              :     /**
     512              :      * Print contact header in certain format
     513              :      */
     514              :     static std::string printContactHeader(const std::string& username,
     515              :                                           const std::string& displayName,
     516              :                                           const std::string& address,
     517              :                                           pj_uint16_t port,
     518              :                                           bool secure,
     519              :                                           const std::string& deviceKey = {});
     520              : 
     521              :     /**
     522              :      * Resolved IP of hostname_ (for registration)
     523              :      */
     524              :     dhtnet::IpAddr hostIp_;
     525              : 
     526              :     /**
     527              :      * The pjsip client registration information
     528              :      */
     529              :     pjsip_regc* regc_ {nullptr};
     530              : 
     531              :     /**
     532              :      * To check if the account is registered
     533              :      */
     534              :     bool bRegister_;
     535              : 
     536              :     /**
     537              :      * Credential information stored for further registration.
     538              :      * Points to credentials_ members.
     539              :      */
     540              :     std::vector<pjsip_cred_info> cred_;
     541              : 
     542              :     /**
     543              :      * The TLS settings, used only if tls is chosen as a sip transport.
     544              :      */
     545              :     pjsip_tls_setting tlsSetting_;
     546              : 
     547              :     /**
     548              :      * Allocate a vector to be used by pjsip to store the supported ciphers on this system.
     549              :      */
     550              :     CipherArray ciphers_;
     551              : 
     552              :     /**
     553              :      * The STUN server name (hostname)
     554              :      */
     555              :     pj_str_t stunServerName_ {nullptr, 0};
     556              : 
     557              :     /**
     558              :      * The STUN server port
     559              :      */
     560              :     pj_uint16_t stunPort_ {PJ_STUN_PORT};
     561              : 
     562              :     /**
     563              :      * Send Request Callback
     564              :      */
     565              :     static void onComplete(void* token, pjsip_event* event);
     566              : 
     567              :     /**
     568              :      * Details about the registration state.
     569              :      * This is a protocol Code:Description pair.
     570              :      */
     571              :     std::pair<int, std::string> registrationStateDetailed_;
     572              : 
     573              :     /**
     574              :      * Optional: "received" parameter from VIA header
     575              :      */
     576              :     std::string receivedParameter_;
     577              : 
     578              :     /**
     579              :      * Optional: "rport" parameter from VIA header
     580              :      */
     581              :     int rPort_ {-1};
     582              : 
     583              :     /**
     584              :      * Optional: via_addr construct from received parameters
     585              :      */
     586              :     pjsip_host_port via_addr_;
     587              : 
     588              :     // This is used at runtime . Mainly by SIPAccount::usePublishedAddressPortInVIA()
     589              :     std::string publishedIpStr_ {};
     590              : 
     591              :     /**
     592              :      * Temporary storage for getUPnPIpAddress().toString()
     593              :      * Used only by useUPnPAddressPortInVIA().
     594              :      */
     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              : 
     604              :     /**
     605              :      * Presence data structure
     606              :      */
     607              :     SIPPresence* presence_;
     608              : 
     609              :     /**
     610              :      * SIP port actually used,
     611              :      * this holds the actual port used for SIP, which may not be the port
     612              :      * selected in the configuration in the case that UPnP is used and the
     613              :      * configured port is already used by another client
     614              :      */
     615              :     pj_uint16_t publishedPortUsed_ {sip_utils::DEFAULT_SIP_PORT};
     616              : };
     617              : 
     618              : } // namespace jami
        

Generated by: LCOV version 2.0-1