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