Line data Source code
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 :
18 : #pragma once
19 :
20 : #ifdef HAVE_CONFIG_H
21 : #include "config.h"
22 : #endif
23 :
24 : #include "account.h"
25 :
26 : #include "connectivity/sip_utils.h"
27 : #include "noncopyable.h"
28 : #include "im/message_engine.h"
29 : #include "sipaccountbase_config.h"
30 :
31 : #include <dhtnet/turn_cache.h>
32 : #include <dhtnet/ip_utils.h>
33 : #include <dhtnet/ice_options.h>
34 :
35 : #include <array>
36 : #include <deque>
37 : #include <map>
38 : #include <memory>
39 : #include <mutex>
40 : #include <vector>
41 :
42 : extern "C" {
43 : #include <pjsip/sip_types.h>
44 : #ifdef _WIN32
45 : typedef uint16_t in_port_t;
46 : #else
47 : #include <netinet/in.h> // For in_port_t support
48 : #endif
49 :
50 : struct pjsip_dialog;
51 : struct pjsip_inv_session;
52 : struct pjmedia_sdp_session;
53 : }
54 :
55 : static constexpr const char MIME_TYPE_TEXT_PLAIN[] {"text/plain"};
56 :
57 : namespace jami {
58 :
59 : class SipTransport;
60 : class Task;
61 :
62 : typedef std::vector<pj_ssl_cipher> CipherArray;
63 :
64 : class SIPVoIPLink;
65 : class SIPCall;
66 :
67 : /**
68 : * @file sipaccount.h
69 : * @brief A SIP Account specify SIP specific functions and object = SIPCall/SIPVoIPLink)
70 : */
71 :
72 : enum class MatchRank { NONE, PARTIAL, FULL };
73 :
74 : class SIPAccountBase : public Account
75 : {
76 : public:
77 : constexpr static unsigned MAX_PORT {65536};
78 : constexpr static unsigned HALF_MAX_PORT {MAX_PORT / 2};
79 :
80 : /**
81 : * Constructor
82 : * @param accountID The account identifier
83 : */
84 : SIPAccountBase(const std::string& accountID);
85 :
86 : virtual ~SIPAccountBase() noexcept;
87 :
88 2060 : const SipAccountBaseConfig& config() const { return *static_cast<const SipAccountBaseConfig*>(&Account::config()); }
89 :
90 : void loadConfig() override;
91 :
92 : /**
93 : * Create incoming SIPCall.
94 : * @param[in] from The origin of the call
95 : * @param mediaList A list of media
96 : * @param sipTr: SIP Transport
97 : * @return A shared pointer on the created call.
98 : */
99 : virtual std::shared_ptr<SIPCall> newIncomingCall(const std::string& from,
100 : const std::vector<libjami::MediaMap>& mediaList,
101 : const std::shared_ptr<SipTransport>& sipTr = {})
102 : = 0;
103 :
104 91 : virtual bool isStunEnabled() const { return false; }
105 :
106 0 : virtual pj_uint16_t getStunPort() const { return 0; };
107 :
108 0 : virtual std::string getDtmfType() const { return config().dtmfType; }
109 :
110 : /**
111 : * Determine if TLS is enabled for this account. TLS provides a secured channel for
112 : * SIP signalization. It is independent of the media encryption (as provided by SRTP).
113 : */
114 0 : virtual bool isTlsEnabled() const { return false; }
115 :
116 : /**
117 : * Create UAC attached dialog and invite session
118 : * @return true if success. false if failure and dlg and inv pointers
119 : * should not be considered as valid.
120 : */
121 : bool CreateClientDialogAndInvite(const pj_str_t* from,
122 : const pj_str_t* contact,
123 : const pj_str_t* to,
124 : const pj_str_t* target,
125 : const pjmedia_sdp_session* local_sdp,
126 : pjsip_dialog** dlg,
127 : pjsip_inv_session** inv);
128 :
129 : /**
130 : * Get the local interface name on which this account is bound.
131 : */
132 291 : const std::string& getLocalInterface() const { return config().interface; }
133 :
134 : /**
135 : * Get the public IP address set by the user for this account.
136 : * If this setting is not provided, the local bound adddress
137 : * will be used.
138 : * @return std::string The public IPv4 or IPv6 address formatted in standard notation.
139 : */
140 0 : std::string getPublishedAddress() const { return config().publishedIp; }
141 :
142 : virtual dhtnet::IpAddr getPublishedIpAddress(uint16_t family = PF_UNSPEC) const;
143 :
144 : void setPublishedAddress(const dhtnet::IpAddr& ip_addr);
145 :
146 : /**
147 : * Get a flag which determine the usage in sip headers of either the local
148 : * IP address and port (_localAddress and localPort_) or to an address set
149 : * manually (_publishedAddress and publishedPort_).
150 : */
151 199 : bool getPublishedSameasLocal() const { return config().publishedSameasLocal; }
152 :
153 : virtual bool isSrtpEnabled() const = 0;
154 :
155 : virtual std::string getToUri(const std::string& username) const = 0;
156 :
157 : /**
158 : * Socket port generators for media
159 : * Note: given ports are application wide, a port is unable to be given again
160 : * by any account instances until it's released by the static method
161 : * releasePort().
162 : */
163 : uint16_t generateAudioPort() const;
164 : #ifdef ENABLE_VIDEO
165 : uint16_t generateVideoPort() const;
166 : #endif
167 : static void releasePort(uint16_t port) noexcept;
168 :
169 : virtual dhtnet::IceTransportOptions getIceOptions() const;
170 :
171 : virtual void sendMessage(const std::string& to,
172 : const std::string& deviceId,
173 : const std::map<std::string, std::string>& payloads,
174 : uint64_t id,
175 : bool retryOnTimeout = true,
176 : bool onlyConnected = false)
177 : = 0;
178 :
179 14403 : virtual uint64_t sendTextMessage(const std::string& to,
180 : const std::string& deviceId,
181 : const std::map<std::string, std::string>& payloads,
182 : uint64_t refreshToken = 0,
183 : bool onlyConnected = false) override
184 : {
185 14403 : if (onlyConnected) {
186 0 : auto token = std::uniform_int_distribution<uint64_t> {1, JAMI_ID_MAX_VAL}(rand);
187 0 : sendMessage(to, deviceId, payloads, token, false, true);
188 0 : return token;
189 : }
190 14403 : return messageEngine_.sendMessage(to, deviceId, payloads, refreshToken);
191 : }
192 :
193 0 : im::MessageStatus getMessageStatus(uint64_t id) const override { return messageEngine_.getStatus(id); }
194 :
195 : virtual void onTextMessage(const std::string& id,
196 : const std::string& from,
197 : const std::shared_ptr<dht::crypto::Certificate>& peerCert,
198 : const std::map<std::string, std::string>& payloads);
199 :
200 : /* Returns true if the username and/or hostname match this account */
201 : virtual MatchRank matches(std::string_view username, std::string_view hostname) const = 0;
202 :
203 0 : void connectivityChanged() override {};
204 :
205 : virtual std::string getUserUri() const = 0;
206 :
207 : std::vector<libjami::Message> getLastMessages(const uint64_t& base_timestamp) override;
208 :
209 : // Build the list of medias to be included in the SDP (offer/answer)
210 : std::vector<MediaAttribute> createDefaultMediaList(bool addVideo, bool hold = false);
211 :
212 : public: // overloaded methods
213 : virtual void flush() override;
214 :
215 : protected:
216 : /**
217 : * Retrieve volatile details such as recent registration errors
218 : * @return std::map< std::string, std::string > The account volatile details
219 : */
220 : virtual std::map<std::string, std::string> getVolatileAccountDetails() const override;
221 :
222 : virtual void setRegistrationState(RegistrationState state,
223 : int code = 0,
224 : const std::string& detail_str = {}) override;
225 :
226 : im::MessageEngine messageEngine_;
227 :
228 : /**
229 : * Voice over IP Link contains a listener thread and calls
230 : */
231 : SIPVoIPLink& link_;
232 :
233 : /**
234 : * Published IPv4/IPv6 addresses, used only if defined by the user in account
235 : * configuration
236 : *
237 : */
238 : dhtnet::IpAddr publishedIp_[2] {};
239 :
240 : pj_status_t transportStatus_ {PJSIP_SC_TRYING};
241 : std::string transportError_ {};
242 :
243 : static std::array<bool, HALF_MAX_PORT>& getPortsReservation() noexcept;
244 : static uint16_t acquirePort(uint16_t port);
245 : uint16_t getRandomEvenPort(const std::pair<uint16_t, uint16_t>& range) const;
246 : uint16_t acquireRandomEvenPort(const std::pair<uint16_t, uint16_t>& range) const;
247 :
248 : /**
249 : * The deamon can be launched without any client (or with a non ready client)
250 : * Like call and file transfer, a client should be able to retrieve current messages.
251 : * To avoid to explode the size in memory, this container should be limited.
252 : * We don't want to see monsters in memory.
253 : */
254 : std::mutex mutexLastMessages_;
255 : static constexpr size_t MAX_WAITING_MESSAGES_SIZE = 1000;
256 : std::deque<libjami::Message> lastMessages_;
257 :
258 : std::shared_ptr<dhtnet::TurnCache> turnCache_;
259 :
260 : private:
261 : NON_COPYABLE(SIPAccountBase);
262 : };
263 :
264 : } // namespace jami
|