Ring Daemon 16.0.0
Loading...
Searching...
No Matches
siptransport.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
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
24
25#include "noncopyable.h"
26#include "logger.h"
27
28#include <pjsip.h>
29#include <pjnath/stun_config.h>
30
31#include <functional>
32#include <mutex>
33#include <condition_variable>
34#include <map>
35#include <string>
36#include <vector>
37#include <list>
38#include <memory>
39
40// OpenDHT
41namespace dht {
42namespace crypto {
43struct Certificate;
44}
45} // namespace dht
46
47namespace dhtnet {
48class ChannelSocket;
49} // namespace dhtnet
50
51namespace jami {
52class SIPAccountBase;
53using onShutdownCb = std::function<void(void)>;
54
56{
59 : listener(f)
60 {}
61 virtual ~TlsListener()
62 {
63 JAMI_DBG("Destroying listener");
64 listener->destroy(listener);
65 }
66 pjsip_tpfactory* get() { return listener; }
67
68private:
70 pjsip_tpfactory* listener {nullptr};
71};
72
80
83
88{
89public:
91 SipTransport(pjsip_transport*, const std::shared_ptr<TlsListener>&);
92 // If the SipTransport is a channeled transport, we are already connected to the peer,
93 // so, we can directly set tlsInfos_.peerCert and avoid any copy
95 const std::shared_ptr<dht::crypto::Certificate>& peerCertficate);
96
98
99 static const char* stateToStr(pjsip_transport_state state);
100
102
103 pjsip_transport* get() { return transport_.get(); }
104
107
108 bool isSecure() const { return PJSIP_TRANSPORT_IS_SECURE(transport_); }
109
110 const TlsInfos& getTlsInfos() const { return tlsInfos_; }
111
112 static bool isAlive(pjsip_transport_state state);
113
115 bool isConnected() const noexcept { return connected_; }
116
117 inline void setDeviceId(const std::string& deviceId) { deviceId_ = deviceId; }
118 inline std::string_view deviceId() const { return deviceId_; }
119 inline void setAccount(const std::shared_ptr<SIPAccountBase>& account) { account_ = account; }
120 inline const std::weak_ptr<SIPAccountBase>& getAccount() const { return account_; }
121
123
124private:
126
127 static void deleteTransport(pjsip_transport* t);
128
129 std::unique_ptr<pjsip_transport, decltype(deleteTransport)&> transport_;
130 std::shared_ptr<TlsListener> tlsListener_;
131 std::mutex stateListenersMutex_;
132 std::map<uintptr_t, SipTransportStateCallback> stateListeners_;
133 std::weak_ptr<SIPAccountBase> account_ {};
134
135 bool connected_ {false};
136 std::string deviceId_ {};
137 TlsInfos tlsInfos_;
138};
139
140class IceTransport;
141namespace tls {
142struct TlsParams;
143};
144
149{
150public:
153
154 std::shared_ptr<SipTransport> getUdpTransport(const dhtnet::IpAddr&);
155
156 std::shared_ptr<TlsListener> getTlsListener(const dhtnet::IpAddr&, const pjsip_tls_setting*);
157
158 std::shared_ptr<SipTransport> getTlsTransport(const std::shared_ptr<TlsListener>&,
159 const dhtnet::IpAddr& remote,
160 const std::string& remote_name = {});
161
162 std::shared_ptr<SipTransport> addTransport(pjsip_transport*);
163
164 std::shared_ptr<SipTransport> getChanneledTransport(
165 const std::shared_ptr<SIPAccountBase>& account,
166 const std::shared_ptr<dhtnet::ChannelSocket>& socket,
167 onShutdownCb&& cb);
168
172 void shutdown();
173
177
178private:
180
187 std::shared_ptr<SipTransport> createUdpTransport(const dhtnet::IpAddr&);
188
192 std::map<pjsip_transport*, std::weak_ptr<SipTransport>> transports_ {};
193 std::mutex transportMapMutex_ {};
194
199 std::map<dhtnet::IpAddr, pjsip_transport*> udpTransports_;
200
201 pjsip_endpoint* endpt_;
202 std::atomic_bool isDestroying_ {false};
203};
204
205} // namespace jami
Manages the transports and receive callbacks from PJSIP.
std::shared_ptr< SipTransport > addTransport(pjsip_transport *)
std::shared_ptr< SipTransport > getChanneledTransport(const std::shared_ptr< SIPAccountBase > &account, const std::shared_ptr< dhtnet::ChannelSocket > &socket, onShutdownCb &&cb)
void transportStateChanged(pjsip_transport *, pjsip_transport_state, const pjsip_transport_state_info *)
std::shared_ptr< SipTransport > getTlsTransport(const std::shared_ptr< TlsListener > &, const dhtnet::IpAddr &remote, const std::string &remote_name={})
std::shared_ptr< TlsListener > getTlsListener(const dhtnet::IpAddr &, const pjsip_tls_setting *)
void shutdown()
Start graceful shutdown procedure for all transports.
std::shared_ptr< SipTransport > getUdpTransport(const dhtnet::IpAddr &)
SIP transport wraps pjsip_transport.
bool isSecure() const
bool removeStateListener(uintptr_t lid)
void addStateListener(uintptr_t lid, SipTransportStateCallback cb)
void setDeviceId(const std::string &deviceId)
void stateCallback(pjsip_transport_state state, const pjsip_transport_state_info *info)
const std::weak_ptr< SIPAccountBase > & getAccount() const
std::string_view deviceId() const
void setAccount(const std::shared_ptr< SIPAccountBase > &account)
static const char * stateToStr(pjsip_transport_state state)
const TlsInfos & getTlsInfos() const
static bool isAlive(pjsip_transport_state state)
bool isConnected() const noexcept
Only makes sense for connection-oriented transports.
pjsip_transport * get()
#define JAMI_DBG(...)
Definition logger.h:216
Definition account.h:55
void emitSignal(Args... args)
Definition ring_signal.h:64
std::function< void(pjsip_transport_state, const pjsip_transport_state_info *)> SipTransportStateCallback
std::function< void(void)> onShutdownCb
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30
pj_ssl_sock_proto proto
pj_ssl_cipher cipher
pj_ssl_cert_verify_flag_t verifyStatus
std::shared_ptr< dht::crypto::Certificate > peerCert
TlsListener(pjsip_tpfactory *f)
virtual ~TlsListener()
pjsip_tpfactory * get()