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 "ring_types.h" 24 : #include "noncopyable.h" 25 : 26 : #include <dhtnet/ip_utils.h> 27 : 28 : #include "connectivity/sip_utils.h" 29 : #include <pjsip.h> 30 : #include <pjlib.h> 31 : #include <pjsip_ua.h> 32 : #include <pjlib-util.h> 33 : #include <pjnath.h> 34 : #include <pjnath/stun_config.h> 35 : 36 : #ifdef ENABLE_VIDEO 37 : #include <queue> 38 : #endif 39 : #include <map> 40 : #include <mutex> 41 : #include <memory> 42 : #include <functional> 43 : #include <thread> 44 : #include <atomic> 45 : 46 : namespace jami { 47 : 48 : class SIPCall; 49 : class SIPAccountBase; 50 : class SIPVoIPLink; 51 : class SipTransportBroker; 52 : 53 : /** 54 : * @file sipvoiplink.h 55 : * @brief Specific VoIPLink for SIP (SIP core for incoming and outgoing events). 56 : */ 57 : 58 : class SIPVoIPLink 59 : { 60 : public: 61 : SIPVoIPLink(); 62 : ~SIPVoIPLink(); 63 : 64 : /** 65 : * Destroy structures 66 : */ 67 : void shutdown(); 68 : 69 : /** 70 : * Event listener. Each event send by the call manager is received and handled from here 71 : */ 72 : void handleEvents(); 73 : 74 : /** 75 : * Register a new keepalive registration timer to this endpoint 76 : */ 77 : void registerKeepAliveTimer(pj_timer_entry& timer, pj_time_val& delay); 78 : 79 : /** 80 : * Abort currently registered timer 81 : */ 82 : void cancelKeepAliveTimer(pj_timer_entry& timer); 83 : 84 : /** 85 : * Get the memory pool factory since each calls has its own memory pool 86 : */ 87 : pj_caching_pool* getMemoryPoolFactory(); 88 : 89 : /** 90 : * Create the default UDP transport according ot Ip2Ip profile settings 91 : */ 92 : void createDefaultSipUdpTransport(); 93 : 94 : public: 95 : static void createSDPOffer(pjsip_inv_session* inv); 96 : 97 : /** 98 : * Instance that maintain and manage transport (UDP, TLS) 99 : */ 100 : std::unique_ptr<SipTransportBroker> sipTransportBroker; 101 : 102 : typedef std::function<void(std::vector<dhtnet::IpAddr>)> SrvResolveCallback; 103 : void resolveSrvName(const std::string& name, 104 : pjsip_transport_type_e type, 105 : SrvResolveCallback&& cb); 106 : 107 : /** 108 : * Guess the account related to an incoming SIP call. 109 : */ 110 : std::shared_ptr<SIPAccountBase> guessAccount(std::string_view userName, 111 : std::string_view server, 112 : std::string_view fromUri) const; 113 : 114 : int getModId(); 115 : pjsip_endpoint* getEndpoint(); 116 : pjsip_module* getMod(); 117 : 118 : pj_caching_pool* getCachingPool() noexcept; 119 : pj_pool_t* getPool() noexcept; 120 : 121 : /** 122 : * Get the correct address to use (ie advertised) from 123 : * a uri. The corresponding transport that should be used 124 : * with that uri will be discovered. 125 : * 126 : * @param uri The uri from which we want to discover the address to use 127 : * @param transport The transport to use to discover the address 128 : */ 129 : void findLocalAddressFromTransport(pjsip_transport* transport, 130 : pjsip_transport_type_e transportType, 131 : const std::string& host, 132 : std::string& address, 133 : pj_uint16_t& port) const; 134 : 135 : bool findLocalAddressFromSTUN(pjsip_transport* transport, 136 : pj_str_t* stunServerName, 137 : int stunPort, 138 : std::string& address, 139 : pj_uint16_t& port) const; 140 : 141 : /** 142 : * Initialize the transport selector 143 : * @param transport A transport associated with an account 144 : * @return A transport selector structure 145 : */ 146 152 : static inline pjsip_tpselector getTransportSelector(pjsip_transport* transport) 147 : { 148 : pjsip_tpselector tp; 149 152 : tp.type = PJSIP_TPSELECTOR_TRANSPORT; 150 152 : tp.u.transport = transport; 151 152 : return tp; 152 : } 153 : 154 : private: 155 : NON_COPYABLE(SIPVoIPLink); 156 : 157 : mutable pj_caching_pool cp_; 158 : sip_utils::PoolPtr pool_; 159 : std::atomic_bool running_ {true}; 160 : std::thread sipThread_; 161 : 162 : friend class SIPTest; 163 : }; 164 : 165 : } // namespace jami