Ring Daemon 16.0.0
Loading...
Searching...
No Matches
sipaccountbase.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
23#include "account.h"
24
26#include "noncopyable.h"
27#include "im/message_engine.h"
29
30#include <dhtnet/turn_cache.h>
31#include <dhtnet/ip_utils.h>
32#include <dhtnet/ice_options.h>
33
34#include <array>
35#include <deque>
36#include <map>
37#include <memory>
38#include <mutex>
39#include <vector>
40
41extern "C" {
42#include <pjsip/sip_types.h>
43#ifdef _WIN32
44typedef uint16_t in_port_t;
45#else
46#include <netinet/in.h> // For in_port_t support
47#endif
48
49struct pjsip_dialog;
50struct pjsip_inv_session;
51struct pjmedia_sdp_session;
52}
53
54static constexpr const char MIME_TYPE_TEXT_PLAIN[] {"text/plain"};
55
56namespace jami {
57
58class SipTransport;
59class Task;
60
61typedef std::vector<pj_ssl_cipher> CipherArray;
62
63class SIPVoIPLink;
64class SIPCall;
65
71enum class MatchRank { NONE, PARTIAL, FULL };
72
73class SIPAccountBase : public Account
74{
75public:
76 constexpr static unsigned MAX_PORT {65536};
77 constexpr static unsigned HALF_MAX_PORT {MAX_PORT / 2};
78
83 SIPAccountBase(const std::string& accountID);
84
85 virtual ~SIPAccountBase() noexcept;
86
88 {
89 return *static_cast<const SipAccountBaseConfig*>(&Account::config());
90 }
91
92 void loadConfig() override;
93
101 virtual std::shared_ptr<SIPCall> newIncomingCall(const std::string& from,
102 const std::vector<libjami::MediaMap>& mediaList,
103 const std::shared_ptr<SipTransport>& sipTr = {})
104 = 0;
105
106 virtual bool isStunEnabled() const { return false; }
107
108 virtual pj_uint16_t getStunPort() const { return 0; };
109
110 virtual std::string getDtmfType() const { return config().dtmfType; }
111
116 virtual bool isTlsEnabled() const { return false; }
117
123 bool CreateClientDialogAndInvite(const pj_str_t* from,
124 const pj_str_t* contact,
125 const pj_str_t* to,
126 const pj_str_t* target,
130
134 const std::string& getLocalInterface() const { return config().interface; }
135
142 std::string getPublishedAddress() const { return config().publishedIp; }
143
144 virtual dhtnet::IpAddr getPublishedIpAddress(uint16_t family = PF_UNSPEC) const;
145
146 void setPublishedAddress(const dhtnet::IpAddr& ip_addr);
147
154
155 virtual bool isSrtpEnabled() const = 0;
156
157 virtual bool getSrtpFallback() const = 0;
158
159 virtual std::string getToUri(const std::string& username) const = 0;
160
168#ifdef ENABLE_VIDEO
170#endif
171 static void releasePort(uint16_t port) noexcept;
172
173 virtual dhtnet::IceTransportOptions getIceOptions() const;
174
175 virtual void sendMessage(const std::string& to,
176 const std::string& deviceId,
177 const std::map<std::string, std::string>& payloads,
178 uint64_t id,
179 bool retryOnTimeout = true,
180 bool onlyConnected = false)
181 = 0;
182
183 virtual uint64_t sendTextMessage(const std::string& to,
184 const std::string& deviceId,
185 const std::map<std::string, std::string>& payloads,
187 bool onlyConnected = false) override
188 {
189 if (onlyConnected) {
190 auto token = std::uniform_int_distribution<uint64_t> {1, JAMI_ID_MAX_VAL}(rand);
191 sendMessage(to, deviceId, payloads, token, false, true);
192 return token;
193 }
194 return messageEngine_.sendMessage(to, deviceId, payloads, refreshToken);
195 }
196
198 {
199 return messageEngine_.getStatus(id);
200 }
201
202 virtual void onTextMessage(const std::string& id,
203 const std::string& from,
204 const std::shared_ptr<dht::crypto::Certificate>& peerCert,
205 const std::map<std::string, std::string>& payloads);
206
207 /* Returns true if the username and/or hostname match this account */
208 virtual MatchRank matches(std::string_view username, std::string_view hostname) const = 0;
209
210 void connectivityChanged() override {};
211
212 virtual std::string getUserUri() const = 0;
213
214 std::vector<libjami::Message> getLastMessages(const uint64_t& base_timestamp) override;
215
216 // Build the list of medias to be included in the SDP (offer/answer)
217 std::vector<MediaAttribute> createDefaultMediaList(bool addVideo, bool onHold = false);
218
219public: // overloaded methods
220 virtual void flush() override;
221
222protected:
227 virtual std::map<std::string, std::string> getVolatileAccountDetails() const override;
228
229 virtual void setRegistrationState(RegistrationState state,
230 int code = 0,
231 const std::string& detail_str = {}) override;
232
234
239
245 dhtnet::IpAddr publishedIp_[2] {};
246
248 std::string transportError_ {};
249
250 static std::array<bool, HALF_MAX_PORT>& getPortsReservation() noexcept;
254
264
266
267private:
269};
270
271} // namespace jami
Interface to protocol account (ex: SIPAccount) It can be enable on loading or activate after.
std::mt19937_64 rand
Random generator engine Logical account state shall never rely on the state of the random generator.
Definition account.h:349
const AccountConfig & config() const
Definition account.h:113
virtual pj_uint16_t getStunPort() const
SIPVoIPLink & link_
Voice over IP Link contains a listener thread and calls.
virtual std::string getUserUri() const =0
static constexpr size_t MAX_WAITING_MESSAGES_SIZE
void connectivityChanged() override
Inform the account that the network status has changed.
static void releasePort(uint16_t port) noexcept
dhtnet::IpAddr publishedIp_[2]
Published IPv4/IPv6 addresses, used only if defined by the user in account configuration.
virtual void sendMessage(const std::string &to, const std::string &deviceId, const std::map< std::string, std::string > &payloads, uint64_t id, bool retryOnTimeout=true, bool onlyConnected=false)=0
bool getPublishedSameasLocal() const
Get a flag which determine the usage in sip headers of either the local IP address and port (localAdd...
std::vector< MediaAttribute > createDefaultMediaList(bool addVideo, bool onHold=false)
const SipAccountBaseConfig & config() const
static constexpr unsigned MAX_PORT
pj_status_t transportStatus_
virtual bool isSrtpEnabled() const =0
virtual bool getSrtpFallback() const =0
virtual void setRegistrationState(RegistrationState state, int code=0, const std::string &detail_str={}) override
Set the registration state of the specified link.
static uint16_t acquirePort(uint16_t port)
uint16_t acquireRandomEvenPort(const std::pair< uint16_t, uint16_t > &range) const
static constexpr unsigned HALF_MAX_PORT
std::deque< libjami::Message > lastMessages_
im::MessageStatus getMessageStatus(uint64_t id) const override
Return the status corresponding to the token.
std::mutex mutexLastMessages_
The deamon can be launched without any client (or with a non ready client) Like call and file transfe...
virtual uint64_t sendTextMessage(const std::string &to, const std::string &deviceId, const std::map< std::string, std::string > &payloads, uint64_t refreshToken=0, bool onlyConnected=false) override
If supported, send a text message from this account.
virtual ~SIPAccountBase() noexcept
virtual std::map< std::string, std::string > getVolatileAccountDetails() const override
Retrieve volatile details such as recent registration errors.
std::string getPublishedAddress() const
Get the public IP address set by the user for this account.
virtual void onTextMessage(const std::string &id, const std::string &from, const std::shared_ptr< dht::crypto::Certificate > &peerCert, const std::map< std::string, std::string > &payloads)
virtual MatchRank matches(std::string_view username, std::string_view hostname) const =0
uint16_t generateAudioPort() const
Socket port generators for media Note: given ports are application wide, a port is unable to be given...
std::vector< libjami::Message > getLastMessages(const uint64_t &base_timestamp) override
virtual bool isTlsEnabled() const
Determine if TLS is enabled for this account.
uint16_t getRandomEvenPort(const std::pair< uint16_t, uint16_t > &range) const
virtual dhtnet::IpAddr getPublishedIpAddress(uint16_t family=PF_UNSPEC) const
std::string transportError_
virtual std::string getDtmfType() const
im::MessageEngine messageEngine_
const std::string & getLocalInterface() const
Get the local interface name on which this account is bound.
bool CreateClientDialogAndInvite(const pj_str_t *from, const pj_str_t *contact, const pj_str_t *to, const pj_str_t *target, const pjmedia_sdp_session *local_sdp, pjsip_dialog **dlg, pjsip_inv_session **inv)
Create UAC attached dialog and invite session.
void setPublishedAddress(const dhtnet::IpAddr &ip_addr)
virtual std::string getToUri(const std::string &username) const =0
static std::array< bool, HALF_MAX_PORT > & getPortsReservation() noexcept
void loadConfig() override
Load the settings in this account.
virtual std::shared_ptr< SIPCall > newIncomingCall(const std::string &from, const std::vector< libjami::MediaMap > &mediaList, const std::shared_ptr< SipTransport > &sipTr={})=0
Create incoming SIPCall.
virtual bool isStunEnabled() const
virtual void flush() override
This method is called to request removal of possible account traces on the system,...
virtual dhtnet::IceTransportOptions getIceOptions() const
std::shared_ptr< dhtnet::TurnCache > turnCache_
MessageToken sendMessage(const std::string &to, const std::string &deviceId, const std::map< std::string, std::string > &payloads, uint64_t refreshToken)
Add a message to the engine and try to send it.
MessageStatus getStatus(MessageToken t) const
RegistrationState
Contains all the Registration states for an account can be in.
void emitSignal(Args... args)
Definition ring_signal.h:64
std::vector< pj_ssl_cipher > CipherArray
Definition sipaccount.h:38
static constexpr uint64_t JAMI_ID_MAX_VAL
Definition account.h:62
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30
static constexpr const char MIME_TYPE_TEXT_PLAIN[]
bool publishedSameasLocal
Flag which determine if localIpAddress_ or publishedIpAddress_ is used in sip headers.
std::string interface
interface name on which this account is bound