Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com> 5 : * Author: Yun Liu <yun.liu@savoirfairelinux.com> 6 : * Author: Pierre-Luc Bacon <pierre-luc.bacon@savoirfairelinux.com> 7 : * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> 8 : * Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com> 9 : * 10 : * This program is free software; you can redistribute it and/or modify 11 : * it under the terms of the GNU General Public License as published by 12 : * the Free Software Foundation; either version 3 of the License, or 13 : * (at your option) any later version. 14 : * 15 : * This program is distributed in the hope that it will be useful, 16 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 : * GNU General Public License for more details. 19 : * 20 : * You should have received a copy of the GNU General Public License 21 : * along with this program; if not, write to the Free Software 22 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 23 : */ 24 : 25 : #pragma once 26 : 27 : #ifdef HAVE_CONFIG_H 28 : #include "config.h" 29 : #endif 30 : 31 : #include "ring_types.h" 32 : #include "noncopyable.h" 33 : 34 : #include <dhtnet/ip_utils.h> 35 : 36 : #include <pjsip.h> 37 : #include <pjlib.h> 38 : #include <pjsip_ua.h> 39 : #include <pjlib-util.h> 40 : #include <pjnath.h> 41 : #include <pjnath/stun_config.h> 42 : 43 : #ifdef ENABLE_VIDEO 44 : #include <queue> 45 : #endif 46 : #include <map> 47 : #include <mutex> 48 : #include <memory> 49 : #include <functional> 50 : #include <thread> 51 : #include <atomic> 52 : 53 : namespace jami { 54 : 55 : class SIPCall; 56 : class SIPAccountBase; 57 : class SIPVoIPLink; 58 : class SipTransportBroker; 59 : 60 : /** 61 : * @file sipvoiplink.h 62 : * @brief Specific VoIPLink for SIP (SIP core for incoming and outgoing events). 63 : */ 64 : 65 : class SIPVoIPLink 66 : { 67 : public: 68 : SIPVoIPLink(); 69 : ~SIPVoIPLink(); 70 : 71 : /** 72 : * Destroy structures 73 : */ 74 : void shutdown(); 75 : 76 : /** 77 : * Event listener. Each event send by the call manager is received and handled from here 78 : */ 79 : void handleEvents(); 80 : 81 : /** 82 : * Register a new keepalive registration timer to this endpoint 83 : */ 84 : void registerKeepAliveTimer(pj_timer_entry& timer, pj_time_val& delay); 85 : 86 : /** 87 : * Abort currently registered timer 88 : */ 89 : void cancelKeepAliveTimer(pj_timer_entry& timer); 90 : 91 : /** 92 : * Get the memory pool factory since each calls has its own memory pool 93 : */ 94 : pj_caching_pool* getMemoryPoolFactory(); 95 : 96 : /** 97 : * Create the default UDP transport according ot Ip2Ip profile settings 98 : */ 99 : void createDefaultSipUdpTransport(); 100 : 101 : public: 102 : static void createSDPOffer(pjsip_inv_session* inv); 103 : 104 : /** 105 : * Instance that maintain and manage transport (UDP, TLS) 106 : */ 107 : std::unique_ptr<SipTransportBroker> sipTransportBroker; 108 : 109 : typedef std::function<void(std::vector<dhtnet::IpAddr>)> SrvResolveCallback; 110 : void resolveSrvName(const std::string& name, 111 : pjsip_transport_type_e type, 112 : SrvResolveCallback&& cb); 113 : 114 : /** 115 : * Guess the account related to an incoming SIP call. 116 : */ 117 : std::shared_ptr<SIPAccountBase> guessAccount(std::string_view userName, 118 : std::string_view server, 119 : std::string_view fromUri) const; 120 : 121 : int getModId(); 122 : pjsip_endpoint* getEndpoint(); 123 : pjsip_module* getMod(); 124 : 125 : pj_caching_pool* getCachingPool() noexcept; 126 : pj_pool_t* getPool() noexcept; 127 : 128 : /** 129 : * Get the correct address to use (ie advertised) from 130 : * a uri. The corresponding transport that should be used 131 : * with that uri will be discovered. 132 : * 133 : * @param uri The uri from which we want to discover the address to use 134 : * @param transport The transport to use to discover the address 135 : */ 136 : void findLocalAddressFromTransport(pjsip_transport* transport, 137 : pjsip_transport_type_e transportType, 138 : const std::string& host, 139 : std::string& address, 140 : pj_uint16_t& port) const; 141 : 142 : bool findLocalAddressFromSTUN(pjsip_transport* transport, 143 : pj_str_t* stunServerName, 144 : int stunPort, 145 : std::string& address, 146 : pj_uint16_t& port) const; 147 : 148 : /** 149 : * Initialize the transport selector 150 : * @param transport A transport associated with an account 151 : * @return A transport selector structure 152 : */ 153 32421 : static inline pjsip_tpselector getTransportSelector(pjsip_transport* transport) 154 : { 155 : pjsip_tpselector tp; 156 32421 : tp.type = PJSIP_TPSELECTOR_TRANSPORT; 157 32421 : tp.u.transport = transport; 158 32421 : return tp; 159 : } 160 : 161 : private: 162 : NON_COPYABLE(SIPVoIPLink); 163 : 164 : mutable pj_caching_pool cp_; 165 : std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> pool_; 166 : std::atomic_bool running_ {true}; 167 : std::thread sipThread_; 168 : 169 : friend class SIPTest; 170 : }; 171 : 172 : } // namespace jami