Ring Daemon 16.0.0
Loading...
Searching...
No Matches
sip_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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
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
36namespace jami {
37namespace sip_utils {
38
39using namespace std::literals;
40
41// SIP methods. Only list methods that need to be explicitly
42// handled
43
44namespace SIP_METHODS {
45constexpr std::string_view MESSAGE = "MESSAGE"sv;
46constexpr std::string_view INFO = "INFO"sv;
47constexpr std::string_view OPTIONS = "OPTIONS"sv;
48constexpr std::string_view PUBLISH = "PUBLISH"sv;
49constexpr std::string_view REFER = "REFER"sv;
50constexpr std::string_view NOTIFY = "NOTIFY"sv;
51} // namespace SIP_METHODS
52
53static constexpr int DEFAULT_SIP_PORT {5060};
54static constexpr int DEFAULT_SIP_TLS_PORT {5061};
55static constexpr int DEFAULT_AUTO_SELECT_PORT {0};
56
58class PjsipErrorCategory final : public std::error_category
59{
60public:
61 const char* name() const noexcept override { return "pjsip"; }
62 std::string message(int condition) const override;
63};
64
67class PjsipFailure : public std::system_error
68{
69private:
70 static constexpr const char* what_ = "PJSIP call failed";
71
72public:
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
82std::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.
87template<typename T, std::size_t N>
88constexpr const pj_str_t
89CONST_PJ_STR(T (&a)[N]) noexcept
90{
91 return {const_cast<char*>(a), N - 1};
92}
93
94inline const pj_str_t
95CONST_PJ_STR(const std::string& str) noexcept
96{
97 return {const_cast<char*>(str.c_str()), (pj_ssize_t) str.size()};
98}
99
100inline constexpr pj_str_t
101CONST_PJ_STR(const std::string_view& str) noexcept
102{
103 return {const_cast<char*>(str.data()), (pj_ssize_t) str.size()};
104}
105
106inline constexpr std::string_view
107as_view(const pj_str_t& str) noexcept
108{
109 return {str.ptr, (size_t) str.slen};
110}
111
112static constexpr const char*
117
118static inline KeyExchangeProtocol
119getKeyExchangeProtocol(std::string_view name)
120{
122}
123
128std::string fetchHeaderValue(pjsip_msg* msg, const std::string& field);
129
131
132std::string_view stripSipUriPrefix(std::string_view sipUri);
133
135std::string parseDisplayName(const pjsip_from_hdr* header);
136std::string parseDisplayName(const pjsip_contact_hdr* header);
137
138std::string_view getHostFromUri(std::string_view sipUri);
139
140void addContactHeader(const std::string& contact, pjsip_tx_data* tdata);
141void addUserAgentHeader(const std::string& userAgent, pjsip_tx_data* tdata);
142std::string_view getPeerUserAgent(const pjsip_rx_data* rdata);
143std::vector<std::string> getPeerAllowMethods(const pjsip_rx_data* rdata);
144void logMessageHeaders(const pjsip_hdr* hdr_list);
145
146constexpr std::string_view DEFAULT_VIDEO_STREAMID = "video_0";
147constexpr std::string_view DEFAULT_AUDIO_STREAMID = "audio_0";
148
149std::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
155{
156public:
158 : dialog_(dialog)
159 {
160 pjsip_dlg_inc_lock(dialog_);
161 }
162
164
165private:
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
172static inline std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&>
174{
176 if (not pool)
177 throw std::bad_alloc();
178 return std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&>(pool, pj_pool_release);
179}
180
182
183static constexpr int POOL_TP_INIT {512};
184static constexpr int POOL_TP_INC {512};
185static constexpr int TRANSPORT_INFO_LENGTH {64};
186
187} // namespace sip_utils
188} // namespace jami
PJDialogLock(pjsip_dialog *dialog)
Definition sip_utils.h:157
PjsipErrorCategory - a PJSIP error category for std::error_code.
Definition sip_utils.h:59
std::string message(int condition) const override
Definition sip_utils.cpp:50
const char * name() const noexcept override
Definition sip_utils.h:61
PJSIP related exception Based on std::system_error with code() returning std::error_code with PjsipEr...
Definition sip_utils.h:68
PjsipFailure(pj_status_t status)
Definition sip_utils.h:77
constexpr std::string_view OPTIONS
Definition sip_utils.h:47
constexpr std::string_view INFO
Definition sip_utils.h:46
constexpr std::string_view MESSAGE
Definition sip_utils.h:45
constexpr std::string_view PUBLISH
Definition sip_utils.h:48
constexpr std::string_view REFER
Definition sip_utils.h:49
constexpr std::string_view NOTIFY
Definition sip_utils.h:50
static constexpr int DEFAULT_SIP_TLS_PORT
Definition sip_utils.h:54
static constexpr int DEFAULT_SIP_PORT
Definition sip_utils.h:53
std::string fetchHeaderValue(pjsip_msg *msg, const std::string &field)
Helper function to parser header from incoming sip messages.
Definition sip_utils.cpp:59
void logMessageHeaders(const pjsip_hdr *hdr_list)
void addUserAgentHeader(const std::string &userAgent, pjsip_tx_data *tdata)
void addContactHeader(const std::string &contactHdr, pjsip_tx_data *tdata)
std::string_view stripSipUriPrefix(std::string_view sipUri)
std::string_view getHostFromUri(std::string_view uri)
static std::unique_ptr< pj_pool_t, decltype(pj_pool_release)& > smart_alloc_pool(pjsip_endpoint *endpt, const char *const name, pj_size_t initial, pj_size_t inc)
Definition sip_utils.h:173
pjsip_route_hdr * createRouteSet(const std::string &route, pj_pool_t *hdr_pool)
Definition sip_utils.cpp:79
std::string sip_strerror(pj_status_t code)
static constexpr int POOL_TP_INIT
Definition sip_utils.h:183
constexpr std::string_view as_view(const pj_str_t &str) noexcept
Definition sip_utils.h:107
std::string_view getPeerUserAgent(const pjsip_rx_data *rdata)
std::vector< std::string > getPeerAllowMethods(const pjsip_rx_data *rdata)
std::string parseDisplayName(const pjsip_name_addr *sip_name_addr)
static constexpr int TRANSPORT_INFO_LENGTH
Definition sip_utils.h:185
static constexpr int POOL_TP_INC
Definition sip_utils.h:184
std::string streamId(const std::string &callId, std::string_view label)
void sockaddr_to_host_port(pj_pool_t *pool, pjsip_host_port *host_port, const pj_sockaddr *addr)
static KeyExchangeProtocol getKeyExchangeProtocol(std::string_view name)
Definition sip_utils.h:119
static constexpr int DEFAULT_AUTO_SELECT_PORT
Definition sip_utils.h:55
constexpr std::string_view DEFAULT_VIDEO_STREAMID
Definition sip_utils.h:146
constexpr std::string_view DEFAULT_AUDIO_STREAMID
Definition sip_utils.h:147
constexpr const pj_str_t CONST_PJ_STR(T(&a)[N]) noexcept
Definition sip_utils.h:89
static constexpr const char * getKeyExchangeName(KeyExchangeProtocol kx)
Definition sip_utils.h:113
void emitSignal(Args... args)
Definition ring_signal.h:64
KeyExchangeProtocol
Definition media_codec.h:36
const std::string & userAgent()
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30