siptransport.h 6.51 KB
Newer Older
1
/*
Adrien Béraud's avatar
Adrien Béraud committed
2
 *  Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
3
4
 *
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 *  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
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 *
 *  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 SIPTRANSPORT_H_
#define SIPTRANSPORT_H_

36
37
38
39
40
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "noncopyable.h"
#include "logger.h"
41
42
43
44

#include <pjsip.h>
#include <pjnath/stun_config.h>

45
46
47
#include <functional>
#include <mutex>
#include <condition_variable>
48
49
50
#include <map>
#include <string>
#include <vector>
51
#include <list>
52
#include <memory>
53

Tristan Matthews's avatar
Tristan Matthews committed
54
55
56
#define DEFAULT_SIP_PORT    5060
#define DEFAULT_SIP_TLS_PORT 5061

Guillaume Roguez's avatar
Guillaume Roguez committed
57
58
namespace ring {

Adrien Béraud's avatar
Adrien Béraud committed
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
struct SipTransportDescr
{
    SipTransportDescr() {}
    SipTransportDescr(pjsip_transport_type_e t)
     : type(t), listenerPort(pjsip_transport_get_default_port_for_type(t)) {}
    SipTransportDescr(pjsip_transport_type_e t, pj_uint16_t port, std::string i)
     : type(t), listenerPort(port), interface(i) {}

    static inline pjsip_transport_type_e actualType(pjsip_transport_type_e t) {
        return (t == PJSIP_TRANSPORT_START_OTHER) ? PJSIP_TRANSPORT_UDP : t;
    }

    inline bool operator==(SipTransportDescr const& o) const {
        return actualType(type) == actualType(o.type)
         && listenerPort == o.listenerPort
         && interface == o.interface;
    }

    inline bool operator<(SipTransportDescr const& o) const {
        return actualType(type) < actualType(o.type)
         || listenerPort < o.listenerPort
         || std::hash<std::string>()(interface) < std::hash<std::string>()(o.interface);
    }

    std::string toString() const;

    pjsip_transport_type_e type {PJSIP_TRANSPORT_UNSPECIFIED};
    pj_uint16_t listenerPort {DEFAULT_SIP_PORT};
    std::string interface {"default"};
};

struct TlsListener
{
    TlsListener() {}
    TlsListener(pjsip_tpfactory* f) : listener(f) {}
    virtual ~TlsListener() {
Adrien Béraud's avatar
Adrien Béraud committed
95
        RING_DBG("Destroying listener");
Adrien Béraud's avatar
Adrien Béraud committed
96
97
98
99
100
101
102
103
104
105
106
        listener->destroy(listener);
    }
    pjsip_tpfactory* get() {
        return listener;
    }
private:
    NON_COPYABLE(TlsListener);
    pjsip_tpfactory* listener {nullptr};
};

typedef std::function<void(pjsip_transport_state, const pjsip_transport_state_info*)> SipTransportStateCallback;
107

108
109
110
/**
 * SIP transport wraps pjsip_transport.
 */
Adrien Béraud's avatar
Adrien Béraud committed
111
112
113
114
115
116
117
118
119
struct SipTransport
{
    SipTransport() {}
    SipTransport(pjsip_transport*, const std::shared_ptr<TlsListener>& l = {});

    virtual ~SipTransport();

    static const char* stateToStr(pjsip_transport_state state);

120
    void stateCallback(pjsip_transport_state state, const pjsip_transport_state_info *info);
Adrien Béraud's avatar
Adrien Béraud committed
121
122
123
124
125

    pjsip_transport* get() {
        return transport;
    }

126
127
    void addStateListener(uintptr_t lid, SipTransportStateCallback cb);
    bool removeStateListener(uintptr_t lid);
128

Adrien Béraud's avatar
Adrien Béraud committed
129
130
131
132
133
134
135
136
137
138
    static bool isAlive(const std::shared_ptr<SipTransport>&, pjsip_transport_state state);

private:
    NON_COPYABLE(SipTransport);
    pjsip_transport* transport {nullptr};
    std::shared_ptr<TlsListener> tlsListener {};
    std::map<uintptr_t, SipTransportStateCallback> stateListeners {};
    std::mutex stateListenersMutex_ {};
};

139
class IpAddr;
140
class SipIceTransport;
Guillaume Roguez's avatar
Guillaume Roguez committed
141
142
class IceTransport;

Adrien Béraud's avatar
Adrien Béraud committed
143
144
145
/**
 * Manages the transports and receive callbacks from PJSIP
 */
146
147
class SipTransportBroker
{
Adrien Béraud's avatar
Adrien Béraud committed
148
149
150
151
152
public:
    SipTransportBroker(pjsip_endpoint *endpt, pj_caching_pool& cp, pj_pool_t& pool);
    ~SipTransportBroker();

    std::shared_ptr<SipTransport> getUdpTransport(const SipTransportDescr&);
153

154
#if HAVE_TLS
Adrien Béraud's avatar
Adrien Béraud committed
155
156
    std::shared_ptr<TlsListener> getTlsListener(const SipTransportDescr&, const pjsip_tls_setting*);

157
    std::shared_ptr<SipTransport> getTlsTransport(const std::shared_ptr<TlsListener>&, const IpAddr& remote);
158
#endif
159

160
#if HAVE_DHT
Guillaume Roguez's avatar
Guillaume Roguez committed
161
    std::shared_ptr<SipTransport> getIceTransport(const std::shared_ptr<IceTransport>, unsigned comp_id);
162
#endif
163

Adrien Béraud's avatar
Adrien Béraud committed
164
165
    std::shared_ptr<SipTransport> findTransport(pjsip_transport*);

166
167
168
169
170
    /**
     * Start gracefull shutdown procedure for all transports
     */
    void shutdown();

Adrien Béraud's avatar
Adrien Béraud committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
private:
    NON_COPYABLE(SipTransportBroker);

    /**
    * Create SIP UDP transport from account's setting
    * @param account The account for which a transport must be created.
    * @param IP protocol version to use, can be pj_AF_INET() or pj_AF_INET6()
    * @return a pointer to the new transport
    */
    std::shared_ptr<SipTransport> createUdpTransport(const SipTransportDescr&);

    static void tp_state_callback(pjsip_transport*, pjsip_transport_state, const pjsip_transport_state_info*);

    void transportStateChanged(pjsip_transport*, pjsip_transport_state, const pjsip_transport_state_info*);

    /**
     * List of transports so we can bubble the events up.
     */
    std::map<pjsip_transport*, std::weak_ptr<SipTransport>> transports_ {};
190
    std::mutex transportMapMutex_ {};
Adrien Béraud's avatar
Adrien Béraud committed
191
192
193
194
195
196
197

    /**
     * Transports are stored in this map in order to retreive them in case
     * several accounts would share the same port number.
     */
    std::map<SipTransportDescr, pjsip_transport*> udpTransports_ {};

198
199
200
    /**
     * Storage for SIP/ICE transport instances.
     */
201
202
203
#if HAVE_DHT
    int ice_pj_transport_type_ {PJSIP_TRANSPORT_START_OTHER};

204
205
    std::list<SipIceTransport> iceTransports_;
    std::mutex iceMutex_ {};
206
#endif
207

Adrien Béraud's avatar
Adrien Béraud committed
208
209
210
    pj_caching_pool& cp_;
    pj_pool_t& pool_;
    pjsip_endpoint *endpt_;
211

212
213
};

Guillaume Roguez's avatar
Guillaume Roguez committed
214
215
} // namespace ring

216
#endif // SIPTRANSPORT_H_