LCOV - code coverage report
Current view: top level - foo/src/sip - siptransport.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 8 18 44.4 %
Date: 2025-10-16 08:11:43 Functions: 8 13 61.5 %

          Line data    Source code
       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 "connectivity/sip_utils.h"
      24             : 
      25             : #include "noncopyable.h"
      26             : #include "logger.h"
      27             : 
      28             : #include <pjsip.h>
      29             : #include <pjnath/stun_config.h>
      30             : 
      31             : #include <functional>
      32             : #include <mutex>
      33             : #include <condition_variable>
      34             : #include <map>
      35             : #include <string>
      36             : #include <vector>
      37             : #include <list>
      38             : #include <memory>
      39             : 
      40             : // OpenDHT
      41             : namespace dht {
      42             : namespace crypto {
      43             : struct Certificate;
      44             : }
      45             : } // namespace dht
      46             : 
      47             : namespace dhtnet {
      48             : class ChannelSocket;
      49             : } // namespace dhtnet
      50             : 
      51             : namespace jami {
      52             : class SIPAccountBase;
      53             : using onShutdownCb = std::function<void(void)>;
      54             : 
      55             : struct TlsListener
      56             : {
      57             :     TlsListener() {}
      58           0 :     TlsListener(pjsip_tpfactory* f)
      59           0 :         : listener(f)
      60           0 :     {}
      61           0 :     virtual ~TlsListener()
      62           0 :     {
      63           0 :         JAMI_DBG("Destroying listener");
      64           0 :         listener->destroy(listener);
      65           0 :     }
      66           0 :     pjsip_tpfactory* get() { return listener; }
      67             : 
      68             : private:
      69             :     NON_COPYABLE(TlsListener);
      70             :     pjsip_tpfactory* listener {nullptr};
      71             : };
      72             : 
      73             : struct TlsInfos
      74             : {
      75             :     pj_ssl_cipher cipher {PJ_TLS_UNKNOWN_CIPHER};
      76             :     pj_ssl_sock_proto proto {PJ_SSL_SOCK_PROTO_DEFAULT};
      77             :     pj_ssl_cert_verify_flag_t verifyStatus {};
      78             :     std::shared_ptr<dht::crypto::Certificate> peerCert {};
      79             : };
      80             : 
      81             : using SipTransportStateCallback
      82             :     = std::function<void(pjsip_transport_state, const pjsip_transport_state_info*)>;
      83             : 
      84             : /**
      85             :  * SIP transport wraps pjsip_transport.
      86             :  */
      87             : class SipTransport
      88             : {
      89             : public:
      90             :     SipTransport(pjsip_transport*);
      91             :     SipTransport(pjsip_transport*, const std::shared_ptr<TlsListener>&);
      92             :     // If the SipTransport is a channeled transport, we are already connected to the peer,
      93             :     // so, we can directly set tlsInfos_.peerCert and avoid any copy
      94             :     SipTransport(pjsip_transport* t,
      95             :                  const std::shared_ptr<dht::crypto::Certificate>& peerCertficate);
      96             : 
      97             :     ~SipTransport();
      98             : 
      99             :     static const char* stateToStr(pjsip_transport_state state);
     100             : 
     101             :     void stateCallback(pjsip_transport_state state, const pjsip_transport_state_info* info);
     102             : 
     103         845 :     pjsip_transport* get() { return transport_.get(); }
     104             : 
     105             :     void addStateListener(uintptr_t lid, SipTransportStateCallback cb);
     106             :     bool removeStateListener(uintptr_t lid);
     107             : 
     108         620 :     bool isSecure() const { return PJSIP_TRANSPORT_IS_SECURE(transport_); }
     109             : 
     110         142 :     const TlsInfos& getTlsInfos() const { return tlsInfos_; }
     111             : 
     112             :     static bool isAlive(pjsip_transport_state state);
     113             : 
     114             :     /** Only makes sense for connection-oriented transports */
     115           0 :     bool isConnected() const noexcept { return connected_; }
     116             : 
     117         188 :     inline void setDeviceId(const std::string& deviceId) { deviceId_ = deviceId; }
     118         227 :     inline std::string_view deviceId() const { return deviceId_; }
     119         188 :     inline void setAccount(const std::shared_ptr<SIPAccountBase>& account) { account_ = account; }
     120         112 :     inline const std::weak_ptr<SIPAccountBase>& getAccount() const { return account_; }
     121             : 
     122             :     uint16_t getTlsMtu();
     123             : 
     124             : private:
     125             :     NON_COPYABLE(SipTransport);
     126             : 
     127             :     static void deleteTransport(pjsip_transport* t);
     128             :     struct TransportDeleter
     129             :     {
     130         212 :         void operator()(pjsip_transport* t) const noexcept { deleteTransport(t); }
     131             :     };
     132             : 
     133             :     std::unique_ptr<pjsip_transport, TransportDeleter> transport_;
     134             :     std::shared_ptr<TlsListener> tlsListener_;
     135             :     std::mutex stateListenersMutex_;
     136             :     std::map<uintptr_t, SipTransportStateCallback> stateListeners_;
     137             :     std::weak_ptr<SIPAccountBase> account_ {};
     138             : 
     139             :     bool connected_ {false};
     140             :     std::string deviceId_ {};
     141             :     TlsInfos tlsInfos_;
     142             : };
     143             : 
     144             : class IceTransport;
     145             : namespace tls {
     146             : struct TlsParams;
     147             : };
     148             : 
     149             : /**
     150             :  * Manages the transports and receive callbacks from PJSIP
     151             :  */
     152             : class SipTransportBroker
     153             : {
     154             : public:
     155             :     SipTransportBroker(pjsip_endpoint* endpt);
     156             :     ~SipTransportBroker();
     157             : 
     158             :     std::shared_ptr<SipTransport> getUdpTransport(const dhtnet::IpAddr&);
     159             : 
     160             :     std::shared_ptr<TlsListener> getTlsListener(const dhtnet::IpAddr&, const pjsip_tls_setting*);
     161             : 
     162             :     std::shared_ptr<SipTransport> getTlsTransport(const std::shared_ptr<TlsListener>&,
     163             :                                                   const dhtnet::IpAddr& remote,
     164             :                                                   const std::string& remote_name = {});
     165             : 
     166             :     std::shared_ptr<SipTransport> addTransport(pjsip_transport*);
     167             : 
     168             :     std::shared_ptr<SipTransport> getChanneledTransport(
     169             :         const std::shared_ptr<SIPAccountBase>& account,
     170             :         const std::shared_ptr<dhtnet::ChannelSocket>& socket,
     171             :         onShutdownCb&& cb);
     172             : 
     173             :     /**
     174             :      * Start graceful shutdown procedure for all transports
     175             :      */
     176             :     void shutdown();
     177             : 
     178             :     void transportStateChanged(pjsip_transport*,
     179             :                                pjsip_transport_state,
     180             :                                const pjsip_transport_state_info*);
     181             : 
     182             : private:
     183             :     NON_COPYABLE(SipTransportBroker);
     184             : 
     185             :     /**
     186             :      * Create SIP UDP transport from account's setting
     187             :      * @param account The account for which a transport must be created.
     188             :      * @param IP protocol version to use, can be pj_AF_INET() or pj_AF_INET6()
     189             :      * @return a pointer to the new transport
     190             :      */
     191             :     std::shared_ptr<SipTransport> createUdpTransport(const dhtnet::IpAddr&);
     192             : 
     193             :     /**
     194             :      * List of transports so we can bubble the events up.
     195             :      */
     196             :     std::map<pjsip_transport*, std::weak_ptr<SipTransport>> transports_ {};
     197             :     std::mutex transportMapMutex_ {};
     198             : 
     199             :     /**
     200             :      * Transports are stored in this map in order to retrieve them in case
     201             :      * several accounts would share the same port number.
     202             :      */
     203             :     std::map<dhtnet::IpAddr, pjsip_transport*> udpTransports_;
     204             : 
     205             :     pjsip_endpoint* endpt_;
     206             :     std::atomic_bool isDestroying_ {false};
     207             : };
     208             : 
     209             : } // namespace jami

Generated by: LCOV version 1.14