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 "ring_types.h" 24 : #include "noncopyable.h" 25 : 26 : #include <dhtnet/ip_utils.h> 27 : 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, 103 : pjsip_transport_type_e type, 104 : SrvResolveCallback&& cb); 105 : 106 : /** 107 : * Guess the account related to an incoming SIP call. 108 : */ 109 : std::shared_ptr<SIPAccountBase> guessAccount(std::string_view userName, 110 : std::string_view server, 111 : std::string_view fromUri) const; 112 : 113 : int getModId(); 114 : pjsip_endpoint* getEndpoint(); 115 : pjsip_module* getMod(); 116 : 117 : pj_caching_pool* getCachingPool() noexcept; 118 : pj_pool_t* getPool() noexcept; 119 : 120 : /** 121 : * Get the correct address to use (ie advertised) from 122 : * a uri. The corresponding transport that should be used 123 : * with that uri will be discovered. 124 : * 125 : * @param uri The uri from which we want to discover the address to use 126 : * @param transport The transport to use to discover the address 127 : */ 128 : void findLocalAddressFromTransport(pjsip_transport* transport, 129 : pjsip_transport_type_e transportType, 130 : const std::string& host, 131 : std::string& address, 132 : pj_uint16_t& port) const; 133 : 134 : bool findLocalAddressFromSTUN(pjsip_transport* transport, 135 : pj_str_t* stunServerName, 136 : int stunPort, 137 : std::string& address, 138 : pj_uint16_t& port) const; 139 : 140 : /** 141 : * Initialize the transport selector 142 : * @param transport A transport associated with an account 143 : * @return A transport selector structure 144 : */ 145 447 : static inline pjsip_tpselector getTransportSelector(pjsip_transport* transport) 146 : { 147 : pjsip_tpselector tp; 148 447 : tp.type = PJSIP_TPSELECTOR_TRANSPORT; 149 447 : tp.u.transport = transport; 150 447 : return tp; 151 : } 152 : 153 : private: 154 : NON_COPYABLE(SIPVoIPLink); 155 : 156 : mutable pj_caching_pool cp_; 157 : std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> pool_; 158 : std::atomic_bool running_ {true}; 159 : std::thread sipThread_; 160 : 161 : friend class SIPTest; 162 : }; 163 : 164 : } // namespace jami