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 : #include "connectivity/ip_utils.h" 20 : #include "media/media_codec.h" 21 : #include "noncopyable.h" 22 : 23 : #include <utility> 24 : #include <string> 25 : #include <vector> 26 : #include <cstring> // strcmp 27 : 28 : #include <dhtnet/ip_utils.h> 29 : 30 : #include <pjsip/sip_msg.h> 31 : #include <pjlib.h> 32 : #include <pj/pool.h> 33 : #include <pjsip/sip_endpoint.h> 34 : #include <pjsip/sip_dialog.h> 35 : 36 : namespace jami { 37 : namespace sip_utils { 38 : 39 : using namespace std::literals; 40 : 41 : // SIP methods. Only list methods that need to be explicitly 42 : // handled 43 : 44 : namespace SIP_METHODS { 45 : constexpr std::string_view MESSAGE = "MESSAGE"sv; 46 : constexpr std::string_view INFO = "INFO"sv; 47 : constexpr std::string_view OPTIONS = "OPTIONS"sv; 48 : constexpr std::string_view PUBLISH = "PUBLISH"sv; 49 : constexpr std::string_view REFER = "REFER"sv; 50 : constexpr std::string_view NOTIFY = "NOTIFY"sv; 51 : } // namespace SIP_METHODS 52 : 53 : static constexpr int DEFAULT_SIP_PORT {5060}; 54 : static constexpr int DEFAULT_SIP_TLS_PORT {5061}; 55 : static constexpr int DEFAULT_AUTO_SELECT_PORT {0}; 56 : 57 : /// PjsipErrorCategory - a PJSIP error category for std::error_code 58 : class PjsipErrorCategory final : public std::error_category 59 : { 60 : public: 61 0 : const char* name() const noexcept override { return "pjsip"; } 62 : std::string message(int condition) const override; 63 : }; 64 : 65 : /// PJSIP related exception 66 : /// Based on std::system_error with code() returning std::error_code with PjsipErrorCategory category 67 : class PjsipFailure : public std::system_error 68 : { 69 : private: 70 : static constexpr const char* what_ = "PJSIP call failed"; 71 : 72 : public: 73 : PjsipFailure() 74 : : std::system_error(std::error_code(PJ_EUNKNOWN, PjsipErrorCategory()), what_) 75 : {} 76 : 77 : explicit PjsipFailure(pj_status_t status) 78 : : std::system_error(std::error_code(status, PjsipErrorCategory()), what_) 79 : {} 80 : }; 81 : 82 : std::string sip_strerror(pj_status_t code); 83 : 84 : // Helper function that return a constant pj_str_t from an array of any types 85 : // that may be statically casted into char pointer. 86 : // Per convention, the input array is supposed to be null terminated. 87 : template<typename T, std::size_t N> 88 : constexpr const pj_str_t 89 2511 : CONST_PJ_STR(T (&a)[N]) noexcept 90 : { 91 2511 : return {const_cast<char*>(a), N - 1}; 92 : } 93 : 94 : inline const pj_str_t 95 10030 : CONST_PJ_STR(const std::string& str) noexcept 96 : { 97 10030 : return {const_cast<char*>(str.c_str()), (pj_ssize_t) str.size()}; 98 : } 99 : 100 : inline constexpr pj_str_t 101 32057 : CONST_PJ_STR(const std::string_view& str) noexcept 102 : { 103 32057 : return {const_cast<char*>(str.data()), (pj_ssize_t) str.size()}; 104 : } 105 : 106 : inline constexpr std::string_view 107 3149 : as_view(const pj_str_t& str) noexcept 108 : { 109 3149 : return {str.ptr, (size_t) str.slen}; 110 : } 111 : 112 : static constexpr const char* 113 315 : getKeyExchangeName(KeyExchangeProtocol kx) 114 : { 115 315 : return kx == KeyExchangeProtocol::SDES ? "sdes" : ""; 116 : } 117 : 118 : static inline KeyExchangeProtocol 119 24 : getKeyExchangeProtocol(std::string_view name) 120 : { 121 24 : return name == "sdes"sv ? KeyExchangeProtocol::SDES : KeyExchangeProtocol::NONE; 122 : } 123 : 124 : /** 125 : * Helper function to parser header from incoming sip messages 126 : * @return Header from SIP message 127 : */ 128 : std::string fetchHeaderValue(pjsip_msg* msg, const std::string& field); 129 : 130 : pjsip_route_hdr* createRouteSet(const std::string& route, pj_pool_t* hdr_pool); 131 : 132 : std::string_view stripSipUriPrefix(std::string_view sipUri); 133 : 134 : std::string parseDisplayName(const pjsip_name_addr* sip_name_addr); 135 : std::string parseDisplayName(const pjsip_from_hdr* header); 136 : std::string parseDisplayName(const pjsip_contact_hdr* header); 137 : 138 : std::string_view getHostFromUri(std::string_view sipUri); 139 : 140 : void addContactHeader(const std::string& contact, pjsip_tx_data* tdata); 141 : void addUserAgentHeader(const std::string& userAgent, pjsip_tx_data* tdata); 142 : std::string_view getPeerUserAgent(const pjsip_rx_data* rdata); 143 : std::vector<std::string> getPeerAllowMethods(const pjsip_rx_data* rdata); 144 : void logMessageHeaders(const pjsip_hdr* hdr_list); 145 : 146 : constexpr std::string_view DEFAULT_VIDEO_STREAMID = "video_0"; 147 : constexpr std::string_view DEFAULT_AUDIO_STREAMID = "audio_0"; 148 : 149 : std::string streamId(const std::string& callId, std::string_view label); 150 : 151 : // PJSIP dialog locking in RAII way 152 : // Usage: declare local variable like this: sip_utils::PJDialogLock lock {dialog}; 153 : // The lock is kept until the local variable is deleted 154 : class PJDialogLock 155 : { 156 : public: 157 311 : explicit PJDialogLock(pjsip_dialog* dialog) 158 311 : : dialog_(dialog) 159 : { 160 311 : pjsip_dlg_inc_lock(dialog_); 161 311 : } 162 : 163 311 : ~PJDialogLock() { pjsip_dlg_dec_lock(dialog_); } 164 : 165 : private: 166 : NON_COPYABLE(PJDialogLock); 167 : pjsip_dialog* dialog_ {nullptr}; 168 : }; 169 : 170 : // Helper on PJSIP memory pool allocation from endpoint 171 : // This encapsulate the allocated memory pool inside a unique_ptr 172 : static inline std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> 173 2736 : smart_alloc_pool(pjsip_endpoint* endpt, const char* const name, pj_size_t initial, pj_size_t inc) 174 : { 175 2736 : auto pool = pjsip_endpt_create_pool(endpt, name, initial, inc); 176 2736 : if (not pool) 177 0 : throw std::bad_alloc(); 178 2736 : return std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&>(pool, pj_pool_release); 179 : } 180 : 181 : void sockaddr_to_host_port(pj_pool_t* pool, pjsip_host_port* host_port, const pj_sockaddr* addr); 182 : 183 : static constexpr int POOL_TP_INIT {512}; 184 : static constexpr int POOL_TP_INC {512}; 185 : static constexpr int TRANSPORT_INFO_LENGTH {64}; 186 : 187 : } // namespace sip_utils 188 : } // namespace jami