Ring Daemon
Loading...
Searching...
No Matches
siptransport.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 <dhtnet/ip_utils.h>
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include "noncopyable.h"
25#include "logger.h"
26
27#include <pjsip.h>
28#include <pjnath/stun_config.h>
29
30#include <functional>
31#include <mutex>
32#include <map>
33#include <string>
34#include <memory>
35
36// OpenDHT
37namespace dht {
38namespace crypto {
39struct Certificate;
40}
41} // namespace dht
42
43namespace dhtnet {
44class ChannelSocket;
45} // namespace dhtnet
46
47namespace jami {
48class SIPAccountBase;
49using onShutdownCb = std::function<void(void)>;
50
52{
55 : listener(f)
56 {}
57 virtual ~TlsListener()
58 {
59 JAMI_DBG("Destroying listener");
60 listener->destroy(listener);
61 }
62 pjsip_tpfactory* get() { return listener; }
63
64private:
66 pjsip_tpfactory* listener {nullptr};
67};
68
76
78
83{
84public:
86 SipTransport(pjsip_transport*, const std::shared_ptr<TlsListener>&);
87 // If the SipTransport is a channeled transport, we are already connected to the peer,
88 // so, we can directly set tlsInfos_.peerCert and avoid any copy
89 SipTransport(pjsip_transport* t, const std::shared_ptr<dht::crypto::Certificate>& peerCertficate);
90
92
93 static const char* stateToStr(pjsip_transport_state state);
94
96
97 pjsip_transport* get() { return transport_.get(); }
98
101
102 bool isSecure() const { return PJSIP_TRANSPORT_IS_SECURE(transport_); }
103
104 const TlsInfos& getTlsInfos() const { return tlsInfos_; }
105
106 static bool isAlive(pjsip_transport_state state);
107
109 bool isConnected() const noexcept { return connected_; }
110
111 inline void setDeviceId(const std::string& deviceId) { deviceId_ = deviceId; }
112 inline std::string_view deviceId() const { return deviceId_; }
113 inline void setAccount(const std::shared_ptr<SIPAccountBase>& account) { account_ = account; }
114 inline const std::weak_ptr<SIPAccountBase>& getAccount() const { return account_; }
115
117
118private:
120
121 static void deleteTransport(pjsip_transport* t);
122 struct TransportDeleter
123 {
124 void operator()(pjsip_transport* t) const noexcept { deleteTransport(t); }
125 };
126
127 std::unique_ptr<pjsip_transport, TransportDeleter> transport_;
128 std::shared_ptr<TlsListener> tlsListener_;
129 std::mutex stateListenersMutex_;
130 std::map<uintptr_t, SipTransportStateCallback> stateListeners_;
131 std::weak_ptr<SIPAccountBase> account_ {};
132
133 bool connected_ {false};
134 std::string deviceId_ {};
135 TlsInfos tlsInfos_;
136};
137
138class IceTransport;
139namespace tls {
140struct TlsParams;
141};
142
147{
148public:
151
152 std::shared_ptr<SipTransport> getUdpTransport(const dhtnet::IpAddr&);
153
154 std::shared_ptr<TlsListener> getTlsListener(const dhtnet::IpAddr&, const pjsip_tls_setting*);
155
156 std::shared_ptr<SipTransport> getTlsTransport(const std::shared_ptr<TlsListener>&,
157 const dhtnet::IpAddr& remote,
158 const std::string& remote_name = {});
159
160 std::shared_ptr<SipTransport> addTransport(pjsip_transport*);
161
162 std::shared_ptr<SipTransport> getChanneledTransport(const std::shared_ptr<SIPAccountBase>& account,
163 const std::shared_ptr<dhtnet::ChannelSocket>& socket,
164 onShutdownCb&& cb);
165
169 void shutdown();
170
172
173private:
175
182 std::shared_ptr<SipTransport> createUdpTransport(const dhtnet::IpAddr&);
183
187 std::map<pjsip_transport*, std::weak_ptr<SipTransport>> transports_ {};
188 std::mutex transportMapMutex_ {};
189
194 std::map<dhtnet::IpAddr, pjsip_transport*> udpTransports_;
195
196 pjsip_endpoint* endpt_;
197 std::atomic_bool isDestroying_ {false};
198};
199
200} // 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, const 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:228
Definition account.h:50
void emitSignal(Args... args)
Definition jami_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()