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