Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 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 |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 16 | */ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include "ip_utils.h" |
| 20 | |
| 21 | #include <utility> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | #include <cstring> // strcmp |
| 25 | |
| 26 | #include <pjsip/sip_msg.h> |
| 27 | #include <pjlib.h> |
| 28 | #include <pj/pool.h> |
| 29 | #include <pjsip/sip_endpoint.h> |
| 30 | #include <pjsip/sip_dialog.h> |
| 31 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 32 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 33 | namespace sip_utils { |
| 34 | |
| 35 | using namespace std::literals; |
| 36 | |
| 37 | // SIP methods. Only list methods that need to be explicitly |
| 38 | // handled |
| 39 | |
| 40 | namespace SIP_METHODS { |
| 41 | constexpr std::string_view MESSAGE = "MESSAGE"sv; |
| 42 | constexpr std::string_view INFO = "INFO"sv; |
| 43 | constexpr std::string_view OPTIONS = "OPTIONS"sv; |
| 44 | constexpr std::string_view PUBLISH = "PUBLISH"sv; |
| 45 | constexpr std::string_view REFER = "REFER"sv; |
| 46 | constexpr std::string_view NOTIFY = "NOTIFY"sv; |
| 47 | } // namespace SIP_METHODS |
| 48 | |
| 49 | static constexpr int DEFAULT_SIP_PORT {5060}; |
| 50 | static constexpr int DEFAULT_SIP_TLS_PORT {5061}; |
| 51 | static constexpr int DEFAULT_AUTO_SELECT_PORT {0}; |
| 52 | |
| 53 | /// PjsipErrorCategory - a PJSIP error category for std::error_code |
| 54 | class PjsipErrorCategory final : public std::error_category |
| 55 | { |
| 56 | public: |
| 57 | const char* name() const noexcept override { return "pjsip"; } |
| 58 | std::string message(int condition) const override; |
| 59 | }; |
| 60 | |
| 61 | /// PJSIP related exception |
| 62 | /// Based on std::system_error with code() returning std::error_code with PjsipErrorCategory category |
| 63 | class PjsipFailure : public std::system_error |
| 64 | { |
| 65 | private: |
| 66 | static constexpr const char* what_ = "PJSIP call failed"; |
| 67 | |
| 68 | public: |
| 69 | PjsipFailure() |
| 70 | : std::system_error(std::error_code(PJ_EUNKNOWN, PjsipErrorCategory()), what_) |
| 71 | {} |
| 72 | |
| 73 | explicit PjsipFailure(pj_status_t status) |
| 74 | : std::system_error(std::error_code(status, PjsipErrorCategory()), what_) |
| 75 | {} |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Helper function to parser header from incoming sip messages |
| 81 | * @return Header from SIP message |
| 82 | */ |
| 83 | /*std::string fetchHeaderValue(pjsip_msg* msg, const std::string& field); |
| 84 | |
| 85 | pjsip_route_hdr* createRouteSet(const std::string& route, pj_pool_t* hdr_pool); |
| 86 | |
| 87 | std::string_view stripSipUriPrefix(std::string_view sipUri); |
| 88 | |
| 89 | std::string parseDisplayName(const pjsip_name_addr* sip_name_addr); |
| 90 | std::string parseDisplayName(const pjsip_from_hdr* header); |
| 91 | std::string parseDisplayName(const pjsip_contact_hdr* header); |
| 92 | |
| 93 | std::string_view getHostFromUri(std::string_view sipUri); |
| 94 | |
| 95 | void addContactHeader(const std::string& contact, pjsip_tx_data* tdata); |
| 96 | void addUserAgentHeader(const std::string& userAgent, pjsip_tx_data* tdata); |
| 97 | std::string_view getPeerUserAgent(const pjsip_rx_data* rdata); |
| 98 | std::vector<std::string> getPeerAllowMethods(const pjsip_rx_data* rdata); |
| 99 | void logMessageHeaders(const pjsip_hdr* hdr_list);*/ |
| 100 | |
| 101 | std::string_view sip_strerror(pj_status_t code); |
| 102 | |
| 103 | // Helper function that return a constant pj_str_t from an array of any types |
| 104 | // that may be statically casted into char pointer. |
| 105 | // Per convention, the input array is supposed to be null terminated. |
| 106 | template<typename T, std::size_t N> |
| 107 | constexpr const pj_str_t |
| 108 | CONST_PJ_STR(T (&a)[N]) noexcept |
| 109 | { |
| 110 | return {const_cast<char*>(a), N - 1}; |
| 111 | } |
| 112 | |
| 113 | inline const pj_str_t |
| 114 | CONST_PJ_STR(const std::string& str) noexcept |
| 115 | { |
| 116 | return {const_cast<char*>(str.c_str()), (pj_ssize_t) str.size()}; |
| 117 | } |
| 118 | |
| 119 | inline constexpr pj_str_t |
| 120 | CONST_PJ_STR(const std::string_view& str) noexcept |
| 121 | { |
| 122 | return {const_cast<char*>(str.data()), (pj_ssize_t) str.size()}; |
| 123 | } |
| 124 | |
| 125 | inline constexpr std::string_view |
| 126 | as_view(const pj_str_t& str) noexcept |
| 127 | { |
| 128 | return {str.ptr, (size_t) str.slen}; |
| 129 | } |
| 130 | |
| 131 | // PJSIP dialog locking in RAII way |
| 132 | // Usage: declare local variable like this: sip_utils::PJDialogLock lock {dialog}; |
| 133 | // The lock is kept until the local variable is deleted |
| 134 | class PJDialogLock |
| 135 | { |
| 136 | public: |
| 137 | explicit PJDialogLock(pjsip_dialog* dialog) |
| 138 | : dialog_(dialog) |
| 139 | { |
| 140 | pjsip_dlg_inc_lock(dialog_); |
| 141 | } |
| 142 | |
| 143 | ~PJDialogLock() { pjsip_dlg_dec_lock(dialog_); } |
| 144 | |
| 145 | private: |
| 146 | PJDialogLock(const PJDialogLock&) = delete; |
| 147 | PJDialogLock& operator=(const PJDialogLock&) = delete; |
| 148 | pjsip_dialog* dialog_ {nullptr}; |
| 149 | }; |
| 150 | |
| 151 | // Helper on PJSIP memory pool allocation from endpoint |
| 152 | // This encapsulate the allocated memory pool inside a unique_ptr |
| 153 | static inline std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> |
| 154 | smart_alloc_pool(pjsip_endpoint* endpt, const char* const name, pj_size_t initial, pj_size_t inc) |
| 155 | { |
| 156 | auto pool = pjsip_endpt_create_pool(endpt, name, initial, inc); |
| 157 | if (not pool) |
| 158 | throw std::bad_alloc(); |
| 159 | return std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&>(pool, pj_pool_release); |
| 160 | } |
| 161 | |
| 162 | void sockaddr_to_host_port(pj_pool_t* pool, pjsip_host_port* host_port, const pj_sockaddr* addr); |
| 163 | |
| 164 | static constexpr int POOL_TP_INIT {512}; |
| 165 | static constexpr int POOL_TP_INC {512}; |
| 166 | static constexpr int TRANSPORT_INFO_LENGTH {64}; |
| 167 | |
| 168 | } // namespace sip_utils |
| 169 | } // namespace jami |