sipaccountbase.h 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 *  Copyright (C) 2014 Savoir-Faire Linux Inc.
 *
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#ifndef SIPACCOUNTBASE_H
#define SIPACCOUNTBASE_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "siptransport.h"
Guillaume Roguez's avatar
Guillaume Roguez committed
40 41
#include "sip_utils.h"

42 43 44 45 46 47 48 49
#include "account.h"
#include "noncopyable.h"
#include "ip_utils.h"

#include <pjsip/sip_types.h>

#include <vector>
#include <map>
50
#include <sstream>
51
#include <memory>
52

53
namespace ring {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

namespace Conf {
    // SIP specific configuration keys
    const char *const INTERFACE_KEY = "interface";
    const char *const PORT_KEY = "port";
    const char *const PUBLISH_ADDR_KEY = "publishAddr";
    const char *const PUBLISH_PORT_KEY = "publishPort";
    const char *const SAME_AS_LOCAL_KEY = "sameasLocal";
    const char *const DTMF_TYPE_KEY = "dtmfType";
    const char *const SERVICE_ROUTE_KEY = "serviceRoute";
    const char *const PRESENCE_ENABLED_KEY = "presenceEnabled";
    const char *const PRESENCE_PUBLISH_SUPPORTED_KEY = "presencePublishSupported";
    const char *const PRESENCE_SUBSCRIBE_SUPPORTED_KEY = "presenceSubscribeSupported";
    const char *const PRESENCE_STATUS_KEY = "presenceStatus";
    const char *const PRESENCE_NOTE_KEY = "presenceNote";

    // TODO: write an object to store tls params which implement serializable
    const char *const TLS_KEY = "tls";
    const char *const TLS_PORT_KEY = "tlsPort";
    const char *const CERTIFICATE_KEY = "certificate";
    const char *const CALIST_KEY = "calist";
    const char *const CIPHERS_KEY = "ciphers";
    const char *const TLS_ENABLE_KEY = "enable";
    const char *const METHOD_KEY = "method";
    const char *const TIMEOUT_KEY = "timeout";
    const char *const TLS_PASSWORD_KEY = "password";
    const char *const PRIVATE_KEY_KEY = "privateKey";
    const char *const REQUIRE_CERTIF_KEY = "requireCertif";
    const char *const SERVER_KEY = "server";
    const char *const VERIFY_CLIENT_KEY = "verifyClient";
    const char *const VERIFY_SERVER_KEY = "verifyServer";

    const char *const STUN_ENABLED_KEY = "stunEnabled";
    const char *const STUN_SERVER_KEY = "stunServer";
    const char *const CRED_KEY = "credential";
    const char *const AUDIO_PORT_MIN_KEY = "audioPortMin";
    const char *const AUDIO_PORT_MAX_KEY = "audioPortMax";
    const char *const VIDEO_PORT_MIN_KEY = "videoPortMin";
    const char *const VIDEO_PORT_MAX_KEY = "videoPortMax";
}

95 96
typedef std::vector<pj_ssl_cipher> CipherArray;

97 98 99 100 101 102 103
class SIPVoIPLink;
class SIPCall;

/**
 * @file sipaccount.h
 * @brief A SIP Account specify SIP specific functions and object = SIPCall/SIPVoIPLink)
 */
104

105 106 107 108 109 110
enum class MatchRank {NONE, PARTIAL, FULL};

class SIPAccountBase : public Account {
public:
    constexpr static const char * const OVERRTP_STR = "overrtp";
    constexpr static const char * const SIPINFO_STR = "sipinfo";
111 112
    constexpr static unsigned MAX_PORT {65536};
    constexpr static unsigned HALF_MAX_PORT {MAX_PORT / 2};
113 114 115 116 117 118 119

    /**
     * Constructor
     * @param accountID The account identifier
     */
    SIPAccountBase(const std::string& accountID);

120
    virtual ~SIPAccountBase();
121

122 123 124 125 126 127 128 129
    /**
     * Create incoming SIPCall.
     * @param[in] id The ID of the call
     * @return std::shared_ptr<T> A shared pointer on the created call.
     *      The type of this instance is given in template argument.
     *      This type can be any base class of SIPCall class (included).
     */
    virtual std::shared_ptr<SIPCall>
130
    newIncomingCall(const std::string& from) = 0;
131 132 133 134 135

    virtual bool isStunEnabled() const {
        return false;
    }

136
    virtual pj_str_t getStunServerName() const { return pj_str_t {nullptr, 0}; };
137 138 139 140 141 142 143

    virtual pj_uint16_t getStunPort() const { return 0; };

    virtual std::string getDtmfType() const {
        return dtmfType_;
    }

Adrien Béraud's avatar
Adrien Béraud committed
144 145 146 147 148 149
    /**
     * Determine if TLS is enabled for this account. TLS provides a secured channel for
     * SIP signalization. It is independant than the media encription provided by SRTP or ZRTP.
     */
    virtual bool isTlsEnabled() const {
        return false;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    }

    virtual pjsip_tls_setting * getTlsSetting() {
        return nullptr;
    }

    /**
     * Get the local port for TLS listener.
     * @return pj_uint16 The port used for that account
     */
    pj_uint16_t getTlsListenerPort() const {
        return tlsListenerPort_;
    }

    /**
     * Get the public IP address set by the user for this account.
     * If this setting is not provided, the local bound adddress
     * will be used.
     * @return std::string The public IPv4 or IPv6 address formatted in standard notation.
     */
    std::string getPublishedAddress() const {
        return publishedIpAddress_;
    }

    IpAddr getPublishedIpAddress() const {
        return publishedIp_;
    }

    void setPublishedAddress(const IpAddr& ip_addr) {
        publishedIp_ = ip_addr;
        publishedIpAddress_ = ip_addr.toString();
    }

    /**
     * Get the local interface name on which this account is bound.
     */
    const std::string& getLocalInterface() const {
        return interface_;
    }

    /**
     * Get a flag which determine the usage in sip headers of either the local
     * IP address and port (_localAddress and localPort_) or to an address set
     * manually (_publishedAddress and publishedPort_).
     */
    bool getPublishedSameasLocal() const {
        return publishedSameasLocal_;
    }

    /**
     * Get the port on which the transport/listener should use, or is
     * actually using.
     * @return pj_uint16 The port used for that account
     */
    pj_uint16_t getLocalPort() const {
        return localPort_;
    }

    /**
     * Set the new port on which this account is running over.
     * @pram port The port used by this account.
     */
    void setLocalPort(pj_uint16_t port) {
        localPort_ = port;
    }

    /**
     * Get the published port, which is the port to be advertised as the port
     * for the chosen SIP transport.
     * @return pj_uint16 The port used for that account
     */
    pj_uint16_t getPublishedPort() const {
        return (pj_uint16_t) publishedPort_;
    }

    /**
     * Set the published port, which is the port to be advertised as the port
     * for the chosen SIP transport.
     * @pram port The port used by this account.
     */
    void setPublishedPort(pj_uint16_t port) {
        publishedPort_ = port;
    }

Adrien Béraud's avatar
Adrien Béraud committed
234 235
    virtual bool getSrtpEnabled() const {
        return false;
236 237
    }

Guillaume Roguez's avatar
Guillaume Roguez committed
238
    virtual sip_utils::KeyExchangeProtocol getSrtpKeyExchange() const = 0;
239

Adrien Béraud's avatar
Adrien Béraud committed
240
    virtual bool getSrtpFallback() const = 0;
241 242 243 244 245

    /**
     * Get the contact header for
     * @return pj_str_t The contact header based on account information
     */
246
    virtual pj_str_t getContactHeader(pjsip_transport* = nullptr) = 0;
247 248 249 250 251

    virtual std::string getToUri(const std::string& username) const = 0;

    virtual std::string getServerUri() const = 0;

252 253 254
    virtual void setTransport(const std::shared_ptr<SipTransport>& = nullptr);

    inline const std::shared_ptr<SipTransport>& getTransport() {
255 256 257 258 259 260 261 262 263 264
        return transport_;
    }

    inline pjsip_transport_type_e getTransportType() const {
        return transportType_;
    }

    /**
     * Shortcut for SipTransport::getTransportSelector(account.getTransport()).
     */
265
    pjsip_tpselector getTransportSelector();
266

267 268 269 270 271 272 273 274 275 276 277 278
    /**
     * Socket port generators for media
     * Note: given ports are application wide, a port cannot be given again
     * by any account instances until it's released by the static method
     * releasePort().
     */
    uint16_t generateAudioPort() const;
#ifdef RING_VIDEO
    uint16_t generateVideoPort() const;
#endif
    static void releasePort(uint16_t port) noexcept;

279
protected:
280
    virtual void serialize(YAML::Emitter &out);
Adrien Béraud's avatar
Adrien Béraud committed
281
    virtual void serializeTls(YAML::Emitter &out);
282 283 284 285 286 287
    virtual void unserialize(const YAML::Node &node);

    virtual void setAccountDetails(const std::map<std::string, std::string> &details);

    virtual std::map<std::string, std::string> getAccountDetails() const;

288 289 290 291 292 293
    /**
     * Retrieve volatile details such as recent registration errors
     * @return std::map< std::string, std::string > The account volatile details
     */
    virtual std::map<std::string, std::string> getVolatileAccountDetails() const;

294
    /**
295 296
     * Callback called by the transport layer when the registration
     * transport state changes.
297
     */
298
    virtual void onTransportStateChanged(pjsip_transport_state state, const pjsip_transport_state_info *info);
299 300

    /**
301
     * Voice over IP Link contains a listener thread and calls
302
     */
303 304 305 306 307
    std::shared_ptr<SIPVoIPLink> link_;

    std::shared_ptr<SipTransport> transport_ {};

    std::shared_ptr<TlsListener> tlsListener_ {};
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

    /**
     * Transport type used for this sip account. Currently supported types:
     *    PJSIP_TRANSPORT_UNSPECIFIED
     *    PJSIP_TRANSPORT_UDP
     *    PJSIP_TRANSPORT_TLS
     */
    pjsip_transport_type_e transportType_ {PJSIP_TRANSPORT_UNSPECIFIED};

    /**
     * interface name on which this account is bound
     */
    std::string interface_ {"default"};

    /**
     * Flag which determine if localIpAddress_ or publishedIpAddress_ is used in
     * sip headers
     */
    bool publishedSameasLocal_ {true};

    /**
     * Published IP address, used only if defined by the user in account
     * configuration
     */
    IpAddr publishedIp_ {};
    std::string publishedIpAddress_ {};

    /**
     * Local port to whih this account is bound
     */
    pj_uint16_t localPort_ {DEFAULT_SIP_PORT};

    /**
     * Published port, used only if defined by the user
     */
    pj_uint16_t publishedPort_ {DEFAULT_SIP_PORT};

    /**
346
     * The TLS listener port
347 348
     */
    pj_uint16_t tlsListenerPort_ {DEFAULT_SIP_TLS_PORT};
349 350 351 352
    std::string tlsCaListFile_;
    std::string tlsCertificateFile_;
    std::string tlsPrivateKeyFile_;
    std::string tlsPassword_;
353 354 355 356 357 358

    /**
     * DTMF type used for this account SIPINFO or RTP
     */
    std::string dtmfType_ {OVERRTP_STR};

359 360 361
    pj_status_t transportStatus_ {PJSIP_SC_TRYING};
    std::string transportError_ {};

362 363 364 365 366 367 368 369 370 371
    /*
     * Port range for audio RTP ports
     */
    std::pair<uint16_t, uint16_t> audioPortRange_ {16384, 32766};

    /**
     * Port range for video RTP ports
     */
    std::pair<uint16_t, uint16_t> videoPortRange_ {49152, (MAX_PORT) - 2};

372
    static std::array<bool, HALF_MAX_PORT>& getPortsReservation() noexcept;
373
    uint16_t acquireRandomEvenPort(const std::pair<uint16_t, uint16_t>& range) const;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

    static void
    addRangeToDetails(std::map<std::string, std::string> &a, const char *minKey, const char *maxKey, const std::pair<uint16_t, uint16_t> &range)
    {
        std::ostringstream os;
        os << range.first;
        a[minKey] = os.str();
        os.str("");
        os << range.second;
        a[maxKey] = os.str();
    }

private:
    NON_COPYABLE(SIPAccountBase);

};

391 392
} // namespace ring

393
#endif