reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
Flow.hxx
Go to the documentation of this file.
1#if !defined(Flow_hxx)
2#define Flow_hxx
3
4#ifdef HAVE_CONFIG_H
5#include "config.h"
6#endif
7
8#include <map>
10#include <rutil/Mutex.hxx>
11
12#include "Srtp2Helper.hxx"
13
21
22#include "FlowContext.hxx"
24
25#include <memory>
26#include <utility>
27
28using namespace reTurn;
29
30namespace flowmanager
31{
32
33class MediaStream;
34class Flow;
35
45{
46public:
47
55
66
67 Flow(asio::io_context& ioService,
68 asio::ssl::context& sslContext,
69 unsigned int componentId,
70 const StunTuple& localBinding,
72 bool forceCOMedia,
73 std::shared_ptr<RTCPEventLoggingHandler> rtcpEventLoggingHandler = nullptr,
74 std::shared_ptr<FlowContext> context = nullptr);
75 ~Flow();
76
79
80 bool isReady() { return mFlowState == Ready; }
81
86 unsigned int getSelectSocketDescriptor();
87
88 unsigned int getSocketDescriptor(); // returns the real socket descriptor - used to correlate callbacks
89
94 void send(char* buffer, unsigned int size);
95 void sendTo(const asio::ip::address& address, unsigned short port, char* buffer, unsigned int size);
96 void rawSendTo(const asio::ip::address& address, unsigned short port, const char* buffer, unsigned int size);
97
99 asio::error_code receive(char* buffer, unsigned int& size, unsigned int timeout, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0);
100 asio::error_code receiveFrom(const asio::ip::address& address, unsigned short port, char* buffer, unsigned int& size, unsigned int timeout);
101
103 void setActiveDestination(const char* address, unsigned short port);
104
106
109 void startDtlsClient(const char* address, unsigned short port);
110
119 void setRemoteSDPFingerprint(const resip::Data& hashFunction, const resip::Data& fingerprint);
120
123
124 const StunTuple& getLocalTuple();
125 StunTuple getSessionTuple(); // returns either local, reflexive, or relay tuple depending on NatTraversalMode
129 unsigned int getComponentId() { return mComponentId; }
130
131private:
132 // Reads mRemoteSDPFingerprint* from its handshakeCompleted callback, which is run with mMutex held
134
135 asio::io_context& mIOService;
136 asio::ssl::context& mSslContext;
137
138 // Note: these member variables are set at creation time and never changed, thus
139 // they do not require mutex protection
140 unsigned int mComponentId;
141 StunTuple mLocalBinding;
142
143 // MediaStream that this Flow belongs too
144 MediaStream& mMediaStream;
145
146 // Use peer's RTP source IP instead of the peer's SDP connection IP
147 bool mForceCOMedia;
148
149 // Logging handler, if set
150 std::shared_ptr<RTCPEventLoggingHandler> mRtcpEventLoggingHandler;
151
152 // Flow context from application layer
153 std::shared_ptr<FlowContext> mFlowContext;
154
155 // mTurnSocket has it's own threading protection
156 std::shared_ptr<TurnAsyncSocket> mTurnSocket;
157 bool mPrivatePeer;
158
159 // These are only set once, then accessed - thus they do not require mutex protection
160 uint8_t mAllocationProps;
161 uint64_t mReservationToken;
162
163 // Mutex to protect the following members that may be get/set from multiple threads. Shared
164 // with the DtlsSockets' retransmission timers, which take it when they fire (on the io thread)
165 // and may still be pending when the Flow is destroyed.
166 std::shared_ptr<resip::Mutex> mSharedMutex;
167 resip::Mutex& mMutex;
168 StunTuple mReflexiveTuple;
169 StunTuple mRelayTuple;
170 resip::Data mRemoteSDPFingerprintHashFunction;
171 resip::Data mRemoteSDPFingerprint;
172 asio::ip::address mActiveDestinationAddress;
173 unsigned short mActiveDestinationPort;
174
175 // Map to store all DtlsSockets - in forking cases there can be more than one
176 std::map<reTurn::StunTuple, dtls::DtlsSocket*> mDtlsSockets;
177 dtls::DtlsSocket* getDtlsSocket(const reTurn::StunTuple& endpoint);
178 dtls::DtlsSocket* createDtlsSocketClient(const StunTuple& endpoint);
179#ifdef USE_SSL
180 // Handles a DTLS packet from an endpoint without a DtlsSocket: through mDtlsListener's cookie
181 // exchange, so a server DtlsSocket is only created once the endpoint proves it receives at its
182 // address, and only up to MaxDtlsSockets. Returns the new socket, if one was created.
183 dtls::DtlsSocket* acceptDtlsSocketServer(const StunTuple& endpoint, const unsigned char* data, size_t len);
184 static const size_t MaxDtlsSockets = 8;
185 std::unique_ptr<dtls::DtlsListener> mDtlsListener;
186 bool mDtlsSocketLimitLogged = false;
187#endif
188
189 volatile FlowState mFlowState;
190 void changeFlowState(FlowState newState);
191 const char* flowStateToString(FlowState state);
192
193 class ReceivedData
194 {
195 public:
196 ReceivedData(const asio::ip::address& address, unsigned short port, std::shared_ptr<DataBuffer> data) :
197 mAddress(address), mPort(port), mData(data) {}
198
199 asio::ip::address mAddress;
200 unsigned short mPort;
201 std::shared_ptr<DataBuffer> mData;
202 };
203 // FIFO for received data
204 typedef resip::TimeLimitFifo<ReceivedData> ReceivedDataFifo;
205 ReceivedDataFifo mReceivedDataFifo;
206
207 // Helpers to perform SRTP protection/unprotection
208 bool processSendData(char* buffer, unsigned int& size, const asio::ip::address& address, unsigned short port);
209 asio::error_code processReceivedData(char* buffer, unsigned int& size, ReceivedData* receivedData, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0);
210 FakeSelectSocketDescriptor mFakeSelectSocketDescriptor;
211
212 virtual void onConnectSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port);
213 virtual void onConnectFailure(unsigned int socketDesc, const asio::error_code& e);
214
215 virtual void onSharedSecretSuccess(unsigned int socketDesc, const char* username, unsigned int usernameSize, const char* password, unsigned int passwordSize);
216 virtual void onSharedSecretFailure(unsigned int socketDesc, const asio::error_code& e);
217
218 virtual void onBindSuccess(unsigned int socketDesc, const StunTuple& reflexiveTuple, const StunTuple& stunServerTuple);
219 virtual void onBindFailure(unsigned int socketDesc, const asio::error_code& e, const StunTuple& stunServerTuple);
220
221 virtual void onAllocationSuccess(unsigned int socketDesc, const StunTuple& reflexiveTuple, const StunTuple& relayTuple, unsigned int lifetime, unsigned int bandwidth, uint64_t reservationToken);
222 virtual void onAllocationFailure(unsigned int socketDesc, const asio::error_code& e);
223
224 virtual void onRefreshSuccess(unsigned int socketDesc, unsigned int lifetime);
225 virtual void onRefreshFailure(unsigned int socketDesc, const asio::error_code& e);
226
227 virtual void onSetActiveDestinationSuccess(unsigned int socketDesc);
228 virtual void onSetActiveDestinationFailure(unsigned int socketDesc, const asio::error_code &e);
229 virtual void onClearActiveDestinationSuccess(unsigned int socketDesc);
230 virtual void onClearActiveDestinationFailure(unsigned int socketDesc, const asio::error_code &e);
231
232 virtual void onChannelBindRequestSent(unsigned int socketDesc, unsigned short channelNumber);
233 virtual void onChannelBindSuccess(unsigned int socketDesc, unsigned short channelNumber);
234 virtual void onChannelBindFailure(unsigned int socketDesc, const asio::error_code& e);
235
236 //virtual void onReceiveSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port, const char* buffer, unsigned int size);
237 virtual void onReceiveSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port, const std::shared_ptr<DataBuffer>& data);
238 virtual void onReceiveFailure(unsigned int socketDesc, const asio::error_code& e);
239
240 virtual void onSendSuccess(unsigned int socketDesc);
241 virtual void onSendFailure(unsigned int socketDesc, const asio::error_code& e);
242
243 virtual void onIncomingBindRequestProcessed(unsigned int socketDesc, const StunTuple& sourceTuple);
244};
245
246}
247
248#endif
249
250
251/* ====================================================================
252
253 Copyright (c) 2007-2026, SIP Spectrum, Inc. http://sipspectrum.com
254 Copyright (c) 2007-2008, Plantronics, Inc.
255 All rights reserved.
256
257 Redistribution and use in source and binary forms, with or without
258 modification, are permitted provided that the following conditions are
259 met:
260
261 1. Redistributions of source code must retain the above copyright
262 notice, this list of conditions and the following disclaimer.
263
264 2. Redistributions in binary form must reproduce the above copyright
265 notice, this list of conditions and the following disclaimer in the
266 documentation and/or other materials provided with the distribution.
267
268 3. Neither the name of Plantronics nor the names of its contributors
269 may be used to endorse or promote products derived from this
270 software without specific prior written permission.
271
272 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
273 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
274 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
275 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
276 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
277 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
278 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
279 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
280 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
281 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
282 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
283
284 ==================================================================== */
This class represents a Flow that is created by the Flow Manager.
Definition Flow.hxx:45
const resip::Data getRemoteSDPFingerprint()
Retrieves the stored remote SDP Fingerprint.
Definition Flow.cxx:591
StunTuple getReflexiveTuple()
Definition Flow.cxx:629
uint64_t getReservationToken()
Definition Flow.cxx:637
bool isReady()
Definition Flow.hxx:80
const StunTuple & getLocalTuple()
Definition Flow.cxx:598
asio::error_code receiveFrom(const asio::ip::address &address, unsigned short port, char *buffer, unsigned int &size, unsigned int timeout)
Definition Flow.cxx:366
unsigned int getSocketDescriptor()
Definition Flow.cxx:265
void send(char *buffer, unsigned int size)
Turn Send Methods WARNING - if using Secure media, then there must be room at the end of the passed i...
Definition Flow.cxx:272
void setRemoteSDPFingerprint(const resip::Data &hashFunction, const resip::Data &fingerprint)
This method should be called when remote fingerprint is discovered via SDP negotiation (RFC 8122 a=fi...
Definition Flow.cxx:574
void rawSendTo(const asio::ip::address &address, unsigned short port, const char *buffer, unsigned int size)
Definition Flow.cxx:307
void setActiveDestination(const char *address, unsigned short port)
Used to set where this flow should be sending to.
Definition Flow.cxx:519
static int maxReceiveFifoDuration
Definition Flow.hxx:48
static int dtlsHandshakeEvictionGrace
Seconds after which a server DTLS handshake still in progress gives up its place, when a flow already...
Definition Flow.hxx:54
unsigned int getComponentId()
Definition Flow.hxx:129
StunTuple getSessionTuple()
Definition Flow.cxx:604
void sendTo(const asio::ip::address &address, unsigned short port, char *buffer, unsigned int size)
Definition Flow.cxx:289
static int maxReceiveFifoSize
Definition Flow.hxx:49
asio::error_code receive(char *buffer, unsigned int &size, unsigned int timeout, asio::ip::address *sourceAddress=0, unsigned short *sourcePort=0)
Receive Methods.
Definition Flow.cxx:412
unsigned int getSelectSocketDescriptor()
Returns a socket descriptor that can be used in a select call WARNING - this descriptor should not be...
Definition Flow.cxx:259
void startDtlsClient(const char *address, unsigned short port)
Dtls-Srtp Methods.
Definition Flow.cxx:566
StunTuple getRelayTuple()
Definition Flow.cxx:621
void activateFlow(uint8_t allocationProps=StunMessage::PropsNone)
Definition Flow.cxx:227
This class represents a media stream, that consists of a series of componenets or flows.
static const uint8_t PropsNone
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
This class is used during media sessions that use Dtls-Srtp for a media transport.
Definition ErrorCode.hxx:4
std::mutex Mutex
A semaphore that can be locked by only one thread at a time.
Definition Mutex.hxx:37
unsigned char uint8_t
Definition stdint.h:83
unsigned __int64 uint64_t
Definition stdint.h:95
void list()
Definition userAdmin.cxx:66