Adrien BĂ©raud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include "ip_utils.h" |
| 24 | |
| 25 | #include <utility> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | #include <cstring> // strcmp |
| 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 | 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 | |
| 83 | /** |
| 84 | * Helper function to parser header from incoming sip messages |
| 85 | * @return Header from SIP message |
| 86 | */ |
| 87 | /*std::string fetchHeaderValue(pjsip_msg* msg, const std::string& field); |
| 88 | |
| 89 | pjsip_route_hdr* createRouteSet(const std::string& route, pj_pool_t* hdr_pool); |
| 90 | |
| 91 | std::string_view stripSipUriPrefix(std::string_view sipUri); |
| 92 | |
| 93 | std::string parseDisplayName(const pjsip_name_addr* sip_name_addr); |
| 94 | std::string parseDisplayName(const pjsip_from_hdr* header); |
| 95 | std::string parseDisplayName(const pjsip_contact_hdr* header); |
| 96 | |
| 97 | std::string_view getHostFromUri(std::string_view sipUri); |
| 98 | |
| 99 | void addContactHeader(const std::string& contact, pjsip_tx_data* tdata); |
| 100 | void addUserAgentHeader(const std::string& userAgent, pjsip_tx_data* tdata); |
| 101 | std::string_view getPeerUserAgent(const pjsip_rx_data* rdata); |
| 102 | std::vector<std::string> getPeerAllowMethods(const pjsip_rx_data* rdata); |
| 103 | void logMessageHeaders(const pjsip_hdr* hdr_list);*/ |
| 104 | |
| 105 | std::string_view sip_strerror(pj_status_t code); |
| 106 | |
| 107 | // Helper function that return a constant pj_str_t from an array of any types |
| 108 | // that may be statically casted into char pointer. |
| 109 | // Per convention, the input array is supposed to be null terminated. |
| 110 | template<typename T, std::size_t N> |
| 111 | constexpr const pj_str_t |
| 112 | CONST_PJ_STR(T (&a)[N]) noexcept |
| 113 | { |
| 114 | return {const_cast<char*>(a), N - 1}; |
| 115 | } |
| 116 | |
| 117 | inline const pj_str_t |
| 118 | CONST_PJ_STR(const std::string& str) noexcept |
| 119 | { |
| 120 | return {const_cast<char*>(str.c_str()), (pj_ssize_t) str.size()}; |
| 121 | } |
| 122 | |
| 123 | inline constexpr pj_str_t |
| 124 | CONST_PJ_STR(const std::string_view& str) noexcept |
| 125 | { |
| 126 | return {const_cast<char*>(str.data()), (pj_ssize_t) str.size()}; |
| 127 | } |
| 128 | |
| 129 | inline constexpr std::string_view |
| 130 | as_view(const pj_str_t& str) noexcept |
| 131 | { |
| 132 | return {str.ptr, (size_t) str.slen}; |
| 133 | } |
| 134 | |
| 135 | // PJSIP dialog locking in RAII way |
| 136 | // Usage: declare local variable like this: sip_utils::PJDialogLock lock {dialog}; |
| 137 | // The lock is kept until the local variable is deleted |
| 138 | class PJDialogLock |
| 139 | { |
| 140 | public: |
| 141 | explicit PJDialogLock(pjsip_dialog* dialog) |
| 142 | : dialog_(dialog) |
| 143 | { |
| 144 | pjsip_dlg_inc_lock(dialog_); |
| 145 | } |
| 146 | |
| 147 | ~PJDialogLock() { pjsip_dlg_dec_lock(dialog_); } |
| 148 | |
| 149 | private: |
| 150 | PJDialogLock(const PJDialogLock&) = delete; |
| 151 | PJDialogLock& operator=(const PJDialogLock&) = delete; |
| 152 | pjsip_dialog* dialog_ {nullptr}; |
| 153 | }; |
| 154 | |
| 155 | // Helper on PJSIP memory pool allocation from endpoint |
| 156 | // This encapsulate the allocated memory pool inside a unique_ptr |
| 157 | static inline std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> |
| 158 | smart_alloc_pool(pjsip_endpoint* endpt, const char* const name, pj_size_t initial, pj_size_t inc) |
| 159 | { |
| 160 | auto pool = pjsip_endpt_create_pool(endpt, name, initial, inc); |
| 161 | if (not pool) |
| 162 | throw std::bad_alloc(); |
| 163 | return std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&>(pool, pj_pool_release); |
| 164 | } |
| 165 | |
| 166 | void sockaddr_to_host_port(pj_pool_t* pool, pjsip_host_port* host_port, const pj_sockaddr* addr); |
| 167 | |
| 168 | static constexpr int POOL_TP_INIT {512}; |
| 169 | static constexpr int POOL_TP_INC {512}; |
| 170 | static constexpr int TRANSPORT_INFO_LENGTH {64}; |
| 171 | |
| 172 | } // namespace sip_utils |
| 173 | } // namespace jami |