Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> 5 : * Author: Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com> 6 : * 7 : * This program is free software; you can redistribute it and/or modify 8 : * it under the terms of the GNU General Public License as published by 9 : * the Free Software Foundation; either version 3 of the License, or 10 : * (at your option) any later version. 11 : * 12 : * This program is distributed in the hope that it will be useful, 13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 : * GNU General Public License for more details. 16 : * 17 : * You should have received a copy of the GNU General Public License 18 : * along with this program; if not, write to the Free Software 19 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 : */ 21 : 22 : #pragma once 23 : 24 : #ifdef HAVE_CONFIG_H 25 : #include "config.h" 26 : #endif 27 : 28 : #include "connectivity/sip_utils.h" 29 : 30 : #include "noncopyable.h" 31 : #include "logger.h" 32 : 33 : #include <pjsip.h> 34 : #include <pjnath/stun_config.h> 35 : 36 : #include <functional> 37 : #include <mutex> 38 : #include <condition_variable> 39 : #include <map> 40 : #include <string> 41 : #include <vector> 42 : #include <list> 43 : #include <memory> 44 : 45 : // OpenDHT 46 : namespace dht { 47 : namespace crypto { 48 : struct Certificate; 49 : } 50 : } // namespace dht 51 : 52 : namespace dhtnet { 53 : class ChannelSocket; 54 : } // namespace dhtnet 55 : 56 : namespace jami { 57 : class SIPAccountBase; 58 : using onShutdownCb = std::function<void(void)>; 59 : 60 : struct TlsListener 61 : { 62 : TlsListener() {} 63 0 : TlsListener(pjsip_tpfactory* f) 64 0 : : listener(f) 65 0 : {} 66 0 : virtual ~TlsListener() 67 0 : { 68 0 : JAMI_DBG("Destroying listener"); 69 0 : listener->destroy(listener); 70 0 : } 71 0 : pjsip_tpfactory* get() { return listener; } 72 : 73 : private: 74 : NON_COPYABLE(TlsListener); 75 : pjsip_tpfactory* listener {nullptr}; 76 : }; 77 : 78 : struct TlsInfos 79 : { 80 : pj_ssl_cipher cipher {PJ_TLS_UNKNOWN_CIPHER}; 81 : pj_ssl_sock_proto proto {PJ_SSL_SOCK_PROTO_DEFAULT}; 82 : pj_ssl_cert_verify_flag_t verifyStatus {}; 83 : std::shared_ptr<dht::crypto::Certificate> peerCert {}; 84 : }; 85 : 86 : using SipTransportStateCallback 87 : = std::function<void(pjsip_transport_state, const pjsip_transport_state_info*)>; 88 : 89 : /** 90 : * SIP transport wraps pjsip_transport. 91 : */ 92 : class SipTransport 93 : { 94 : public: 95 : SipTransport(pjsip_transport*); 96 : SipTransport(pjsip_transport*, const std::shared_ptr<TlsListener>&); 97 : // If the SipTransport is a channeled transport, we are already connected to the peer, 98 : // so, we can directly set tlsInfos_.peerCert and avoid any copy 99 : SipTransport(pjsip_transport* t, 100 : const std::shared_ptr<dht::crypto::Certificate>& peerCertficate); 101 : 102 : ~SipTransport(); 103 : 104 : static const char* stateToStr(pjsip_transport_state state); 105 : 106 : void stateCallback(pjsip_transport_state state, const pjsip_transport_state_info* info); 107 : 108 29010 : pjsip_transport* get() { return transport_.get(); } 109 : 110 : void addStateListener(uintptr_t lid, SipTransportStateCallback cb); 111 : bool removeStateListener(uintptr_t lid); 112 : 113 1676 : bool isSecure() const { return PJSIP_TRANSPORT_IS_SECURE(transport_); } 114 : 115 143 : const TlsInfos& getTlsInfos() const { return tlsInfos_; } 116 : 117 : static bool isAlive(pjsip_transport_state state); 118 : 119 : /** Only makes sense for connection-oriented transports */ 120 0 : bool isConnected() const noexcept { return connected_; } 121 : 122 1997 : inline void setDeviceId(const std::string& deviceId) { deviceId_ = deviceId; } 123 28382 : inline std::string_view deviceId() const { return deviceId_; } 124 1997 : inline void setAccount(const std::shared_ptr<SIPAccountBase>& account) { account_ = account; } 125 28274 : inline const std::weak_ptr<SIPAccountBase>& getAccount() const { return account_; } 126 : 127 : uint16_t getTlsMtu(); 128 : 129 : private: 130 : NON_COPYABLE(SipTransport); 131 : 132 : static void deleteTransport(pjsip_transport* t); 133 : 134 : std::unique_ptr<pjsip_transport, decltype(deleteTransport)&> transport_; 135 : std::shared_ptr<TlsListener> tlsListener_; 136 : std::mutex stateListenersMutex_; 137 : std::map<uintptr_t, SipTransportStateCallback> stateListeners_; 138 : std::weak_ptr<SIPAccountBase> account_ {}; 139 : 140 : bool connected_ {false}; 141 : std::string deviceId_ {}; 142 : TlsInfos tlsInfos_; 143 : }; 144 : 145 : class IceTransport; 146 : namespace tls { 147 : struct TlsParams; 148 : }; 149 : 150 : /** 151 : * Manages the transports and receive callbacks from PJSIP 152 : */ 153 : class SipTransportBroker 154 : { 155 : public: 156 : SipTransportBroker(pjsip_endpoint* endpt); 157 : ~SipTransportBroker(); 158 : 159 : std::shared_ptr<SipTransport> getUdpTransport(const dhtnet::IpAddr&); 160 : 161 : std::shared_ptr<TlsListener> getTlsListener(const dhtnet::IpAddr&, const pjsip_tls_setting*); 162 : 163 : std::shared_ptr<SipTransport> getTlsTransport(const std::shared_ptr<TlsListener>&, 164 : const dhtnet::IpAddr& remote, 165 : const std::string& remote_name = {}); 166 : 167 : std::shared_ptr<SipTransport> addTransport(pjsip_transport*); 168 : 169 : std::shared_ptr<SipTransport> getChanneledTransport( 170 : const std::shared_ptr<SIPAccountBase>& account, 171 : const std::shared_ptr<dhtnet::ChannelSocket>& socket, 172 : onShutdownCb&& cb); 173 : 174 : /** 175 : * Start graceful shutdown procedure for all transports 176 : */ 177 : void shutdown(); 178 : 179 : void transportStateChanged(pjsip_transport*, 180 : pjsip_transport_state, 181 : const pjsip_transport_state_info*); 182 : 183 : private: 184 : NON_COPYABLE(SipTransportBroker); 185 : 186 : /** 187 : * Create SIP UDP transport from account's setting 188 : * @param account The account for which a transport must be created. 189 : * @param IP protocol version to use, can be pj_AF_INET() or pj_AF_INET6() 190 : * @return a pointer to the new transport 191 : */ 192 : std::shared_ptr<SipTransport> createUdpTransport(const dhtnet::IpAddr&); 193 : 194 : /** 195 : * List of transports so we can bubble the events up. 196 : */ 197 : std::map<pjsip_transport*, std::weak_ptr<SipTransport>> transports_ {}; 198 : std::mutex transportMapMutex_ {}; 199 : 200 : /** 201 : * Transports are stored in this map in order to retrieve them in case 202 : * several accounts would share the same port number. 203 : */ 204 : std::map<dhtnet::IpAddr, pjsip_transport*> udpTransports_; 205 : 206 : pjsip_endpoint* endpt_; 207 : std::atomic_bool isDestroying_ {false}; 208 : }; 209 : 210 : } // namespace jami