LCOV - code coverage report
Current view: top level - src/sip - siptransport.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 7 17 41.2 %
Date: 2024-12-21 08:56:24 Functions: 7 12 58.3 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 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        1143 :     pjsip_transport* get() { return transport_.get(); }
     104             : 
     105             :     void addStateListener(uintptr_t lid, SipTransportStateCallback cb);
     106             :     bool removeStateListener(uintptr_t lid);
     107             : 
     108        1353 :     bool isSecure() const { return PJSIP_TRANSPORT_IS_SECURE(transport_); }
     109             : 
     110         143 :     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        1368 :     inline void setDeviceId(const std::string& deviceId) { deviceId_ = deviceId; }
     118         515 :     inline std::string_view deviceId() const { return deviceId_; }
     119        1368 :     inline void setAccount(const std::shared_ptr<SIPAccountBase>& account) { account_ = account; }
     120         409 :     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             : 
     129             :     std::unique_ptr<pjsip_transport, decltype(deleteTransport)&> transport_;
     130             :     std::shared_ptr<TlsListener> tlsListener_;
     131             :     std::mutex stateListenersMutex_;
     132             :     std::map<uintptr_t, SipTransportStateCallback> stateListeners_;
     133             :     std::weak_ptr<SIPAccountBase> account_ {};
     134             : 
     135             :     bool connected_ {false};
     136             :     std::string deviceId_ {};
     137             :     TlsInfos tlsInfos_;
     138             : };
     139             : 
     140             : class IceTransport;
     141             : namespace tls {
     142             : struct TlsParams;
     143             : };
     144             : 
     145             : /**
     146             :  * Manages the transports and receive callbacks from PJSIP
     147             :  */
     148             : class SipTransportBroker
     149             : {
     150             : public:
     151             :     SipTransportBroker(pjsip_endpoint* endpt);
     152             :     ~SipTransportBroker();
     153             : 
     154             :     std::shared_ptr<SipTransport> getUdpTransport(const dhtnet::IpAddr&);
     155             : 
     156             :     std::shared_ptr<TlsListener> getTlsListener(const dhtnet::IpAddr&, const pjsip_tls_setting*);
     157             : 
     158             :     std::shared_ptr<SipTransport> getTlsTransport(const std::shared_ptr<TlsListener>&,
     159             :                                                   const dhtnet::IpAddr& remote,
     160             :                                                   const std::string& remote_name = {});
     161             : 
     162             :     std::shared_ptr<SipTransport> addTransport(pjsip_transport*);
     163             : 
     164             :     std::shared_ptr<SipTransport> getChanneledTransport(
     165             :         const std::shared_ptr<SIPAccountBase>& account,
     166             :         const std::shared_ptr<dhtnet::ChannelSocket>& socket,
     167             :         onShutdownCb&& cb);
     168             : 
     169             :     /**
     170             :      * Start graceful shutdown procedure for all transports
     171             :      */
     172             :     void shutdown();
     173             : 
     174             :     void transportStateChanged(pjsip_transport*,
     175             :                                pjsip_transport_state,
     176             :                                const pjsip_transport_state_info*);
     177             : 
     178             : private:
     179             :     NON_COPYABLE(SipTransportBroker);
     180             : 
     181             :     /**
     182             :      * Create SIP UDP transport from account's setting
     183             :      * @param account The account for which a transport must be created.
     184             :      * @param IP protocol version to use, can be pj_AF_INET() or pj_AF_INET6()
     185             :      * @return a pointer to the new transport
     186             :      */
     187             :     std::shared_ptr<SipTransport> createUdpTransport(const dhtnet::IpAddr&);
     188             : 
     189             :     /**
     190             :      * List of transports so we can bubble the events up.
     191             :      */
     192             :     std::map<pjsip_transport*, std::weak_ptr<SipTransport>> transports_ {};
     193             :     std::mutex transportMapMutex_ {};
     194             : 
     195             :     /**
     196             :      * Transports are stored in this map in order to retrieve them in case
     197             :      * several accounts would share the same port number.
     198             :      */
     199             :     std::map<dhtnet::IpAddr, pjsip_transport*> udpTransports_;
     200             : 
     201             :     pjsip_endpoint* endpt_;
     202             :     std::atomic_bool isDestroying_ {false};
     203             : };
     204             : 
     205             : } // namespace jami

Generated by: LCOV version 1.14