Ring Daemon
Loading...
Searching...
No Matches
sip_utils.cpp
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
19#include "logger.h"
21
22#include <pjsip.h>
23#include <pjsip_ua.h>
24#include <pjlib-util.h>
25#include <pjnath.h>
26#include <pjnath/stun_config.h>
27#include <pj/string.h>
28#include <pjsip/sip_types.h>
29#include <pjsip/sip_uri.h>
30#include <pj/list.h>
31
32#ifndef _WIN32
33#include <netdb.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#endif
38
39#include <vector>
40
41using namespace std::literals;
42
43namespace jami {
44namespace sip_utils {
45
46constexpr pj_str_t USER_AGENT_STR = CONST_PJ_STR("User-Agent");
47
48std::string
50{
51 std::string err_msg;
52 err_msg.reserve(PJ_ERR_MSG_SIZE);
53 err_msg.resize(pj_strerror(condition, &err_msg[0], err_msg.capacity()).slen);
54 return err_msg;
55}
56
57std::string
58fetchHeaderValue(pjsip_msg* msg, const std::string& field)
59{
60 pj_str_t name = pj_str((char*) field.c_str());
62
63 if (!hdr)
64 return "";
65
66 std::string value(hdr->hvalue.ptr, hdr->hvalue.slen);
67
68 size_t pos = value.find('\n');
69
70 if (pos != std::string::npos)
71 return value.substr(0, pos);
72 else
73 return "";
74}
75
78{
80
81 std::string host;
82 int port = 0;
83 size_t found = route.find(':');
84 if (found != std::string::npos) {
85 host = route.substr(0, found);
86 port = atoi(route.substr(found + 1, route.length() - found).c_str());
87 } else
88 host = route;
89
92 url->lr_param = 1;
93 routing->name_addr.uri = (pjsip_uri*) url;
94 pj_strdup2(hdr_pool, &url->host, host.c_str());
95 url->port = port;
96
97 JAMI_DBG("Adding route %s", host.c_str());
99
100 return route_set;
101}
102
103std::string
105{
106 if (not sip_name_addr->display.ptr or not sip_name_addr->display.slen)
107 return {};
108
109 auto displayName = as_view(sip_name_addr->display);
110
111 // Filter out invalid UTF-8 characters to avoid getting kicked from D-Bus
112 if (not utf8_validate(displayName))
113 return utf8_make_valid(displayName);
114
115 return std::string(displayName);
116}
117
118std::string
120{
121 // PJSIP return a pjsip_name_addr for To, From and Contact headers
122 return parseDisplayName(reinterpret_cast<pjsip_name_addr*>(header->uri));
123}
124
125std::string
127{
128 // PJSIP return a pjsip_name_addr for To, From and Contact headers
129 return parseDisplayName(reinterpret_cast<pjsip_name_addr*>(header->uri));
130}
131
132std::string_view
133stripSipUriPrefix(std::string_view sipUri)
134{
135 // Remove sip: prefix
136 static constexpr auto SIP_PREFIX = "sip:"sv;
137 size_t found = sipUri.find(SIP_PREFIX);
138
139 if (found != std::string_view::npos)
140 sipUri = sipUri.substr(found + SIP_PREFIX.size());
141
142 // URI may or may not be between brackets
143 found = sipUri.find('<');
144 if (found != std::string_view::npos)
145 sipUri = sipUri.substr(found + 1);
146
147 found = sipUri.find('@');
148 if (found != std::string_view::npos)
149 sipUri = sipUri.substr(0, found);
150
151 found = sipUri.find('>');
152 if (found != std::string_view::npos)
153 sipUri = sipUri.substr(0, found);
154
155 return sipUri;
156}
157
158std::string_view
159getHostFromUri(std::string_view uri)
160{
161 auto found = uri.find('@');
162 if (found != std::string_view::npos)
163 uri = uri.substr(found + 1);
164
165 found = uri.find('>');
166 if (found != std::string_view::npos)
167 uri = uri.substr(0, found);
168
169 return uri;
170}
171
172void
174{
175 if (contactHdr.empty()) {
176 JAMI_WARN("Contact header is unable to be added (empty string)");
177 return;
178 }
179
180 /*
181 * Duplicate contact header because tdata->msg keep a reference to it and
182 * can be used in a callback after destruction of the contact header in
183 * Jami. Bind lifetime of the duplicated string to the pool allocator of
184 * tdata.
185 */
186 auto pjContact = pj_strdup3(tdata->pool, contactHdr.c_str());
187
190 // remove old contact header (if present)
193}
194
195void
197{
198 if (tdata == nullptr or userAgent.empty())
199 return;
200
202
203 // Do nothing if user-agent header is present.
204 if (pjsip_msg_find_hdr_by_name(tdata->msg, &USER_AGENT_STR, nullptr) != nullptr) {
205 return;
206 }
207
208 // Add Header
209 auto* hdr = reinterpret_cast<pjsip_hdr*>(pjsip_user_agent_hdr_create(tdata->pool, &USER_AGENT_STR, &pjUserAgent));
210
211 if (hdr != nullptr) {
213 }
214}
215
216std::string_view
218{
219 if (rdata == nullptr or rdata->msg_info.msg == nullptr) {
220 JAMI_ERR("Unexpected null pointer!");
221 return {};
222 }
223
226 nullptr)) {
227 return as_view(uaHdr->hvalue);
228 }
229 return {};
230}
231
232std::vector<std::string>
234{
235 if (rdata == nullptr or rdata->msg_info.msg == nullptr) {
236 JAMI_ERR("Unexpected null pointer!");
237 return {};
238 }
239
240 std::vector<std::string> methods;
241
242 pjsip_allow_hdr* allow = static_cast<pjsip_allow_hdr*>(
243 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ALLOW, nullptr));
244
245 if (allow != nullptr) {
246 methods.reserve(allow->count);
247 for (unsigned i = 0; i < allow->count; i++) {
248 methods.emplace_back(allow->values[i].ptr, allow->values[i].slen);
249 }
250 }
251
252 return methods;
253}
254
255void
257{
258 const pjsip_hdr* hdr = hdr_list->next;
259 const pjsip_hdr* end = hdr_list;
260 std::string msgHdrStr("Message headers:\n");
261 for (; hdr != end; hdr = hdr->next) {
262 char buf[1024];
263 int size = pjsip_hdr_print_on((void*) hdr, buf, sizeof(buf));
264 if (size > 0) {
265 msgHdrStr.append(buf, size);
266 msgHdrStr.push_back('\n');
267 }
268 }
269
270 JAMI_LOG("{}", msgHdrStr);
271}
272
273std::string
275{
277 auto ret = pj_strerror(code, err_msg, sizeof err_msg);
278 return std::string {ret.ptr, ret.ptr + ret.slen};
279}
280
281std::string
282streamId(const std::string& callId, std::string_view label)
283{
284 if (callId.empty())
285 return fmt::format("host_{}", label);
286 return fmt::format("{}_{}", callId, label);
287}
288
289void
291{
292 host_port->host.ptr = (char*) pj_pool_alloc(pool, PJ_INET6_ADDRSTRLEN + 4);
293 pj_sockaddr_print(addr, host_port->host.ptr, PJ_INET6_ADDRSTRLEN + 4, 0);
294 host_port->host.slen = pj_ansi_strlen(host_port->host.ptr);
295 host_port->port = pj_sockaddr_get_port(addr);
296}
297
298} // namespace sip_utils
299} // namespace jami
std::string message(int condition) const override
Definition sip_utils.cpp:49
#define JAMI_ERR(...)
Definition logger.h:230
#define JAMI_DBG(...)
Definition logger.h:228
#define JAMI_WARN(...)
Definition logger.h:229
#define JAMI_LOG(formatstr,...)
Definition logger.h:237
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)
constexpr pj_str_t USER_AGENT_STR
Definition sip_utils.cpp:46
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)
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)
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)
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)
constexpr const pj_str_t CONST_PJ_STR(T(&a)[N]) noexcept
Definition sip_utils.h:87
bool utf8_validate(std::string_view str)
utf8_validate:
void emitSignal(Args... args)
Definition jami_signal.h:64
std::string utf8_make_valid(std::string_view name)
const std::string & userAgent()