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